Skip to content

Commit d15f6c3

Browse files
committed
Don't cache the database encoding
Database encoding can be changed, and we don't want to intercept all queries just to check if someone changed the encoding. This commit removes the encoding cache. If applications find that they are querying the database encoding frequently, they should cache the return value of this method themselves
1 parent 44ab2aa commit d15f6c3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/sqlite3/database.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def initialize file, options = {}, zvfs = nil
117117

118118
@tracefunc = nil
119119
@authorizer = nil
120-
@encoding = nil
121120
@busy_handler = nil
122121
@collations = {}
123122
@functions = {}
@@ -138,9 +137,7 @@ def initialize file, options = {}, zvfs = nil
138137
#
139138
# Fetch the encoding set on this database
140139
def encoding
141-
@encoding ||= prepare("PRAGMA encoding") { |stmt|
142-
Encoding.find(stmt.first.first)
143-
}
140+
prepare("PRAGMA encoding") { |stmt| Encoding.find(stmt.first.first) }
144141
end
145142

146143
# 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_change_encoding
17+
db = SQLite3::Database.new(":memory:")
18+
assert_equal Encoding.find("UTF-8"), db.encoding
19+
20+
db.execute "PRAGMA encoding='UTF-16le'"
21+
assert_equal Encoding.find("UTF-16le"), db.encoding
22+
end
23+
1624
def test_encoding_when_results_are_hash
1725
db = SQLite3::Database.new(":memory:", results_as_hash: true)
1826
assert_equal Encoding.find("UTF-8"), db.encoding

0 commit comments

Comments
 (0)