Skip to content

Commit 5b1d095

Browse files
committed
Add several RuboCop checks
1 parent a253352 commit 5b1d095

File tree

12 files changed

+79
-122
lines changed

12 files changed

+79
-122
lines changed

.rubocop_todo.yml

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-12 18:00:35 -0700 using RuboCop version 0.29.1.
2+
# on 2015-03-12 18:28:12 -0700 using RuboCop version 0.29.1.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

8-
# Offense count: 1
9-
Lint/UselessAccessModifier:
10-
Enabled: false
11-
12-
# Offense count: 1
13-
Lint/Void:
14-
Enabled: false
15-
168
# Offense count: 2
179
Metrics/AbcSize:
18-
Max: 64
10+
Max: 66
1911

2012
# Offense count: 1
2113
Metrics/BlockNesting:
@@ -30,7 +22,7 @@ Metrics/CyclomaticComplexity:
3022
Metrics/LineLength:
3123
Max: 232
3224

33-
# Offense count: 4
25+
# Offense count: 5
3426
# Configuration parameters: CountComments.
3527
Metrics/MethodLength:
3628
Max: 40
@@ -44,53 +36,21 @@ Metrics/PerceivedComplexity:
4436
Style/BlockDelimiters:
4537
Enabled: false
4638

47-
# Offense count: 1
48-
# Configuration parameters: EnforcedStyle, SupportedStyles.
49-
Style/ClassAndModuleChildren:
50-
Enabled: false
51-
52-
# Offense count: 1
53-
Style/ClassVars:
54-
Enabled: false
55-
56-
# Offense count: 9
39+
# Offense count: 10
5740
Style/Documentation:
5841
Enabled: false
5942

60-
# Offense count: 1
61-
Style/DoubleNegation:
62-
Enabled: false
63-
6443
# Offense count: 9
6544
# Configuration parameters: AllowedVariables.
6645
Style/GlobalVars:
6746
Enabled: false
6847

69-
# Offense count: 4
70-
# Configuration parameters: MaxLineLength.
71-
Style/IfUnlessModifier:
72-
Enabled: false
73-
74-
# Offense count: 1
75-
Style/MultilineBlockChain:
76-
Enabled: false
77-
78-
# Offense count: 13
48+
# Offense count: 12
7949
# Cop supports --auto-correct.
8050
Style/NumericLiterals:
8151
MinDigits: 20
8252

83-
# Offense count: 549
8453
# Cop supports --auto-correct.
8554
# Configuration parameters: EnforcedStyle, SupportedStyles.
8655
Style/StringLiterals:
8756
Enabled: false
88-
89-
# Offense count: 2
90-
Style/UnlessElse:
91-
Enabled: false
92-
93-
# Offense count: 1
94-
# Configuration parameters: MaxLineLength.
95-
Style/WhileUntilModifier:
96-
Enabled: false

benchmark/query_with_mysql_casting.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def mysql_cast(type, value)
3030
when Mysql::Field::TYPE_TIME, Mysql::Field::TYPE_DATETIME, Mysql::Field::TYPE_TIMESTAMP
3131
Time.parse(value)
3232
when Mysql::Field::TYPE_BLOB, Mysql::Field::TYPE_BIT, Mysql::Field::TYPE_STRING,
33-
Mysql::Field::TYPE_VAR_STRING, Mysql::Field::TYPE_CHAR, Mysql::Field::TYPE_SET
34-
Mysql::Field::TYPE_ENUM
33+
Mysql::Field::TYPE_VAR_STRING, Mysql::Field::TYPE_CHAR, Mysql::Field::TYPE_SET,
34+
Mysql::Field::TYPE_ENUM
3535
value
3636
else
3737
value

ext/mysql2/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ static VALUE rb_mysql_client_abandon_results(VALUE self) {
648648
* client.query(sql, options = {})
649649
*
650650
* Query the database with +sql+, with optional +options+. For the possible
651-
* options, see @@default_query_options on the Mysql2::Client class.
651+
* options, see default_query_options on the Mysql2::Client class.
652652
*/
653653
static VALUE rb_query(VALUE self, VALUE sql, VALUE current) {
654654
#ifndef _WIN32

ext/mysql2/extconf.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ def asplode(lib)
6666
abort unless $CHILD_STATUS.success?
6767
libs = `#{mc} --libs_r`.chomp
6868
# MySQL 5.5 and above already have re-entrant code in libmysqlclient (no _r).
69-
if ver >= 5.5 || libs.empty?
70-
libs = `#{mc} --libs`.chomp
71-
end
69+
libs = `#{mc} --libs`.chomp if ver >= 5.5 || libs.empty?
7270
abort unless $CHILD_STATUS.success?
7371
$INCFLAGS += ' ' + includes
7472
$libs = libs + " " + $libs
@@ -104,10 +102,8 @@ def asplode(lib)
104102
-Wno-unused-function
105103
-Wno-declaration-after-statement
106104
-Wno-missing-field-initializers
107-
).select do |flag|
108-
try_link('int main() {return 0;}', flag)
109-
end.each do |flag|
110-
$CFLAGS << ' ' << flag
105+
).each do |flag|
106+
$CFLAGS << ' ' << flag if try_link('int main() {return 0;}', flag)
111107
end
112108

113109
if RUBY_PLATFORM =~ /mswin|mingw/

lib/mysql2.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
dll_path = if ENV['RUBY_MYSQL2_LIBMYSQL_DLL']
1111
# If this environment variable is set, it overrides any other paths
1212
# The user is advised to use backslashes not forward slashes
13-
ENV['RUBY_MYSQL2_LIBMYSQL_DLL'].dup
13+
ENV['RUBY_MYSQL2_LIBMYSQL_DLL']
1414
elsif File.exist?(File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)))
1515
# Use vendor/libmysql.dll if it exists, convert slashes for Win32 LoadLibrary
1616
File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)).gsub('/', '\\')
@@ -53,12 +53,14 @@ module Mysql2
5353
end
5454

5555
# For holding utility methods
56-
module Mysql2::Util
57-
#
58-
# Rekey a string-keyed hash with equivalent symbols.
59-
#
60-
def self.key_hash_as_symbols(hash)
61-
return nil unless hash
62-
Hash[hash.map { |k, v| [k.to_sym, v] }]
56+
module Mysql2
57+
module Util
58+
#
59+
# Rekey a string-keyed hash with equivalent symbols.
60+
#
61+
def self.key_hash_as_symbols(hash)
62+
return nil unless hash
63+
Hash[hash.map { |k, v| [k.to_sym, v] }]
64+
end
6365
end
6466
end

lib/mysql2/client.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
module Mysql2
22
class Client
33
attr_reader :query_options, :read_timeout
4-
@@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,
16-
}
4+
5+
def self.default_query_options
6+
@default_query_options ||= {
7+
:as => :hash, # the type of object you want each row back as; also supports :array (an array of values)
8+
: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
9+
:cast_booleans => false, # cast tinyint(1) fields as true/false in ruby
10+
:symbolize_keys => false, # return field names as symbols instead of strings
11+
:database_timezone => :local, # timezone Mysql2 will assume datetime objects are stored in
12+
:application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller
13+
:cache_rows => true, # tells Mysql2 to use it's internal row cache for results
14+
:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
15+
:cast => true,
16+
:default_file => nil,
17+
:default_group => nil,
18+
}
19+
end
1720

1821
def initialize(opts = {})
1922
opts = Mysql2::Util.key_hash_as_symbols(opts)
2023
@read_timeout = nil
21-
@query_options = @@default_query_options.dup
24+
@query_options = self.class.default_query_options.dup
2225
@query_options.merge! opts
2326

2427
initialize_ext
2528

2629
# Set default connect_timeout to avoid unlimited retries from signal interruption
2730
opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout)
2831

32+
# TODO: stricter validation rather than silent massaging
2933
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command].each do |key|
3034
next unless opts.key?(key)
3135
case key
3236
when :reconnect, :local_infile, :secure_auth
33-
send(:"#{key}=", !!opts[key])
37+
send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation
3438
when :connect_timeout, :read_timeout, :write_timeout
3539
send(:"#{key}=", opts[key].to_i)
3640
else
@@ -75,10 +79,6 @@ def initialize(opts = {})
7579
connect user, pass, host, port, database, socket, flags
7680
end
7781

78-
def self.default_query_options
79-
@@default_query_options
80-
end
81-
8282
if Thread.respond_to?(:handle_interrupt)
8383
require 'timeout'
8484

@@ -105,10 +105,12 @@ def info
105105
self.class.info
106106
end
107107

108-
private
108+
class << self
109+
private
109110

110-
def self.local_offset
111-
::Time.local(2010).utc_offset.to_r / 86400
111+
def local_offset
112+
::Time.local(2010).utc_offset.to_r / 86400
113+
end
112114
end
113115
end
114116
end

lib/mysql2/em.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def unbind
3434
end
3535

3636
def close(*args)
37-
if @watch
38-
@watch.detach if @watch.watching?
39-
end
37+
@watch.detach if @watch && @watch.watching?
38+
4039
super(*args)
4140
end
4241

spec/mysql2/client_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ def run_gc
574574

575575
it "does not interfere with other statements" do
576576
@multi_client.query("SELECT 1 AS 'set_1'; SELECT 2 AS 'set_2'")
577-
while @multi_client.next_result
578-
@multi_client.store_result
579-
end
577+
@multi_client.store_result while @multi_client.next_result
580578

581579
expect(@multi_client.query("SELECT 3 AS 'next'").first).to eq('next' => 3)
582580
end

spec/mysql2/result_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@
279279
end
280280

281281
if 1.size == 4 # 32bit
282-
unless RUBY_VERSION =~ /1.8/
283-
klass = Time
282+
klass = if RUBY_VERSION =~ /1.8/
283+
DateTime
284284
else
285-
klass = DateTime
285+
Time
286286
end
287287

288288
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
@@ -297,27 +297,27 @@
297297
expect(r.first['test']).to be_an_instance_of(klass)
298298
end
299299
elsif 1.size == 8 # 64bit
300-
unless RUBY_VERSION =~ /1.8/
301-
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
302-
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
300+
if RUBY_VERSION =~ /1.8/
301+
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
302+
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
303303
expect(r.first['test']).to be_an_instance_of(Time)
304304
end
305305

306+
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
307+
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
308+
expect(r.first['test']).to be_an_instance_of(DateTime)
309+
end
310+
306311
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
307312
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
308313
expect(r.first['test']).to be_an_instance_of(Time)
309314
end
310315
else
311-
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
312-
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
316+
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
317+
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
313318
expect(r.first['test']).to be_an_instance_of(Time)
314319
end
315320

316-
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
317-
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
318-
expect(r.first['test']).to be_an_instance_of(DateTime)
319-
end
320-
321321
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
322322
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
323323
expect(r.first['test']).to be_an_instance_of(Time)

spec/mysql2/statement_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@
366366
end
367367

368368
if 1.size == 4 # 32bit
369-
unless RUBY_VERSION =~ /1.8/
370-
klass = Time
369+
klass = if RUBY_VERSION =~ /1.8/
370+
DateTime
371371
else
372-
klass = DateTime
372+
Time
373373
end
374374

375375
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
@@ -384,27 +384,27 @@
384384
expect(r.first['test']).to be_an_instance_of(klass)
385385
end
386386
elsif 1.size == 8 # 64bit
387-
unless RUBY_VERSION =~ /1.8/
388-
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
389-
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
387+
if RUBY_VERSION =~ /1.8/
388+
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
389+
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
390390
expect(r.first['test']).to be_an_instance_of(Time)
391391
end
392392

393+
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
394+
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
395+
expect(r.first['test']).to be_an_instance_of(DateTime)
396+
end
397+
393398
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
394399
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
395400
expect(r.first['test']).to be_an_instance_of(Time)
396401
end
397402
else
398-
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
399-
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
403+
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
404+
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
400405
expect(r.first['test']).to be_an_instance_of(Time)
401406
end
402407

403-
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
404-
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
405-
expect(r.first['test']).to be_an_instance_of(DateTime)
406-
end
407-
408408
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
409409
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
410410
expect(r.first['test']).to be_an_instance_of(Time)

0 commit comments

Comments
 (0)