Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/mysql_api/mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ static VALUE make_field_obj(MYSQL_FIELD* f)
rb_iv_set(obj, "max_length", INT2NUM(f->max_length));
rb_iv_set(obj, "flags", INT2NUM(f->flags));
rb_iv_set(obj, "decimals", INT2NUM(f->decimals));
rb_iv_set(obj, "charsetnr", INT2NUM(f->charsetnr));
return obj;
}

Expand Down Expand Up @@ -1191,6 +1192,7 @@ static VALUE field_hash(VALUE obj)
rb_hash_aset(h, rb_str_new2("max_length"), rb_iv_get(obj, "max_length"));
rb_hash_aset(h, rb_str_new2("flags"), rb_iv_get(obj, "flags"));
rb_hash_aset(h, rb_str_new2("decimals"), rb_iv_get(obj, "decimals"));
rb_hash_aset(h, rb_str_new2("charsetnr"), rb_iv_get(obj, "charsetnr"));
return h;
}

Expand All @@ -1215,6 +1217,7 @@ DefineMysqlFieldMemberMethod(length)
DefineMysqlFieldMemberMethod(max_length)
DefineMysqlFieldMemberMethod(flags)
DefineMysqlFieldMemberMethod(decimals)
DefineMysqlFieldMemberMethod(charsetnr)

#ifdef IS_NUM
/* is_num? */
Expand Down Expand Up @@ -2147,6 +2150,7 @@ void Init_mysql_api(void)
rb_define_method(cMysqlField, "max_length", field_max_length, 0);
rb_define_method(cMysqlField, "flags", field_flags, 0);
rb_define_method(cMysqlField, "decimals", field_decimals, 0);
rb_define_method(cMysqlField, "charsetnr", field_charsetnr, 0);
rb_define_method(cMysqlField, "hash", field_hash, 0);
rb_define_method(cMysqlField, "inspect", field_inspect, 0);
#ifdef IS_NUM
Expand Down
46 changes: 34 additions & 12 deletions test/test_mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def test_fetch_field()
assert_equal(1, f.max_length)
assert_equal(Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG, f.flags)
assert_equal(0, f.decimals)
assert_equal(63, f.charsetnr) # 63 = binary
f = @res.fetch_field
assert_equal("str", f.name)
assert_equal("t", f.table)
Expand All @@ -346,6 +347,7 @@ def test_fetch_field()
assert_equal(4, f.max_length)
assert_equal(0, f.flags)
assert_equal(0, f.decimals)
assert_equal(33, f.charsetnr) # 33 = utf8_general_ci
f = @res.fetch_field
assert_equal(nil, f)
end
Expand Down Expand Up @@ -391,6 +393,7 @@ def test_field_hash()
"max_length" => 1,
"flags" => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
"decimals" => 0,
"charsetnr" => 63, # 63 = binary
}
assert_equal(h, f.hash)
f = @res.fetch_field
Expand All @@ -403,6 +406,7 @@ def test_field_hash()
"max_length" => 4,
"flags" => 0,
"decimals" => 0,
"charsetnr" => 33, # 33 = utf8_general_ci
}
assert_equal(h, f.hash)
end
Expand Down Expand Up @@ -752,22 +756,40 @@ def test_fetch_bit()
@m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
@s.prepare("select i from t")
@s.execute
assert_equal(["\x00"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\x7f"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
if ''.respond_to?( :force_encoding )
assert_equal(["\x00".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\x7f".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
else
assert_equal(["\x00"], @s.fetch)
assert_equal(["\xff"], @s.fetch, ["\xff".encoding, @s.fetch.map(&:encoding)])
assert_equal(["\x7f"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
assert_equal(["\xff"], @s.fetch)
end
@m.query("create temporary table t2 (i bit(64))")
@m.query("insert into t2 values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)")
@s.prepare("select i from t2")
@s.execute
assert_equal(["\x00\x00\x00\x00\x00\x00\x00\x00"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
assert_equal(["\x00\x00\x00\x01\x00\x00\x00\x00"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
if ''.respond_to?( :force_encoding )
assert_equal(["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding(Encoding::ASCII_8BIT)], @s.fetch)
else
assert_equal(["\x00\x00\x00\x00\x00\x00\x00\x00"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
assert_equal(["\x00\x00\x00\x01\x00\x00\x00\x00"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
end
end
end

Expand Down