mysql2 0.3.18-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +539 -0
- data/examples/eventmachine.rb +21 -0
- data/examples/threaded.rb +20 -0
- data/ext/mysql2/client.c +1416 -0
- data/ext/mysql2/client.h +56 -0
- data/ext/mysql2/extconf.rb +177 -0
- data/ext/mysql2/infile.c +122 -0
- data/ext/mysql2/infile.h +1 -0
- data/ext/mysql2/mysql2_ext.c +12 -0
- data/ext/mysql2/mysql2_ext.h +45 -0
- data/ext/mysql2/mysql_enc_name_to_ruby.h +168 -0
- data/ext/mysql2/mysql_enc_to_ruby.h +246 -0
- data/ext/mysql2/result.c +684 -0
- data/ext/mysql2/result.h +23 -0
- data/ext/mysql2/wait_for_single_fd.h +36 -0
- data/lib/mysql2.rb +64 -0
- data/lib/mysql2/2.0/mysql2.so +0 -0
- data/lib/mysql2/2.1/mysql2.so +0 -0
- data/lib/mysql2/client.rb +90 -0
- data/lib/mysql2/console.rb +5 -0
- data/lib/mysql2/em.rb +56 -0
- data/lib/mysql2/error.rb +80 -0
- data/lib/mysql2/mysql2.rb +2 -0
- data/lib/mysql2/result.rb +5 -0
- data/lib/mysql2/version.rb +3 -0
- data/spec/configuration.yml.example +17 -0
- data/spec/em/em_spec.rb +135 -0
- data/spec/my.cnf.example +9 -0
- data/spec/mysql2/client_spec.rb +897 -0
- data/spec/mysql2/error_spec.rb +83 -0
- data/spec/mysql2/result_spec.rb +505 -0
- data/spec/rcov.opts +3 -0
- data/spec/spec_helper.rb +87 -0
- data/spec/test_data +1 -0
- data/support/libmysql.def +219 -0
- data/support/mysql_enc_to_ruby.rb +82 -0
- data/support/ruby_enc_to_mysql.rb +61 -0
- data/vendor/README +654 -0
- data/vendor/libmysql.dll +0 -0
- metadata +149 -0
data/README.md
ADDED
@@ -0,0 +1,539 @@
|
|
1
|
+
# Mysql2 - A modern, simple and very fast MySQL library for Ruby - binding to libmysql
|
2
|
+
|
3
|
+
Travis CI [![Travis CI Status](https://travis-ci.org/brianmario/mysql2.png)](https://travis-ci.org/brianmario/mysql2)
|
4
|
+
Appveyor CI [![Appveyor CI Status](https://ci.appveyor.com/api/projects/status/github/sodabrew/mysql2)](https://ci.appveyor.com/project/sodabrew/mysql2)
|
5
|
+
|
6
|
+
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
|
7
|
+
Some database libraries out there serve as direct 1:1 mappings of the already complex C APIs available.
|
8
|
+
This one is not.
|
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.
|
11
|
+
|
12
|
+
The API consists of two classes:
|
13
|
+
|
14
|
+
`Mysql2::Client` - your connection to the database.
|
15
|
+
|
16
|
+
`Mysql2::Result` - returned from issuing a #query on the connection. It includes Enumerable.
|
17
|
+
|
18
|
+
## Installing
|
19
|
+
### General Instructions
|
20
|
+
``` sh
|
21
|
+
gem install mysql2
|
22
|
+
```
|
23
|
+
|
24
|
+
This gem links against MySQL's `libmysqlclient` library or `Connector/C`
|
25
|
+
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.
|
29
|
+
|
30
|
+
By default, the mysql2 gem will try to find a copy of MySQL in this order:
|
31
|
+
|
32
|
+
* Option `--with-mysql-dir`, if provided (see below).
|
33
|
+
* Option `--with-mysql-config`, if provided (see below).
|
34
|
+
* Several typical paths for `mysql_config` (default for the majority of users).
|
35
|
+
* The directory `/usr/local`.
|
36
|
+
|
37
|
+
### Configuration options
|
38
|
+
|
39
|
+
Use these options by `gem install mysql2 -- [--optionA] [--optionB=argument]`.
|
40
|
+
|
41
|
+
* `--with-mysql-dir[=/path/to/mysqldir]` -
|
42
|
+
Specify the directory where MySQL is installed. The mysql2 gem will not use
|
43
|
+
`mysql_config`, but will instead look at `mysqldir/lib` and `mysqldir/include`
|
44
|
+
for the library and header files.
|
45
|
+
This option is mutually exclusive with `--with-mysql-config`.
|
46
|
+
|
47
|
+
* `--with-mysql-config[=/path/to/mysql_config]` -
|
48
|
+
Specify a path to the `mysql_config` binary provided by your copy of MySQL. The
|
49
|
+
mysql2 gem will ask this `mysql_config` binary about the compiler and linker
|
50
|
+
arguments needed.
|
51
|
+
This option is mutually exclusive with `--with-mysql-dir`.
|
52
|
+
|
53
|
+
* `--with-mysql-rpath=/path/to/mysql/lib` / `--without-mysql-rpath` -
|
54
|
+
Override the runtime path used to find the MySQL libraries.
|
55
|
+
This may be needed if you deploy to a system where these libraries
|
56
|
+
are located somewhere different than on your build system.
|
57
|
+
This overrides any rpath calculated by default or by the options above.
|
58
|
+
|
59
|
+
### Linux and other Unixes
|
60
|
+
|
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.
|
65
|
+
|
66
|
+
### Mac OS X
|
67
|
+
|
68
|
+
You may use MacPorts, Homebrew, or a native MySQL installer package. The most
|
69
|
+
common paths will be automatically searched. If you want to select a specific
|
70
|
+
MySQL directory, use the `--with-mysql-dir` or `--with-mysql-config` options above.
|
71
|
+
|
72
|
+
### Windows
|
73
|
+
Make sure that you have Ruby and the DevKit compilers installed. We recommend
|
74
|
+
the [Ruby Installer](http://rubyinstaller.org) distribution.
|
75
|
+
|
76
|
+
By default, the mysql2 gem will download and use MySQL Connector/C from
|
77
|
+
mysql.com. If you prefer to use a local installation of Connector/C, add the
|
78
|
+
flag `--with-mysql-dir=c:/mysql-connector-c-x-y-z` (_this path may use forward slashes_).
|
79
|
+
|
80
|
+
By default, the `libmysql.dll` library will be copied into the mysql2 gem
|
81
|
+
directory. To prevent this, add the flag `--no-vendor-libmysql`. The mysql2 gem
|
82
|
+
will search for `libmysql.dll` in the following paths, in order:
|
83
|
+
|
84
|
+
* Environment variable `RUBY_MYSQL2_LIBMYSQL_DLL=C:\path\to\libmysql.dll`
|
85
|
+
(_note the Windows-style backslashes_).
|
86
|
+
* In the mysql2 gem's own directory `vendor/libmysql.dll`
|
87
|
+
* In the system's default library search paths.
|
88
|
+
|
89
|
+
## Usage
|
90
|
+
|
91
|
+
Connect to a database:
|
92
|
+
|
93
|
+
``` ruby
|
94
|
+
# this takes a hash of options, almost all of which map directly
|
95
|
+
# to the familiar database.yml in rails
|
96
|
+
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
|
97
|
+
client = Mysql2::Client.new(:host => "localhost", :username => "root")
|
98
|
+
```
|
99
|
+
|
100
|
+
Then query it:
|
101
|
+
|
102
|
+
``` ruby
|
103
|
+
results = client.query("SELECT * FROM users WHERE group='githubbers'")
|
104
|
+
```
|
105
|
+
|
106
|
+
Need to escape something first?
|
107
|
+
|
108
|
+
``` ruby
|
109
|
+
escaped = client.escape("gi'thu\"bbe\0r's")
|
110
|
+
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
|
111
|
+
```
|
112
|
+
|
113
|
+
You can get a count of your results with `results.count`.
|
114
|
+
|
115
|
+
Finally, iterate over the results:
|
116
|
+
|
117
|
+
``` ruby
|
118
|
+
results.each do |row|
|
119
|
+
# conveniently, row is a hash
|
120
|
+
# the keys are the fields, as you'd expect
|
121
|
+
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
|
122
|
+
puts row["id"] # row["id"].class == Fixnum
|
123
|
+
if row["dne"] # non-existant hash entry is nil
|
124
|
+
puts row["dne"]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
Or, you might just keep it simple:
|
130
|
+
|
131
|
+
``` ruby
|
132
|
+
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
|
133
|
+
# do something with row, it's ready to rock
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
How about with symbolized keys?
|
138
|
+
|
139
|
+
``` ruby
|
140
|
+
client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true) do |row|
|
141
|
+
# do something with row, it's ready to rock
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
You can get the headers and the columns in the order that they were returned
|
146
|
+
by the query like this:
|
147
|
+
|
148
|
+
``` ruby
|
149
|
+
headers = results.fields # <= that's an array of field names, in order
|
150
|
+
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"
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
## Connection options
|
157
|
+
|
158
|
+
You may set the following connection options in Mysql2::Client.new(...):
|
159
|
+
|
160
|
+
``` ruby
|
161
|
+
Mysql2::Client.new(
|
162
|
+
:host,
|
163
|
+
:username,
|
164
|
+
:password,
|
165
|
+
:port,
|
166
|
+
:database,
|
167
|
+
:socket = '/path/to/mysql.sock',
|
168
|
+
:flags = REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | MULTI_STATEMENTS,
|
169
|
+
:encoding = 'utf8',
|
170
|
+
:read_timeout = seconds,
|
171
|
+
:write_timeout = seconds,
|
172
|
+
:connect_timeout = seconds,
|
173
|
+
:reconnect = true/false,
|
174
|
+
:local_infile = true/false,
|
175
|
+
:secure_auth = true/false,
|
176
|
+
:default_file = '/path/to/my.cfg',
|
177
|
+
:default_group = 'my.cfg section',
|
178
|
+
:init_command => sql
|
179
|
+
)
|
180
|
+
```
|
181
|
+
|
182
|
+
|
183
|
+
### SSL options
|
184
|
+
|
185
|
+
Setting any of the following options will enable an SSL connection, but only if
|
186
|
+
your MySQL client library and server have been compiled with SSL support.
|
187
|
+
MySQL client library defaults will be used for any parameters that are left out
|
188
|
+
or set to nil. Relative paths are allowed, and may be required by managed
|
189
|
+
hosting providers such as Heroku.
|
190
|
+
|
191
|
+
``` ruby
|
192
|
+
Mysql2::Client.new(
|
193
|
+
# ...options as above...,
|
194
|
+
:sslkey => '/path/to/client-key.pem',
|
195
|
+
:sslcert => '/path/to/client-cert.pem',
|
196
|
+
:sslca => '/path/to/ca-cert.pem',
|
197
|
+
:sslcapath => '/path/to/cacerts',
|
198
|
+
:sslcipher => 'DHE-RSA-AES256-SHA'
|
199
|
+
)
|
200
|
+
```
|
201
|
+
|
202
|
+
### Multiple result sets
|
203
|
+
|
204
|
+
You can also retrieve multiple result sets. For this to work you need to
|
205
|
+
connect with flags `Mysql2::Client::MULTI_STATEMENTS`. Multiple result sets can
|
206
|
+
be used with stored procedures that return more than one result set, and for
|
207
|
+
bundling several SQL statements into a single call to `client.query`.
|
208
|
+
|
209
|
+
``` ruby
|
210
|
+
client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS)
|
211
|
+
result = client.query('CALL sp_customer_list( 25, 10 )')
|
212
|
+
# result now contains the first result set
|
213
|
+
while client.next_result
|
214
|
+
result = client.store_result
|
215
|
+
# result now contains the next result set
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Repeated calls to `client.next_result` will return true, false, or raise an
|
220
|
+
exception if the respective query erred. When `client.next_result` returns true,
|
221
|
+
call `client.store_result` to retrieve a result object. Exceptions are not
|
222
|
+
raised until `client.next_result` is called to find the status of the respective
|
223
|
+
query. Subsequent queries are not executed if an earlier query raised an
|
224
|
+
exception. Subsequent calls to `client.next_result` will return false.
|
225
|
+
|
226
|
+
``` ruby
|
227
|
+
result = client.query('SELECT 1; SELECT 2; SELECT A; SELECT 3')
|
228
|
+
p result.first
|
229
|
+
|
230
|
+
while client.next_result
|
231
|
+
result = client.store_result
|
232
|
+
p result.first
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
Yields:
|
237
|
+
```
|
238
|
+
{"1"=>1}
|
239
|
+
{"2"=>2}
|
240
|
+
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
|
241
|
+
```
|
242
|
+
|
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
|
+
## Cascading config
|
285
|
+
|
286
|
+
The default config hash is at:
|
287
|
+
|
288
|
+
``` ruby
|
289
|
+
Mysql2::Client.default_query_options
|
290
|
+
```
|
291
|
+
|
292
|
+
which defaults to:
|
293
|
+
|
294
|
+
``` ruby
|
295
|
+
{:async => false, :as => :hash, :symbolize_keys => false}
|
296
|
+
```
|
297
|
+
|
298
|
+
that can be used as so:
|
299
|
+
|
300
|
+
``` ruby
|
301
|
+
# these are the defaults all Mysql2::Client instances inherit
|
302
|
+
Mysql2::Client.default_query_options.merge!(:as => :array)
|
303
|
+
```
|
304
|
+
|
305
|
+
or
|
306
|
+
|
307
|
+
``` ruby
|
308
|
+
# this will change the defaults for all future results returned by the #query method _for this connection only_
|
309
|
+
c = Mysql2::Client.new
|
310
|
+
c.query_options.merge!(:symbolize_keys => true)
|
311
|
+
```
|
312
|
+
|
313
|
+
or
|
314
|
+
|
315
|
+
``` ruby
|
316
|
+
# this will set the options for the Mysql2::Result instance returned from the #query method
|
317
|
+
c = Mysql2::Client.new
|
318
|
+
c.query(sql, :symbolize_keys => true)
|
319
|
+
```
|
320
|
+
|
321
|
+
## Result types
|
322
|
+
|
323
|
+
### Array of Arrays
|
324
|
+
|
325
|
+
Pass the `:as => :array` option to any of the above methods of configuration
|
326
|
+
|
327
|
+
### Array of Hashes
|
328
|
+
|
329
|
+
The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
|
330
|
+
|
331
|
+
### Timezones
|
332
|
+
|
333
|
+
Mysql2 now supports two timezone options:
|
334
|
+
|
335
|
+
``` ruby
|
336
|
+
: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
|
337
|
+
:application_timezone # this is the timezone Mysql2 will convert to before finally handing back to the caller
|
338
|
+
```
|
339
|
+
|
340
|
+
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.
|
341
|
+
Then, if `:application_timezone` is set to say - `:local` - Mysql2 will then convert the just-created UTC Time object to local time.
|
342
|
+
|
343
|
+
Both options only allow two values - `:local` or `:utc` - with the exception that `:application_timezone` can be [and defaults to] nil
|
344
|
+
|
345
|
+
### Casting "boolean" columns
|
346
|
+
|
347
|
+
You can now tell Mysql2 to cast `tinyint(1)` fields to boolean values in Ruby with the `:cast_booleans` option.
|
348
|
+
|
349
|
+
``` ruby
|
350
|
+
client = Mysql2::Client.new
|
351
|
+
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
|
352
|
+
```
|
353
|
+
|
354
|
+
### Skipping casting
|
355
|
+
|
356
|
+
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.)
|
357
|
+
|
358
|
+
``` ruby
|
359
|
+
client = Mysql2::Client.new
|
360
|
+
result = client.query("SELECT * FROM table", :cast => false)
|
361
|
+
```
|
362
|
+
|
363
|
+
Here are the results from the `query_without_mysql_casting.rb` script in the benchmarks folder:
|
364
|
+
|
365
|
+
``` sh
|
366
|
+
user system total real
|
367
|
+
Mysql2 (cast: true) 0.340000 0.000000 0.340000 ( 0.405018)
|
368
|
+
Mysql2 (cast: false) 0.160000 0.010000 0.170000 ( 0.209937)
|
369
|
+
Mysql 0.080000 0.000000 0.080000 ( 0.129355)
|
370
|
+
do_mysql 0.520000 0.010000 0.530000 ( 0.574619)
|
371
|
+
```
|
372
|
+
|
373
|
+
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.
|
374
|
+
|
375
|
+
### Async
|
376
|
+
|
377
|
+
NOTE: Not supported on Windows.
|
378
|
+
|
379
|
+
`Mysql2::Client` takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
|
380
|
+
But, in order to take full advantage of it in your Ruby code, you can do:
|
381
|
+
|
382
|
+
``` ruby
|
383
|
+
client.query("SELECT sleep(5)", :async => true)
|
384
|
+
```
|
385
|
+
|
386
|
+
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
|
387
|
+
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
|
388
|
+
|
389
|
+
``` ruby
|
390
|
+
# result will be a Mysql2::Result instance
|
391
|
+
result = client.async_result
|
392
|
+
```
|
393
|
+
|
394
|
+
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
|
395
|
+
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
|
396
|
+
If you need multiple query concurrency take a look at using a connection pool.
|
397
|
+
|
398
|
+
### Row Caching
|
399
|
+
|
400
|
+
By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
|
401
|
+
This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
|
402
|
+
|
403
|
+
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.
|
404
|
+
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.
|
405
|
+
|
406
|
+
### Streaming
|
407
|
+
|
408
|
+
`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.
|
409
|
+
|
410
|
+
``` ruby
|
411
|
+
result = client.query("SELECT * FROM really_big_Table", :stream => true)
|
412
|
+
```
|
413
|
+
|
414
|
+
There are a few things that need to be kept in mind while using streaming:
|
415
|
+
|
416
|
+
* `:cache_rows` is ignored currently. (if you want to use `:cache_rows` you probably don't want to be using `:stream`)
|
417
|
+
* 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
|
+
|
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.
|
420
|
+
|
421
|
+
### Lazy Everything
|
422
|
+
|
423
|
+
Well... almost ;)
|
424
|
+
|
425
|
+
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.
|
426
|
+
|
427
|
+
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
|
428
|
+
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).
|
429
|
+
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.
|
430
|
+
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.
|
431
|
+
|
432
|
+
This caching behavior can be disabled by setting the `:cache_rows` option to false.
|
433
|
+
|
434
|
+
As for field values themselves, I'm workin on it - but expect that soon.
|
435
|
+
|
436
|
+
## Compatibility
|
437
|
+
|
438
|
+
This gem is tested with the following Ruby versions on Linux and Mac OS X:
|
439
|
+
|
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
|
443
|
+
|
444
|
+
This gem is tested with the following MySQL and MariaDB versions:
|
445
|
+
|
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
|
449
|
+
|
450
|
+
### Active Record
|
451
|
+
|
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
|
454
|
+
|
455
|
+
### Asynchronous Active Record
|
456
|
+
|
457
|
+
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
|
458
|
+
|
459
|
+
### Sequel
|
460
|
+
|
461
|
+
Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01).
|
462
|
+
Use the prefix "mysql2://" in your connection specification.
|
463
|
+
|
464
|
+
### EventMachine
|
465
|
+
|
466
|
+
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
|
467
|
+
while specifying callbacks for success for failure. Here's a simple example:
|
468
|
+
|
469
|
+
``` ruby
|
470
|
+
require 'mysql2/em'
|
471
|
+
|
472
|
+
EM.run do
|
473
|
+
client1 = Mysql2::EM::Client.new
|
474
|
+
defer1 = client1.query "SELECT sleep(3) as first_query"
|
475
|
+
defer1.callback do |result|
|
476
|
+
puts "Result: #{result.to_a.inspect}"
|
477
|
+
end
|
478
|
+
|
479
|
+
client2 = Mysql2::EM::Client.new
|
480
|
+
defer2 = client2.query "SELECT sleep(1) second_query"
|
481
|
+
defer2.callback do |result|
|
482
|
+
puts "Result: #{result.to_a.inspect}"
|
483
|
+
end
|
484
|
+
end
|
485
|
+
```
|
486
|
+
|
487
|
+
## Benchmarks and Comparison
|
488
|
+
|
489
|
+
The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.
|
490
|
+
|
491
|
+
The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.
|
492
|
+
|
493
|
+
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.
|
494
|
+
|
495
|
+
For a comparative benchmark, the script below performs a basic "SELECT * FROM"
|
496
|
+
query on a table with 30k rows and fields of nearly every Ruby-representable
|
497
|
+
data type, then iterating over every row using an #each like method yielding a
|
498
|
+
block:
|
499
|
+
|
500
|
+
``` sh
|
501
|
+
user system total real
|
502
|
+
Mysql2 0.750000 0.180000 0.930000 (1.821655)
|
503
|
+
do_mysql 1.650000 0.200000 1.850000 (2.811357)
|
504
|
+
Mysql 7.500000 0.210000 7.710000 (8.065871)
|
505
|
+
```
|
506
|
+
|
507
|
+
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder.
|
508
|
+
|
509
|
+
## Development
|
510
|
+
|
511
|
+
Use 'bundle install' to install the necessary development and testing gems:
|
512
|
+
|
513
|
+
``` sh
|
514
|
+
bundle install
|
515
|
+
rake
|
516
|
+
```
|
517
|
+
|
518
|
+
The tests require the "test" database to exist, and expect to connect
|
519
|
+
both as root and the running user, both with a blank password:
|
520
|
+
|
521
|
+
``` sql
|
522
|
+
CREATE DATABASE test;
|
523
|
+
CREATE USER '<user>'@'localhost' IDENTIFIED BY '';
|
524
|
+
GRANT ALL PRIVILEGES ON test.* TO '<user>'@'localhost';
|
525
|
+
```
|
526
|
+
|
527
|
+
You can change these defaults in the spec/configuration.yml which is generated
|
528
|
+
automatically when you run rake (or explicitly `rake spec/configuration.yml`).
|
529
|
+
|
530
|
+
For a normal installation on a Mac, you most likely do not need to do anything,
|
531
|
+
though.
|
532
|
+
|
533
|
+
## Special Thanks
|
534
|
+
|
535
|
+
* 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.
|