Skip to content

Commit 8aec02d

Browse files
committed
Separate out connect options from query options
1 parent d0fef7c commit 8aec02d

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

lib/mysql2/client.rb

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
module Mysql2
22
class Client
3-
attr_reader :query_options, :read_timeout
3+
attr_reader :connect_options, :query_options, :read_timeout
4+
5+
VALID_CONNECT_KEYS = [:connect_flags, :connect_timeout, :encoding, :default_file, :default_group, :read_timeout, :write_timeout, :secure_auth, :init_command, :reconnect, :local_infile]
6+
@@default_connect_options = {
7+
:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
8+
:connect_timeout => 120, # Set default connect_timeout to avoid unlimited retries from signal interruption
9+
:encoding => 'utf8'
10+
}
11+
12+
VALID_QUERY_KEYS = [:as, :async, :cast_booleans, :symbolize_keys, :database_timezone, :application_timezone, :cache_rows, :cast]
413
@@default_query_options = {
5-
:as => :hash, # the type of object you want each row back as; also supports :array (an array of values)
6-
:async => false, # don't wait for a result after sending the query, you'll have to monitor the socket yourself then eventually call Mysql2::Client#async_result
7-
:cast_booleans => false, # cast tinyint(1) fields as true/false in ruby
8-
:symbolize_keys => false, # return field names as symbols instead of strings
9-
:database_timezone => :local, # timezone Mysql2 will assume datetime objects are stored in
10-
:application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller
11-
:cache_rows => true, # tells Mysql2 to use it's internal row cache for results
12-
:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
13-
:cast => true,
14-
:default_file => nil,
15-
:default_group => nil
14+
:as => :hash, # the type of object you want each row back as; also supports :array (an array of values)
15+
:async => false, # don't wait for a result after sending the query, you'll have to monitor the socket yourself then eventually call Mysql2::Client#async_result
16+
:cast_booleans => false, # cast tinyint(1) fields as true/false in ruby
17+
:symbolize_keys => false, # return field names as symbols instead of strings
18+
:database_timezone => :local, # timezone Mysql2 will assume datetime objects are stored in
19+
:application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller
20+
:cache_rows => true, # tells Mysql2 to use it's internal row cache for results
21+
:cast => true # cast result fields to corresponding Ruby data types
1622
}
1723

1824
def initialize(opts = {})
19-
opts = Mysql2::Util.key_hash_as_symbols( opts )
20-
@read_timeout = nil
21-
@query_options = @@default_query_options.dup
22-
@query_options.merge! opts
25+
opts = Mysql2::Util.key_hash_as_symbols(opts)
26+
@read_timeout = nil # by default don't timeout on read
27+
@connect_options = @@default_connect_options.merge Hash[ opts.select { |k, v| VALID_CONNECT_KEYS.include? k } ]
28+
@query_options = @@default_query_options.merge Hash[ opts.select { |k, v| VALID_QUERY_KEYS.include? k } ]
2329

2430
initialize_ext
2531

26-
# Set default connect_timeout to avoid unlimited retries from signal interruption
27-
opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout)
28-
2932
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command].each do |key|
30-
next unless opts.key?(key)
33+
next unless @connect_options.key?(key)
3134
case key
3235
when :reconnect, :local_infile, :secure_auth
33-
send(:"#{key}=", !!opts[key])
36+
send(:"#{key}=", !!@connect_options[key])
3437
when :connect_timeout, :read_timeout, :write_timeout
35-
send(:"#{key}=", opts[key].to_i)
38+
send(:"#{key}=", @connect_options[key].to_i)
3639
else
37-
send(:"#{key}=", opts[key])
40+
send(:"#{key}=", @connect_options[key])
3841
end
3942
end
4043

41-
# force the encoding to utf8
42-
self.charset_name = opts[:encoding] || 'utf8'
44+
# force the encoding to utf8 even if set to nil
45+
self.charset_name = @connect_options[:encoding] || 'utf8'
4346

4447
ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher)
4548
ssl_set(*ssl_options) if ssl_options.any?
@@ -57,7 +60,7 @@ def initialize(opts = {})
5760
port = opts[:port]
5861
database = opts[:database] || opts[:dbname] || opts[:db]
5962
socket = opts[:socket] || opts[:sock]
60-
flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]
63+
flags = opts[:flags] ? opts[:flags] | @connect_options[:connect_flags] : @connect_options[:connect_flags]
6164

6265
# Correct the data types before passing these values down to the C level
6366
user = user.to_s unless user.nil?
@@ -66,10 +69,15 @@ def initialize(opts = {})
6669
port = port.to_i unless port.nil?
6770
database = database.to_s unless database.nil?
6871
socket = socket.to_s unless socket.nil?
72+
flags = flags.to_i # if nil then 0
6973

7074
connect user, pass, host, port, database, socket, flags
7175
end
7276

77+
def self.default_connect_options
78+
@@default_connect_options
79+
end
80+
7381
def self.default_query_options
7482
@@default_query_options
7583
end

0 commit comments

Comments
 (0)