mysql2 0.3.18 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/LICENSE +21 -0
  4. data/README.md +135 -55
  5. data/examples/eventmachine.rb +1 -1
  6. data/examples/threaded.rb +4 -6
  7. data/ext/mysql2/client.c +368 -197
  8. data/ext/mysql2/client.h +13 -3
  9. data/ext/mysql2/extconf.rb +118 -35
  10. data/ext/mysql2/infile.c +2 -2
  11. data/ext/mysql2/mysql2_ext.c +1 -0
  12. data/ext/mysql2/mysql2_ext.h +7 -6
  13. data/ext/mysql2/mysql_enc_name_to_ruby.h +2 -2
  14. data/ext/mysql2/mysql_enc_to_ruby.h +25 -22
  15. data/ext/mysql2/result.c +512 -138
  16. data/ext/mysql2/result.h +13 -6
  17. data/ext/mysql2/statement.c +585 -0
  18. data/ext/mysql2/statement.h +19 -0
  19. data/lib/mysql2/client.rb +85 -26
  20. data/lib/mysql2/console.rb +1 -1
  21. data/lib/mysql2/em.rb +5 -6
  22. data/lib/mysql2/error.rb +18 -27
  23. data/lib/mysql2/field.rb +3 -0
  24. data/lib/mysql2/statement.rb +17 -0
  25. data/lib/mysql2/version.rb +1 -1
  26. data/lib/mysql2.rb +38 -18
  27. data/spec/configuration.yml.example +0 -6
  28. data/spec/em/em_spec.rb +22 -21
  29. data/spec/mysql2/client_spec.rb +525 -388
  30. data/spec/mysql2/error_spec.rb +38 -39
  31. data/spec/mysql2/result_spec.rb +223 -214
  32. data/spec/mysql2/statement_spec.rb +757 -0
  33. data/spec/spec_helper.rb +80 -59
  34. data/spec/ssl/ca-cert.pem +17 -0
  35. data/spec/ssl/ca-key.pem +27 -0
  36. data/spec/ssl/ca.cnf +22 -0
  37. data/spec/ssl/cert.cnf +22 -0
  38. data/spec/ssl/client-cert.pem +17 -0
  39. data/spec/ssl/client-key.pem +27 -0
  40. data/spec/ssl/client-req.pem +15 -0
  41. data/spec/ssl/gen_certs.sh +48 -0
  42. data/spec/ssl/pkcs8-client-key.pem +28 -0
  43. data/spec/ssl/pkcs8-server-key.pem +28 -0
  44. data/spec/ssl/server-cert.pem +17 -0
  45. data/spec/ssl/server-key.pem +27 -0
  46. data/spec/ssl/server-req.pem +15 -0
  47. data/support/mysql_enc_to_ruby.rb +7 -8
  48. data/support/ruby_enc_to_mysql.rb +1 -1
  49. metadata +42 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 791bc0352c6464ca1765498b0babcedfad3c284e
4
- data.tar.gz: 08b44b761494ec18a0c9903fcb67bfe61718b77b
3
+ metadata.gz: 49904d19056ae4c90f12a539d56dfc3789fd8bc8
4
+ data.tar.gz: a9c541dd2693bc2bce4eba0a81654e0cefd2d704
5
5
  SHA512:
6
- metadata.gz: 5554897f395a4fcd71e26612ef895da5d3e1d528ef29604380974950f7216783bf7636197d04228c39b471a51863f75c56e9140e05bb79410e1582c64842d0dc
7
- data.tar.gz: afad060a7a899d69b75ca2c44aa69a07c743984fc6897cce825c6d9ec3269bbb3733671243afe7fb33a615e61e4aa8c7b9e7863fa18289578ab29abc8409e2e8
6
+ metadata.gz: 6648cd5f75da53d33a4d2630d138264974bc8bf902b68ebe988a261a6ed7c4e91ad21939153b3bd78a2cd5202a26f03dfac3834a0fc074464aeef61ea8417c45
7
+ data.tar.gz: 76e9e4b9a2d35d87011de33426c88600b070b5cc600aede7e522d9a5f43186d3e19421f8ee540a52a265d1e19d25d6bdbb4bd1732e71ff234e3d634cdf0f99f4
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ Changes are maintained under [Releases](https://github.com/brianmario/mysql2/releases)
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Brian Lopez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -9,12 +9,14 @@ This one is not.
9
9
 
10
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.
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
19
21
  ### General Instructions
20
22
  ``` sh
@@ -56,6 +58,20 @@ This may be needed if you deploy to a system where these libraries
56
58
  are located somewhere different than on your build system.
57
59
  This overrides any rpath calculated by default or by the options above.
58
60
 
61
+ * `--with-sanitize[=address,cfi,integer,memory,thread,undefined]` -
62
+ Enable sanitizers for Clang / GCC. If no argument is given, try to enable
63
+ all sanitizers or fail if none are available. If a command-separated list of
64
+ specific sanitizers is given, configure will fail unless they all are available.
65
+ Note that the some sanitizers may incur a performance penalty, and the Address
66
+ Sanitizer may require a runtime library.
67
+ To see line numbers in backtraces, declare these environment variables
68
+ (adjust the llvm-symbolizer path as needed for your system):
69
+
70
+ ``` sh
71
+ export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
72
+ export ASAN_OPTIONS=symbolize=1
73
+ ```
74
+
59
75
  ### Linux and other Unixes
60
76
 
61
77
  You may need to install a package such as `libmysqlclient-dev` or `mysql-devel`;
@@ -69,6 +85,9 @@ You may use MacPorts, Homebrew, or a native MySQL installer package. The most
69
85
  common paths will be automatically searched. If you want to select a specific
70
86
  MySQL directory, use the `--with-mysql-dir` or `--with-mysql-config` options above.
71
87
 
88
+ If you have not done so already, you will need to install the XCode select tools by running
89
+ `xcode-select --install`.
90
+
72
91
  ### Windows
73
92
  Make sure that you have Ruby and the DevKit compilers installed. We recommend
74
93
  the [Ruby Installer](http://rubyinstaller.org) distribution.
@@ -93,7 +112,7 @@ Connect to a database:
93
112
  ``` ruby
94
113
  # this takes a hash of options, almost all of which map directly
95
114
  # to the familiar database.yml in rails
96
- # See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
115
+ # See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html
97
116
  client = Mysql2::Client.new(:host => "localhost", :username => "root")
98
117
  ```
99
118
 
@@ -148,11 +167,25 @@ by the query like this:
148
167
  ``` ruby
149
168
  headers = results.fields # <= that's an array of field names, in order
150
169
  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"
170
+ # Each row is an array, ordered the same as the query results
171
+ # An otter's den is called a "holt" or "couch"
153
172
  end
154
173
  ```
155
174
 
175
+ Prepared statements are supported, as well. In a prepared statement, use a `?`
176
+ in place of each value and then execute the statement to retrieve a result set.
177
+ Pass your arguments to the execute method in the same number and order as the
178
+ question marks in the statement.
179
+
180
+ ``` ruby
181
+ statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
182
+ result1 = statement.execute(1)
183
+ result2 = statement.execute(2)
184
+
185
+ statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
186
+ result = statement.execute(1, "CA")
187
+ ```
188
+
156
189
  ## Connection options
157
190
 
158
191
  You may set the following connection options in Mysql2::Client.new(...):
@@ -173,12 +206,28 @@ Mysql2::Client.new(
173
206
  :reconnect = true/false,
174
207
  :local_infile = true/false,
175
208
  :secure_auth = true/false,
209
+ :ssl_mode = :disabled / :preferred / :required / :verify_ca / :verify_identity,
176
210
  :default_file = '/path/to/my.cfg',
177
211
  :default_group = 'my.cfg section',
178
212
  :init_command => sql
179
213
  )
180
214
  ```
181
215
 
216
+ ### Connecting to localhost
217
+
218
+ The underlying MySQL client library has a special interpretation of the "localhost" default connection host name:
219
+
220
+ 1. Attempt to connect via local socket (as specified in your distribution's my.cnf or `default_file` config file).
221
+ 2. But if the socket is not available, look up "localhost" to find an IP address...
222
+ * The file "/etc/hosts" is generally the source of truth for "localhost", and can override its value.
223
+ * Systems with IPv4 use "127.0.0.1"
224
+ * Systems with IPv6 use "::1"
225
+ * Systems with both IPv4 and IPv6 can be configured to prefer one or the other.
226
+ 3. If an address is found for "localhost", connect to it over TCP/IP.
227
+
228
+ You can be explicit about using either a local socket or a TCP/IP connection,
229
+ and whether to use IPv4 or IPv6, by specifying a value other than "localhost"
230
+ for the `host` or `socket` connection options.
182
231
 
183
232
  ### SSL options
184
233
 
@@ -186,7 +235,8 @@ Setting any of the following options will enable an SSL connection, but only if
186
235
  your MySQL client library and server have been compiled with SSL support.
187
236
  MySQL client library defaults will be used for any parameters that are left out
188
237
  or set to nil. Relative paths are allowed, and may be required by managed
189
- hosting providers such as Heroku.
238
+ hosting providers such as Heroku. Set `:sslverify => true` to require that the
239
+ server presents a valid certificate.
190
240
 
191
241
  ``` ruby
192
242
  Mysql2::Client.new(
@@ -195,10 +245,67 @@ Mysql2::Client.new(
195
245
  :sslcert => '/path/to/client-cert.pem',
196
246
  :sslca => '/path/to/ca-cert.pem',
197
247
  :sslcapath => '/path/to/cacerts',
198
- :sslcipher => 'DHE-RSA-AES256-SHA'
248
+ :sslcipher => 'DHE-RSA-AES256-SHA',
249
+ :sslverify => true,
199
250
  )
200
251
  ```
201
252
 
253
+ ### Secure auth
254
+
255
+ Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
256
+ When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
257
+ The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
258
+ To bypass this restriction in the client, pass the option `:secure_auth => false` to Mysql2::Client.new().
259
+
260
+ ### Flags option parsing
261
+
262
+ The `:flags` parameter accepts an integer, a string, or an array. The integer
263
+ form allows the client to assemble flags from constants defined under
264
+ `Mysql2::Client` such as `Mysql2::Client::FOUND_ROWS`. Use a bitwise `|` (OR)
265
+ to specify several flags.
266
+
267
+ The string form will be split on whitespace and parsed as with the array form:
268
+ Plain flags are added to the default flags, while flags prefixed with `-`
269
+ (minus) are removed from the default flags.
270
+
271
+ This allows easier use with ActiveRecord's database.yml, avoiding the need for magic flag numbers.
272
+ For example, to disable protocol compression, and enable multiple statements and result sets:
273
+
274
+ ``` yaml
275
+ development:
276
+ adapter: mysql2
277
+ encoding: utf8
278
+ database: my_db_name
279
+ username: root
280
+ password: my_password
281
+ host: 127.0.0.1
282
+ port: 3306
283
+ flags:
284
+ - -COMPRESS
285
+ - FOUND_ROWS
286
+ - MULTI_STATEMENTS
287
+ secure_auth: false
288
+ ```
289
+
290
+ ### Reading a MySQL config file
291
+
292
+ You may read configuration options from a MySQL configuration file by passing
293
+ the `:default_file` and `:default_group` parameters. For example:
294
+
295
+ ``` ruby
296
+ Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
297
+ ```
298
+
299
+ ### Initial command on connect and reconnect
300
+
301
+ If you specify the `:init_command` option, the SQL string you provide will be executed after the connection is established.
302
+ If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
303
+ It is useful if you want to provide session options which survive reconnection.
304
+
305
+ ``` ruby
306
+ Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
307
+ ```
308
+
202
309
  ### Multiple result sets
203
310
 
204
311
  You can also retrieve multiple result sets. For this to work you need to
@@ -240,47 +347,6 @@ Yields:
240
347
  next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
241
348
  ```
242
349
 
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
350
  ## Cascading config
285
351
 
286
352
  The default config hash is at:
@@ -351,6 +417,15 @@ client = Mysql2::Client.new
351
417
  result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
352
418
  ```
353
419
 
420
+ Keep in mind that this works only with fields and not with computed values, e.g. this result will contain `1`, not `true`:
421
+
422
+ ``` ruby
423
+ client = Mysql2::Client.new
424
+ result = client.query("SELECT true", :cast_booleans => true)
425
+ ```
426
+
427
+ 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).
428
+
354
429
  ### Skipping casting
355
430
 
356
431
  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.)
@@ -437,20 +512,21 @@ As for field values themselves, I'm workin on it - but expect that soon.
437
512
 
438
513
  This gem is tested with the following Ruby versions on Linux and Mac OS X:
439
514
 
440
- * Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x (ongoing patch releases)
515
+ * Ruby MRI 1.8.7, 1.9.3, 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x
441
516
  * Ruby Enterprise Edition (based on MRI 1.8.7)
442
- * Rubinius 2.x
517
+ * Rubinius 2.x and 3.x do work but may fail under some workloads
443
518
 
444
519
  This gem is tested with the following MySQL and MariaDB versions:
445
520
 
446
- * MySQL 5.0, 5.1, 5.5, 5.6, 5.7
521
+ * MySQL 5.5, 5.6, 5.7, 8.0
447
522
  * MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
448
- * MariaDB 5.5, 10.0
523
+ * MariaDB 5.5, 10.0, 10.1
449
524
 
450
- ### Active Record
525
+ ### Ruby on Rails / Active Record
451
526
 
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
527
+ * mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.
528
+ * mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.
529
+ * mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.
454
530
 
455
531
  ### Asynchronous Active Record
456
532
 
@@ -536,4 +612,8 @@ though.
536
612
  * Yury Korolev (http://github.com/yury) - for TONS of help testing the Active Record adapter
537
613
  * Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
538
614
  * 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.
615
+ * Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support
616
+ * Kouhei Ueno (https://github.com/nyaxt) - for the original work on Prepared Statements way back in 2012
617
+ * John Cant (http://github.com/johncant) - polishing and updating Prepared Statements support
618
+ * Justin Case (http://github.com/justincase) - polishing and updating Prepared Statements support and getting it merged
619
+ * Tamir Duberstein (http://github.com/tamird) - for help with timeouts and all around updates and cleanups
@@ -18,4 +18,4 @@ EM.run do
18
18
  defer2.callback do |result|
19
19
  puts "Result: #{result.to_a.inspect}"
20
20
  end
21
- end
21
+ end
data/examples/threaded.rb CHANGED
@@ -4,17 +4,15 @@ $LOAD_PATH.unshift 'lib'
4
4
  require 'mysql2'
5
5
  require 'timeout'
6
6
 
7
- threads = []
8
7
  # Should never exceed worst case 3.5 secs across all 20 threads
9
8
  Timeout.timeout(3.5) do
10
- 20.times do
11
- threads << Thread.new do
9
+ 20.times.map do
10
+ Thread.new do
12
11
  overhead = rand(3)
13
12
  puts ">> thread #{Thread.current.object_id} query, #{overhead} sec overhead"
14
13
  # 3 second overhead per query
15
14
  Mysql2::Client.new(:host => "localhost", :username => "root").query("SELECT sleep(#{overhead}) as result")
16
15
  puts "<< thread #{Thread.current.object_id} result, #{overhead} sec overhead"
17
16
  end
18
- end
19
- threads.each{|t| t.join }
20
- end
17
+ end.each(&:join)
18
+ end