Skip to content

Commit 91f178c

Browse files
committed
Merge branch 'master' into 0.2.x
2 parents 3c75488 + d0a5199 commit 91f178c

File tree

9 files changed

+268
-110
lines changed

9 files changed

+268
-110
lines changed

.travis.yml

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
language: ruby
2-
rvm:
3-
- 1.8.7
4-
- 1.9.2
5-
- 1.9.3
6-
- 2.0.0
7-
- 2.1.0
8-
- ree
9-
- rbx
10-
env: RBXOPT=-Xgc.honor_start=true
112
bundler_args: --without benchmarks
123
script:
134
- bundle exec rake spec
145
# Temporary workaround for broken Rubygems on Travis
156
before_install:
167
- gem update --system 2.1.11
178
- gem --version
9+
- |
10+
bash -c " # Install MariaDB if DB=mariadb
11+
if [[ x$DB =~ xmariadb ]]; then
12+
sudo service mysql stop
13+
sudo apt-get purge '^mysql*' 'libmysql*'
14+
sudo apt-get install python-software-properties
15+
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
16+
if [[ x$DB = xmariadb55 ]]; then
17+
sudo add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/5.5/ubuntu precise main'
18+
elif [[ x$DB = xmariadb10 ]]; then
19+
sudo add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/ubuntu precise main'
20+
fi
21+
sudo apt-get update
22+
sudo apt-get -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold -y install mariadb-server libmariadbd-dev
23+
fi
24+
"
25+
- mysqld --version
26+
rvm:
27+
- 1.8.7
28+
- 1.9.2
29+
- 1.9.3
30+
- 2.0.0
31+
- 2.1.1
32+
- ree
1833
matrix:
1934
allow_failures:
2035
- rvm: rbx
36+
include:
37+
- rvm: 2.0.0
38+
env: DB=mariadb55
39+
- rvm: 2.0.0
40+
env: DB=mariadb10
41+
- rvm: rbx
42+
env: RBXOPT=-Xgc.honor_start=true

README.md

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ It also forces the use of UTF-8 [or binary] for the connection [and all strings
1010

1111
The API consists of two classes:
1212

13-
Mysql2::Client - your connection to the database
13+
`Mysql2::Client` - your connection to the database
1414

15-
Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.
15+
`Mysql2::Result` - returned from issuing a #query on the connection. It includes Enumerable.
1616

1717
## Installing
1818
### OSX / Linux
@@ -119,8 +119,7 @@ end
119119
How about with symbolized keys?
120120

121121
``` ruby
122-
# NOTE: the :symbolize_keys and future options will likely move to the #query method soon
123-
client.query("SELECT * FROM users WHERE group='githubbers'").each(:symbolize_keys => true) do |row|
122+
client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true) do |row|
124123
# do something with row, it's ready to rock
125124
end
126125
```
@@ -157,10 +156,12 @@ Mysql2::Client.new(
157156
:local_infile = true/false,
158157
:secure_auth = true/false,
159158
:default_file = '/path/to/my.cfg',
160-
:default_group = 'my.cfg section'
159+
:default_group = 'my.cfg section',
160+
:init_command => sql
161161
)
162162
```
163163

164+
164165
### SSL options
165166

166167
Setting any of the following options will enable an SSL connection, but only if
@@ -252,6 +253,15 @@ the `:default_file` and `:default_group` paramters. For example:
252253
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
253254
```
254255

256+
### Initial command on connect and reconnect
257+
258+
If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
259+
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
260+
It is useful if you want to provide session options which survive reconnection.
261+
262+
``` ruby
263+
Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
264+
```
255265

256266
## Cascading config
257267

@@ -390,23 +400,50 @@ There are a few things that need to be kept in mind while using streaming:
390400

391401
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.
392402

393-
## Active Record
403+
### Lazy Everything
404+
405+
Well... almost ;)
406+
407+
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.
408+
409+
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
410+
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).
411+
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.
412+
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.
413+
414+
This caching behavior can be disabled by setting the `:cache_rows` option to false.
415+
416+
As for field values themselves, I'm workin on it - but expect that soon.
417+
418+
## Compatibility
419+
420+
This gem is tested with the following Ruby versions on Linux and Mac OS X:
421+
422+
* Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x (ongoing patch releases)
423+
* Ruby Enterprise Edition (based on MRI 1.8.7)
424+
* Rubinius 2.x
425+
426+
This gem is tested with the following MySQL and MariaDB versions:
427+
428+
* MySQL 5.0, 5.1, 5.5, 5.6
429+
* MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
430+
* MariaDB 5.5, 10.0
394431

395-
To use the Active Record driver (with or without rails), all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
396-
That was easy right? :)
432+
### Active Record
397433

398-
NOTE: as of 0.3.0, and Active Record 3.1 - the Active Record adapter has been pulled out of this gem and into Active Record itself. If you need to use mysql2 with
399-
Rails versions < 3.1 make sure and specify `gem "mysql2", "~> 0.2.7"` in your Gemfile
434+
* mysql2 0.2.x includes an Active Record driver compatible with AR 2.3 and 3.0
435+
* mysql2 0.3.x does not include an AR driver because it is included in AR 3.1 and above
400436

401-
## Asynchronous Active Record
437+
### Asynchronous Active Record
402438

403439
Please see the [em-synchrony](https://github.com/igrigorik/em-synchrony) project for details about using EventMachine with mysql2 and Rails.
404440

405-
## Sequel
441+
### Sequel
406442

407-
The Sequel adapter was pulled out into Sequel core (will be part of the next release) and can be used by specifying the "mysql2://" prefix to your connection specification.
443+
Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01).
444+
Use the prefix "mysql2://" in your connection specification.
408445

409-
## EventMachine
446+
### EventMachine
410447

411448
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
412449
while specifying callbacks for success for failure. Here's a simple example:
@@ -429,64 +466,30 @@ EM.run do
429466
end
430467
```
431468

432-
## Lazy Everything
469+
## Benchmarks and Comparison
433470

434-
Well... almost ;)
435-
436-
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.
437-
438-
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
439-
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).
440-
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.
441-
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.
442-
443-
This caching behavior can be disabled by setting the :cache_rows option to false.
444-
445-
As for field values themselves, I'm workin on it - but expect that soon.
471+
The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.
446472

447-
## Compatibility
448-
449-
This gem is regularly tested against the following Ruby versions on Linux and Mac OS X:
450-
451-
* Ruby MRI 1.8.7, 1.9.2, 1.9.3, 2.0.0 (ongoing patch releases).
452-
* Ruby Enterprise Edition (based on MRI 1.8.7).
453-
* Rubinius 2.0 in compatibility modes 1.8, 1.9, 2.0.
454-
455-
The mysql2 gem 0.2.x series includes an Active Record driver that works with AR
456-
2.3.x and 3.0.x. Starting in Active Record 3.1, a mysql2 driver is included in
457-
the Active Record codebase and no longer provided in mysql2 gem 0.3 and above.
458-
459-
## Yeah... but why?
460-
461-
Someone: Dude, the Mysql gem works fiiiiiine.
473+
The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.
462474

463-
Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert
464-
them into proper Ruby types in Ruby-land - which is slow as balls.
475+
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.
465476

466-
Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.
467-
468-
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
469-
470-
## Benchmarks
471-
472-
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
473-
then iterating over every row using an #each like method yielding a block:
474-
475-
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder
477+
For a comparative benchmark, the script below performs a basic "SELECT * FROM"
478+
query on a table with 30k rows and fields of nearly every Ruby-representable
479+
data type, then iterating over every row using an #each like method yielding a
480+
block:
476481

477482
``` sh
478-
user system total real
479-
Mysql2
480-
0.750000 0.180000 0.930000 ( 1.821655)
481-
do_mysql
482-
1.650000 0.200000 1.850000 ( 2.811357)
483-
Mysql
484-
7.500000 0.210000 7.710000 ( 8.065871)
483+
user system total real
484+
Mysql2 0.750000 0.180000 0.930000 (1.821655)
485+
do_mysql 1.650000 0.200000 1.850000 (2.811357)
486+
Mysql 7.500000 0.210000 7.710000 (8.065871)
485487
```
486488

489+
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder.
490+
487491
## Development
488492

489-
To run the tests, you can use RVM and Bundler to create a pristine environment for mysql2 development/hacking.
490493
Use 'bundle install' to install the necessary development and testing gems:
491494

492495
``` sh

0 commit comments

Comments
 (0)