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

fix: Database#execute_batch properly handles non-ASCII strings #526

Merged
merged 1 commit into from
Apr 20, 2024

Conversation

flavorjones
Copy link
Member

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

@@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to reuse the encoding of the original string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of the original encoding, the query ends up encoded as UTF-8 in Statement#initialize and the remainder string being returned from sqlite_prepare is also UTF-8.

This call isn't re-encoding anything, it's just telling Ruby that the C string is in fact UTF-8 (the default is otherwise ASCII-8BIT which is what led to the problem you reported).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Alright, thanks for fixing this!

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
@flavorjones flavorjones force-pushed the 524-utf8-execute-batch branch from 3c67c7b to f4c326e Compare April 20, 2024 02:11
@flavorjones flavorjones enabled auto-merge April 20, 2024 02:18
@flavorjones flavorjones merged commit 3ba4a15 into main Apr 20, 2024
125 checks passed
@flavorjones flavorjones deleted the 524-utf8-execute-batch branch April 20, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQLite3::Database#execute_batch fails with non-ASCII characters
2 participants