Module: Sequel::SQLite::DatabaseMethods
- Includes:
- UnmodifiedIdentifiers::DatabaseMethods
- Included in:
- Amalgalite::Database, DataObjects::SQLite::DatabaseMethods, JDBC::SQLite::DatabaseMethods, Database, Sequel::Swift::SQLite::DatabaseMethods
- Defined in:
- lib/sequel/adapters/shared/sqlite.rb
Overview
No matter how you connect to SQLite, the following Database options can be used to set PRAGMAs on connections in a thread-safe manner: :auto_vacuum, :foreign_keys, :synchronous, and :temp_store.
Constant Summary collapse
- PRIMARY_KEY_INDEX_RE =
/\Asqlite_autoindex_/.freeze
- AUTO_VACUUM =
[:none, :full, :incremental].freeze
- SYNCHRONOUS =
[:off, :normal, :full].freeze
- TABLES_FILTER =
Sequel.~(:name=>'sqlite_sequence'.freeze) & {:type => 'table'.freeze}
- TEMP_STORE =
[:default, :file, :memory].freeze
- VIEWS_FILTER =
{:type => 'view'.freeze}.freeze
- TRANSACTION_MODE =
{ :deferred => "BEGIN DEFERRED TRANSACTION".freeze, :immediate => "BEGIN IMMEDIATE TRANSACTION".freeze, :exclusive => "BEGIN EXCLUSIVE TRANSACTION".freeze, nil => "BEGIN".freeze }.freeze
Instance Attribute Summary collapse
-
#integer_booleans ⇒ Object
Whether to use integers for booleans in the database.
-
#transaction_mode ⇒ Object
A symbol signifying the value of the default transaction mode.
-
#use_timestamp_timezones ⇒ Object
writeonly
Override the default setting for whether to use timezones in timestamps.
Instance Method Summary collapse
-
#auto_vacuum ⇒ Object
A symbol signifying the value of the auto_vacuum PRAGMA.
-
#auto_vacuum=(value) ⇒ Object
Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental).
-
#case_sensitive_like=(value) ⇒ Object
Set the case_sensitive_like PRAGMA using the given boolean value, if using SQLite 3.2.3+.
-
#database_type ⇒ Object
SQLite uses the :sqlite database type.
-
#foreign_key_list(table, opts = OPTS) ⇒ Object
Return the array of foreign key info hashes using the foreign_key_list PRAGMA, including information for the :on_update and :on_delete entries.
-
#foreign_keys ⇒ Object
Boolean signifying the value of the foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.
-
#foreign_keys=(value) ⇒ Object
Set the foreign_keys PRAGMA using the given boolean value, if using SQLite 3.6.19+.
- #freeze ⇒ Object
-
#indexes(table, opts = OPTS) ⇒ Object
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
-
#pragma_get(name) ⇒ Object
Get the value of the given PRAGMA.
-
#pragma_set(name, value) ⇒ Object
Set the value of the given PRAGMA to value.
-
#set_integer_booleans ⇒ Object
Set the integer_booleans option using the passed in :integer_boolean option.
-
#sqlite_version ⇒ Object
The version of the server as an integer, where 3.6.19 = 30619.
-
#supports_create_table_if_not_exists? ⇒ Boolean
SQLite supports CREATE TABLE IF NOT EXISTS syntax since 3.3.0.
-
#supports_deferrable_foreign_key_constraints? ⇒ Boolean
SQLite 3.6.19+ supports deferrable foreign key constraints.
-
#supports_partial_indexes? ⇒ Boolean
SQLite 3.8.0+ supports partial indexes.
-
#supports_savepoints? ⇒ Boolean
SQLite 3.6.8+ supports savepoints.
-
#synchronous ⇒ Object
A symbol signifying the value of the synchronous PRAGMA.
-
#synchronous=(value) ⇒ Object
Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full).
-
#tables(opts = OPTS) ⇒ Object
Array of symbols specifying the table names in the current database.
-
#temp_store ⇒ Object
A symbol signifying the value of the temp_store PRAGMA.
-
#temp_store=(value) ⇒ Object
Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory).
-
#use_timestamp_timezones? ⇒ Boolean
SQLite supports timezones in timestamps, since it just stores them as strings, but it breaks the usage of SQLite’s datetime functions.
-
#values(v) ⇒ Object
Creates a dataset that uses the VALUES clause:.
-
#views(opts = OPTS) ⇒ Object
Array of symbols specifying the view names in the current database.
Instance Attribute Details
#integer_booleans ⇒ Object
Whether to use integers for booleans in the database. SQLite recommends booleans be stored as integers, but historically Sequel has used ‘t’/‘f’.
38 39 40 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 38 def integer_booleans @integer_booleans end |
#transaction_mode ⇒ Object
A symbol signifying the value of the default transaction mode
61 62 63 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 61 def transaction_mode @transaction_mode end |
#use_timestamp_timezones=(value) ⇒ Object (writeonly)
Override the default setting for whether to use timezones in timestamps. It is set to false by default, as SQLite’s date/time methods do not support timezones in timestamps.
185 186 187 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 185 def (value) = value end |
Instance Method Details
#auto_vacuum ⇒ Object
A symbol signifying the value of the auto_vacuum PRAGMA.
41 42 43 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 41 def auto_vacuum AUTO_VACUUM[pragma_get(:auto_vacuum).to_i] end |
#auto_vacuum=(value) ⇒ Object
Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental). See pragma_set. Consider using the :auto_vacuum Database option instead.
48 49 50 51 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 48 def auto_vacuum=(value) value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.") pragma_set(:auto_vacuum, value) end |
#case_sensitive_like=(value) ⇒ Object
Set the case_sensitive_like PRAGMA using the given boolean value, if using SQLite 3.2.3+. If not using 3.2.3+, no error is raised. See pragma_set. Consider using the :case_sensitive_like Database option instead.
56 57 58 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 56 def case_sensitive_like=(value) pragma_set(:case_sensitive_like, !!value ? 'on' : 'off') if sqlite_version >= 30203 end |
#database_type ⇒ Object
SQLite uses the :sqlite database type.
73 74 75 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 73 def database_type :sqlite end |
#foreign_key_list(table, opts = OPTS) ⇒ Object
Return the array of foreign key info hashes using the foreign_key_list PRAGMA, including information for the :on_update and :on_delete entries.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 92 def foreign_key_list(table, opts=OPTS) m = output_identifier_meth h = {} .with_sql("PRAGMA foreign_key_list(?)", input_identifier_meth.call(table)).each do |row| if r = h[row[:id]] r[:columns] << m.call(row[:from]) r[:key] << m.call(row[:to]) if r[:key] else h[row[:id]] = {:columns=>[m.call(row[:from])], :table=>m.call(row[:table]), :key=>([m.call(row[:to])] if row[:to]), :on_update=>on_delete_sql_to_sym(row[:on_update]), :on_delete=>on_delete_sql_to_sym(row[:on_delete])} end end h.values end |
#foreign_keys ⇒ Object
Boolean signifying the value of the foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.
79 80 81 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 79 def foreign_keys pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619 end |
#foreign_keys=(value) ⇒ Object
Set the foreign_keys PRAGMA using the given boolean value, if using SQLite 3.6.19+. If not using 3.6.19+, no error is raised. See pragma_set. Consider using the :foreign_keys Database option instead.
86 87 88 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 86 def foreign_keys=(value) pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619 end |
#freeze ⇒ Object
106 107 108 109 110 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 106 def freeze sqlite_version super end |
#indexes(table, opts = OPTS) ⇒ Object
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 113 def indexes(table, opts=OPTS) m = output_identifier_meth im = input_identifier_meth indexes = {} .with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| # :only_autocreated internal option can be used to get only autocreated indexes next if (!!(r[:name] =~ /\Asqlite_autoindex_/) ^ !!opts[:only_autocreated]) indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1} end indexes.each do |k, v| v[:columns] = .with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)} end indexes end |
#pragma_get(name) ⇒ Object
Get the value of the given PRAGMA.
129 130 131 132 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 129 def pragma_get(name) Sequel::Deprecation.deprecate('Database#{pragma_get,auto_vacuum,case_sensitive_like,foreign_keys,synchronous,temp_store} on SQLite', "These methods may not be safe when using multiple connections, call fetch(#{"PRAGMA #{name}".inspect}).single_value if you really want to get the pragma value") self["PRAGMA #{name}"].single_value end |
#pragma_set(name, value) ⇒ Object
Set the value of the given PRAGMA to value.
This method is not thread safe, and will not work correctly if there are multiple connections in the Database’s connection pool. PRAGMA modifications should be done when the connection is created, using an option provided when creating the Database object.
140 141 142 143 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 140 def pragma_set(name, value) Sequel::Deprecation.deprecate('Database#{pragma_set,auto_vacuum=,case_sensitive_like=,foreign_keys=,synchronous=,temp_store=} on SQLite', "These methods are not thread safe or safe when using multiple connections, pass the appropriate option when connecting to set the pragma correctly for all connections") execute_ddl("PRAGMA #{name} = #{value}") end |
#set_integer_booleans ⇒ Object
Set the integer_booleans option using the passed in :integer_boolean option.
146 147 148 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 146 def set_integer_booleans @integer_booleans = @opts.has_key?(:integer_booleans) ? typecast_value_boolean(@opts[:integer_booleans]) : true end |
#sqlite_version ⇒ Object
The version of the server as an integer, where 3.6.19 = 30619. If the server version can’t be determined, 0 is used.
152 153 154 155 156 157 158 159 160 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 152 def sqlite_version return @sqlite_version if defined?(@sqlite_version) @sqlite_version = begin v = fetch('SELECT sqlite_version()').single_value [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])} rescue 0 end end |
#supports_create_table_if_not_exists? ⇒ Boolean
SQLite supports CREATE TABLE IF NOT EXISTS syntax since 3.3.0.
163 164 165 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 163 def supports_create_table_if_not_exists? sqlite_version >= 30300 end |
#supports_deferrable_foreign_key_constraints? ⇒ Boolean
SQLite 3.6.19+ supports deferrable foreign key constraints.
168 169 170 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 168 def supports_deferrable_foreign_key_constraints? sqlite_version >= 30619 end |
#supports_partial_indexes? ⇒ Boolean
SQLite 3.8.0+ supports partial indexes.
173 174 175 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 173 def supports_partial_indexes? sqlite_version >= 30800 end |
#supports_savepoints? ⇒ Boolean
SQLite 3.6.8+ supports savepoints.
178 179 180 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 178 def supports_savepoints? sqlite_version >= 30608 end |
#synchronous ⇒ Object
A symbol signifying the value of the synchronous PRAGMA.
194 195 196 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 194 def synchronous SYNCHRONOUS[pragma_get(:synchronous).to_i] end |
#synchronous=(value) ⇒ Object
Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full). See pragma_set. Consider using the :synchronous Database option instead.
200 201 202 203 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 200 def synchronous=(value) value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.") pragma_set(:synchronous, value) end |
#tables(opts = OPTS) ⇒ Object
Array of symbols specifying the table names in the current database.
Options:
- :server
-
Set the server to use.
209 210 211 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 209 def tables(opts=OPTS) tables_and_views(TABLES_FILTER, opts) end |
#temp_store ⇒ Object
A symbol signifying the value of the temp_store PRAGMA.
214 215 216 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 214 def temp_store TEMP_STORE[pragma_get(:temp_store).to_i] end |
#temp_store=(value) ⇒ Object
Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory). See pragma_set. Consider using the :temp_store Database option instead.
220 221 222 223 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 220 def temp_store=(value) value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.") pragma_set(:temp_store, value) end |
#use_timestamp_timezones? ⇒ Boolean
SQLite supports timezones in timestamps, since it just stores them as strings, but it breaks the usage of SQLite’s datetime functions.
189 190 191 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 189 def defined?() ? : ( = false) end |
#values(v) ⇒ Object
Creates a dataset that uses the VALUES clause:
DB.values([[1, 2], [3, 4]])
VALUES ((1, 2), (3, 4))
229 230 231 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 229 def values(v) @default_dataset.clone(:values=>v) end |
#views(opts = OPTS) ⇒ Object
Array of symbols specifying the view names in the current database.
Options:
- :server
-
Set the server to use.
237 238 239 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 237 def views(opts=OPTS) tables_and_views(VIEWS_FILTER, opts) end |