Skip to content

Commit

Permalink
Fix some simple cops
Browse files Browse the repository at this point in the history
  • Loading branch information
mishina2228 committed May 30, 2022
1 parent d238d7f commit a9517ac
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Layout/FirstHashElementIndentation:
Layout/EndAlignment:
EnforcedStyleAlignWith: variable

Layout/HashAlignment:
EnforcedHashRocketStyle: table

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

Expand Down
10 changes: 5 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ source 'https://rubygems.org'

gemspec

gem 'rake', if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2")
'~> 13.0.1'
else
'< 13'
end
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2")
gem 'rake', '~> 13.0.1'
else
gem 'rake', '< 13'
end
gem 'rake-compiler', '~> 1.1.0'

# For local debugging, irb is Gemified since Ruby 2.6
Expand Down
2 changes: 1 addition & 1 deletion benchmark/allocations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def bench_allocations(feature, iterations = 10, batch_size = 1000)
GC::Profiler.clear
GC::Profiler.enable
iterations.times { yield batch_size }
GC::Profiler.report(STDOUT)
GC::Profiler.report($stdout)
GC::Profiler.disable
end

Expand Down
1 change: 1 addition & 0 deletions lib/mysql2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module Util
#
def self.key_hash_as_symbols(hash)
return nil unless hash

Hash[hash.map { |k, v| [k.to_sym, v] }]
end

Expand Down
4 changes: 4 additions & 0 deletions lib/mysql2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def self.default_query_options

def initialize(opts = {})
raise Mysql2::Error, "Options parameter must be a Hash" unless opts.is_a? Hash

opts = Mysql2::Util.key_hash_as_symbols(opts)
@read_timeout = nil
@query_options = self.class.default_query_options.dup
Expand All @@ -33,6 +34,7 @@ def initialize(opts = {})
# TODO: stricter validation rather than silent massaging
%i[reconnect connect_timeout local_infile read_timeout write_timeout default_file default_group secure_auth init_command automatic_close enable_cleartext_plugin default_auth].each do |key|
next unless opts.key?(key)

case key
when :reconnect, :local_infile, :secure_auth, :automatic_close, :enable_cleartext_plugin
send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation
Expand Down Expand Up @@ -136,6 +138,7 @@ def find_default_ca_path
# and performance_schema.session_account_connect_attrs
def parse_connect_attrs(conn_attrs)
return {} if Mysql2::Client::CONNECT_ATTRS.zero?

conn_attrs ||= {}
conn_attrs[:program_name] ||= $PROGRAM_NAME
conn_attrs.each_with_object({}) do |(key, value), hash|
Expand All @@ -152,6 +155,7 @@ def query(sql, options = {})
def query_info
info = query_info_string
return {} unless info

info_hash = {}
info.split.each_slice(2) { |s| info_hash[s[0].downcase.delete(':').to_sym] = s[1].to_i }
info_hash
Expand Down
2 changes: 2 additions & 0 deletions spec/mysql2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

Klient = Class.new(Mysql2::Client) do
attr_reader :connect_args

def connect(*args)
@connect_args ||= []
@connect_args << args
Expand Down Expand Up @@ -212,6 +213,7 @@ def run_gc
10.times do
closed = @client.query("SHOW PROCESSLIST").none? { |row| row['Id'] == connection_id }
break if closed

sleep(0.1)
end
expect(closed).to eq(true)
Expand Down
18 changes: 9 additions & 9 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,17 +497,17 @@
end

{
'char_test' => 'CHAR',
'varchar_test' => 'VARCHAR',
'varbinary_test' => 'VARBINARY',
'tiny_blob_test' => 'TINYBLOB',
'tiny_text_test' => 'TINYTEXT',
'blob_test' => 'BLOB',
'text_test' => 'TEXT',
'char_test' => 'CHAR',
'varchar_test' => 'VARCHAR',
'varbinary_test' => 'VARBINARY',
'tiny_blob_test' => 'TINYBLOB',
'tiny_text_test' => 'TINYTEXT',
'blob_test' => 'BLOB',
'text_test' => 'TEXT',
'medium_blob_test' => 'MEDIUMBLOB',
'medium_text_test' => 'MEDIUMTEXT',
'long_blob_test' => 'LONGBLOB',
'long_text_test' => 'LONGTEXT',
'long_blob_test' => 'LONGBLOB',
'long_text_test' => 'LONGTEXT',
}.each do |field, type|
it "should return a String for #{type}" do
expect(test_result[field]).to be_an_instance_of(String)
Expand Down
24 changes: 12 additions & 12 deletions spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require './spec/spec_helper.rb'
require './spec/spec_helper'

RSpec.describe Mysql2::Statement do
before(:example) do
Expand Down Expand Up @@ -277,7 +277,7 @@ def stmt_count
end

context "#each" do
# note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
# NOTE: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
# The drawback of this is that args of Result#each is ignored...

it "should yield rows as hash's" do
Expand Down Expand Up @@ -320,7 +320,7 @@ def stmt_count
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: false)
expect do
result.each {}
result.each {}
result.each {} # rubocop:disable Style/CombinableLoops
end.to raise_exception(Mysql2::Error)
end
end
Expand Down Expand Up @@ -573,17 +573,17 @@ def stmt_count
end

{
'char_test' => 'CHAR',
'varchar_test' => 'VARCHAR',
'varbinary_test' => 'VARBINARY',
'tiny_blob_test' => 'TINYBLOB',
'tiny_text_test' => 'TINYTEXT',
'blob_test' => 'BLOB',
'text_test' => 'TEXT',
'char_test' => 'CHAR',
'varchar_test' => 'VARCHAR',
'varbinary_test' => 'VARBINARY',
'tiny_blob_test' => 'TINYBLOB',
'tiny_text_test' => 'TINYTEXT',
'blob_test' => 'BLOB',
'text_test' => 'TEXT',
'medium_blob_test' => 'MEDIUMBLOB',
'medium_text_test' => 'MEDIUMTEXT',
'long_blob_test' => 'LONGBLOB',
'long_text_test' => 'LONGTEXT',
'long_blob_test' => 'LONGBLOB',
'long_text_test' => 'LONGTEXT',
}.each do |field, type|
it "should return a String for #{type}" do
expect(test_result[field]).to be_an_instance_of(String)
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def new_client(option_overrides = {})
@clients ||= []
@clients << client
return client unless block_given?

begin
yield client
ensure
Expand All @@ -42,7 +43,7 @@ def new_client(option_overrides = {})

def num_classes
# rubocop:disable Lint/UnifiedInteger
0.class == Integer ? [Integer] : [Fixnum, Bignum]
0.instance_of?(Integer) ? [Integer] : [Fixnum, Bignum]
# rubocop:enable Lint/UnifiedInteger
end

Expand Down
2 changes: 1 addition & 1 deletion support/mysql_enc_to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
collations.each do |collation|
mysql_col_idx = collation[2].to_i
rb_enc = mysql_to_rb.fetch(collation[1]) do |mysql_enc|
$stderr.puts "WARNING: Missing mapping for collation \"#{collation[0]}\" with encoding \"#{mysql_enc}\" and id #{mysql_col_idx}, assuming NULL"
warn "WARNING: Missing mapping for collation \"#{collation[0]}\" with encoding \"#{mysql_enc}\" and id #{mysql_col_idx}, assuming NULL"
"NULL"
end
encodings[mysql_col_idx - 1] = [mysql_col_idx, rb_enc]
Expand Down

0 comments on commit a9517ac

Please sign in to comment.