Skip to content

Commit

Permalink
Merge pull request #481 from sparklemotion/encoding-when-results-are-…
Browse files Browse the repository at this point in the history
…hash

Ensure the `Database#encoding` method works when results are "as hash"
  • Loading branch information
tenderlove authored Jan 24, 2024
2 parents 6a5d21f + 271addd commit 718b47b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 718b47b

Please sign in to comment.