Skip to content

Commit 9ffb27d

Browse files
committed
Simplify PRAGMA related constants
This commit just simplifies the PRAGMA related constants
1 parent d8aed51 commit 9ffb27d

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

lib/sqlite3/pragmas.rb

+50-10
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,20 @@ def get_enum_pragma(name)
6060
# have duplicate values. See #synchronous, #default_synchronous,
6161
# #temp_store, and #default_temp_store for usage examples.
6262
def set_enum_pragma(name, mode, enums)
63-
match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
63+
match = if enums.is_a?(Array)
64+
# maybe deprecate this?
65+
enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
66+
elsif mode.is_a?(String)
67+
enums.fetch(mode.downcase)
68+
else
69+
mode
70+
end
71+
6472
unless match
6573
raise SQLite3::Exception, "unrecognized #{name} #{mode.inspect}"
6674
end
67-
execute("PRAGMA #{name}='#{match.first.upcase}'")
75+
76+
execute("PRAGMA #{name}='#{match}'")
6877
end
6978

7079
# Returns the value of the given pragma as an integer.
@@ -79,26 +88,57 @@ def set_int_pragma(name, value)
7988
end
8089

8190
# The enumeration of valid synchronous modes.
82-
SYNCHRONOUS_MODES = [["full", 2], ["normal", 1], ["off", 0]].map(&:freeze).freeze
91+
SYNCHRONOUS_MODES = {
92+
"full" => 2,
93+
"normal" => 1,
94+
"off" => 0
95+
}.freeze
8396

8497
# The enumeration of valid temp store modes.
85-
TEMP_STORE_MODES = [["default", 0], ["file", 1], ["memory", 2]].map(&:freeze).freeze
98+
TEMP_STORE_MODES = {
99+
"default" => 0,
100+
"file" => 1,
101+
"memory" => 2
102+
}.freeze
86103

87104
# The enumeration of valid auto vacuum modes.
88-
AUTO_VACUUM_MODES = [["none", 0], ["full", 1], ["incremental", 2]].map(&:freeze).freeze
105+
AUTO_VACUUM_MODES = {
106+
"none" => 0,
107+
"full" => 1,
108+
"incremental" => 2
109+
}.freeze
89110

90111
# The list of valid journaling modes.
91-
JOURNAL_MODES = [["delete"], ["truncate"], ["persist"], ["memory"],
92-
["wal"], ["off"]].map(&:freeze).freeze
112+
JOURNAL_MODES = {
113+
"delete" => "delete",
114+
"truncate" => "truncate",
115+
"persist" => "persist",
116+
"memory" => "memory",
117+
"wal" => "wal",
118+
"off" => "off"
119+
}.freeze
93120

94121
# The list of valid locking modes.
95-
LOCKING_MODES = [["normal"], ["exclusive"]].map(&:freeze).freeze
122+
LOCKING_MODES = {
123+
"normal" => "normal",
124+
"exclusive" => "exclusive"
125+
}.freeze
96126

97127
# The list of valid encodings.
98-
ENCODINGS = [["utf-8"], ["utf-16"], ["utf-16le"], ["utf-16be"]].map(&:freeze).freeze
128+
ENCODINGS = {
129+
"utf-8" => "utf-8",
130+
"utf-16" => "utf-16",
131+
"utf-16le" => "utf-16le",
132+
"utf-16be" => "utf-16be"
133+
}.freeze
99134

100135
# The list of valid WAL checkpoints.
101-
WAL_CHECKPOINTS = [["passive"], ["full"], ["restart"], ["truncate"]].map(&:freeze).freeze
136+
WAL_CHECKPOINTS = {
137+
"passive" => "passive",
138+
"full" => "full",
139+
"restart" => "restart",
140+
"truncate" => "truncate"
141+
}.freeze
102142

103143
def application_id
104144
get_int_pragma "application_id"

0 commit comments

Comments
 (0)