Skip to content

Commit

Permalink
Ensure the Database#encoding method works when results are "as hash"
Browse files Browse the repository at this point in the history
This is a regression test for Rails. Rails instantiates SQLite3 with
`results_as_hash` set to `true` by default. I broke the encoding method
when that value is set to true, so this patch fixes it.
  • Loading branch information
tenderlove committed Jan 24, 2024
1 parent 6a5d21f commit e0bcae8
Show file tree
Hide file tree
Showing 2 changed files with 8 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
5 changes: 5 additions & 0 deletions test/test_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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
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 e0bcae8

Please sign in to comment.