Skip to content

Commit cfd2781

Browse files
committed
move to markdown for syntax awesomeness
1 parent 3f97238 commit cfd2781

File tree

1 file changed

+138
-94
lines changed

1 file changed

+138
-94
lines changed

README.rdoc renamed to README.md

Lines changed: 138 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
= Mysql2 - A modern, simple and very fast Mysql library for Ruby - binding to libmysql
1+
# Mysql2 - A modern, simple and very fast Mysql library for Ruby - binding to libmysql
22

33
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
44
Some database libraries out there serve as direct 1:1 mappings of the already complex C API's available.
@@ -12,198 +12,237 @@ Mysql2::Client - your connection to the database
1212

1313
Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.
1414

15-
== Installing
15+
## Installing
1616

17-
gem install mysql2
17+
``` sh
18+
gem install mysql2
19+
```
1820

1921
You may have to specify --with-mysql-config=/some/random/path/bin/mysql_config
2022

21-
== Usage
23+
## Usage
2224

2325
Connect to a database:
2426

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")
27+
``` ruby
28+
# this takes a hash of options, almost all of which map directly
29+
# to the familiar database.yml in rails
30+
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
31+
client = Mysql2::Client.new(:host => "localhost", :username => "root")
32+
```
2933

3034
Then query it:
3135

32-
results = client.query("SELECT * FROM users WHERE group='githubbers'")
36+
``` ruby
37+
results = client.query("SELECT * FROM users WHERE group='githubbers'")
38+
```
3339

3440
Need to escape something first?
3541

36-
escaped = client.escape("gi'thu\"bbe\0r's")
37-
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
42+
``` ruby
43+
escaped = client.escape("gi'thu\"bbe\0r's")
44+
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
45+
```
3846

3947
Finally, iterate over the results:
4048

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
49+
``` ruby
50+
results.each do |row|
51+
# conveniently, row is a hash
52+
# the keys are the fields, as you'd expect
53+
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
54+
# Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
55+
end
56+
```
4757

4858
Or, you might just keep it simple:
4959

50-
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
51-
# do something with row, it's ready to rock
52-
end
60+
``` ruby
61+
client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
62+
# do something with row, it's ready to rock
63+
end
64+
```
5365

5466
How about with symbolized keys?
5567

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
68+
``` ruby
69+
# NOTE: the :symbolize_keys and future options will likely move to the #query method soon
70+
client.query("SELECT * FROM users WHERE group='githubbers'").each(:symbolize_keys => true) do |row|
71+
# do something with row, it's ready to rock
72+
end
73+
```
6074

6175
You can get the headers and the columns in the order that they were returned
6276
by the query like this:
6377

64-
headers = results.fields # <= that's an array of field names, in order
65-
results.each(:as => :array) do |row|
66-
# Each row is an array, ordered the same as the query results
67-
# An otter's den is called a "holt" or "couch"
68-
end
78+
``` ruby
79+
headers = results.fields # <= that's an array of field names, in order
80+
results.each(:as => :array) do |row|
81+
# Each row is an array, ordered the same as the query results
82+
# An otter's den is called a "holt" or "couch"
83+
end
84+
```
6985

70-
== Cascading config
86+
## Cascading config
7187

7288
The default config hash is at:
7389

74-
Mysql2::Client.default_query_options
90+
``` ruby
91+
Mysql2::Client.default_query_options
92+
```
7593

7694
which defaults to:
7795

78-
{:async => false, :as => :hash, :symbolize_keys => false}
96+
``` ruby
97+
{:async => false, :as => :hash, :symbolize_keys => false}
98+
```
7999

80100
that can be used as so:
81101

82-
# these are the defaults all Mysql2::Client instances inherit
83-
Mysql2::Client.default_query_options.merge!(:as => :array)
102+
``` ruby
103+
# these are the defaults all Mysql2::Client instances inherit
104+
Mysql2::Client.default_query_options.merge!(:as => :array)
105+
```
84106

85107
or
86108

87-
# 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+
```
90114

91115
or
92116

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+
```
96122

97-
== Result types
123+
## Result types
98124

99-
=== Array of Arrays
125+
### Array of Arrays
100126

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
102128

103-
=== Array of Hashes
129+
### Array of Hashes
104130

105131
The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
106132

107-
=== Others...
133+
### Others...
108134

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.
110136
If you'd like to see either of these (or others), open an issue and start bugging me about it ;)
111137

112-
=== Timezones
138+
### Timezones
113139

114140
Mysql2 now supports two timezone options:
115141

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+
```
118146

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.
121149

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
123151

124-
=== Casting "boolean" columns
152+
### Casting "boolean" columns
125153

126154
You can now tell Mysql2 to cast tinyint(1) fields to boolean values in Ruby with the :cast_booleans option.
127155

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+
```
130160

131-
=== Async
161+
### Async
132162

133163
Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
134164
But, in order to take full advantage of it in your Ruby code, you can do:
135165

136-
client.query("SELECT sleep(5)", :async => true)
166+
``` ruby
167+
client.query("SELECT sleep(5)", :async => true)
168+
```
137169

138170
Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
139171
like EventMachine or even IO.select. Once the socket becomes readable, you can do:
140172

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+
```
143177

144178
NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
145179
So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
146180
If you need multiple query concurrency take a look at using a connection pool.
147181

148-
=== Row Caching
182+
### Row Caching
149183

150184
By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
151185
This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
152186

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.
154188
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.
155189

156-
== ActiveRecord
190+
## ActiveRecord
157191

158192
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".
159193
That was easy right? :)
160194

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
162199

163200
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
164201
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.
165202

166-
== Sequel
203+
## Sequel
167204

168205
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.
169206

170-
== EventMachine
207+
## EventMachine
171208

172209
The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
173210
while specifying callbacks for success for failure. Here's a simple example:
174211

175-
require 'mysql2/em'
212+
``` ruby
213+
require 'mysql2/em'
176214

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"
218+
defer1.callback do |result|
219+
puts "Result: #{result.to_a.inspect}"
220+
end
183221

184-
client2 = Mysql2::EM::Client.new
185-
defer2 = client2.query "SELECT sleep(1) second_query"
186-
defer2.callback do |result|
187-
puts "Result: #{result.to_a.inspect}"
188-
end
222+
client2 = Mysql2::EM::Client.new
223+
defer2 = client2.query "SELECT sleep(1) second_query"
224+
defer2.callback do |result|
225+
puts "Result: #{result.to_a.inspect}"
189226
end
227+
end
228+
```
190229

191-
== Lazy Everything
230+
## Lazy Everything
192231

193232
Well... almost ;)
194233

195234
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.
196235

197236
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).
199238
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.
200239
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.
201240

202241
This caching behavior can be disabled by setting the :cache_rows option to false.
203242

204243
As for field values themselves, I'm workin on it - but expect that soon.
205244

206-
== Compatibility
245+
## Compatibility
207246

208247
The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
209248

@@ -215,7 +254,7 @@ The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
215254

216255
The ActiveRecord driver should work on 2.3.5 and 3.0
217256

218-
== Yeah... but why?
257+
## Yeah... but why?
219258

220259
Someone: Dude, the Mysql gem works fiiiiiine.
221260

@@ -227,29 +266,34 @@ Someone: OK fine, but do_mysql can already give me back values with Ruby objects
227266

228267
Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
229268

230-
== Benchmarks
269+
## Benchmarks
231270

232271
Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
233272
then iterating over every row using an #each like method yielding a block:
234273

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+
```
243285

244-
== Development
286+
## Development
245287

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

249-
bundle install
250-
rake
291+
``` sh
292+
bundle install
293+
rake
294+
```
251295

252-
== Special Thanks
296+
## Special Thanks
253297

254298
* Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
255299
* Yury Korolev (http://github.com/yury) - for TONS of help testing the ActiveRecord adapter

0 commit comments

Comments
 (0)