Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing bug where tinyint(4) would be incorrectly cast to boolean when…

… convert_tinyint_to_bool is set.

Only tinyint(1) should be cast to booleans in this case.
  • Loading branch information...
commit 13bade74d950aa927699f186836df7a78ca52a49 1 parent 582ef63
@knaveofdiamonds knaveofdiamonds authored committed
Showing with 14 additions and 5 deletions.
  1. +7 −1 lib/sequel/adapters/mysql.rb
  2. +7 −4 spec/adapters/mysql_spec.rb
View
8 lib/sequel/adapters/mysql.rb
@@ -323,7 +323,13 @@ def delete
def fetch_rows(sql, &block)
execute(sql) do |r|
i = -1
- cols = r.fetch_fields.map{|f| [output_identifier(f.name), MYSQL_TYPES[f.type], i+=1]}
+ cols = r.fetch_fields.map do |f|
+ # Pretend tinyint is another integer type if its length is not 1, to
+ # avoid casting to boolean if Sequel::MySQL.convert_tinyint_to_bool
+ # is set.
+ type_proc = f.type == 1 && f.length != 1 ? MYSQL_TYPES[2] : MYSQL_TYPES[f.type]
+ [output_identifier(f.name), type_proc, i+=1]
+ end
@columns = cols.map{|c| c.first}
if opts[:split_multiple_result_sets]
s = []
View
11 spec/adapters/mysql_spec.rb
@@ -107,14 +107,17 @@ def logger.method_missing(m, msg)
@db.schema(:booltest, :reload=>true).should == [[:b, {:type=>:integer, :allow_null=>true, :primary_key=>false, :default=>nil, :ruby_default=>nil, :db_type=>"tinyint(1)"}, ], [:i, {:type=>:integer, :allow_null=>true, :primary_key=>false, :default=>nil, :ruby_default=>nil, :db_type=>"tinyint(4)"}, ]]
end
- specify "should return tinyints as bools when set" do
+ specify "should return tinyint(1)s as bools and tinyint(4)s as integers when set" do
+ Sequel::MySQL.convert_tinyint_to_bool = true
@ds.delete
@ds << {:b=>true, :i=>10}
- @ds.all.should == [{:b=>true, :i=>true}]
+ @ds.all.should == [{:b=>true, :i=>10}]
@ds.delete
@ds << {:b=>false, :i=>0}
- @ds.all.should == [{:b=>false, :i=>false}]
-
+ @ds.all.should == [{:b=>false, :i=>0}]
+ end
+
+ specify "should return all tinyints as integers when unset" do
Sequel::MySQL.convert_tinyint_to_bool = false
@ds.delete
@ds << {:b=>true, :i=>10}
Please sign in to comment.
Something went wrong with that request. Please try again.