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

Ensure the Database#encoding method works when results are "as hash" #481

Merged
merged 1 commit into from
Jan 24, 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
4 changes: 3 additions & 1 deletion lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def initialize file, options = {}, zvfs = nil
#
# Fetch the encoding set on this database
def encoding
@encoding ||= Encoding.find(execute("PRAGMA encoding").first.first)
@encoding ||= prepare("PRAGMA encoding") { |stmt|
Encoding.find(stmt.first.first)
}
end

# Installs (or removes) a block that will be invoked for every access
Expand Down
8 changes: 8 additions & 0 deletions test/test_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def teardown
@db.close
end

def test_encoding_when_results_are_hash
db = SQLite3::Database.new(":memory:", results_as_hash: true)
assert_equal Encoding.find("UTF-8"), db.encoding

db = SQLite3::Database.new(":memory:")
assert_equal Encoding.find("UTF-8"), db.encoding
end

def test_select_encoding_on_utf_16
str = "foo"
utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
Expand Down