Skip to content

Commit 50c572a

Browse files
committed
Add README section for flags parsing and change negated flags to a - (minus) prefix
As I was writing the README for this, I found that unquoted !FLAG_FOO was being interpreted by the YAML engine as a Ruby class invocations. This would surely lead to some confusing or downright bad results if used unquoted in database.yml files.
1 parent 7cb3647 commit 50c572a

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,26 @@ Yields:
272272
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)
273273
```
274274

275-
See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
276-
277275
### Secure auth
278276

279277
Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this).
280278
When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format.
281279
The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password.
282-
To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new().
283-
If using ActiveRecord, your database.yml might look something like this:
280+
To bypass this restriction in the client, pass the option `:secure_auth => false` to Mysql2::Client.new().
281+
282+
### Flags option parsing
283+
284+
The `:flags` parameter accepts an integer, a string, or an array. The integer
285+
form allows the client to assemble flags from constants defined under
286+
`Mysql2::Client` such as `Mysql2::Client::FOUND_ROWS`. Use a bitwise `|` (OR)
287+
to specify several flags.
288+
289+
The string form will be split on whitespace and parsed as with the array form:
290+
Plain flags are added to the default flags, while flags prefixed with `-`
291+
(minus) are removed from the default flags.
292+
293+
This allows easier use with ActiveRecord's database.yml, avoiding the need for magic flag numbers.
294+
For example, to disable protocol compression, and enable multiple statements and result sets:
284295

285296
``` yaml
286297
development:
@@ -291,21 +302,25 @@ development:
291302
password: my_password
292303
host: 127.0.0.1
293304
port: 3306
305+
flags:
306+
- -COMPRESS
307+
- FOUND_ROWS
308+
- MULTI_STATEMENTS
294309
secure_auth: false
295310
```
296311
297312
### Reading a MySQL config file
298313
299314
You may read configuration options from a MySQL configuration file by passing
300-
the `:default_file` and `:default_group` paramters. For example:
315+
the `:default_file` and `:default_group` parameters. For example:
301316

302317
``` ruby
303318
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')
304319
```
305320

306321
### Initial command on connect and reconnect
307322

308-
If you specify the init_command option, the SQL string you provide will be executed after the connection is established.
323+
If you specify the `:init_command` option, the SQL string you provide will be executed after the connection is established.
309324
If `:reconnect` is set to `true`, init_command will also be executed after a successful reconnect.
310325
It is useful if you want to provide session options which survive reconnection.
311326

lib/mysql2/client.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ def initialize(opts = {})
8989

9090
def parse_flags_array(flags, initial = 0)
9191
flags.reduce(initial) do |memo, f|
92-
# const_defined? does not like a leading !
93-
if !f.start_with?('!') && Mysql2::Client.const_defined?(f)
92+
fneg = f.start_with?('-') ? f[1..-1] : nil
93+
if fneg && fneg =~ /^\w+$/ && Mysql2::Client.const_defined?(fneg)
94+
memo & ~ Mysql2::Client.const_get(fneg)
95+
elsif f && f =~ /^\w+$/ && Mysql2::Client.const_defined?(f)
9496
memo | Mysql2::Client.const_get(f)
95-
elsif f.start_with?('!') && Mysql2::Client.const_defined?(f[1..-1])
96-
memo & ~ Mysql2::Client.const_get(f[1..-1])
9797
else
98-
warn "Unknown MySQL connection flag: #{f}"
98+
warn "Unknown MySQL connection flag: '#{f}'"
9999
memo
100100
end
101101
end

spec/mysql2/client_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def connect(*args)
5757
end
5858

5959
it "should parse flags array" do
60-
client = Klient.new :flags => %w( FOUND_ROWS !PROTOCOL_41 )
60+
client = Klient.new :flags => %w( FOUND_ROWS -PROTOCOL_41 )
6161
expect(client.connect_args.last[6] & Mysql2::Client::FOUND_ROWS).to eql(Mysql2::Client::FOUND_ROWS)
6262
expect(client.connect_args.last[6] & Mysql2::Client::PROTOCOL_41).to eql(0)
6363
end
6464

6565
it "should parse flags string" do
66-
client = Klient.new :flags => "FOUND_ROWS !PROTOCOL_41"
66+
client = Klient.new :flags => "FOUND_ROWS -PROTOCOL_41"
6767
expect(client.connect_args.last[6] & Mysql2::Client::FOUND_ROWS).to eql(Mysql2::Client::FOUND_ROWS)
6868
expect(client.connect_args.last[6] & Mysql2::Client::PROTOCOL_41).to eql(0)
6969
end

0 commit comments

Comments
 (0)