Skip to content

Commit 718b47b

Browse files
authored
Merge pull request #481 from sparklemotion/encoding-when-results-are-hash
Ensure the `Database#encoding` method works when results are "as hash"
2 parents 6a5d21f + 271addd commit 718b47b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/sqlite3/database.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ def initialize file, options = {}, zvfs = nil
138138
#
139139
# Fetch the encoding set on this database
140140
def encoding
141-
@encoding ||= Encoding.find(execute("PRAGMA encoding").first.first)
141+
@encoding ||= prepare("PRAGMA encoding") { |stmt|
142+
Encoding.find(stmt.first.first)
143+
}
142144
end
143145

144146
# Installs (or removes) a block that will be invoked for every access

test/test_encoding.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def teardown
1313
@db.close
1414
end
1515

16+
def test_encoding_when_results_are_hash
17+
db = SQLite3::Database.new(":memory:", results_as_hash: true)
18+
assert_equal Encoding.find("UTF-8"), db.encoding
19+
20+
db = SQLite3::Database.new(":memory:")
21+
assert_equal Encoding.find("UTF-8"), db.encoding
22+
end
23+
1624
def test_select_encoding_on_utf_16
1725
str = "foo"
1826
utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"

0 commit comments

Comments
 (0)