This module is intended for inclusion solely by the Database class. It defines convenience methods for the various pragmas supported by SQLite3.
For a detailed description of these pragmas, see the SQLite3 documentation at sqlite.org/pragma.html.
The enumeration of valid synchronous modes.
The enumeration of valid temp store modes.
# File lib/sqlite3/pragmas.rb, line 104
104: def auto_vacuum
105: get_boolean_pragma "auto_vacuum"
106: end
# File lib/sqlite3/pragmas.rb, line 108
108: def auto_vacuum=( mode )
109: set_boolean_pragma "auto_vacuum", mode
110: end
# File lib/sqlite3/pragmas.rb, line 128
128: def cache_size
129: get_int_pragma "cache_size"
130: end
# File lib/sqlite3/pragmas.rb, line 132
132: def cache_size=( size )
133: set_int_pragma "cache_size", size
134: end
# File lib/sqlite3/pragmas.rb, line 200
200: def database_list( &block ) # :yields: row
201: get_query_pragma "database_list", &block
202: end
# File lib/sqlite3/pragmas.rb, line 136
136: def default_cache_size
137: get_int_pragma "default_cache_size"
138: end
# File lib/sqlite3/pragmas.rb, line 140
140: def default_cache_size=( size )
141: set_int_pragma "default_cache_size", size
142: end
# File lib/sqlite3/pragmas.rb, line 144
144: def default_synchronous
145: get_enum_pragma "default_synchronous"
146: end
# File lib/sqlite3/pragmas.rb, line 148
148: def default_synchronous=( mode )
149: set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
150: end
# File lib/sqlite3/pragmas.rb, line 160
160: def default_temp_store
161: get_enum_pragma "default_temp_store"
162: end
# File lib/sqlite3/pragmas.rb, line 164
164: def default_temp_store=( mode )
165: set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
166: end
# File lib/sqlite3/pragmas.rb, line 204
204: def foreign_key_list( table, &block ) # :yields: row
205: get_query_pragma "foreign_key_list", table, &block
206: end
# File lib/sqlite3/pragmas.rb, line 176
176: def full_column_names
177: get_boolean_pragma "full_column_names"
178: end
# File lib/sqlite3/pragmas.rb, line 180
180: def full_column_names=( mode )
181: set_boolean_pragma "full_column_names", mode
182: end
# File lib/sqlite3/pragmas.rb, line 208
208: def index_info( index, &block ) # :yields: row
209: get_query_pragma "index_info", index, &block
210: end
# File lib/sqlite3/pragmas.rb, line 212
212: def index_list( table, &block ) # :yields: row
213: get_query_pragma "index_list", table, &block
214: end
Does an integrity check on the database. If the check fails, a SQLite3::Exception will be raised. Otherwise it returns silently.
# File lib/sqlite3/pragmas.rb, line 98
98: def integrity_check
99: execute( "PRAGMA integrity_check" ) do |row|
100: raise Exception, row[0] if row[0] != "ok"
101: end
102: end
# File lib/sqlite3/pragmas.rb, line 184
184: def parser_trace
185: get_boolean_pragma "parser_trace"
186: end
# File lib/sqlite3/pragmas.rb, line 188
188: def parser_trace=( mode )
189: set_boolean_pragma "parser_trace", mode
190: end
# File lib/sqlite3/pragmas.rb, line 152
152: def synchronous
153: get_enum_pragma "synchronous"
154: end
# File lib/sqlite3/pragmas.rb, line 156
156: def synchronous=( mode )
157: set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
158: end
Returns information about table. Yields each row of table information if a block is provided.
# File lib/sqlite3/pragmas.rb, line 219
219: def table_info table
220: stmt = prepare "PRAGMA table_info(#{table})"
221: columns = stmt.columns
222:
223: needs_tweak_default =
224: version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
225:
226: result = [] unless block_given?
227: stmt.each do |row|
228: new_row = Hash[*columns.zip(row).flatten]
229:
230: # FIXME: This should be removed but is required for older versions
231: # of rails
232: if(Object.const_defined?(:ActiveRecord))
233: new_row['notnull'] = new_row['notnull'].to_s
234: end
235:
236: tweak_default(new_row) if needs_tweak_default
237:
238: if block_given?
239: yield new_row
240: else
241: result << new_row
242: end
243: end
244: stmt.close
245:
246: result
247: end
# File lib/sqlite3/pragmas.rb, line 168
168: def temp_store
169: get_enum_pragma "temp_store"
170: end
# File lib/sqlite3/pragmas.rb, line 172
172: def temp_store=( mode )
173: set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
174: end
Returns true or false depending on the value of the named pragma.
# File lib/sqlite3/pragmas.rb, line 13
13: def get_boolean_pragma( name )
14: get_first_value( "PRAGMA #{name}" ) != "0"
15: end
Return the value of the given pragma.
# File lib/sqlite3/pragmas.rb, line 58
58: def get_enum_pragma( name )
59: get_first_value( "PRAGMA #{name}" )
60: end
Returns the value of the given pragma as an integer.
# File lib/sqlite3/pragmas.rb, line 77
77: def get_int_pragma( name )
78: get_first_value( "PRAGMA #{name}" ).to_i
79: end
Requests the given pragma (and parameters), and if the block is given, each row of the result set will be yielded to it. Otherwise, the results are returned as an array.
# File lib/sqlite3/pragmas.rb, line 47
47: def get_query_pragma( name, *parms, &block ) # :yields: row
48: if parms.empty?
49: execute( "PRAGMA #{name}", &block )
50: else
51: args = "'" + parms.join("','") + "'"
52: execute( "PRAGMA #{name}( #{args} )", &block )
53: end
54: end
Sets the given pragma to the given boolean value. The value itself may be true or false, or any other commonly used string or integer that represents truth.
# File lib/sqlite3/pragmas.rb, line 21
21: def set_boolean_pragma( name, mode )
22: case mode
23: when String
24: case mode.downcase
25: when "on", "yes", "true", "y", "t"; mode = "'ON'"
26: when "off", "no", "false", "n", "f"; mode = "'OFF'"
27: else
28: raise Exception,
29: "unrecognized pragma parameter #{mode.inspect}"
30: end
31: when true, 1
32: mode = "ON"
33: when false, 0, nil
34: mode = "OFF"
35: else
36: raise Exception,
37: "unrecognized pragma parameter #{mode.inspect}"
38: end
39:
40: execute( "PRAGMA #{name}=#{mode}" )
41: end
Set the value of the given pragma to mode. The mode parameter must conform to one of the values in the given enum array. Each entry in the array is another array comprised of elements in the enumeration that have duplicate values. See #, #, #, and # for usage examples.
# File lib/sqlite3/pragmas.rb, line 68
68: def set_enum_pragma( name, mode, enums )
69: match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
70: raise Exception,
71: "unrecognized #{name} #{mode.inspect}" unless match
72: execute( "PRAGMA #{name}='#{match.first.upcase}'" )
73: end
Set the value of the given pragma to the integer value of the value parameter.
# File lib/sqlite3/pragmas.rb, line 84
84: def set_int_pragma( name, value )
85: execute( "PRAGMA #{name}=#{value.to_i}" )
86: end
Since SQLite 3.3.8, the table_info pragma has returned the default value of the row as a quoted SQL value. This method essentially unquotes those values.
# File lib/sqlite3/pragmas.rb, line 268
268: def tweak_default(hash)
269: case hash["dflt_value"]
270: when /^null$/
271: hash["dflt_value"] = nil
272: when /^'(.*)'$/
273: hash["dflt_value"] = $1.gsub(/''/, "'")
274: when /^"(.*)"$/
275: hash["dflt_value"] = $1.gsub(/""/, '"')
276: end
277: end
Compares two version strings
# File lib/sqlite3/pragmas.rb, line 252
252: def version_compare(v1, v2)
253: v1 = v1.split(".").map { |i| i.to_i }
254: v2 = v2.split(".").map { |i| i.to_i }
255: parts = [v1.length, v2.length].max
256: v1.push 0 while v1.length < parts
257: v2.push 0 while v2.length < parts
258: v1.zip(v2).each do |a,b|
259: return 1 if a < b
260: return 1 if a > b
261: end
262: return 0
263: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.