mysql2 0.3.18 → 0.4.5

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 +81 -19
  5. data/examples/eventmachine.rb +1 -1
  6. data/examples/threaded.rb +4 -6
  7. data/ext/mysql2/client.c +307 -174
  8. data/ext/mysql2/client.h +12 -1
  9. data/ext/mysql2/extconf.rb +115 -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 +5 -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 +12 -6
  17. data/ext/mysql2/statement.c +574 -0
  18. data/ext/mysql2/statement.h +19 -0
  19. data/lib/mysql2/client.rb +84 -25
  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 +21 -21
  29. data/spec/mysql2/client_spec.rb +461 -362
  30. data/spec/mysql2/error_spec.rb +37 -36
  31. data/spec/mysql2/result_spec.rb +222 -208
  32. data/spec/mysql2/statement_spec.rb +759 -0
  33. data/spec/spec_helper.rb +7 -0
  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: d82596af22ef47aa19d633dabb82afc9928c3f3f
4
+ data.tar.gz: 09b024bdf203f596f35b4df22ed3149b19af6feb
5
5
  SHA512:
6
- metadata.gz: 5554897f395a4fcd71e26612ef895da5d3e1d528ef29604380974950f7216783bf7636197d04228c39b471a51863f75c56e9140e05bb79410e1582c64842d0dc
7
- data.tar.gz: afad060a7a899d69b75ca2c44aa69a07c743984fc6897cce825c6d9ec3269bbb3733671243afe7fb33a615e61e4aa8c7b9e7863fa18289578ab29abc8409e2e8
6
+ metadata.gz: 23f6e023618c65e82d5f4289242257449da6138e731f27cc45553aac2ffd5b712350d59e5b5e90c0e8961795394fc437007ba8651e1a2b6863a3d6951b0a6c16
7
+ data.tar.gz: 03bede95a81a4dc20b1dfc288a5f5807eb8d7c243b091e03f6809623d2e30726c5b69dedd9de7b94ca6a56ce13c854927c2241e528c7bb8aa42cc39dc235eba7
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`;
@@ -148,11 +164,25 @@ by the query like this:
148
164
  ``` ruby
149
165
  headers = results.fields # <= that's an array of field names, in order
150
166
  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"
167
+ # Each row is an array, ordered the same as the query results
168
+ # An otter's den is called a "holt" or "couch"
153
169
  end
154
170
  ```
155
171
 
172
+ Prepared statements are supported, as well. In a prepared statement, use a `?`
173
+ in place of each value and then execute the statement to retrieve a result set.
174
+ Pass your arguments to the execute method in the same number and order as the
175
+ question marks in the statement.
176
+
177
+ ``` ruby
178
+ statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
179
+ result1 = statement.execute(1)
180
+ result2 = statement.execute(2)
181
+
182
+ statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
183
+ result = statement.execute(1, "CA")
184
+ ```
185
+
156
186
  ## Connection options
157
187
 
158
188
  You may set the following connection options in Mysql2::Client.new(...):
@@ -173,6 +203,7 @@ Mysql2::Client.new(
173
203
  :reconnect = true/false,
174
204
  :local_infile = true/false,
175
205
  :secure_auth = true/false,
206
+ :ssl_mode = :disabled / :preferred / :required / :verify_ca / :verify_identity,
176
207
  :default_file = '/path/to/my.cfg',
177
208
  :default_group = 'my.cfg section',
178
209
  :init_command => sql
@@ -186,7 +217,8 @@ Setting any of the following options will enable an SSL connection, but only if
186
217
  your MySQL client library and server have been compiled with SSL support.
187
218
  MySQL client library defaults will be used for any parameters that are left out
188
219
  or set to nil. Relative paths are allowed, and may be required by managed
189
- hosting providers such as Heroku.
220
+ hosting providers such as Heroku. Set `:sslverify => true` to require that the
221
+ server presents a valid certificate.
190
222
 
191
223
  ``` ruby
192
224
  Mysql2::Client.new(
@@ -195,7 +227,8 @@ Mysql2::Client.new(
195
227
  :sslcert => '/path/to/client-cert.pem',
196
228
  :sslca => '/path/to/ca-cert.pem',
197
229
  :sslcapath => '/path/to/cacerts',
198
- :sslcipher => 'DHE-RSA-AES256-SHA'
230
+ :sslcipher => 'DHE-RSA-AES256-SHA',
231
+ :sslverify => true,
199
232
  )
200
233
  ```
201
234
 
@@ -240,15 +273,26 @@ Yields:
240
273
  next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
241
274
  ```
242
275
 
243
- See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
244
-
245
276
  ### Secure auth
246
277
 
247
278
  Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
248
279
  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
280
  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:
281
+ To bypass this restriction in the client, pass the option `:secure_auth => false` to Mysql2::Client.new().
282
+
283
+ ### Flags option parsing
284
+
285
+ The `:flags` parameter accepts an integer, a string, or an array. The integer
286
+ form allows the client to assemble flags from constants defined under
287
+ `Mysql2::Client` such as `Mysql2::Client::FOUND_ROWS`. Use a bitwise `|` (OR)
288
+ to specify several flags.
289
+
290
+ The string form will be split on whitespace and parsed as with the array form:
291
+ Plain flags are added to the default flags, while flags prefixed with `-`
292
+ (minus) are removed from the default flags.
293
+
294
+ This allows easier use with ActiveRecord's database.yml, avoiding the need for magic flag numbers.
295
+ For example, to disable protocol compression, and enable multiple statements and result sets:
252
296
 
253
297
  ``` yaml
254
298
  development:
@@ -259,13 +303,17 @@ development:
259
303
  password: my_password
260
304
  host: 127.0.0.1
261
305
  port: 3306
306
+ flags:
307
+ - -COMPRESS
308
+ - FOUND_ROWS
309
+ - MULTI_STATEMENTS
262
310
  secure_auth: false
263
311
  ```
264
312
 
265
313
  ### Reading a MySQL config file
266
314
 
267
315
  You may read configuration options from a MySQL configuration file by passing
268
- the `:default_file` and `:default_group` paramters. For example:
316
+ the `:default_file` and `:default_group` parameters. For example:
269
317
 
270
318
  ``` ruby
271
319
  Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
@@ -273,7 +321,7 @@ Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
273
321
 
274
322
  ### Initial command on connect and reconnect
275
323
 
276
- If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
324
+ If you specify the `:init_command` option, the SQL string you provide will be executed after the connection is established.
277
325
  If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
278
326
  It is useful if you want to provide session options which survive reconnection.
279
327
 
@@ -351,6 +399,15 @@ client = Mysql2::Client.new
351
399
  result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
352
400
  ```
353
401
 
402
+ Keep in mind that this works only with fields and not with computed values, e.g. this result will contain `1`, not `true`:
403
+
404
+ ``` ruby
405
+ client = Mysql2::Client.new
406
+ result = client.query("SELECT true", :cast_booleans => true)
407
+ ```
408
+
409
+ 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).
410
+
354
411
  ### Skipping casting
355
412
 
356
413
  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 +494,21 @@ As for field values themselves, I'm workin on it - but expect that soon.
437
494
 
438
495
  This gem is tested with the following Ruby versions on Linux and Mac OS X:
439
496
 
440
- * Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x (ongoing patch releases)
497
+ * Ruby MRI 1.8.7, 1.9.3, 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x
441
498
  * Ruby Enterprise Edition (based on MRI 1.8.7)
442
- * Rubinius 2.x
499
+ * Rubinius 2.x and 3.x do work but may fail under some workloads
443
500
 
444
501
  This gem is tested with the following MySQL and MariaDB versions:
445
502
 
446
- * MySQL 5.0, 5.1, 5.5, 5.6, 5.7
503
+ * MySQL 5.5, 5.6, 5.7, 8.0
447
504
  * MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
448
- * MariaDB 5.5, 10.0
505
+ * MariaDB 5.5, 10.0, 10.1
449
506
 
450
- ### Active Record
507
+ ### Ruby on Rails / Active Record
451
508
 
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
509
+ * mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.
510
+ * mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.
511
+ * mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.
454
512
 
455
513
  ### Asynchronous Active Record
456
514
 
@@ -536,4 +594,8 @@ though.
536
594
  * Yury Korolev (http://github.com/yury) - for TONS of help testing the Active Record adapter
537
595
  * Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
538
596
  * 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.
597
+ * Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support
598
+ * Kouhei Ueno (https://github.com/nyaxt) - for the original work on Prepared Statements way back in 2012
599
+ * John Cant (http://github.com/johncant) - polishing and updating Prepared Statements support
600
+ * Justin Case (http://github.com/justincase) - polishing and updating Prepared Statements support and getting it merged
601
+ * 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