mysql2 0.3.20 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6261e72195a2e1281c59ad35c44fe9edefc69241
4
- data.tar.gz: 5dd5b66d3bc11fcd978d4a2d491ac47e3a005510
2
+ SHA256:
3
+ metadata.gz: 8be4e0f4afbea38c5460544e39c9e0d987dd040c9de5ebdbc4c1e200102ca8e4
4
+ data.tar.gz: 996e05358297b03bf8f06abffd37d83a590912246ec607d2c3ee53b325c43022
5
5
  SHA512:
6
- metadata.gz: 9a1dc5ee4f77d51c5042b5eb8153a18c2d199506c5514a5064c818fe5f800fcbd32dae5a73c29eec268b8f96e1f31c358f87e9306e50fbee7836f7a3a88c5393
7
- data.tar.gz: fa5d2a4b7815f4b834598e647e0aecb328e8997638ea7c73ad313cfa6b9eba5099a329c402071fb08a8a09b99e5a99784191bf7ff5722f0707a69a9c5d041d62
6
+ metadata.gz: daf37441bcb29366453963529b5a144df0d599b79b7a0b9d90b2ae5ed3dee97c1945a58111d6fe6bc2f1c20f75c9960c3cc1cd988a0e5d1be5ce44b7dcdf3633
7
+ data.tar.gz: 421df08c45f4257c2f04f3dc0a0e1c1557c695598c4e1a80236d618d1a79510459b8919b753ad9af2d46d21fa19ba060d5b53da2f54a00ff90ca45a4534c9108
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ Changes are maintained under [Releases](https://github.com/brianmario/mysql2/releases)
data/README.md CHANGED
@@ -7,25 +7,29 @@ The Mysql2 gem is meant to serve the extremely common use-case of connecting, qu
7
7
  Some database libraries out there serve as direct 1:1 mappings of the already complex C APIs available.
8
8
  This one is not.
9
9
 
10
- It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9, unless Encoding.default_internal is set then it'll convert from UTF-8 to that encoding] and uses encoding-aware MySQL API calls where it can.
10
+ It also forces the use of UTF-8 [or binary] for the connection and uses encoding-aware MySQL API calls where it can.
11
11
 
12
- The API consists of two classes:
12
+ The API consists of three classes:
13
13
 
14
14
  `Mysql2::Client` - your connection to the database.
15
15
 
16
16
  `Mysql2::Result` - returned from issuing a #query on the connection. It includes Enumerable.
17
17
 
18
+ `Mysql2::Statement` - returned from issuing a #prepare on the connection. Execute the statement to get a Result.
19
+
18
20
  ## Installing
21
+
19
22
  ### General Instructions
23
+
20
24
  ``` sh
21
25
  gem install mysql2
22
26
  ```
23
27
 
24
28
  This gem links against MySQL's `libmysqlclient` library or `Connector/C`
25
29
  library, and compatible alternatives such as MariaDB.
26
- You may need to install a package such as `libmysqlclient-dev`, `mysql-devel`,
27
- or other appropriate package for your system. See below for system-specific
28
- instructions.
30
+ You may need to install a package such as `libmariadb-dev`, `libmysqlclient-dev`,
31
+ `mysql-devel`, or other appropriate package for your system. See below for
32
+ system-specific instructions.
29
33
 
30
34
  By default, the mysql2 gem will try to find a copy of MySQL in this order:
31
35
 
@@ -56,12 +60,27 @@ This may be needed if you deploy to a system where these libraries
56
60
  are located somewhere different than on your build system.
57
61
  This overrides any rpath calculated by default or by the options above.
58
62
 
63
+ * `--with-sanitize[=address,cfi,integer,memory,thread,undefined]` -
64
+ Enable sanitizers for Clang / GCC. If no argument is given, try to enable
65
+ all sanitizers or fail if none are available. If a command-separated list of
66
+ specific sanitizers is given, configure will fail unless they all are available.
67
+ Note that the some sanitizers may incur a performance penalty, and the Address
68
+ Sanitizer may require a runtime library.
69
+ To see line numbers in backtraces, declare these environment variables
70
+ (adjust the llvm-symbolizer path as needed for your system):
71
+
72
+ ``` sh
73
+ export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
74
+ export ASAN_OPTIONS=symbolize=1
75
+ ```
76
+
59
77
  ### Linux and other Unixes
60
78
 
61
- You may need to install a package such as `libmysqlclient-dev` or `mysql-devel`;
62
- refer to your distribution's package guide to find the particular package.
63
- The most common issue we see is a user who has the library file `libmysqlclient.so` but is
64
- missing the header file `mysql.h` -- double check that you have the _-dev_ packages installed.
79
+ You may need to install a package such as `libmariadb-dev`, `libmysqlclient-dev`,
80
+ `mysql-devel`, or `default-libmysqlclient-dev`; refer to your distribution's package guide to
81
+ find the particular package. The most common issue we see is a user who has
82
+ the library file `libmysqlclient.so` but is missing the header file `mysql.h`
83
+ -- double check that you have the _-dev_ packages installed.
65
84
 
66
85
  ### Mac OS X
67
86
 
@@ -69,7 +88,11 @@ You may use MacPorts, Homebrew, or a native MySQL installer package. The most
69
88
  common paths will be automatically searched. If you want to select a specific
70
89
  MySQL directory, use the `--with-mysql-dir` or `--with-mysql-config` options above.
71
90
 
91
+ If you have not done so already, you will need to install the XCode select tools by running
92
+ `xcode-select --install`.
93
+
72
94
  ### Windows
95
+
73
96
  Make sure that you have Ruby and the DevKit compilers installed. We recommend
74
97
  the [Ruby Installer](http://rubyinstaller.org) distribution.
75
98
 
@@ -93,7 +116,7 @@ Connect to a database:
93
116
  ``` ruby
94
117
  # this takes a hash of options, almost all of which map directly
95
118
  # to the familiar database.yml in rails
96
- # See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
119
+ # See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html
97
120
  client = Mysql2::Client.new(:host => "localhost", :username => "root")
98
121
  ```
99
122
 
@@ -119,7 +142,7 @@ results.each do |row|
119
142
  # conveniently, row is a hash
120
143
  # the keys are the fields, as you'd expect
121
144
  # the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
122
- puts row["id"] # row["id"].class == Fixnum
145
+ puts row["id"] # row["id"].is_a? Integer
123
146
  if row["dne"] # non-existant hash entry is nil
124
147
  puts row["dne"]
125
148
  end
@@ -137,7 +160,7 @@ end
137
160
  How about with symbolized keys?
138
161
 
139
162
  ``` ruby
140
- client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true) do |row|
163
+ client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true).each do |row|
141
164
  # do something with row, it's ready to rock
142
165
  end
143
166
  ```
@@ -148,11 +171,32 @@ by the query like this:
148
171
  ``` ruby
149
172
  headers = results.fields # <= that's an array of field names, in order
150
173
  results.each(:as => :array) do |row|
151
- # Each row is an array, ordered the same as the query results
152
- # An otter's den is called a "holt" or "couch"
174
+ # Each row is an array, ordered the same as the query results
175
+ # An otter's den is called a "holt" or "couch"
153
176
  end
154
177
  ```
155
178
 
179
+ Prepared statements are supported, as well. In a prepared statement, use a `?`
180
+ in place of each value and then execute the statement to retrieve a result set.
181
+ Pass your arguments to the execute method in the same number and order as the
182
+ question marks in the statement. Query options can be passed as keyword arguments
183
+ to the execute method.
184
+
185
+ Be sure to read about the known limitations of prepared statements at
186
+ [https://dev.mysql.com/doc/refman/5.6/en/c-api-prepared-statement-problems.html](https://dev.mysql.com/doc/refman/5.6/en/c-api-prepared-statement-problems.html)
187
+
188
+ ``` ruby
189
+ statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
190
+ result1 = statement.execute(1)
191
+ result2 = statement.execute(2)
192
+
193
+ statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
194
+ result = statement.execute(1, "CA")
195
+
196
+ statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
197
+ result = statement.execute(1, "CA", :as => :array)
198
+ ```
199
+
156
200
  ## Connection options
157
201
 
158
202
  You may set the following connection options in Mysql2::Client.new(...):
@@ -170,15 +214,30 @@ Mysql2::Client.new(
170
214
  :read_timeout = seconds,
171
215
  :write_timeout = seconds,
172
216
  :connect_timeout = seconds,
217
+ :connect_attrs = {:program_name => $PROGRAM_NAME, ...},
173
218
  :reconnect = true/false,
174
219
  :local_infile = true/false,
175
220
  :secure_auth = true/false,
221
+ :ssl_mode = :disabled / :preferred / :required / :verify_ca / :verify_identity,
176
222
  :default_file = '/path/to/my.cfg',
177
223
  :default_group = 'my.cfg section',
224
+ :default_auth = 'authentication_windows_client'
178
225
  :init_command => sql
179
226
  )
180
227
  ```
181
228
 
229
+ ### Connecting to MySQL on localhost and elsewhere
230
+
231
+ The underlying MySQL client library uses the `:host` parameter to determine the
232
+ type of connection to make, with special interpretation you should be aware of:
233
+
234
+ * An empty value or `"localhost"` will attempt a local connection:
235
+ * On Unix, connect to the default local socket path. (To set a custom socket
236
+ path, use the `:socket` parameter).
237
+ * On Windows, connect using a shared-memory connection, if enabled, or TCP.
238
+ * A value of `"."` on Windows specifies a named-pipe connection.
239
+ * An IPv4 or IPv6 address will result in a TCP connection.
240
+ * Any other value will be looked up as a hostname for a TCP connection.
182
241
 
183
242
  ### SSL options
184
243
 
@@ -186,7 +245,8 @@ Setting any of the following options will enable an SSL connection, but only if
186
245
  your MySQL client library and server have been compiled with SSL support.
187
246
  MySQL client library defaults will be used for any parameters that are left out
188
247
  or set to nil. Relative paths are allowed, and may be required by managed
189
- hosting providers such as Heroku.
248
+ hosting providers such as Heroku. Set `:sslverify => true` to require that the
249
+ server presents a valid certificate.
190
250
 
191
251
  ``` ruby
192
252
  Mysql2::Client.new(
@@ -195,10 +255,78 @@ Mysql2::Client.new(
195
255
  :sslcert => '/path/to/client-cert.pem',
196
256
  :sslca => '/path/to/ca-cert.pem',
197
257
  :sslcapath => '/path/to/cacerts',
198
- :sslcipher => 'DHE-RSA-AES256-SHA'
258
+ :sslcipher => 'DHE-RSA-AES256-SHA',
259
+ :sslverify => true,
199
260
  )
200
261
  ```
201
262
 
263
+ ### Secure auth
264
+
265
+ Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
266
+ When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
267
+ The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
268
+ To bypass this restriction in the client, pass the option `:secure_auth => false` to Mysql2::Client.new().
269
+
270
+ ### Flags option parsing
271
+
272
+ The `:flags` parameter accepts an integer, a string, or an array. The integer
273
+ form allows the client to assemble flags from constants defined under
274
+ `Mysql2::Client` such as `Mysql2::Client::FOUND_ROWS`. Use a bitwise `|` (OR)
275
+ to specify several flags.
276
+
277
+ The string form will be split on whitespace and parsed as with the array form:
278
+ Plain flags are added to the default flags, while flags prefixed with `-`
279
+ (minus) are removed from the default flags.
280
+
281
+ ### Using Active Record's database.yml
282
+
283
+ Active Record typically reads its configuration from a file named `database.yml` or an environment variable `DATABASE_URL`.
284
+ Use the value `mysql2` as the adapter name. For example:
285
+
286
+ ``` yaml
287
+ development:
288
+ adapter: mysql2
289
+ encoding: utf8
290
+ database: my_db_name
291
+ username: root
292
+ password: my_password
293
+ host: 127.0.0.1
294
+ port: 3306
295
+ flags:
296
+ - -COMPRESS
297
+ - FOUND_ROWS
298
+ - MULTI_STATEMENTS
299
+ secure_auth: false
300
+ ```
301
+
302
+ ### Using Active Record's DATABASE_URL
303
+
304
+ Active Record typically reads its configuration from a file named `database.yml` or an environment variable `DATABASE_URL`.
305
+ Use the value `mysql2` as the protocol name. For example:
306
+
307
+ ``` shell
308
+ DATABASE_URL=mysql2://sql_user:sql_pass@sql_host_name:port/sql_db_name?option1=value1&option2=value2
309
+ ```
310
+
311
+ ### Reading a MySQL config file
312
+
313
+ You may read configuration options from a MySQL configuration file by passing
314
+ the `:default_file` and `:default_group` parameters. For example:
315
+
316
+ ``` ruby
317
+ Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
318
+ ```
319
+
320
+ ### Initial command on connect and reconnect
321
+
322
+ If you specify the `:init_command` option, the SQL string you provide will be executed after the connection is established.
323
+ If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
324
+ It is useful if you want to provide session options which survive reconnection.
325
+
326
+ ``` ruby
327
+ Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
328
+ ```
329
+
202
330
  ### Multiple result sets
203
331
 
204
332
  You can also retrieve multiple result sets. For this to work you need to
@@ -234,53 +362,13 @@ end
234
362
  ```
235
363
 
236
364
  Yields:
237
- ```
365
+
366
+ ```ruby
238
367
  {"1"=>1}
239
368
  {"2"=>2}
240
369
  next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
241
370
  ```
242
371
 
243
- See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
244
-
245
- ### Secure auth
246
-
247
- Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
248
- When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
249
- The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
250
- To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new().
251
- If using ActiveRecord, your database.yml might look something like this:
252
-
253
- ``` yaml
254
- development:
255
- adapter: mysql2
256
- encoding: utf8
257
- database: my_db_name
258
- username: root
259
- password: my_password
260
- host: 127.0.0.1
261
- port: 3306
262
- secure_auth: false
263
- ```
264
-
265
- ### Reading a MySQL config file
266
-
267
- You may read configuration options from a MySQL configuration file by passing
268
- the `:default_file` and `:default_group` paramters. For example:
269
-
270
- ``` ruby
271
- Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
272
- ```
273
-
274
- ### Initial command on connect and reconnect
275
-
276
- If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
277
- If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
278
- It is useful if you want to provide session options which survive reconnection.
279
-
280
- ``` ruby
281
- Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
282
- ```
283
-
284
372
  ## Cascading config
285
373
 
286
374
  The default config hash is at:
@@ -318,6 +406,15 @@ c = Mysql2::Client.new
318
406
  c.query(sql, :symbolize_keys => true)
319
407
  ```
320
408
 
409
+ or
410
+
411
+ ``` ruby
412
+ # this will set the options for the Mysql2::Result instance returned from the #execute method
413
+ c = Mysql2::Client.new
414
+ s = c.prepare(sql)
415
+ s.execute(arg1, args2, :symbolize_keys => true)
416
+ ```
417
+
321
418
  ## Result types
322
419
 
323
420
  ### Array of Arrays
@@ -351,6 +448,15 @@ client = Mysql2::Client.new
351
448
  result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
352
449
  ```
353
450
 
451
+ Keep in mind that this works only with fields and not with computed values, e.g. this result will contain `1`, not `true`:
452
+
453
+ ``` ruby
454
+ client = Mysql2::Client.new
455
+ result = client.query("SELECT true", :cast_booleans => true)
456
+ ```
457
+
458
+ CAST function wouldn't help here as there's no way to cast to TINYINT(1). Apparently the only way to solve this is to use a stored procedure with return type set to TINYINT(1).
459
+
354
460
  ### Skipping casting
355
461
 
356
462
  Mysql2 casting is fast, but not as fast as not casting data. In rare cases where typecasting is not needed, it will be faster to disable it by providing :cast => false. (Note that :cast => false overrides :cast_booleans => true.)
@@ -416,7 +522,7 @@ There are a few things that need to be kept in mind while using streaming:
416
522
  * `:cache_rows` is ignored currently. (if you want to use `:cache_rows` you probably don't want to be using `:stream`)
417
523
  * You must fetch all rows in the result set of your query before you can make new queries. (i.e. with `Mysql2::Result#each`)
418
524
 
419
- Read more about the consequences of using `mysql_use_result` (what streaming is implemented with) here: http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html.
525
+ Read more about the consequences of using `mysql_use_result` (what streaming is implemented with) here: [http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html](http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html).
420
526
 
421
527
  ### Lazy Everything
422
528
 
@@ -437,20 +543,21 @@ As for field values themselves, I'm workin on it - but expect that soon.
437
543
 
438
544
  This gem is tested with the following Ruby versions on Linux and Mac OS X:
439
545
 
440
- * Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x (ongoing patch releases)
441
- * Ruby Enterprise Edition (based on MRI 1.8.7)
442
- * Rubinius 2.x
546
+ * Ruby MRI 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x
547
+ * Rubinius 2.x and 3.x do work but may fail under some workloads
443
548
 
444
549
  This gem is tested with the following MySQL and MariaDB versions:
445
550
 
446
- * MySQL 5.0, 5.1, 5.5, 5.6, 5.7
447
- * MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
448
- * MariaDB 5.5, 10.0
551
+ * MySQL 5.5, 5.6, 5.7, 8.0
552
+ * MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
553
+ * MariaDB 5.5, 10.0, 10.1, 10.2, 10.3
449
554
 
450
- ### Active Record
555
+ ### Ruby on Rails / Active Record
451
556
 
452
- * mysql2 0.2.x includes an Active Record driver compatible with AR 2.3 and 3.0
453
- * mysql2 0.3.x does not include an AR driver because it is included in AR 3.1 and above
557
+ * mysql2 0.5.x works with Rails / Active Record 5.0.7, 5.1.6, and higher.
558
+ * mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.
559
+ * mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.
560
+ * mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.
454
561
 
455
562
  ### Asynchronous Active Record
456
563
 
@@ -533,7 +640,11 @@ though.
533
640
  ## Special Thanks
534
641
 
535
642
  * Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
536
- * Yury Korolev (http://github.com/yury) - for TONS of help testing the Active Record adapter
537
- * Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
538
- * Mike Perham (http://github.com/mperham) - Async Active Record adapter (uses Fibers and EventMachine)
539
- * Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support.
643
+ * [Yury Korolev](http://github.com/yury) - for TONS of help testing the Active Record adapter
644
+ * [Aaron Patterson](http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
645
+ * [Mike Perham](http://github.com/mperham) - Async Active Record adapter (uses Fibers and EventMachine)
646
+ * [Aaron Stone](http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support
647
+ * [Kouhei Ueno](https://github.com/nyaxt) - for the original work on Prepared Statements way back in 2012
648
+ * [John Cant](http://github.com/johncant) - polishing and updating Prepared Statements support
649
+ * [Justin Case](http://github.com/justincase) - polishing and updating Prepared Statements support and getting it merged
650
+ * [Tamir Duberstein](http://github.com/tamird) - for help with timeouts and all around updates and cleanups