mysql2 0.2.6-x86-mswin32-60 → 0.2.15-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/CHANGELOG.md +60 -1
- data/Gemfile +3 -0
- data/MIT-LICENSE +1 -1
- data/README.md +326 -0
- data/benchmark/active_record.rb +2 -4
- data/benchmark/active_record_threaded.rb +42 -0
- data/benchmark/escape.rb +3 -6
- data/benchmark/query_with_mysql_casting.rb +3 -6
- data/benchmark/query_without_mysql_casting.rb +13 -7
- data/benchmark/sequel.rb +4 -6
- data/benchmark/setup_db.rb +17 -13
- data/benchmark/threaded.rb +44 -0
- data/ext/mysql2/client.c +314 -80
- data/ext/mysql2/client.h +3 -2
- data/ext/mysql2/extconf.rb +9 -1
- data/ext/mysql2/mysql2_ext.h +10 -0
- data/ext/mysql2/result.c +128 -37
- data/ext/mysql2/result.h +2 -2
- data/ext/mysql2/wait_for_single_fd.h +36 -0
- data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +10 -9
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +9 -58
- data/lib/active_record/fiber_patches.rb +37 -9
- data/lib/mysql2.rb +7 -2
- data/lib/mysql2/client.rb +9 -2
- data/lib/mysql2/em.rb +10 -6
- data/lib/mysql2/em_fiber.rb +31 -0
- data/lib/mysql2/version.rb +3 -0
- data/mysql2.gemspec +18 -78
- data/spec/em/em_fiber_spec.rb +22 -0
- data/spec/mysql2/client_spec.rb +179 -62
- data/spec/mysql2/error_spec.rb +47 -3
- data/spec/mysql2/result_spec.rb +78 -8
- data/spec/spec_helper.rb +2 -2
- data/tasks/benchmarks.rake +15 -3
- data/tasks/compile.rake +23 -6
- data/tasks/vendor_mysql.rake +6 -7
- metadata +145 -48
- data/README.rdoc +0 -240
- data/VERSION +0 -1
- data/tasks/jeweler.rake +0 -17
data/README.rdoc
DELETED
@@ -1,240 +0,0 @@
|
|
1
|
-
= Mysql2 - A modern, simple and very fast Mysql library for Ruby - binding to libmysql
|
2
|
-
|
3
|
-
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
|
4
|
-
Some database libraries out there serve as direct 1:1 mappings of the already complex C API's available.
|
5
|
-
This one is not.
|
6
|
-
|
7
|
-
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.
|
8
|
-
|
9
|
-
The API consists of two clases:
|
10
|
-
|
11
|
-
Mysql2::Client - your connection to the database
|
12
|
-
|
13
|
-
Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.
|
14
|
-
|
15
|
-
== Installing
|
16
|
-
|
17
|
-
gem install mysql2
|
18
|
-
|
19
|
-
You may have to specify --with-mysql-config=/some/random/path/bin/mysql_config
|
20
|
-
|
21
|
-
== Usage
|
22
|
-
|
23
|
-
Connect to a database:
|
24
|
-
|
25
|
-
# this takes a hash of options, almost all of which map directly
|
26
|
-
# to the familiar database.yml in rails
|
27
|
-
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
|
28
|
-
client = Mysql2::Client.new(:host => "localhost", :username => "root")
|
29
|
-
|
30
|
-
Then query it:
|
31
|
-
|
32
|
-
results = client.query("SELECT * FROM users WHERE group='githubbers'")
|
33
|
-
|
34
|
-
Need to escape something first?
|
35
|
-
|
36
|
-
escaped = client.escape("gi'thu\"bbe\0r's")
|
37
|
-
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
|
38
|
-
|
39
|
-
Finally, iterate over the results:
|
40
|
-
|
41
|
-
results.each do |row|
|
42
|
-
# conveniently, row is a hash
|
43
|
-
# the keys are the fields, as you'd expect
|
44
|
-
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
|
45
|
-
# Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
|
46
|
-
end
|
47
|
-
|
48
|
-
Or, you might just keep it simple:
|
49
|
-
|
50
|
-
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
|
51
|
-
# do something with row, it's ready to rock
|
52
|
-
end
|
53
|
-
|
54
|
-
How about with symbolized keys?
|
55
|
-
|
56
|
-
# NOTE: the :symbolize_keys and future options will likely move to the #query method soon
|
57
|
-
client.query("SELECT * FROM users WHERE group='githubbers'").each(:symbolize_keys => true) do |row|
|
58
|
-
# do something with row, it's ready to rock
|
59
|
-
end
|
60
|
-
|
61
|
-
== Cascading config
|
62
|
-
|
63
|
-
The default config hash is at:
|
64
|
-
|
65
|
-
Mysql2::Client.default_query_options
|
66
|
-
|
67
|
-
which defaults to:
|
68
|
-
|
69
|
-
{:async => false, :as => :hash, :symbolize_keys => false}
|
70
|
-
|
71
|
-
that can be used as so:
|
72
|
-
|
73
|
-
# these are the defaults all Mysql2::Client instances inherit
|
74
|
-
Mysql2::Client.default_query_options.merge!(:as => :array)
|
75
|
-
|
76
|
-
or
|
77
|
-
|
78
|
-
# this will change the defaults for all future results returned by the #query method _for this connection only_
|
79
|
-
c = Mysql2::Client.new
|
80
|
-
c.query_options.merge!(:symbolize_keys => true)
|
81
|
-
|
82
|
-
or
|
83
|
-
|
84
|
-
# this will set the options for the Mysql2::Result instance returned from the #query method
|
85
|
-
c = Mysql2::Client.new
|
86
|
-
c.query(sql, :symbolize_keys => true)
|
87
|
-
|
88
|
-
== Result types
|
89
|
-
|
90
|
-
=== Array of Arrays
|
91
|
-
|
92
|
-
Pass the :as => :array option to any of the above methods of configuration
|
93
|
-
|
94
|
-
=== Array of Hashes
|
95
|
-
|
96
|
-
The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
|
97
|
-
|
98
|
-
=== Others...
|
99
|
-
|
100
|
-
I may add support for :as => :csv or even :as => :json to allow for *much* more efficient generation of those data types from result sets.
|
101
|
-
If you'd like to see either of these (or others), open an issue and start bugging me about it ;)
|
102
|
-
|
103
|
-
=== Timezones
|
104
|
-
|
105
|
-
Mysql2 now supports two timezone options:
|
106
|
-
|
107
|
-
: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
|
108
|
-
:application_timezone - this is the timezone Mysql2 will convert to before finally handing back to the caller
|
109
|
-
|
110
|
-
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.
|
111
|
-
Then, if :application_timezone is set to say - :local - Mysql2 will then convert the just-created UTC Time object to local time.
|
112
|
-
|
113
|
-
Both options only allow two values - :local or :utc - with the exception that :application_timezone can be [and defaults to] nil
|
114
|
-
|
115
|
-
=== Casting "boolean" columns
|
116
|
-
|
117
|
-
You can now tell Mysql2 to cast tinyint(1) fields to boolean values in Ruby with the :cast_booleans option.
|
118
|
-
|
119
|
-
client = Mysql2::Client.new
|
120
|
-
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
|
121
|
-
|
122
|
-
=== Async
|
123
|
-
|
124
|
-
Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
|
125
|
-
But, in order to take full advantage of it in your Ruby code, you can do:
|
126
|
-
|
127
|
-
client.query("SELECT sleep(5)", :async => true)
|
128
|
-
|
129
|
-
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
|
130
|
-
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
|
131
|
-
|
132
|
-
# result will be a Mysql2::Result instance
|
133
|
-
result = client.async_result
|
134
|
-
|
135
|
-
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
|
136
|
-
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
|
137
|
-
If you need multiple query concurrency take a look at using a connection pool.
|
138
|
-
|
139
|
-
=== Row Caching
|
140
|
-
|
141
|
-
By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
|
142
|
-
This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
|
143
|
-
|
144
|
-
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.
|
145
|
-
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.
|
146
|
-
|
147
|
-
== ActiveRecord
|
148
|
-
|
149
|
-
To use the ActiveRecord driver, all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
|
150
|
-
That was easy right? :)
|
151
|
-
|
152
|
-
== Asynchronous ActiveRecord
|
153
|
-
|
154
|
-
You can also use Mysql2 with asynchronous Rails (first introduced at http://www.mikeperham.com/2010/04/03/introducing-phat-an-asynchronous-rails-app/) by
|
155
|
-
setting the adapter in your database.yml to "em_mysql2". You must be running Ruby 1.9, thin and the rack-fiber_pool middleware for it to work.
|
156
|
-
|
157
|
-
== Sequel
|
158
|
-
|
159
|
-
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.
|
160
|
-
|
161
|
-
== EventMachine
|
162
|
-
|
163
|
-
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
|
164
|
-
while specifying callbacks for success for failure. Here's a simple example:
|
165
|
-
|
166
|
-
require 'mysql2/em'
|
167
|
-
|
168
|
-
EM.run do
|
169
|
-
client1 = Mysql2::EM::Client.new
|
170
|
-
defer1 = client1.query "SELECT sleep(3) as first_query"
|
171
|
-
defer1.callback do |result|
|
172
|
-
puts "Result: #{result.to_a.inspect}"
|
173
|
-
end
|
174
|
-
|
175
|
-
client2 = Mysql2::EM::Client.new
|
176
|
-
defer2 = client2.query "SELECT sleep(1) second_query"
|
177
|
-
defer2.callback do |result|
|
178
|
-
puts "Result: #{result.to_a.inspect}"
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
== Lazy Everything
|
183
|
-
|
184
|
-
Well... almost ;)
|
185
|
-
|
186
|
-
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.
|
187
|
-
|
188
|
-
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
|
189
|
-
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).
|
190
|
-
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.
|
191
|
-
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.
|
192
|
-
|
193
|
-
This caching behavior can be disabled by setting the :cache_rows option to false.
|
194
|
-
|
195
|
-
As for field values themselves, I'm workin on it - but expect that soon.
|
196
|
-
|
197
|
-
== Compatibility
|
198
|
-
|
199
|
-
The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
|
200
|
-
|
201
|
-
* 1.8.7-p249
|
202
|
-
* ree-1.8.7-2010.01
|
203
|
-
* 1.9.1-p378
|
204
|
-
* ruby-trunk
|
205
|
-
* rbx-head - broken at the moment, working with the rbx team for a solution
|
206
|
-
|
207
|
-
The ActiveRecord driver should work on 2.3.5 and 3.0
|
208
|
-
|
209
|
-
== Yeah... but why?
|
210
|
-
|
211
|
-
Someone: Dude, the Mysql gem works fiiiiiine.
|
212
|
-
|
213
|
-
Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert
|
214
|
-
them into proper Ruby types in Ruby-land - which is slow as balls.
|
215
|
-
|
216
|
-
|
217
|
-
Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.
|
218
|
-
|
219
|
-
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
|
220
|
-
|
221
|
-
== Benchmarks
|
222
|
-
|
223
|
-
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
|
224
|
-
then iterating over every row using an #each like method yielding a block:
|
225
|
-
|
226
|
-
# These results are from the query_with_mysql_casting.rb script in the benchmarks folder
|
227
|
-
user system total real
|
228
|
-
Mysql2
|
229
|
-
0.750000 0.180000 0.930000 ( 1.821655)
|
230
|
-
do_mysql
|
231
|
-
1.650000 0.200000 1.850000 ( 2.811357)
|
232
|
-
Mysql
|
233
|
-
7.500000 0.210000 7.710000 ( 8.065871)
|
234
|
-
|
235
|
-
== Special Thanks
|
236
|
-
|
237
|
-
* Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
|
238
|
-
* Yury Korolev (http://github.com/yury) - for TONS of help testing the ActiveRecord adapter
|
239
|
-
* Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
|
240
|
-
* Mike Perham (http://github.com/mperham) - Async ActiveRecord adapter (uses Fibers and EventMachine)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.2.6
|
data/tasks/jeweler.rake
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'jeweler'
|
3
|
-
JEWELER = Jeweler::Tasks.new do |gem|
|
4
|
-
gem.name = "mysql2"
|
5
|
-
gem.summary = "A simple, fast Mysql library for Ruby, binding to libmysql"
|
6
|
-
gem.email = "seniorlopez@gmail.com"
|
7
|
-
gem.homepage = "http://github.com/brianmario/mysql2"
|
8
|
-
gem.authors = ["Brian Lopez"]
|
9
|
-
gem.require_paths = ["lib", "ext"]
|
10
|
-
gem.extra_rdoc_files = `git ls-files *.rdoc`.split("\n")
|
11
|
-
gem.files = `git ls-files`.split("\n")
|
12
|
-
gem.extensions = ["ext/mysql2/extconf.rb"]
|
13
|
-
gem.files.include %w(lib/jeweler/templates/.document lib/jeweler/templates/.gitignore)
|
14
|
-
end
|
15
|
-
rescue LoadError
|
16
|
-
puts "jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
17
|
-
end
|