diff --git a/ext/sqlite3/statement.c b/ext/sqlite3/statement.c index ad1765f2..b8511bf8 100644 --- a/ext/sqlite3/statement.c +++ b/ext/sqlite3/statement.c @@ -382,8 +382,7 @@ column_name(VALUE self, VALUE index) VALUE ret = Qnil; if (name) { - ret = SQLITE3_UTF8_STR_NEW2(name); - rb_obj_freeze(ret); + ret = rb_enc_interned_str_cstr(name, rb_utf8_encoding()); } return ret; } diff --git a/test/test_statement.rb b/test/test_statement.rb index c4a22662..bfcef572 100644 --- a/test/test_statement.rb +++ b/test/test_statement.rb @@ -48,6 +48,22 @@ def test_raises_type_error end end + def test_column_names_are_deduped + @db.execute 'CREATE TABLE "things" ("float" float, "int" int, "text" blob, "string" string, "nil" string)' + stmt = @db.prepare 'SELECT float, int, text, string, nil FROM things' + assert_equal ["float", "int", "text", "string", "nil"], stmt.columns + columns = stmt.columns + stmt.close + + stmt = @db.prepare 'SELECT float, int, text, string, nil FROM things' + # Make sure this new statement returns the same interned strings + stmt.columns.each_with_index do |str, i| + assert_same columns[i], str + end + ensure + stmt&.close + end + def test_insert_duplicate_records @db.execute 'CREATE TABLE "things" ("name" varchar(20) CONSTRAINT "index_things_on_name" UNIQUE)' stmt = @db.prepare("INSERT INTO things(name) VALUES(?)")