Skip to content

Commit

Permalink
fix: Database#execute_batch properly handles non-ASCII strings
Browse files Browse the repository at this point in the history
Fix a regression in v2.0.0 that caused `Database#execute_batch` to
raise an encoding exception when passed some non-ASCII strings. As a
result of this fix, `Database#prepare` now ensures the "remainder"
string will always be encoded as UTF-8.

Closes #524
  • Loading branch information
flavorjones committed Apr 19, 2024
1 parent 59eee1d commit 3c67c7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# sqlite3-ruby Changelog

## next / unreleased

### Fixed

- Fixed a regression in v2.0.0 that caused `Database#execute_batch` to raise an encoding exception when passed some non-ascii strings. As a result of this fix, `Database#prepare` now ensures the "remainder" string will always be encoded as UTF-8. [#524] @flavorjones


## 2.0.0 / 2024-04-17

This is a major release which contains some breaking changes, primarily the removal of
Expand Down
2 changes: 1 addition & 1 deletion ext/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ prepare(VALUE self, VALUE db, VALUE sql)
CHECK(db_ctx->db, status);
timespecclear(&db_ctx->stmt_deadline);

return rb_str_new2(tail);
return rb_utf8_str_new_cstr(tail);
}

/* call-seq: stmt.close
Expand Down
18 changes: 18 additions & 0 deletions test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ def test_block_prepare_does_not_double_close
assert_equal :foo, r
end

def test_prepare_batch_split
db = SQLite3::Database.new(":memory:")
s1 = db.prepare("select 'asdf'; select 'qwer'; select 'côté'")

assert_equal " select 'qwer'; select 'côté'", s1.remainder
assert_equal Encoding::UTF_8, s1.remainder.encoding

s2 = db.prepare(s1.remainder)

assert_equal " select 'côté'", s2.remainder
assert_equal Encoding::UTF_8, s2.remainder.encoding

s3 = db.prepare(s2.remainder)

assert_equal "", s3.remainder
assert_equal Encoding::UTF_8, s3.remainder.encoding
end

def test_total_changes
db = SQLite3::Database.new(":memory:")
db.execute("create table foo ( a integer primary key, b text )")
Expand Down

0 comments on commit 3c67c7b

Please sign in to comment.