Skip to content

Commit 73acde8

Browse files
committed
Simplify PRAGMA related constants
This commit just simplifies the PRAGMA related constants
1 parent d8aed51 commit 73acde8

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

lib/sqlite3/pragmas.rb

+52-10
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,22 @@ 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+
else
67+
if mode.is_a?(String)
68+
enums.fetch(mode.downcase)
69+
else
70+
mode
71+
end
72+
end
73+
6474
unless match
6575
raise SQLite3::Exception, "unrecognized #{name} #{mode.inspect}"
6676
end
67-
execute("PRAGMA #{name}='#{match.first.upcase}'")
77+
78+
execute("PRAGMA #{name}='#{match}'")
6879
end
6980

7081
# Returns the value of the given pragma as an integer.
@@ -79,26 +90,57 @@ def set_int_pragma(name, value)
7990
end
8091

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

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

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

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

94123
# The list of valid locking modes.
95-
LOCKING_MODES = [["normal"], ["exclusive"]].map(&:freeze).freeze
124+
LOCKING_MODES = {
125+
"normal" => "normal",
126+
"exclusive" => "exclusive"
127+
}.freeze
96128

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

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

103145
def application_id
104146
get_int_pragma "application_id"

0 commit comments

Comments
 (0)