Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column names should be frozen #474

Merged
merged 1 commit into from
Jan 10, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This release drops support for Ruby 2.7. [#453] @flavorjones
### Changed

- Raise `StandardError` in a few places where `Exception` was previously raised.
- `Database#columns` returns a list of frozen strings now


### Removed
Expand Down
3 changes: 1 addition & 2 deletions ext/sqlite3/sqlite3_ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#define UTF8_P(_obj) (rb_enc_get_index(_obj) == rb_utf8_encindex())
#define UTF16_LE_P(_obj) (rb_enc_get_index(_obj) == rb_enc_find_index("UTF-16LE"))
#define UTF16_BE_P(_obj) (rb_enc_get_index(_obj) == rb_enc_find_index("UTF-16BE"))
#define SQLITE3_UTF8_STR_NEW2(_obj) \
(rb_enc_associate_index(rb_str_new2(_obj), rb_utf8_encindex()))
#define SQLITE3_UTF8_STR_NEW2(_obj) (rb_utf8_str_new_cstr(_obj))

#ifdef USING_SQLCIPHER_INC_SUBDIR
# include <sqlcipher/sqlite3.h>
Expand Down
9 changes: 7 additions & 2 deletions ext/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,13 @@ column_name(VALUE self, VALUE index)

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

if (name) { return SQLITE3_UTF8_STR_NEW2(name); }
return Qnil;
VALUE ret = Qnil;

if (name) {
ret = SQLITE3_UTF8_STR_NEW2(name);
rb_obj_freeze(ret);
}
return ret;
}

/* call-seq: stmt.column_decltype(index)
Expand Down
4 changes: 4 additions & 0 deletions test/test_integration_resultset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def teardown
@db.close
end

def test_column_names_should_be_frozen
assert @stmt.columns.all?(&:frozen?)
end

def test_reset_unused
assert_nothing_raised { @result.reset }
assert_empty @result.to_a
Expand Down