mysql2 0.2.24 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/CHANGELOG.md +148 -0
- data/Gemfile +3 -0
- data/README.rdoc +257 -0
- data/Rakefile +5 -0
- data/benchmark/active_record.rb +51 -0
- data/benchmark/active_record_threaded.rb +42 -0
- data/benchmark/allocations.rb +33 -0
- data/benchmark/escape.rb +36 -0
- data/benchmark/query_with_mysql_casting.rb +80 -0
- data/benchmark/query_without_mysql_casting.rb +47 -0
- data/benchmark/sequel.rb +37 -0
- data/benchmark/setup_db.rb +119 -0
- data/benchmark/threaded.rb +44 -0
- data/ext/mysql2/client.c +272 -849
- data/ext/mysql2/client.h +12 -27
- data/ext/mysql2/extconf.rb +14 -72
- data/ext/mysql2/mysql2_ext.h +4 -7
- data/ext/mysql2/result.c +123 -319
- data/ext/mysql2/result.h +1 -4
- data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +64 -0
- data/lib/active_record/fiber_patches.rb +104 -0
- data/lib/mysql2.rb +5 -20
- data/lib/mysql2/client.rb +200 -50
- data/lib/mysql2/em.rb +3 -13
- data/lib/mysql2/em_fiber.rb +31 -0
- data/lib/mysql2/error.rb +6 -71
- data/lib/mysql2/version.rb +2 -2
- data/mysql2.gemspec +32 -0
- data/spec/em/em_fiber_spec.rb +22 -0
- data/spec/em/em_spec.rb +9 -74
- data/spec/mysql2/client_spec.rb +126 -593
- data/spec/mysql2/error_spec.rb +44 -58
- data/spec/mysql2/result_spec.rb +85 -257
- data/spec/spec_helper.rb +3 -24
- data/tasks/benchmarks.rake +20 -0
- data/tasks/compile.rake +71 -0
- data/tasks/rspec.rake +16 -0
- data/tasks/vendor_mysql.rake +40 -0
- metadata +179 -92
- checksums.yaml +0 -7
- data/README.md +0 -524
- data/ext/mysql2/infile.c +0 -122
- data/ext/mysql2/infile.h +0 -1
- data/ext/mysql2/mysql_enc_name_to_ruby.h +0 -168
- data/ext/mysql2/mysql_enc_to_ruby.h +0 -246
- data/ext/mysql2/wait_for_single_fd.h +0 -36
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -635
- data/lib/arel/engines/sql/compilers/mysql2_compiler.rb +0 -11
- data/lib/mysql2/console.rb +0 -5
- data/spec/configuration.yml.example +0 -17
- data/spec/my.cnf.example +0 -9
- data/spec/test_data +0 -1
- data/support/mysql_enc_to_ruby.rb +0 -82
- data/support/ruby_enc_to_mysql.rb +0 -61
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 53c6d16a0c12323bd8da93f743e94a27236d37f1
|
4
|
-
data.tar.gz: 278f215f56357c0c606359dd5565c4a9ec1bdc2a
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: a4d27f578c9b0a772f3e7835fe34bcfb0844dfca568a12f244f86aaea706abb5523ed665c7cac44714f49780a1d04afa4f9264a2fcf0ec1272fc394980d97a8b
|
7
|
-
data.tar.gz: 570628b590e4abc575f7699997b101133ee56c70ae0a06c2ed0a530621d9a77c78acd7cc81cf75ff5b2af8d23f31fe618c2d73314f3d677159aabac0095107e0
|
data/README.md
DELETED
@@ -1,524 +0,0 @@
|
|
1
|
-
# Mysql2 - A modern, simple and very fast MySQL library for Ruby - binding to libmysql
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/brianmario/mysql2.png)](https://travis-ci.org/brianmario/mysql2)
|
4
|
-
|
5
|
-
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
|
6
|
-
Some database libraries out there serve as direct 1:1 mappings of the already complex C APIs available.
|
7
|
-
This one is not.
|
8
|
-
|
9
|
-
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
|
-
|
11
|
-
The API consists of two classes:
|
12
|
-
|
13
|
-
`Mysql2::Client` - your connection to the database.
|
14
|
-
|
15
|
-
`Mysql2::Result` - returned from issuing a #query on the connection. It includes Enumerable.
|
16
|
-
|
17
|
-
## Installing
|
18
|
-
### OSX / Linux
|
19
|
-
``` sh
|
20
|
-
gem install mysql2
|
21
|
-
```
|
22
|
-
|
23
|
-
This gem links against MySQL's `libmysqlclient` C shared library. You may need to install a package such as `libmysqlclient-dev`, `mysql-devel`, or other appropriate package for your system.
|
24
|
-
|
25
|
-
By default, the mysql2 gem will try to find a copy of MySQL in this order:
|
26
|
-
|
27
|
-
* Option `--with-mysql-dir`, if provided (see below).
|
28
|
-
* Option `--with-mysql-config`, if provided (see below).
|
29
|
-
* Several typical paths for `mysql_config` (default for the majority of users).
|
30
|
-
* The directory `/usr/local`.
|
31
|
-
|
32
|
-
### Configuration options
|
33
|
-
|
34
|
-
Use these options by `gem install mysql2 -- [--optionA] [--optionB=argument]`.
|
35
|
-
|
36
|
-
* `--with-mysql-dir[=/path/to/mysqldir]` -
|
37
|
-
Specify the directory where MySQL is installed. The mysql2 gem will not use
|
38
|
-
`mysql_config`, but will instead look at `mysqldir/lib` and `mysqldir/include`
|
39
|
-
for the library and header files.
|
40
|
-
This option is mutually exclusive with `--with-mysql-config`.
|
41
|
-
|
42
|
-
* `--with-mysql-config[=/path/to/mysql_config]` -
|
43
|
-
Specify a path to the `mysql_config` binary provided by your copy of MySQL. The
|
44
|
-
mysql2 gem will ask this `mysql_config` binary about the compiler and linker
|
45
|
-
arguments needed.
|
46
|
-
This option is mutually exclusive with `--with-mysql-dir`.
|
47
|
-
|
48
|
-
* `--with-mysql-rpath=/path/to/mysql/lib` / `--without-mysql-rpath` -
|
49
|
-
Override the runtime path used to find the MySQL libraries.
|
50
|
-
This may be needed if you deploy to a system where these libraries
|
51
|
-
are located somewhere different than on your build system.
|
52
|
-
This overrides any rpath calculated by default or by the options above.
|
53
|
-
|
54
|
-
### Windows
|
55
|
-
First, make sure you have the DevKit installed (http://rubyinstaller.org/downloads/) and its variables
|
56
|
-
are loaded by running devkit\devktvars.bat .
|
57
|
-
|
58
|
-
Next, you need a MySQL library to link against. If you have MySQL loaded on your development machine,
|
59
|
-
you can use that. If not, you will need to either copy the MySQL directory from your server, or else
|
60
|
-
obtain a copy of the MySQL C connector: http://dev.mysql.com/downloads/connector/c/
|
61
|
-
|
62
|
-
If you're using the connector, I recommend just getting the .zip file and unzipping it someplace convenient.
|
63
|
-
|
64
|
-
Now you can install mysql2. You must use the `--with-mysql-dir` option to tell gem where your MySQL library
|
65
|
-
files are. For example, if you unzipped the connector to c:\mysql-connector-c-6.1.1-win32 you would install
|
66
|
-
the gem like this:
|
67
|
-
|
68
|
-
gem install mysql2 -- --with-mysql-dir=c:\mysql-connector-c-6.1.1-win32
|
69
|
-
|
70
|
-
Finally, you must copy libmysql.dll from the lib subdirectory of your MySQL or MySQL connector directory into
|
71
|
-
your ruby\bin directory. In the above example, libmysql.dll would be located at
|
72
|
-
c:\mysql-connector-c-6.1.1-win32\lib .
|
73
|
-
|
74
|
-
## Usage
|
75
|
-
|
76
|
-
Connect to a database:
|
77
|
-
|
78
|
-
``` ruby
|
79
|
-
# this takes a hash of options, almost all of which map directly
|
80
|
-
# to the familiar database.yml in rails
|
81
|
-
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
|
82
|
-
client = Mysql2::Client.new(:host => "localhost", :username => "root")
|
83
|
-
```
|
84
|
-
|
85
|
-
Then query it:
|
86
|
-
|
87
|
-
``` ruby
|
88
|
-
results = client.query("SELECT * FROM users WHERE group='githubbers'")
|
89
|
-
```
|
90
|
-
|
91
|
-
Need to escape something first?
|
92
|
-
|
93
|
-
``` ruby
|
94
|
-
escaped = client.escape("gi'thu\"bbe\0r's")
|
95
|
-
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
|
96
|
-
```
|
97
|
-
|
98
|
-
You can get a count of your results with `results.count`.
|
99
|
-
|
100
|
-
Finally, iterate over the results:
|
101
|
-
|
102
|
-
``` ruby
|
103
|
-
results.each do |row|
|
104
|
-
# conveniently, row is a hash
|
105
|
-
# the keys are the fields, as you'd expect
|
106
|
-
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
|
107
|
-
puts row["id"] # row["id"].class == Fixnum
|
108
|
-
if row["dne"] # non-existant hash entry is nil
|
109
|
-
puts row["dne"]
|
110
|
-
end
|
111
|
-
end
|
112
|
-
```
|
113
|
-
|
114
|
-
Or, you might just keep it simple:
|
115
|
-
|
116
|
-
``` ruby
|
117
|
-
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
|
118
|
-
# do something with row, it's ready to rock
|
119
|
-
end
|
120
|
-
```
|
121
|
-
|
122
|
-
How about with symbolized keys?
|
123
|
-
|
124
|
-
``` ruby
|
125
|
-
client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true) do |row|
|
126
|
-
# do something with row, it's ready to rock
|
127
|
-
end
|
128
|
-
```
|
129
|
-
|
130
|
-
You can get the headers and the columns in the order that they were returned
|
131
|
-
by the query like this:
|
132
|
-
|
133
|
-
``` ruby
|
134
|
-
headers = results.fields # <= that's an array of field names, in order
|
135
|
-
results.each(:as => :array) do |row|
|
136
|
-
# Each row is an array, ordered the same as the query results
|
137
|
-
# An otter's den is called a "holt" or "couch"
|
138
|
-
end
|
139
|
-
```
|
140
|
-
|
141
|
-
## Connection options
|
142
|
-
|
143
|
-
You may set the following connection options in Mysql2::Client.new(...):
|
144
|
-
|
145
|
-
``` ruby
|
146
|
-
Mysql2::Client.new(
|
147
|
-
:host,
|
148
|
-
:username,
|
149
|
-
:password,
|
150
|
-
:port,
|
151
|
-
:database,
|
152
|
-
:socket = '/path/to/mysql.sock',
|
153
|
-
:flags = REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | MULTI_STATEMENTS,
|
154
|
-
:encoding = 'utf8',
|
155
|
-
:read_timeout = seconds,
|
156
|
-
:write_timeout = seconds,
|
157
|
-
:connect_timeout = seconds,
|
158
|
-
:reconnect = true/false,
|
159
|
-
:local_infile = true/false,
|
160
|
-
:secure_auth = true/false,
|
161
|
-
:default_file = '/path/to/my.cfg',
|
162
|
-
:default_group = 'my.cfg section',
|
163
|
-
:init_command => sql
|
164
|
-
)
|
165
|
-
```
|
166
|
-
|
167
|
-
|
168
|
-
### SSL options
|
169
|
-
|
170
|
-
Setting any of the following options will enable an SSL connection, but only if
|
171
|
-
your MySQL client library and server have been compiled with SSL support.
|
172
|
-
MySQL client library defaults will be used for any parameters that are left out
|
173
|
-
or set to nil. Relative paths are allowed, and may be required by managed
|
174
|
-
hosting providers such as Heroku.
|
175
|
-
|
176
|
-
``` ruby
|
177
|
-
Mysql2::Client.new(
|
178
|
-
# ...options as above...,
|
179
|
-
:sslkey => '/path/to/client-key.pem',
|
180
|
-
:sslcert => '/path/to/client-cert.pem',
|
181
|
-
:sslca => '/path/to/ca-cert.pem',
|
182
|
-
:sslcapath => '/path/to/cacerts',
|
183
|
-
:sslcipher => 'DHE-RSA-AES256-SHA'
|
184
|
-
)
|
185
|
-
```
|
186
|
-
|
187
|
-
### Multiple result sets
|
188
|
-
|
189
|
-
You can also retrieve multiple result sets. For this to work you need to
|
190
|
-
connect with flags `Mysql2::Client::MULTI_STATEMENTS`. Multiple result sets can
|
191
|
-
be used with stored procedures that return more than one result set, and for
|
192
|
-
bundling several SQL statements into a single call to `client.query`.
|
193
|
-
|
194
|
-
``` ruby
|
195
|
-
client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS)
|
196
|
-
result = client.query('CALL sp_customer_list( 25, 10 )')
|
197
|
-
# result now contains the first result set
|
198
|
-
while client.next_result
|
199
|
-
result = client.store_result
|
200
|
-
# result now contains the next result set
|
201
|
-
end
|
202
|
-
```
|
203
|
-
|
204
|
-
Repeated calls to `client.next_result` will return true, false, or raise an
|
205
|
-
exception if the respective query erred. When `client.next_result` returns true,
|
206
|
-
call `client.store_result` to retrieve a result object. Exceptions are not
|
207
|
-
raised until `client.next_result` is called to find the status of the respective
|
208
|
-
query. Subsequent queries are not executed if an earlier query raised an
|
209
|
-
exception. Subsequent calls to `client.next_result` will return false.
|
210
|
-
|
211
|
-
``` ruby
|
212
|
-
result = client.query('SELECT 1; SELECT 2; SELECT A; SELECT 3')
|
213
|
-
p result.first
|
214
|
-
|
215
|
-
while client.next_result
|
216
|
-
result = client.store_result
|
217
|
-
p result.first
|
218
|
-
end
|
219
|
-
```
|
220
|
-
|
221
|
-
Yields:
|
222
|
-
```
|
223
|
-
{"1"=>1}
|
224
|
-
{"2"=>2}
|
225
|
-
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
|
226
|
-
```
|
227
|
-
|
228
|
-
See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
|
229
|
-
|
230
|
-
### Secure auth
|
231
|
-
|
232
|
-
Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
|
233
|
-
When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
|
234
|
-
The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
|
235
|
-
To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new().
|
236
|
-
If using ActiveRecord, your database.yml might look something like this:
|
237
|
-
|
238
|
-
``` yaml
|
239
|
-
development:
|
240
|
-
adapter: mysql2
|
241
|
-
encoding: utf8
|
242
|
-
database: my_db_name
|
243
|
-
username: root
|
244
|
-
password: my_password
|
245
|
-
host: 127.0.0.1
|
246
|
-
port: 3306
|
247
|
-
secure_auth: false
|
248
|
-
```
|
249
|
-
|
250
|
-
### Reading a MySQL config file
|
251
|
-
|
252
|
-
You may read configuration options from a MySQL configuration file by passing
|
253
|
-
the `:default_file` and `:default_group` paramters. For example:
|
254
|
-
|
255
|
-
``` ruby
|
256
|
-
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
|
257
|
-
```
|
258
|
-
|
259
|
-
### Initial command on connect and reconnect
|
260
|
-
|
261
|
-
If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
|
262
|
-
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
|
263
|
-
It is useful if you want to provide session options which survive reconnection.
|
264
|
-
|
265
|
-
``` ruby
|
266
|
-
Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
|
267
|
-
```
|
268
|
-
|
269
|
-
## Cascading config
|
270
|
-
|
271
|
-
The default config hash is at:
|
272
|
-
|
273
|
-
``` ruby
|
274
|
-
Mysql2::Client.default_query_options
|
275
|
-
```
|
276
|
-
|
277
|
-
which defaults to:
|
278
|
-
|
279
|
-
``` ruby
|
280
|
-
{:async => false, :as => :hash, :symbolize_keys => false}
|
281
|
-
```
|
282
|
-
|
283
|
-
that can be used as so:
|
284
|
-
|
285
|
-
``` ruby
|
286
|
-
# these are the defaults all Mysql2::Client instances inherit
|
287
|
-
Mysql2::Client.default_query_options.merge!(:as => :array)
|
288
|
-
```
|
289
|
-
|
290
|
-
or
|
291
|
-
|
292
|
-
``` ruby
|
293
|
-
# this will change the defaults for all future results returned by the #query method _for this connection only_
|
294
|
-
c = Mysql2::Client.new
|
295
|
-
c.query_options.merge!(:symbolize_keys => true)
|
296
|
-
```
|
297
|
-
|
298
|
-
or
|
299
|
-
|
300
|
-
``` ruby
|
301
|
-
# this will set the options for the Mysql2::Result instance returned from the #query method
|
302
|
-
c = Mysql2::Client.new
|
303
|
-
c.query(sql, :symbolize_keys => true)
|
304
|
-
```
|
305
|
-
|
306
|
-
## Result types
|
307
|
-
|
308
|
-
### Array of Arrays
|
309
|
-
|
310
|
-
Pass the `:as => :array` option to any of the above methods of configuration
|
311
|
-
|
312
|
-
### Array of Hashes
|
313
|
-
|
314
|
-
The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
|
315
|
-
|
316
|
-
### Timezones
|
317
|
-
|
318
|
-
Mysql2 now supports two timezone options:
|
319
|
-
|
320
|
-
``` ruby
|
321
|
-
:database_timezone # this is the timezone Mysql2 will assume fields are already stored as, and will use this when creating the initial Time objects in ruby
|
322
|
-
:application_timezone # this is the timezone Mysql2 will convert to before finally handing back to the caller
|
323
|
-
```
|
324
|
-
|
325
|
-
In other words, if `:database_timezone` is set to `:utc` - Mysql2 will create the Time objects using `Time.utc(...)` from the raw value libmysql hands over initially.
|
326
|
-
Then, if `:application_timezone` is set to say - `:local` - Mysql2 will then convert the just-created UTC Time object to local time.
|
327
|
-
|
328
|
-
Both options only allow two values - `:local` or `:utc` - with the exception that `:application_timezone` can be [and defaults to] nil
|
329
|
-
|
330
|
-
### Casting "boolean" columns
|
331
|
-
|
332
|
-
You can now tell Mysql2 to cast `tinyint(1)` fields to boolean values in Ruby with the `:cast_booleans` option.
|
333
|
-
|
334
|
-
``` ruby
|
335
|
-
client = Mysql2::Client.new
|
336
|
-
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
|
337
|
-
```
|
338
|
-
|
339
|
-
### Skipping casting
|
340
|
-
|
341
|
-
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.)
|
342
|
-
|
343
|
-
``` ruby
|
344
|
-
client = Mysql2::Client.new
|
345
|
-
result = client.query("SELECT * FROM table", :cast => false)
|
346
|
-
```
|
347
|
-
|
348
|
-
Here are the results from the `query_without_mysql_casting.rb` script in the benchmarks folder:
|
349
|
-
|
350
|
-
``` sh
|
351
|
-
user system total real
|
352
|
-
Mysql2 (cast: true) 0.340000 0.000000 0.340000 ( 0.405018)
|
353
|
-
Mysql2 (cast: false) 0.160000 0.010000 0.170000 ( 0.209937)
|
354
|
-
Mysql 0.080000 0.000000 0.080000 ( 0.129355)
|
355
|
-
do_mysql 0.520000 0.010000 0.530000 ( 0.574619)
|
356
|
-
```
|
357
|
-
|
358
|
-
Although Mysql2 performs reasonably well at retrieving uncasted data, it (currently) is not as fast as the Mysql gem. In spite of this small disadvantage, Mysql2 still sports a friendlier interface and doesn't block the entire ruby process when querying.
|
359
|
-
|
360
|
-
### Async
|
361
|
-
|
362
|
-
NOTE: Not supported on Windows.
|
363
|
-
|
364
|
-
`Mysql2::Client` takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
|
365
|
-
But, in order to take full advantage of it in your Ruby code, you can do:
|
366
|
-
|
367
|
-
``` ruby
|
368
|
-
client.query("SELECT sleep(5)", :async => true)
|
369
|
-
```
|
370
|
-
|
371
|
-
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
|
372
|
-
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
|
373
|
-
|
374
|
-
``` ruby
|
375
|
-
# result will be a Mysql2::Result instance
|
376
|
-
result = client.async_result
|
377
|
-
```
|
378
|
-
|
379
|
-
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
|
380
|
-
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
|
381
|
-
If you need multiple query concurrency take a look at using a connection pool.
|
382
|
-
|
383
|
-
### Row Caching
|
384
|
-
|
385
|
-
By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
|
386
|
-
This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
|
387
|
-
|
388
|
-
If you only plan on using each row once, then it's much more efficient to disable this behavior by setting the `:cache_rows` option to false.
|
389
|
-
This would be helpful if you wanted to iterate over the results in a streaming manner. Meaning the GC would cleanup rows you don't need anymore as you're iterating over the result set.
|
390
|
-
|
391
|
-
### Streaming
|
392
|
-
|
393
|
-
`Mysql2::Client` can optionally only fetch rows from the server on demand by setting `:stream => true`. This is handy when handling very large result sets which might not fit in memory on the client.
|
394
|
-
|
395
|
-
``` ruby
|
396
|
-
result = client.query("SELECT * FROM really_big_Table", :stream => true)
|
397
|
-
```
|
398
|
-
|
399
|
-
There are a few things that need to be kept in mind while using streaming:
|
400
|
-
|
401
|
-
* `:cache_rows` is ignored currently. (if you want to use `:cache_rows` you probably don't want to be using `:stream`)
|
402
|
-
* You must fetch all rows in the result set of your query before you can make new queries. (i.e. with `Mysql2::Result#each`)
|
403
|
-
|
404
|
-
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.
|
405
|
-
|
406
|
-
### Lazy Everything
|
407
|
-
|
408
|
-
Well... almost ;)
|
409
|
-
|
410
|
-
Field name strings/symbols are shared across all the rows so only one object is ever created to represent the field name for an entire dataset.
|
411
|
-
|
412
|
-
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
|
413
|
-
For example, if you were to yield 4 rows from a 100 row dataset, only 4 hashes will be created. The rest will sit and wait in C-land until you want them (or when the GC goes to cleanup your `Mysql2::Result` instance).
|
414
|
-
Now say you were to iterate over that same collection again, this time yielding 15 rows - the 4 previous rows that had already been turned into ruby hashes would be pulled from an internal cache, then 11 more would be created and stored in that cache.
|
415
|
-
Once the entire dataset has been converted into ruby objects, Mysql2::Result will free the Mysql C result object as it's no longer needed.
|
416
|
-
|
417
|
-
This caching behavior can be disabled by setting the `:cache_rows` option to false.
|
418
|
-
|
419
|
-
As for field values themselves, I'm workin on it - but expect that soon.
|
420
|
-
|
421
|
-
## Compatibility
|
422
|
-
|
423
|
-
This gem is tested with the following Ruby versions on Linux and Mac OS X:
|
424
|
-
|
425
|
-
* Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x (ongoing patch releases)
|
426
|
-
* Ruby Enterprise Edition (based on MRI 1.8.7)
|
427
|
-
* Rubinius 2.x
|
428
|
-
|
429
|
-
This gem is tested with the following MySQL and MariaDB versions:
|
430
|
-
|
431
|
-
* MySQL 5.0, 5.1, 5.5, 5.6
|
432
|
-
* MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
|
433
|
-
* MariaDB 5.5, 10.0
|
434
|
-
|
435
|
-
### Active Record
|
436
|
-
|
437
|
-
* mysql2 0.2.x includes an Active Record driver compatible with AR 2.3 and 3.0
|
438
|
-
* mysql2 0.3.x does not include an AR driver because it is included in AR 3.1 and above
|
439
|
-
|
440
|
-
### Asynchronous Active Record
|
441
|
-
|
442
|
-
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
|
443
|
-
|
444
|
-
### Sequel
|
445
|
-
|
446
|
-
Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01).
|
447
|
-
Use the prefix "mysql2://" in your connection specification.
|
448
|
-
|
449
|
-
### EventMachine
|
450
|
-
|
451
|
-
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
|
452
|
-
while specifying callbacks for success for failure. Here's a simple example:
|
453
|
-
|
454
|
-
``` ruby
|
455
|
-
require 'mysql2/em'
|
456
|
-
|
457
|
-
EM.run do
|
458
|
-
client1 = Mysql2::EM::Client.new
|
459
|
-
defer1 = client1.query "SELECT sleep(3) as first_query"
|
460
|
-
defer1.callback do |result|
|
461
|
-
puts "Result: #{result.to_a.inspect}"
|
462
|
-
end
|
463
|
-
|
464
|
-
client2 = Mysql2::EM::Client.new
|
465
|
-
defer2 = client2.query "SELECT sleep(1) second_query"
|
466
|
-
defer2.callback do |result|
|
467
|
-
puts "Result: #{result.to_a.inspect}"
|
468
|
-
end
|
469
|
-
end
|
470
|
-
```
|
471
|
-
|
472
|
-
## Benchmarks and Comparison
|
473
|
-
|
474
|
-
The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.
|
475
|
-
|
476
|
-
The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.
|
477
|
-
|
478
|
-
The mysql gem returns only nil or string data types, leaving you to convert field values to Ruby types in Ruby-land, which is much slower than mysql2's C code.
|
479
|
-
|
480
|
-
For a comparative benchmark, the script below performs a basic "SELECT * FROM"
|
481
|
-
query on a table with 30k rows and fields of nearly every Ruby-representable
|
482
|
-
data type, then iterating over every row using an #each like method yielding a
|
483
|
-
block:
|
484
|
-
|
485
|
-
``` sh
|
486
|
-
user system total real
|
487
|
-
Mysql2 0.750000 0.180000 0.930000 (1.821655)
|
488
|
-
do_mysql 1.650000 0.200000 1.850000 (2.811357)
|
489
|
-
Mysql 7.500000 0.210000 7.710000 (8.065871)
|
490
|
-
```
|
491
|
-
|
492
|
-
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder.
|
493
|
-
|
494
|
-
## Development
|
495
|
-
|
496
|
-
Use 'bundle install' to install the necessary development and testing gems:
|
497
|
-
|
498
|
-
``` sh
|
499
|
-
bundle install
|
500
|
-
rake
|
501
|
-
```
|
502
|
-
|
503
|
-
The tests require the "test" database to exist, and expect to connect
|
504
|
-
both as root and the running user, both with a blank password:
|
505
|
-
|
506
|
-
``` sql
|
507
|
-
CREATE DATABASE test;
|
508
|
-
CREATE USER '<user>'@'localhost' IDENTIFIED BY '';
|
509
|
-
GRANT ALL PRIVILEGES ON test.* TO '<user>'@'localhost';
|
510
|
-
```
|
511
|
-
|
512
|
-
You can change these defaults in the spec/configuration.yml which is generated
|
513
|
-
automatically when you run rake (or explicitly `rake spec/configuration.yml`).
|
514
|
-
|
515
|
-
For a normal installation on a Mac, you most likely do not need to do anything,
|
516
|
-
though.
|
517
|
-
|
518
|
-
## Special Thanks
|
519
|
-
|
520
|
-
* Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
|
521
|
-
* Yury Korolev (http://github.com/yury) - for TONS of help testing the Active Record adapter
|
522
|
-
* Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
|
523
|
-
* Mike Perham (http://github.com/mperham) - Async Active Record adapter (uses Fibers and EventMachine)
|
524
|
-
* Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support.
|