Skip to content

Commit 2f48f3a

Browse files
committed
Move @@default_..._options to class methods to avoid @@ semantics
Recommended in #596
1 parent 11ba701 commit 2f48f3a

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

ext/mysql2/client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ static VALUE rb_mysql_client_abandon_results(VALUE self) {
639639
/* call-seq:
640640
* client.query(sql, options = {})
641641
*
642-
* Query the database with +sql+, with optional +options+. For the possible
643-
* options, see @@default_query_options on the Mysql2::Client class.
642+
* Query the database with +sql+, with optional +options+.
643+
* For available options, see VALID_QUERY_KEYS in the Mysql2::Client class.
644644
*/
645645
static VALUE rb_query(VALUE self, VALUE sql, VALUE current) {
646646
#ifndef _WIN32

lib/mysql2/client.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,13 @@ class Client
33
attr_reader :connect_options, :query_options, :read_timeout
44

55
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-
126
VALID_QUERY_KEYS = [:as, :async, :cast_booleans, :symbolize_keys, :database_timezone, :application_timezone, :cache_rows, :cast]
13-
@@default_query_options = {
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
22-
}
237

248
def initialize(opts = {})
259
opts = Mysql2::Util.key_hash_as_symbols(opts)
2610
@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 } ]
11+
@connect_options = self.class.default_connect_options.merge Hash[ opts.select { |k, v| VALID_CONNECT_KEYS.include? k } ]
12+
@query_options = self.class.default_query_options.merge Hash[ opts.select { |k, v| VALID_QUERY_KEYS.include? k } ]
2913

3014
initialize_ext
3115

@@ -68,11 +52,24 @@ def initialize(opts = {})
6852
end
6953

7054
def self.default_connect_options
71-
@@default_connect_options
55+
@default_connect_options ||= {
56+
:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
57+
:connect_timeout => 120, # Set default connect_timeout to avoid unlimited retries from signal interruption
58+
:encoding => 'utf8',
59+
}
7260
end
7361

7462
def self.default_query_options
75-
@@default_query_options
63+
@default_query_options ||= {
64+
:as => :hash, # the type of object you want each row back as; also supports :array (an array of values)
65+
: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
66+
:cast_booleans => false, # cast tinyint(1) fields as true/false in ruby
67+
:symbolize_keys => false, # return field names as symbols instead of strings
68+
:database_timezone => :local, # timezone Mysql2 will assume datetime objects are stored in
69+
:application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller
70+
:cache_rows => true, # tells Mysql2 to use it's internal row cache for results
71+
:cast => true, # cast result fields to corresponding Ruby data types
72+
}
7673
end
7774

7875
if Thread.respond_to?(:handle_interrupt)

0 commit comments

Comments
 (0)