Module: Sequel::SQLite::DatabaseMethods

Included in:
JDBC::SQLite::DatabaseMethods, Database
Defined in:
lib/sequel_core/adapters/shared/sqlite.rb

Constant Summary collapse

AUTO_VACUUM =
{'0' => :none, '1' => :full, '2' => :incremental}.freeze
SYNCHRONOUS =
{'0' => :off, '1' => :normal, '2' => :full}.freeze
TABLES_FILTER =
"type = 'table' AND NOT name = 'sqlite_sequence'"
TEMP_STORE =
{'0' => :default, '1' => :file, '2' => :memory}.freeze

Instance Method Summary collapse

Instance Method Details

#alter_table(name, generator = nil, &block) ⇒ Object

Run all alter_table commands in a transaction. This is technically only needed for drop column.



11
12
13
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 11

def alter_table(name, generator=nil, &block)
  transaction{super}
end

#alter_table_sql(table, op) ⇒ Object

SQLite supports limited table modification. You can add a column or an index. Dropping columns is supported by copying the table into a temporary table, dropping the table, and creating a new table without the column inside of a transaction.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 19

def alter_table_sql(table, op)
  case op[:op]
  when :add_column, :add_index, :drop_index
    super
  when :drop_column
    columns_str = (schema_parse_table(table, {}).map{|c| c[0]} - Array(op[:name])).join(",")
    defined_columns_str = column_list_sql parse_pragma(table, {}).reject{ |c| c[:name] == op[:name].to_s}
    ["CREATE TEMPORARY TABLE #{table}_backup(#{defined_columns_str})",
     "INSERT INTO #{table}_backup SELECT #{columns_str} FROM #{table}",
     "DROP TABLE #{table}",
     "CREATE TABLE #{table}(#{defined_columns_str})",
     "INSERT INTO #{table} SELECT #{columns_str} FROM #{table}_backup",
     "DROP TABLE #{table}_backup"]
  else
    raise Error, "Unsupported ALTER TABLE operation"
  end
end

#auto_vacuumObject

A symbol signifying the value of the auto_vacuum PRAGMA.



38
39
40
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 38

def auto_vacuum
  AUTO_VACUUM[pragma_get(:auto_vacuum).to_s]
end

#auto_vacuum=(value) ⇒ Object

Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental).



44
45
46
47
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 44

def auto_vacuum=(value)
  value = AUTO_VACUUM.key(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.")
  pragma_set(:auto_vacuum, value)
end

#pragma_get(name) ⇒ Object

Get the value of the given PRAGMA.



50
51
52
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 50

def pragma_get(name)
  self["PRAGMA #{name}"].single_value
end

#pragma_set(name, value) ⇒ Object

Set the value of the given PRAGMA to value.



55
56
57
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 55

def pragma_set(name, value)
  execute_ddl("PRAGMA #{name} = #{value}")
end

#synchronousObject

A symbol signifying the value of the synchronous PRAGMA.



60
61
62
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 60

def synchronous
  SYNCHRONOUS[pragma_get(:synchronous).to_s]
end

#synchronous=(value) ⇒ Object

Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full).



65
66
67
68
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 65

def synchronous=(value)
  value = SYNCHRONOUS.key(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.")
  pragma_set(:synchronous, value)
end

#tablesObject

Array of symbols specifying the table names in the current database.



71
72
73
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 71

def tables
  self[:sqlite_master].filter(TABLES_FILTER).map {|r| r[:name].to_sym}
end

#temp_storeObject

A symbol signifying the value of the temp_store PRAGMA.



76
77
78
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 76

def temp_store
  TEMP_STORE[pragma_get(:temp_store).to_s]
end

#temp_store=(value) ⇒ Object

Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory).



81
82
83
84
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 81

def temp_store=(value)
  value = TEMP_STORE.key(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.")
  pragma_set(:temp_store, value)
end