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
3 changes: 2 additions & 1 deletion ext/sqlite3/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def configure_extension

abort_could_not_find(libname) unless find_library(libname, "sqlite3_libversion_number", "sqlite3.h")

# Truffle Ruby doesn't support this yet:
# Ruby 3.0+, but not on every implementation:
# https://github.com/oracle/truffleruby/issues/3408
have_func("rb_enc_interned_str_cstr")
have_func("rb_enc_interned_str")

# Functions defined in 1.9 but not 1.8
have_func("rb_proc_arity")
Expand Down
47 changes: 46 additions & 1 deletion ext/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,33 @@ interned_utf8_cstr(const char *str)
}
#endif

/* Like interned_utf8_cstr, but ASCII-downcases as it copies. */
static VALUE
interned_downcased_utf8_cstr(const char *str)
{
#if HAVE_RB_ENC_INTERNED_STR
VALUE tmp, ret;
char *buf;
size_t i, len;

len = strlen(str);
buf = ALLOCV_N(char, tmp, len);

for (i = 0; i < len; i++) {
unsigned char c = (unsigned char)str[i];
buf[i] = (char)((c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c);
}

ret = rb_enc_interned_str(buf, (long)len, rb_utf8_encoding());
ALLOCV_END(tmp);
return ret;
#else
VALUE rb_str = rb_funcall(rb_utf8_str_new_cstr(str), rb_intern("downcase"), 1,
ID2SYM(rb_intern("ascii")));
return rb_funcall(rb_str, rb_intern("-@"), 0);
#endif
}

/* call-seq: stmt.column_name(index)
*
* Get the column name at +index+. 0 based.
Expand Down Expand Up @@ -455,7 +482,24 @@ column_decltype(VALUE self, VALUE index)

name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));

if (name) { return rb_str_new2(name); }
if (name) { return rb_utf8_str_new_cstr(name); }
return Qnil;
}

static VALUE
column_decltype_downcased(VALUE self, VALUE index)
{
sqlite3StmtRubyPtr ctx;
const char *name;

TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);

REQUIRE_LIVE_DB(ctx);
REQUIRE_OPEN_STMT(ctx);

name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));

if (name) { return interned_downcased_utf8_cstr(name); }
return Qnil;
}

Expand Down Expand Up @@ -748,6 +792,7 @@ init_sqlite3_statement(void)
rb_define_method(cSqlite3Statement, "memused", memused, 0);
#endif

rb_define_private_method(cSqlite3Statement, "column_decltype_downcased", column_decltype_downcased, 1);
rb_define_private_method(cSqlite3Statement, "prepare", prepare, 2);
rb_define_private_method(cSqlite3Statement, "stats_as_hash", stats_as_hash, 0);
rb_define_private_method(cSqlite3Statement, "stat_for", stat_for, 1);
Expand Down
7 changes: 3 additions & 4 deletions lib/sqlite3/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ def stat key = nil
def get_metadata
@columns = Array.new(column_count) do |column|
column_name column
end
end.freeze
@types = Array.new(column_count) do |column|
val = column_decltype(column)
val&.downcase
end
column_decltype_downcased(column)
end.freeze
end
end
end
17 changes: 17 additions & 0 deletions test/test_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ def test_bind_blob
end
end

def test_decltype_is_utf8
@db.execute %(create table foo(a integer, b "VARCHÄR(255)"))
@db.prepare("select a, b from foo") do |stmt|
assert_equal ["INTEGER", "VARCHÄR(255)"], [stmt.column_decltype(0), stmt.column_decltype(1)]
assert_equal [Encoding::UTF_8, Encoding::UTF_8],
[stmt.column_decltype(0).encoding, stmt.column_decltype(1).encoding]
end
end

def test_types_are_utf8_and_downcased_as_ascii
@db.execute %(create table foo(a integer, b "VARCHÄR(255)"))
@db.prepare("select a, b from foo") do |stmt|
assert_equal ["integer", "varchÄr(255)"], stmt.types
assert_equal [Encoding::UTF_8, Encoding::UTF_8], stmt.types.map(&:encoding)
end
end

def test_bind_64
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, 2**31)
Expand Down
Loading