11module Mysql2
22 class Client
3- attr_reader :query_options , :read_timeout
3+ attr_reader :query_options , :connect_options , :read_timeout
4+
45 @@default_connect_options = {
56 :connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION ,
7+ :connect_timeout => 120 , # Set default connect_timeout to avoid unlimited retries from signal interruption
68 :default_file => nil ,
7- :default_group => nil
9+ :default_group => nil ,
10+ :read_timeout => nil ,
11+ :encoding => 'utf8'
812 }
913
1014 @@default_query_options = {
@@ -19,31 +23,28 @@ class Client
1923 }
2024
2125 def initialize ( opts = { } )
22- opts = Mysql2 ::Util . key_hash_as_symbols ( opts )
23- @read_timeout = nil
26+ opts = Mysql2 ::Util . key_hash_as_symbols ( opts )
2427 @query_options = @@default_query_options . dup
25- @query_options . merge! @@default_connect_options
2628 @query_options . merge! opts
29+ @connect_options = @@default_connect_options . dup
30+ @connect_options . merge! opts
2731
2832 initialize_ext
2933
30- # Set default connect_timeout to avoid unlimited retries from signal interruption
31- opts [ :connect_timeout ] = 120 unless opts . key? ( :connect_timeout )
32-
3334 [ :reconnect , :connect_timeout , :local_infile , :read_timeout , :write_timeout , :default_file , :default_group , :secure_auth , :init_command ] . each do |key |
34- next unless opts . key? ( key )
35+ next unless @connect_options . key? ( key )
3536 case key
3637 when :reconnect , :local_infile , :secure_auth
37- send ( :"#{ key } =" , !!opts [ key ] )
38+ send ( :"#{ key } =" , !!@connect_options [ key ] )
3839 when :connect_timeout , :read_timeout , :write_timeout
39- send ( :"#{ key } =" , opts [ key ] . to_i )
40+ send ( :"#{ key } =" , @connect_options [ key ] . to_i )
4041 else
41- send ( :"#{ key } =" , opts [ key ] )
42+ send ( :"#{ key } =" , @connect_options [ key ] )
4243 end
4344 end
4445
45- # force the encoding to utf8
46- self . charset_name = opts [ :encoding ] || 'utf8'
46+ # force the encoding to utf8 even if set to nil
47+ self . charset_name = @connect_options [ :encoding ] || 'utf8'
4748
4849 ssl_options = opts . values_at ( :sslkey , :sslcert , :sslca , :sslcapath , :sslcipher )
4950 ssl_set ( *ssl_options ) if ssl_options . any?
@@ -78,6 +79,10 @@ def self.default_query_options
7879 @@default_query_options
7980 end
8081
82+ def self . default_connect_options
83+ @@default_connect_options
84+ end
85+
8186 def query_info
8287 info = query_info_string
8388 return { } unless info
0 commit comments