Skip to content

Commit 3ba4a15

Browse files
authored
Merge pull request #526 from sparklemotion/524-utf8-execute-batch
fix: Database#execute_batch properly handles non-ASCII strings
2 parents 19baf76 + f4c326e commit 3ba4a15

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Raise an exception if `Database#execute`, `#execute_batch`, or `#query` are passed multiple bind parameters that are not in an Array. In v2.0.0 these methods would silently swallow additional arguments. [#527] @flavorjones
8+
- 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
89

910

1011
## 2.0.0 / 2024-04-17

ext/sqlite3/statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ prepare(VALUE self, VALUE db, VALUE sql)
7272
CHECK(db_ctx->db, status);
7373
timespecclear(&db_ctx->stmt_deadline);
7474

75-
return rb_str_new2(tail);
75+
return rb_utf8_str_new_cstr(tail);
7676
}
7777

7878
/* call-seq: stmt.close

test/test_database.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ def test_block_prepare_does_not_double_close
326326
assert_equal :foo, r
327327
end
328328

329+
def test_prepare_batch_split
330+
db = SQLite3::Database.new(":memory:")
331+
s1 = db.prepare("select 'asdf'; select 'qwer'; select 'côté'")
332+
333+
assert_equal " select 'qwer'; select 'côté'", s1.remainder
334+
assert_equal Encoding::UTF_8, s1.remainder.encoding
335+
336+
s2 = db.prepare(s1.remainder)
337+
338+
assert_equal " select 'côté'", s2.remainder
339+
assert_equal Encoding::UTF_8, s2.remainder.encoding
340+
341+
s3 = db.prepare(s2.remainder)
342+
343+
assert_equal "", s3.remainder
344+
assert_equal Encoding::UTF_8, s3.remainder.encoding
345+
end
346+
329347
def test_total_changes
330348
db = SQLite3::Database.new(":memory:")
331349
db.execute("create table foo ( a integer primary key, b text )")

0 commit comments

Comments
 (0)