You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# this will change the defaults for all future results returned by the #query method _for this connection only_
88
-
c = Mysql2::Client.new
89
-
c.query_options.merge!(:symbolize_keys => true)
109
+
```ruby
110
+
# this will change the defaults for all future results returned by the #query method _for this connection only_
111
+
c =Mysql2::Client.new
112
+
c.query_options.merge!(:symbolize_keys => true)
113
+
```
90
114
91
115
or
92
116
93
-
# this will set the options for the Mysql2::Result instance returned from the #query method
94
-
c = Mysql2::Client.new
95
-
c.query(sql, :symbolize_keys => true)
117
+
```ruby
118
+
# this will set the options for the Mysql2::Result instance returned from the #query method
119
+
c =Mysql2::Client.new
120
+
c.query(sql, :symbolize_keys => true)
121
+
```
96
122
97
-
== Result types
123
+
##Result types
98
124
99
-
=== Array of Arrays
125
+
###Array of Arrays
100
126
101
-
Pass the :as => :array option to any of the above methods of configuration
127
+
Pass the `:as => :array` option to any of the above methods of configuration
102
128
103
-
=== Array of Hashes
129
+
###Array of Hashes
104
130
105
131
The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
106
132
107
-
=== Others...
133
+
###Others...
108
134
109
-
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.
135
+
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.
110
136
If you'd like to see either of these (or others), open an issue and start bugging me about it ;)
111
137
112
-
=== Timezones
138
+
###Timezones
113
139
114
140
Mysql2 now supports two timezone options:
115
141
116
-
: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
117
-
:application_timezone - this is the timezone Mysql2 will convert to before finally handing back to the caller
142
+
```ruby
143
+
: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
144
+
:application_timezone# this is the timezone Mysql2 will convert to before finally handing back to the caller
145
+
```
118
146
119
-
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.
120
-
Then, if :application_timezone is set to say - :local - Mysql2 will then convert the just-created UTC Time object to local time.
147
+
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.
148
+
Then, if `:application_timezone` is set to say - `:local` - Mysql2 will then convert the just-created UTC Time object to local time.
121
149
122
-
Both options only allow two values - :local or :utc - with the exception that :application_timezone can be [and defaults to] nil
150
+
Both options only allow two values - `:local` or `:utc` - with the exception that `:application_timezone` can be [and defaults to] nil
123
151
124
-
=== Casting "boolean" columns
152
+
###Casting "boolean" columns
125
153
126
154
You can now tell Mysql2 to cast tinyint(1) fields to boolean values in Ruby with the :cast_booleans option.
127
155
128
-
client = Mysql2::Client.new
129
-
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
156
+
```ruby
157
+
client =Mysql2::Client.new
158
+
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
159
+
```
130
160
131
-
=== Async
161
+
###Async
132
162
133
163
Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
134
164
But, in order to take full advantage of it in your Ruby code, you can do:
135
165
136
-
client.query("SELECT sleep(5)", :async => true)
166
+
```ruby
167
+
client.query("SELECT sleep(5)", :async => true)
168
+
```
137
169
138
170
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
139
171
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
140
172
141
-
# result will be a Mysql2::Result instance
142
-
result = client.async_result
173
+
```ruby
174
+
# result will be a Mysql2::Result instance
175
+
result = client.async_result
176
+
```
143
177
144
178
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
145
179
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
146
180
If you need multiple query concurrency take a look at using a connection pool.
147
181
148
-
=== Row Caching
182
+
###Row Caching
149
183
150
184
By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
151
185
This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
152
186
153
-
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.
187
+
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.
154
188
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.
155
189
156
-
== ActiveRecord
190
+
##ActiveRecord
157
191
158
192
To use the ActiveRecord driver (with our without rails), all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
159
193
That was easy right? :)
160
194
161
-
== Asynchronous ActiveRecord
195
+
NOTE: as of 0.3.0, and ActiveRecord 3.1 - the ActiveRecord adapter has been pulled out of this gem and into ActiveRecord itself. If you need to use mysql2 with
196
+
Rails versions < 3.1 make sure and specify `gem "mysql2", "~> 0.2.7"` in your Gemfile
197
+
198
+
## Asynchronous ActiveRecord
162
199
163
200
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
164
201
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.
165
202
166
-
== Sequel
203
+
##Sequel
167
204
168
205
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.
169
206
170
-
== EventMachine
207
+
##EventMachine
171
208
172
209
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
173
210
while specifying callbacks for success for failure. Here's a simple example:
174
211
175
-
require 'mysql2/em'
212
+
```ruby
213
+
require'mysql2/em'
176
214
177
-
EM.run do
178
-
client1 = Mysql2::EM::Client.new
179
-
defer1 = client1.query "SELECT sleep(3) as first_query"
180
-
defer1.callback do |result|
181
-
puts "Result: #{result.to_a.inspect}"
182
-
end
215
+
EM.run do
216
+
client1 =Mysql2::EM::Client.new
217
+
defer1 = client1.query "SELECT sleep(3) as first_query"
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.
196
235
197
236
Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
198
-
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).
237
+
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).
199
238
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.
200
239
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.
201
240
202
241
This caching behavior can be disabled by setting the :cache_rows option to false.
203
242
204
243
As for field values themselves, I'm workin on it - but expect that soon.
205
244
206
-
== Compatibility
245
+
##Compatibility
207
246
208
247
The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
209
248
@@ -215,7 +254,7 @@ The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
215
254
216
255
The ActiveRecord driver should work on 2.3.5 and 3.0
217
256
218
-
== Yeah... but why?
257
+
##Yeah... but why?
219
258
220
259
Someone: Dude, the Mysql gem works fiiiiiine.
221
260
@@ -227,29 +266,34 @@ Someone: OK fine, but do_mysql can already give me back values with Ruby objects
227
266
228
267
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
229
268
230
-
== Benchmarks
269
+
##Benchmarks
231
270
232
271
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
233
272
then iterating over every row using an #each like method yielding a block:
234
273
235
-
# These results are from the query_with_mysql_casting.rb script in the benchmarks folder
236
-
user system total real
237
-
Mysql2
238
-
0.750000 0.180000 0.930000 ( 1.821655)
239
-
do_mysql
240
-
1.650000 0.200000 1.850000 ( 2.811357)
241
-
Mysql
242
-
7.500000 0.210000 7.710000 ( 8.065871)
274
+
These results are from the `query_with_mysql_casting.rb` script in the benchmarks folder
275
+
276
+
```sh
277
+
user system total real
278
+
Mysql2
279
+
0.750000 0.180000 0.930000 ( 1.821655)
280
+
do_mysql
281
+
1.650000 0.200000 1.850000 ( 2.811357)
282
+
Mysql
283
+
7.500000 0.210000 7.710000 ( 8.065871)
284
+
```
243
285
244
-
== Development
286
+
##Development
245
287
246
288
To run the tests, you can use RVM and Bundler to create a pristine environment for mysql2 development/hacking.
247
289
Use 'bundle install' to install the necessary development and testing gems:
248
290
249
-
bundle install
250
-
rake
291
+
```sh
292
+
bundle install
293
+
rake
294
+
```
251
295
252
-
== Special Thanks
296
+
##Special Thanks
253
297
254
298
* Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
255
299
* Yury Korolev (http://github.com/yury) - for TONS of help testing the ActiveRecord adapter
0 commit comments