Skip to content

Commit 888748c

Browse files
committed
Merge branch 'readme_updates'
2 parents f961df8 + 2cd8550 commit 888748c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ This one is not.
99

1010
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.
1111

12-
The API consists of two classes:
12+
The API consists of three classes:
1313

1414
`Mysql2::Client` - your connection to the database.
1515

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

18+
`Mysql2::Statement` - returned from issuing a #prepare on the connection. Execute the statement to get a Result.
19+
1820
## Installing
1921
### General Instructions
2022
``` sh
@@ -153,6 +155,20 @@ results.each(:as => :array) do |row|
153155
end
154156
```
155157

158+
Prepared statements are supported, as well. In a prepared statement, use a `?`
159+
in place of each value and then execute the statement to retrieve a result set.
160+
Pass your arguments to the execute method in the same number and order as the
161+
question marks in the statement.
162+
163+
``` ruby
164+
statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
165+
result1 = statement.execute(1)
166+
result2 = statement.execute(2)
167+
168+
statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
169+
result = statement.execute(1, "CA")
170+
```
171+
156172
## Connection options
157173

158174
You may set the following connection options in Mysql2::Client.new(...):
@@ -538,4 +554,8 @@ though.
538554
* Yury Korolev (http://github.com/yury) - for TONS of help testing the Active Record adapter
539555
* Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
540556
* Mike Perham (http://github.com/mperham) - Async Active Record adapter (uses Fibers and EventMachine)
541-
* Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support.
557+
* Aaron Stone (http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support
558+
* Kouhei Ueno (https://github.com/nyaxt) - for the original work on Prepared Statements way back in 2012
559+
* John Cant (http://github.com/johncant) - polishing and updating Prepared Statements support
560+
* Justin Case (http://github.com/justincase) - polishing and updating Prepared Statements support and getting it merged
561+
* Tamir Duberstein (http://github.com/tamird) - for help with timeouts and all around updates and cleanups

mysql2.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ require File.expand_path('../lib/mysql2/version', __FILE__)
33
Gem::Specification.new do |s|
44
s.name = %q{mysql2}
55
s.version = Mysql2::VERSION
6-
s.authors = ["Brian Lopez"]
6+
s.authors = ['Brian Lopez', 'Aaron Stone']
77
s.license = "MIT"
8-
s.email = %q{[email protected]}
8+
99
s.extensions = ["ext/mysql2/extconf.rb"]
1010
s.homepage = %q{http://github.com/brianmario/mysql2}
1111
s.rdoc_options = ["--charset=UTF-8"]

spec/mysql2/statement_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@
8383
result = statement.execute
8484
expect(result.to_a.length).to eq(1)
8585
end
86+
87+
it "should handle comparisons and likes" do
88+
@client.query 'USE test'
89+
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int, b varchar(10))'
90+
@client.query 'INSERT INTO mysql2_stmt_q (a, b) VALUES (1, "Hello"), (2, "World")'
91+
statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE a < ?'
92+
results = statement.execute(2)
93+
results.first.should == {"a" => 1, "b" => "Hello"}
94+
95+
statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE b LIKE ?'
96+
results = statement.execute('%orld')
97+
results.first.should == {"a" => 2, "b" => "World"}
98+
99+
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
86100
end
87101

88102
it "should select dates" do

0 commit comments

Comments
 (0)