1
1
module Mysql2
2
2
class Client
3
- attr_reader :query_options , :read_timeout
3
+ attr_reader :query_options , :connect_options , :read_timeout
4
+
4
5
@@default_connect_options = {
5
6
: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
6
8
:default_file => nil ,
7
- :default_group => nil
9
+ :default_group => nil ,
10
+ :read_timeout => nil ,
11
+ :encoding => 'utf8'
8
12
}
9
13
10
14
@@default_query_options = {
@@ -19,31 +23,28 @@ class Client
19
23
}
20
24
21
25
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 )
24
27
@query_options = @@default_query_options . dup
25
- @query_options . merge! @@default_connect_options
26
28
@query_options . merge! opts
29
+ @connect_options = @@default_connect_options . dup
30
+ @connect_options . merge! opts
27
31
28
32
initialize_ext
29
33
30
- # Set default connect_timeout to avoid unlimited retries from signal interruption
31
- opts [ :connect_timeout ] = 120 unless opts . key? ( :connect_timeout )
32
-
33
34
[ :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 )
35
36
case key
36
37
when :reconnect , :local_infile , :secure_auth
37
- send ( :"#{ key } =" , !!opts [ key ] )
38
+ send ( :"#{ key } =" , !!@connect_options [ key ] )
38
39
when :connect_timeout , :read_timeout , :write_timeout
39
- send ( :"#{ key } =" , opts [ key ] . to_i )
40
+ send ( :"#{ key } =" , @connect_options [ key ] . to_i )
40
41
else
41
- send ( :"#{ key } =" , opts [ key ] )
42
+ send ( :"#{ key } =" , @connect_options [ key ] )
42
43
end
43
44
end
44
45
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'
47
48
48
49
ssl_options = opts . values_at ( :sslkey , :sslcert , :sslca , :sslcapath , :sslcipher )
49
50
ssl_set ( *ssl_options ) if ssl_options . any?
@@ -78,6 +79,10 @@ def self.default_query_options
78
79
@@default_query_options
79
80
end
80
81
82
+ def self . default_connect_options
83
+ @@default_connect_options
84
+ end
85
+
81
86
def query_info
82
87
info = query_info_string
83
88
return { } unless info
0 commit comments