Skip to content

Commit

Permalink
Merge pull request #474 from sparklemotion/frozen-column-names
Browse files Browse the repository at this point in the history
Column names should be frozen
  • Loading branch information
tenderlove committed Jan 10, 2024
2 parents db19743 + be07b51 commit 7d2cec6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
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 @@ -375,8 +375,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

0 comments on commit 7d2cec6

Please sign in to comment.