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
SCHEMA_TYPE_RE =
/\A(\w+)\((\d+)\)\z/
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_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.



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

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

#auto_vacuumObject

A symbol signifying the value of the auto_vacuum PRAGMA.



36
37
38
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 36

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).



42
43
44
45
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 42

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.



48
49
50
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 48

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

#pragma_set(name, value) ⇒ Object

Set the value of the given PRAGMA to value.



53
54
55
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 53

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

#synchronousObject

A symbol signifying the value of the synchronous PRAGMA.



58
59
60
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 58

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

#synchronous=(value) ⇒ Object

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



63
64
65
66
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 63

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.



69
70
71
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 69

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.



74
75
76
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 74

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).



79
80
81
82
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 79

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