mysql2 0.2.24 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1 -0
- data/LICENSE +21 -0
- data/README.md +237 -85
- data/ext/mysql2/client.c +582 -249
- data/ext/mysql2/client.h +10 -38
- data/ext/mysql2/extconf.rb +217 -66
- data/ext/mysql2/infile.c +2 -2
- data/ext/mysql2/mysql2_ext.c +9 -2
- data/ext/mysql2/mysql2_ext.h +13 -14
- data/ext/mysql2/mysql_enc_name_to_ruby.h +62 -58
- data/ext/mysql2/mysql_enc_to_ruby.h +82 -18
- data/ext/mysql2/result.c +736 -200
- data/ext/mysql2/result.h +13 -6
- data/ext/mysql2/statement.c +612 -0
- data/ext/mysql2/statement.h +17 -0
- data/ext/mysql2/wait_for_single_fd.h +2 -1
- data/lib/mysql2/client.rb +110 -28
- data/lib/mysql2/console.rb +1 -1
- data/lib/mysql2/em.rb +15 -9
- data/lib/mysql2/error.rb +57 -36
- data/lib/mysql2/field.rb +3 -0
- data/lib/mysql2/result.rb +2 -0
- data/lib/mysql2/statement.rb +9 -0
- data/lib/mysql2/version.rb +1 -1
- data/lib/mysql2.rb +66 -15
- data/support/3A79BD29.asc +49 -0
- data/support/5072E1F5.asc +432 -0
- data/support/libmysql.def +219 -0
- data/support/mysql_enc_to_ruby.rb +15 -11
- data/support/ruby_enc_to_mysql.rb +8 -6
- metadata +30 -94
- data/MIT-LICENSE +0 -20
- data/examples/eventmachine.rb +0 -21
- data/examples/threaded.rb +0 -20
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -635
- data/lib/arel/engines/sql/compilers/mysql2_compiler.rb +0 -11
- data/spec/configuration.yml.example +0 -17
- data/spec/em/em_spec.rb +0 -114
- data/spec/my.cnf.example +0 -9
- data/spec/mysql2/client_spec.rb +0 -897
- data/spec/mysql2/error_spec.rb +0 -83
- data/spec/mysql2/result_spec.rb +0 -505
- data/spec/rcov.opts +0 -3
- data/spec/spec_helper.rb +0 -87
- data/spec/test_data +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f387f841adc885eb4000a960ee34f475c7edab79e276d17ce8889f370bdcf387
|
4
|
+
data.tar.gz: 0c8732c2a45ed8ac68aea92765daba5969f860f1fe5a95d44e89b76deae7108b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d6afb1b661525183075d14046630195bc79a5f945b92489d3daa0aeea8582e8d17a0fa9d1781fadeaaefaaeac567ead5ad7d0266e1fb9aa96076d26f1c03ad1
|
7
|
+
data.tar.gz: 9b2202a06e36ca910a9648f71d8646e67a6e665153b3ac04dcd535cd768bb78942dd799893e205df841b0461aeb5810985704e18e4a045b540f2d272b11776cd
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Changes are maintained under [Releases](https://github.com/brianmario/mysql2/releases)
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Brian Lopez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,26 +1,40 @@
|
|
1
1
|
# Mysql2 - A modern, simple and very fast MySQL library for Ruby - binding to libmysql
|
2
2
|
|
3
|
-
|
3
|
+
GitHub Actions
|
4
|
+
[![GitHub Actions Status: Build](https://github.com/brianmario/mysql2/actions/workflows/build.yml/badge.svg)](https://github.com/brianmario/mysql2/actions/workflows/build.yml)
|
5
|
+
[![GitHub Actions Status: Container](https://github.com/brianmario/mysql2/actions/workflows/container.yml/badge.svg)](https://github.com/brianmario/mysql2/actions/workflows/container.yml)
|
6
|
+
Travis CI
|
7
|
+
[![Travis CI Status](https://travis-ci.org/brianmario/mysql2.png)](https://travis-ci.org/brianmario/mysql2)
|
8
|
+
Appveyor CI
|
9
|
+
[![Appveyor CI Status](https://ci.appveyor.com/api/projects/status/github/sodabrew/mysql2)](https://ci.appveyor.com/project/sodabrew/mysql2)
|
4
10
|
|
5
11
|
The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
|
6
12
|
Some database libraries out there serve as direct 1:1 mappings of the already complex C APIs available.
|
7
13
|
This one is not.
|
8
14
|
|
9
|
-
It also forces the use of UTF-8 [or binary] for the connection
|
15
|
+
It also forces the use of UTF-8 [or binary] for the connection and uses encoding-aware MySQL API calls where it can.
|
10
16
|
|
11
|
-
The API consists of
|
17
|
+
The API consists of three classes:
|
12
18
|
|
13
19
|
`Mysql2::Client` - your connection to the database.
|
14
20
|
|
15
21
|
`Mysql2::Result` - returned from issuing a #query on the connection. It includes Enumerable.
|
16
22
|
|
23
|
+
`Mysql2::Statement` - returned from issuing a #prepare on the connection. Execute the statement to get a Result.
|
24
|
+
|
17
25
|
## Installing
|
18
|
-
|
26
|
+
|
27
|
+
### General Instructions
|
28
|
+
|
19
29
|
``` sh
|
20
30
|
gem install mysql2
|
21
31
|
```
|
22
32
|
|
23
|
-
This gem links against MySQL's `libmysqlclient`
|
33
|
+
This gem links against MySQL's `libmysqlclient` library or `Connector/C`
|
34
|
+
library, and compatible alternatives such as MariaDB.
|
35
|
+
You may need to install a package such as `libmariadb-dev`, `libmysqlclient-dev`,
|
36
|
+
`mysql-devel`, or other appropriate package for your system. See below for
|
37
|
+
system-specific instructions.
|
24
38
|
|
25
39
|
By default, the mysql2 gem will try to find a copy of MySQL in this order:
|
26
40
|
|
@@ -51,25 +65,54 @@ This may be needed if you deploy to a system where these libraries
|
|
51
65
|
are located somewhere different than on your build system.
|
52
66
|
This overrides any rpath calculated by default or by the options above.
|
53
67
|
|
54
|
-
|
55
|
-
|
56
|
-
|
68
|
+
* `--with-sanitize[=address,cfi,integer,memory,thread,undefined]` -
|
69
|
+
Enable sanitizers for Clang / GCC. If no argument is given, try to enable
|
70
|
+
all sanitizers or fail if none are available. If a command-separated list of
|
71
|
+
specific sanitizers is given, configure will fail unless they all are available.
|
72
|
+
Note that the some sanitizers may incur a performance penalty, and the Address
|
73
|
+
Sanitizer may require a runtime library.
|
74
|
+
To see line numbers in backtraces, declare these environment variables
|
75
|
+
(adjust the llvm-symbolizer path as needed for your system):
|
57
76
|
|
58
|
-
|
59
|
-
|
60
|
-
|
77
|
+
``` sh
|
78
|
+
export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
|
79
|
+
export ASAN_OPTIONS=symbolize=1
|
80
|
+
```
|
61
81
|
|
62
|
-
|
82
|
+
### Linux and other Unixes
|
63
83
|
|
64
|
-
|
65
|
-
|
66
|
-
the
|
84
|
+
You may need to install a package such as `libmariadb-dev`, `libmysqlclient-dev`,
|
85
|
+
`mysql-devel`, or `default-libmysqlclient-dev`; refer to your distribution's package guide to
|
86
|
+
find the particular package. The most common issue we see is a user who has
|
87
|
+
the library file `libmysqlclient.so` but is missing the header file `mysql.h`
|
88
|
+
-- double check that you have the _-dev_ packages installed.
|
67
89
|
|
68
|
-
|
90
|
+
### Mac OS X
|
69
91
|
|
70
|
-
|
71
|
-
|
72
|
-
|
92
|
+
You may use MacPorts, Homebrew, or a native MySQL installer package. The most
|
93
|
+
common paths will be automatically searched. If you want to select a specific
|
94
|
+
MySQL directory, use the `--with-mysql-dir` or `--with-mysql-config` options above.
|
95
|
+
|
96
|
+
If you have not done so already, you will need to install the XCode select tools by running
|
97
|
+
`xcode-select --install`.
|
98
|
+
|
99
|
+
### Windows
|
100
|
+
|
101
|
+
Make sure that you have Ruby and the DevKit compilers installed. We recommend
|
102
|
+
the [Ruby Installer](http://rubyinstaller.org) distribution.
|
103
|
+
|
104
|
+
By default, the mysql2 gem will download and use MySQL Connector/C from
|
105
|
+
mysql.com. If you prefer to use a local installation of Connector/C, add the
|
106
|
+
flag `--with-mysql-dir=c:/mysql-connector-c-x-y-z` (_this path may use forward slashes_).
|
107
|
+
|
108
|
+
By default, the `libmysql.dll` library will be copied into the mysql2 gem
|
109
|
+
directory. To prevent this, add the flag `--no-vendor-libmysql`. The mysql2 gem
|
110
|
+
will search for `libmysql.dll` in the following paths, in order:
|
111
|
+
|
112
|
+
* Environment variable `RUBY_MYSQL2_LIBMYSQL_DLL=C:\path\to\libmysql.dll`
|
113
|
+
(_note the Windows-style backslashes_).
|
114
|
+
* In the mysql2 gem's own directory `vendor/libmysql.dll`
|
115
|
+
* In the system's default library search paths.
|
73
116
|
|
74
117
|
## Usage
|
75
118
|
|
@@ -78,7 +121,7 @@ Connect to a database:
|
|
78
121
|
``` ruby
|
79
122
|
# this takes a hash of options, almost all of which map directly
|
80
123
|
# to the familiar database.yml in rails
|
81
|
-
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/
|
124
|
+
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html
|
82
125
|
client = Mysql2::Client.new(:host => "localhost", :username => "root")
|
83
126
|
```
|
84
127
|
|
@@ -104,8 +147,8 @@ results.each do |row|
|
|
104
147
|
# conveniently, row is a hash
|
105
148
|
# the keys are the fields, as you'd expect
|
106
149
|
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
|
107
|
-
puts row["id"] # row["id"].
|
108
|
-
if row["dne"] # non-
|
150
|
+
puts row["id"] # row["id"].is_a? Integer
|
151
|
+
if row["dne"] # non-existent hash entry is nil
|
109
152
|
puts row["dne"]
|
110
153
|
end
|
111
154
|
end
|
@@ -122,22 +165,61 @@ end
|
|
122
165
|
How about with symbolized keys?
|
123
166
|
|
124
167
|
``` ruby
|
125
|
-
client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true) do |row|
|
168
|
+
client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true).each do |row|
|
126
169
|
# do something with row, it's ready to rock
|
127
170
|
end
|
128
171
|
```
|
129
172
|
|
130
|
-
You can get the headers and the
|
173
|
+
You can get the headers, columns, and the field types in the order that they were returned
|
131
174
|
by the query like this:
|
132
175
|
|
133
176
|
``` ruby
|
134
177
|
headers = results.fields # <= that's an array of field names, in order
|
178
|
+
types = results.field_types # <= that's an array of field types, in order
|
135
179
|
results.each(:as => :array) do |row|
|
136
|
-
# Each row is an array, ordered the same as the query results
|
137
|
-
# An otter's den is called a "holt" or "couch"
|
180
|
+
# Each row is an array, ordered the same as the query results
|
181
|
+
# An otter's den is called a "holt" or "couch"
|
138
182
|
end
|
139
183
|
```
|
140
184
|
|
185
|
+
Prepared statements are supported, as well. In a prepared statement, use a `?`
|
186
|
+
in place of each value and then execute the statement to retrieve a result set.
|
187
|
+
Pass your arguments to the execute method in the same number and order as the
|
188
|
+
question marks in the statement. Query options can be passed as keyword arguments
|
189
|
+
to the execute method.
|
190
|
+
|
191
|
+
Be sure to read about the known limitations of prepared statements at
|
192
|
+
[https://dev.mysql.com/doc/refman/5.6/en/c-api-prepared-statement-problems.html](https://dev.mysql.com/doc/refman/5.6/en/c-api-prepared-statement-problems.html)
|
193
|
+
|
194
|
+
``` ruby
|
195
|
+
statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
|
196
|
+
result1 = statement.execute(1)
|
197
|
+
result2 = statement.execute(2)
|
198
|
+
|
199
|
+
statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
|
200
|
+
result = statement.execute(1, "CA")
|
201
|
+
|
202
|
+
statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
|
203
|
+
result = statement.execute(1, "CA", :as => :array)
|
204
|
+
```
|
205
|
+
|
206
|
+
Session Tracking information can be accessed with
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
c = Mysql2::Client.new(
|
210
|
+
host: "127.0.0.1",
|
211
|
+
username: "root",
|
212
|
+
flags: "SESSION_TRACK",
|
213
|
+
init_command: "SET @@SESSION.session_track_schema=ON"
|
214
|
+
)
|
215
|
+
c.query("INSERT INTO test VALUES (1)")
|
216
|
+
session_track_type = Mysql2::Client::SESSION_TRACK_SCHEMA
|
217
|
+
session_track_data = c.session_track(session_track_type)
|
218
|
+
```
|
219
|
+
|
220
|
+
The types of session track types can be found at
|
221
|
+
[https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html](https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html)
|
222
|
+
|
141
223
|
## Connection options
|
142
224
|
|
143
225
|
You may set the following connection options in Mysql2::Client.new(...):
|
@@ -155,15 +237,30 @@ Mysql2::Client.new(
|
|
155
237
|
:read_timeout = seconds,
|
156
238
|
:write_timeout = seconds,
|
157
239
|
:connect_timeout = seconds,
|
240
|
+
:connect_attrs = {:program_name => $PROGRAM_NAME, ...},
|
158
241
|
:reconnect = true/false,
|
159
242
|
:local_infile = true/false,
|
160
243
|
:secure_auth = true/false,
|
244
|
+
:ssl_mode = :disabled / :preferred / :required / :verify_ca / :verify_identity,
|
161
245
|
:default_file = '/path/to/my.cfg',
|
162
246
|
:default_group = 'my.cfg section',
|
247
|
+
:default_auth = 'authentication_windows_client'
|
163
248
|
:init_command => sql
|
164
249
|
)
|
165
250
|
```
|
166
251
|
|
252
|
+
### Connecting to MySQL on localhost and elsewhere
|
253
|
+
|
254
|
+
The underlying MySQL client library uses the `:host` parameter to determine the
|
255
|
+
type of connection to make, with special interpretation you should be aware of:
|
256
|
+
|
257
|
+
* An empty value or `"localhost"` will attempt a local connection:
|
258
|
+
* On Unix, connect to the default local socket path. (To set a custom socket
|
259
|
+
path, use the `:socket` parameter).
|
260
|
+
* On Windows, connect using a shared-memory connection, if enabled, or TCP.
|
261
|
+
* A value of `"."` on Windows specifies a named-pipe connection.
|
262
|
+
* An IPv4 or IPv6 address will result in a TCP connection.
|
263
|
+
* Any other value will be looked up as a hostname for a TCP connection.
|
167
264
|
|
168
265
|
### SSL options
|
169
266
|
|
@@ -171,7 +268,8 @@ Setting any of the following options will enable an SSL connection, but only if
|
|
171
268
|
your MySQL client library and server have been compiled with SSL support.
|
172
269
|
MySQL client library defaults will be used for any parameters that are left out
|
173
270
|
or set to nil. Relative paths are allowed, and may be required by managed
|
174
|
-
hosting providers such as Heroku.
|
271
|
+
hosting providers such as Heroku. Set `:sslverify => true` to require that the
|
272
|
+
server presents a valid certificate.
|
175
273
|
|
176
274
|
``` ruby
|
177
275
|
Mysql2::Client.new(
|
@@ -180,10 +278,80 @@ Mysql2::Client.new(
|
|
180
278
|
:sslcert => '/path/to/client-cert.pem',
|
181
279
|
:sslca => '/path/to/ca-cert.pem',
|
182
280
|
:sslcapath => '/path/to/cacerts',
|
183
|
-
:sslcipher => 'DHE-RSA-AES256-SHA'
|
281
|
+
:sslcipher => 'DHE-RSA-AES256-SHA',
|
282
|
+
:sslverify => true,
|
184
283
|
)
|
185
284
|
```
|
186
285
|
|
286
|
+
### Secure auth
|
287
|
+
|
288
|
+
Starting with MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
|
289
|
+
When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
|
290
|
+
The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
|
291
|
+
To bypass this restriction in the client, pass the option `:secure_auth => false` to Mysql2::Client.new().
|
292
|
+
|
293
|
+
### Flags option parsing
|
294
|
+
|
295
|
+
The `:flags` parameter accepts an integer, a string, or an array. The integer
|
296
|
+
form allows the client to assemble flags from constants defined under
|
297
|
+
`Mysql2::Client` such as `Mysql2::Client::FOUND_ROWS`. Use a bitwise `|` (OR)
|
298
|
+
to specify several flags.
|
299
|
+
|
300
|
+
The string form will be split on whitespace and parsed as with the array form:
|
301
|
+
Plain flags are added to the default flags, while flags prefixed with `-`
|
302
|
+
(minus) are removed from the default flags.
|
303
|
+
|
304
|
+
### Using Active Record's database.yml
|
305
|
+
|
306
|
+
Active Record typically reads its configuration from a file named `database.yml` or an environment variable `DATABASE_URL`.
|
307
|
+
Use the value `mysql2` as the adapter name. For example:
|
308
|
+
|
309
|
+
``` yaml
|
310
|
+
development:
|
311
|
+
adapter: mysql2
|
312
|
+
encoding: utf8
|
313
|
+
database: my_db_name
|
314
|
+
username: root
|
315
|
+
password: my_password
|
316
|
+
host: 127.0.0.1
|
317
|
+
port: 3306
|
318
|
+
flags:
|
319
|
+
- -COMPRESS
|
320
|
+
- FOUND_ROWS
|
321
|
+
- MULTI_STATEMENTS
|
322
|
+
secure_auth: false
|
323
|
+
```
|
324
|
+
|
325
|
+
In this example, the compression flag is negated with `-COMPRESS`.
|
326
|
+
|
327
|
+
### Using Active Record's DATABASE_URL
|
328
|
+
|
329
|
+
Active Record typically reads its configuration from a file named `database.yml` or an environment variable `DATABASE_URL`.
|
330
|
+
Use the value `mysql2` as the protocol name. For example:
|
331
|
+
|
332
|
+
``` shell
|
333
|
+
DATABASE_URL=mysql2://sql_user:sql_pass@sql_host_name:port/sql_db_name?option1=value1&option2=value2
|
334
|
+
```
|
335
|
+
|
336
|
+
### Reading a MySQL config file
|
337
|
+
|
338
|
+
You may read configuration options from a MySQL configuration file by passing
|
339
|
+
the `:default_file` and `:default_group` parameters. For example:
|
340
|
+
|
341
|
+
``` ruby
|
342
|
+
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
|
343
|
+
```
|
344
|
+
|
345
|
+
### Initial command on connect and reconnect
|
346
|
+
|
347
|
+
If you specify the `:init_command` option, the SQL string you provide will be executed after the connection is established.
|
348
|
+
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
|
349
|
+
It is useful if you want to provide session options which survive reconnection.
|
350
|
+
|
351
|
+
``` ruby
|
352
|
+
Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
|
353
|
+
```
|
354
|
+
|
187
355
|
### Multiple result sets
|
188
356
|
|
189
357
|
You can also retrieve multiple result sets. For this to work you need to
|
@@ -219,53 +387,13 @@ end
|
|
219
387
|
```
|
220
388
|
|
221
389
|
Yields:
|
222
|
-
|
390
|
+
|
391
|
+
```ruby
|
223
392
|
{"1"=>1}
|
224
393
|
{"2"=>2}
|
225
394
|
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
|
226
395
|
```
|
227
396
|
|
228
|
-
See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
|
229
|
-
|
230
|
-
### Secure auth
|
231
|
-
|
232
|
-
Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
|
233
|
-
When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
|
234
|
-
The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
|
235
|
-
To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new().
|
236
|
-
If using ActiveRecord, your database.yml might look something like this:
|
237
|
-
|
238
|
-
``` yaml
|
239
|
-
development:
|
240
|
-
adapter: mysql2
|
241
|
-
encoding: utf8
|
242
|
-
database: my_db_name
|
243
|
-
username: root
|
244
|
-
password: my_password
|
245
|
-
host: 127.0.0.1
|
246
|
-
port: 3306
|
247
|
-
secure_auth: false
|
248
|
-
```
|
249
|
-
|
250
|
-
### Reading a MySQL config file
|
251
|
-
|
252
|
-
You may read configuration options from a MySQL configuration file by passing
|
253
|
-
the `:default_file` and `:default_group` paramters. For example:
|
254
|
-
|
255
|
-
``` ruby
|
256
|
-
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
|
257
|
-
```
|
258
|
-
|
259
|
-
### Initial command on connect and reconnect
|
260
|
-
|
261
|
-
If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
|
262
|
-
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
|
263
|
-
It is useful if you want to provide session options which survive reconnection.
|
264
|
-
|
265
|
-
``` ruby
|
266
|
-
Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")
|
267
|
-
```
|
268
|
-
|
269
397
|
## Cascading config
|
270
398
|
|
271
399
|
The default config hash is at:
|
@@ -303,6 +431,15 @@ c = Mysql2::Client.new
|
|
303
431
|
c.query(sql, :symbolize_keys => true)
|
304
432
|
```
|
305
433
|
|
434
|
+
or
|
435
|
+
|
436
|
+
``` ruby
|
437
|
+
# this will set the options for the Mysql2::Result instance returned from the #execute method
|
438
|
+
c = Mysql2::Client.new
|
439
|
+
s = c.prepare(sql)
|
440
|
+
s.execute(arg1, args2, :symbolize_keys => true)
|
441
|
+
```
|
442
|
+
|
306
443
|
## Result types
|
307
444
|
|
308
445
|
### Array of Arrays
|
@@ -311,7 +448,7 @@ Pass the `:as => :array` option to any of the above methods of configuration
|
|
311
448
|
|
312
449
|
### Array of Hashes
|
313
450
|
|
314
|
-
The default result type is set to
|
451
|
+
The default result type is set to `:hash`, but you can override a previous setting to something else with `:as => :hash`
|
315
452
|
|
316
453
|
### Timezones
|
317
454
|
|
@@ -336,6 +473,15 @@ client = Mysql2::Client.new
|
|
336
473
|
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
|
337
474
|
```
|
338
475
|
|
476
|
+
Keep in mind that this works only with fields and not with computed values, e.g. this result will contain `1`, not `true`:
|
477
|
+
|
478
|
+
``` ruby
|
479
|
+
client = Mysql2::Client.new
|
480
|
+
result = client.query("SELECT true", :cast_booleans => true)
|
481
|
+
```
|
482
|
+
|
483
|
+
CAST function wouldn't help here as there's no way to cast to TINYINT(1). Apparently the only way to solve this is to use a stored procedure with return type set to TINYINT(1).
|
484
|
+
|
339
485
|
### Skipping casting
|
340
486
|
|
341
487
|
Mysql2 casting is fast, but not as fast as not casting data. In rare cases where typecasting is not needed, it will be faster to disable it by providing :cast => false. (Note that :cast => false overrides :cast_booleans => true.)
|
@@ -401,7 +547,7 @@ There are a few things that need to be kept in mind while using streaming:
|
|
401
547
|
* `:cache_rows` is ignored currently. (if you want to use `:cache_rows` you probably don't want to be using `:stream`)
|
402
548
|
* You must fetch all rows in the result set of your query before you can make new queries. (i.e. with `Mysql2::Result#each`)
|
403
549
|
|
404
|
-
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.
|
550
|
+
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](http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html).
|
405
551
|
|
406
552
|
### Lazy Everything
|
407
553
|
|
@@ -422,20 +568,21 @@ As for field values themselves, I'm workin on it - but expect that soon.
|
|
422
568
|
|
423
569
|
This gem is tested with the following Ruby versions on Linux and Mac OS X:
|
424
570
|
|
425
|
-
|
426
|
-
|
427
|
-
* Rubinius 2.x
|
571
|
+
* Ruby MRI 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x
|
572
|
+
* Rubinius 2.x and 3.x do work but may fail under some workloads
|
428
573
|
|
429
574
|
This gem is tested with the following MySQL and MariaDB versions:
|
430
575
|
|
431
|
-
|
432
|
-
|
433
|
-
|
576
|
+
* MySQL 5.5, 5.6, 5.7, 8.0
|
577
|
+
* MySQL Connector/C 6.0 and 6.1 (primarily on Windows)
|
578
|
+
* MariaDB 5.5, 10.0, 10.1, 10.2, 10.3
|
434
579
|
|
435
|
-
### Active Record
|
580
|
+
### Ruby on Rails / Active Record
|
436
581
|
|
437
|
-
|
438
|
-
|
582
|
+
* mysql2 0.5.x works with Rails / Active Record 4.2.11, 5.0.7, 5.1.6, and higher.
|
583
|
+
* mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.
|
584
|
+
* mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.
|
585
|
+
* mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.
|
439
586
|
|
440
587
|
### Asynchronous Active Record
|
441
588
|
|
@@ -518,7 +665,12 @@ though.
|
|
518
665
|
## Special Thanks
|
519
666
|
|
520
667
|
* Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
|
521
|
-
* Yury Korolev
|
522
|
-
* Aaron Patterson
|
523
|
-
* Mike Perham
|
524
|
-
* Aaron Stone
|
668
|
+
* [Yury Korolev](http://github.com/yury) - for TONS of help testing the Active Record adapter
|
669
|
+
* [Aaron Patterson](http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
|
670
|
+
* [Mike Perham](http://github.com/mperham) - Async Active Record adapter (uses Fibers and EventMachine)
|
671
|
+
* [Aaron Stone](http://github.com/sodabrew) - additional client settings, local files, microsecond time, maintenance support
|
672
|
+
* [Kouhei Ueno](https://github.com/nyaxt) - for the original work on Prepared Statements way back in 2012
|
673
|
+
* [John Cant](http://github.com/johncant) - polishing and updating Prepared Statements support
|
674
|
+
* [Justin Case](http://github.com/justincase) - polishing and updating Prepared Statements support and getting it merged
|
675
|
+
* [Tamir Duberstein](http://github.com/tamird) - for help with timeouts and all around updates and cleanups
|
676
|
+
* [Jun Aruga](http://github.com/junaruga) - for migrating CI tests to GitHub Actions and other improvements
|