Module: Sequel::Litedb::DatabaseMethods
- Includes:
- UnmodifiedIdentifiers::DatabaseMethods
- Defined in:
- lib/sequel/adapters/shared/litedb.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
- AUTO_VACUUM =
[:none, :full, :incremental].freeze
- SYNCHRONOUS =
[:off, :normal, :full].freeze
- TEMP_STORE =
[:default, :file, :memory].freeze
- TRANSACTION_MODE =
{ :deferred => "BEGIN DEFERRED TRANSACTION", :immediate => "BEGIN IMMEDIATE TRANSACTION", :exclusive => "BEGIN EXCLUSIVE TRANSACTION", nil => "BEGIN" }.freeze
Instance Attribute Summary collapse
-
#current_timestamp_utc ⇒ Object
Whether to keep CURRENT_TIMESTAMP and similar expressions in UTC.
-
#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
-
#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.
- #freeze ⇒ Object
-
#indexes(table, opts = OPTS) ⇒ Object
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
-
#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.
-
#tables(opts = OPTS) ⇒ Object
Array of symbols specifying the table names in the current database.
-
#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
#current_timestamp_utc ⇒ Object
Whether to keep CURRENT_TIMESTAMP and similar expressions in UTC. By default, the expressions are converted to localtime.
43 44 45 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 43 def @current_timestamp_utc end |
#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’.
39 40 41 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 39 def integer_booleans @integer_booleans end |
#transaction_mode ⇒ Object
A symbol signifying the value of the default transaction mode
46 47 48 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 46 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.
151 152 153 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 151 def (value) @use_timestamp_timezones = value end |
Instance Method Details
#database_type ⇒ Object
SQLite uses the :sqlite database type.
58 59 60 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 58 def database_type :litedb 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 69 def foreign_key_list(table, opts = OPTS) m = output_identifier_meth h = {} _foreign_key_list_ds(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 |
#freeze ⇒ Object
83 84 85 86 87 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 83 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 90 def indexes(table, opts = OPTS) m = output_identifier_meth im = input_identifier_meth indexes = {} table = table.value if table.is_a?(Sequel::SQL::Identifier) .with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| if opts[:only_autocreated] # If specifically asked for only autocreated indexes, then return those an only those next unless r[:name].start_with?("sqlite_autoindex_") elsif r.has_key?(:origin) # If origin is set, then only exclude primary key indexes and partial indexes next if r[:origin] == "pk" next if r[:partial].to_i == 1 elsif r[:name].start_with?("sqlite_autoindex_") next end # When :origin key not present, assume any autoindex could be a primary key one and exclude it 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 |
#set_integer_booleans ⇒ Object
Set the integer_booleans option using the passed in :integer_boolean option.
63 64 65 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 63 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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 118 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.
129 130 131 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 129 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.
134 135 136 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 134 def supports_deferrable_foreign_key_constraints? sqlite_version >= 30619 end |
#supports_partial_indexes? ⇒ Boolean
SQLite 3.8.0+ supports partial indexes.
139 140 141 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 139 def supports_partial_indexes? sqlite_version >= 30800 end |
#supports_savepoints? ⇒ Boolean
SQLite 3.6.8+ supports savepoints.
144 145 146 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 144 def supports_savepoints? sqlite_version >= 30608 end |
#tables(opts = OPTS) ⇒ Object
Array of symbols specifying the table names in the current database.
Options:
- :server
-
Set the server to use.
163 164 165 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 163 def tables(opts = OPTS) tables_and_views(Sequel.~(name: "sqlite_sequence") & {type: "table"}, opts) 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.
155 156 157 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 155 def defined?(@use_timestamp_timezones) ? @use_timestamp_timezones : (@use_timestamp_timezones = false) end |
#values(v) ⇒ Object
Creates a dataset that uses the VALUES clause:
DB.values([[1, 2], [3, 4]])
# VALUES ((1, 2), (3, 4))
171 172 173 174 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 171 def values(v) raise Error, "Cannot provide an empty array for values" if v.empty? @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.
180 181 182 |
# File 'lib/sequel/adapters/shared/litedb.rb', line 180 def views(opts = OPTS) tables_and_views({type: "view"}, opts) end |