Skip to content

Commit 79bc620

Browse files
committed
Separate out connect options from query options
1 parent 5e21bf2 commit 79bc620

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

lib/mysql2/client.rb

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
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+
@connect_options = @@default_connect_options.merge Hash[ opts.select { |k, v| VALID_CONNECT_KEYS.include? k } ]
27+
@query_options = @@default_query_options.merge Hash[ opts.select { |k, v| VALID_QUERY_KEYS.include? k } ]
2328

2429
initialize_ext
2530

26-
# Set default connect_timeout to avoid unlimited retries from signal interruption
27-
opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout)
28-
2931
[: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)
32+
next unless @connect_options.key?(key)
3133
case key
3234
when :reconnect, :local_infile, :secure_auth
33-
send(:"#{key}=", !!opts[key])
35+
send(:"#{key}=", !!@connect_options[key])
3436
when :connect_timeout, :read_timeout, :write_timeout
35-
send(:"#{key}=", opts[key].to_i)
37+
send(:"#{key}=", @connect_options[key].to_i)
3638
else
37-
send(:"#{key}=", opts[key])
39+
send(:"#{key}=", @connect_options[key])
3840
end
3941
end
4042

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

4446
ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher)
4547
ssl_set(*ssl_options) if ssl_options.any?
@@ -57,7 +59,7 @@ def initialize(opts = {})
5759
port = opts[:port]
5860
database = opts[:database] || opts[:dbname] || opts[:db]
5961
socket = opts[:socket] || opts[:sock]
60-
flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]
62+
flags = opts[:flags] ? opts[:flags] | @connect_options[:connect_flags] : @connect_options[:connect_flags]
6163

6264
# Correct the data types before passing these values down to the C level
6365
user = user.to_s unless user.nil?
@@ -66,10 +68,15 @@ def initialize(opts = {})
6668
port = port.to_i unless port.nil?
6769
database = database.to_s unless database.nil?
6870
socket = socket.to_s unless socket.nil?
71+
flags = flags.to_i # if nil then 0
6972

7073
connect user, pass, host, port, database, socket, flags
7174
end
7275

76+
def self.default_connect_options
77+
@@default_connect_options
78+
end
79+
7380
def self.default_query_options
7481
@@default_query_options
7582
end

0 commit comments

Comments
 (0)