Skip to content

Commit fcaf15e

Browse files
author
Paul Bowsher
committed
Make cast_booleans also cast BIT(1)
Using `BIT(1)` to represent booleans is a common pattern within MySQL. This makes the existing `cast_booleans` switch cast `BIT(1)` as TrueClass or FalseClass
1 parent 72d0d22 commit fcaf15e

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

ext/mysql2/result.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
223223
val = Qnil;
224224
break;
225225
case MYSQL_TYPE_BIT: /* BIT field (MySQL 5.0.3 and up) */
226-
val = rb_str_new(row[i], fieldLengths[i]);
226+
if (castBool && fields[i].length == 1) {
227+
val = *row[i] == 1 ? Qtrue : Qfalse;
228+
}else{
229+
val = rb_str_new(row[i], fieldLengths[i]);
230+
}
227231
break;
228232
case MYSQL_TYPE_TINY: /* TINYINT field */
229233
if (castBool && fields[i].length == 1) {

spec/mysql2/result_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,16 @@
160160
@test_result['null_test'].should eql(nil)
161161
end
162162

163-
it "should return Fixnum for a BIT value" do
163+
it "should return String for a BIT(64) value" do
164164
@test_result['bit_test'].class.should eql(String)
165165
@test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
166166
end
167167

168+
it "should return String for a BIT(1) value" do
169+
@test_result['single_bit_test'].class.should eql(String)
170+
@test_result['single_bit_test'].should eql("\001")
171+
end
172+
168173
it "should return Fixnum for a TINYINT value" do
169174
[Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
170175
@test_result['tiny_int_test'].should eql(1)
@@ -188,6 +193,20 @@
188193
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
189194
end
190195

196+
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
197+
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
198+
id1 = @client.last_id
199+
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
200+
id2 = @client.last_id
201+
202+
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
203+
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
204+
result1.first['single_bit_test'].should be_true
205+
result2.first['single_bit_test'].should be_false
206+
207+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
208+
end
209+
191210
it "should return Fixnum for a SMALLINT value" do
192211
[Fixnum, Bignum].should include(@test_result['small_int_test'].class)
193212
@test_result['small_int_test'].should eql(10)

spec/spec_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
id MEDIUMINT NOT NULL AUTO_INCREMENT,
1515
null_test VARCHAR(10),
1616
bit_test BIT(64),
17+
single_bit_test BIT(1),
1718
tiny_int_test TINYINT,
1819
bool_cast_test TINYINT(1),
1920
small_int_test SMALLINT,
@@ -50,15 +51,15 @@
5051
client.query "DELETE FROM mysql2_test;"
5152
client.query %[
5253
INSERT INTO mysql2_test (
53-
null_test, bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
54+
null_test, bit_test, single_bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
5455
float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
5556
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
5657
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
5758
long_blob_test, long_text_test, enum_test, set_test
5859
)
5960
6061
VALUES (
61-
NULL, b'101', 1, 1, 10, 10, 10, 10,
62+
NULL, b'101', b'1', 1, 1, 10, 10, 10, 10,
6263
10.3, 0, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
6364
2009, "test", "test", "test", "test", "test",
6465
"test", "test", "test", "test", "test",

0 commit comments

Comments
 (0)