Class: Sequel::SQLite::Database

Inherits:
Database show all
Defined in:
lib/sequel_core/adapters/sqlite.rb

Constant Summary collapse

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

Constants inherited from Database

Database::ADAPTERS, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK

Constants included from Sequel::Schema::SQL

Sequel::Schema::SQL::AUTOINCREMENT, Sequel::Schema::SQL::CASCADE, Sequel::Schema::SQL::COMMA_SEPARATOR, Sequel::Schema::SQL::NOT_NULL, Sequel::Schema::SQL::NO_ACTION, Sequel::Schema::SQL::NULL, Sequel::Schema::SQL::PRIMARY_KEY, Sequel::Schema::SQL::RESTRICT, Sequel::Schema::SQL::SET_DEFAULT, Sequel::Schema::SQL::SET_NULL, Sequel::Schema::SQL::TYPES, Sequel::Schema::SQL::UNDERSCORE, Sequel::Schema::SQL::UNIQUE, Sequel::Schema::SQL::UNSIGNED

Instance Attribute Summary

Attributes inherited from Database

#loggers, #opts, #pool, #quote_identifiers

Instance Method Summary collapse

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #drop_column, #drop_index, #drop_table, #drop_view, #fetch, #from, #get, #initialize, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, #uri, uri_to_options

Methods included from Sequel::Schema::SQL

#alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_table_sql, #filter_expr, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #quote_identifier, #rename_table_sql, #schema, #schema_utility_dataset, #type_literal

Constructor Details

This class inherits a constructor from Sequel::Database

Instance Method Details

#alter_table_sql(table, op) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/sequel_core/adapters/sqlite.rb', line 115

def alter_table_sql(table, op)
  case op[:op]
  when :add_column
    "ALTER TABLE #{table} ADD #{column_definition_sql(op)}"
  else
    raise Error, "Unsupported ALTER TABLE operation"
  end
end

#auto_vacuumObject



84
85
86
# File 'lib/sequel_core/adapters/sqlite.rb', line 84

def auto_vacuum
  AUTO_VACUUM[pragma_get(:auto_vacuum)]
end

#auto_vacuum=(value) ⇒ Object



88
89
90
91
# File 'lib/sequel_core/adapters/sqlite.rb', line 88

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

#connectObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sequel_core/adapters/sqlite.rb', line 12

def connect
  @opts[:database] = ':memory:' if @opts[:database].blank?
  db = ::SQLite3::Database.new(@opts[:database])
  db.busy_timeout(@opts.fetch(:timeout, 5000))
  db.type_translation = true
  # fix for timestamp translation
  db.translator.add_translator("timestamp") do |t, v|
    v =~ /^\d+$/ ? Time.at(v.to_i) : Time.parse(v) 
  end 
  db
end

#dataset(opts = nil) ⇒ Object



28
29
30
# File 'lib/sequel_core/adapters/sqlite.rb', line 28

def dataset(opts = nil)
  SQLite::Dataset.new(self, opts)
end

#disconnectObject



24
25
26
# File 'lib/sequel_core/adapters/sqlite.rb', line 24

def disconnect
  @pool.disconnect {|c| c.close}
end

#execute(sql) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/sequel_core/adapters/sqlite.rb', line 38

def execute(sql)
  begin
    log_info(sql)
    @pool.hold {|conn| conn.execute_batch(sql); conn.changes}
  rescue SQLite3::Exception => e
    raise Error::InvalidStatement, "#{sql}\r\n#{e.message}"
  end
end

#execute_insert(sql) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/sequel_core/adapters/sqlite.rb', line 47

def execute_insert(sql)
  begin
    log_info(sql)
    @pool.hold {|conn| conn.execute(sql); conn.last_insert_row_id}
  rescue SQLite3::Exception => e
    raise Error::InvalidStatement, "#{sql}\r\n#{e.message}"
  end
end

#execute_select(sql, &block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/sequel_core/adapters/sqlite.rb', line 65

def execute_select(sql, &block)
  begin
    log_info(sql)
    @pool.hold {|conn| conn.query(sql, &block)}
  rescue SQLite3::Exception => e
    raise Error::InvalidStatement, "#{sql}\r\n#{e.message}"
  end
end

#pragma_get(name) ⇒ Object



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

def pragma_get(name)
  single_value("PRAGMA #{name}")
end

#pragma_set(name, value) ⇒ Object



78
79
80
# File 'lib/sequel_core/adapters/sqlite.rb', line 78

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

#serial_primary_key_optionsObject



8
9
10
# File 'lib/sequel_core/adapters/sqlite.rb', line 8

def serial_primary_key_options
  {:primary_key => true, :type => :integer, :auto_increment => true}
end

#single_value(sql) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/sequel_core/adapters/sqlite.rb', line 56

def single_value(sql)
  begin
    log_info(sql)
    @pool.hold {|conn| conn.get_first_value(sql)}
  rescue SQLite3::Exception => e
    raise Error::InvalidStatement, "#{sql}\r\n#{e.message}"
  end
end

#synchronousObject



95
96
97
# File 'lib/sequel_core/adapters/sqlite.rb', line 95

def synchronous
  SYNCHRONOUS[pragma_get(:synchronous)]
end

#synchronous=(value) ⇒ Object



99
100
101
102
# File 'lib/sequel_core/adapters/sqlite.rb', line 99

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

#tablesObject



34
35
36
# File 'lib/sequel_core/adapters/sqlite.rb', line 34

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

#temp_storeObject



106
107
108
# File 'lib/sequel_core/adapters/sqlite.rb', line 106

def temp_store
  TEMP_STORE[pragma_get(:temp_store)]
end

#temp_store=(value) ⇒ Object



110
111
112
113
# File 'lib/sequel_core/adapters/sqlite.rb', line 110

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

#transaction(&block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sequel_core/adapters/sqlite.rb', line 124

def transaction(&block)
  @pool.hold do |conn|
    if conn.transaction_active?
      return yield(conn)
    end
    begin
      result = nil
      conn.transaction {result = yield(conn)}
      result
    rescue ::Exception => e
      raise (SQLite3::Exception === e ? Error.new(e.message) : e) unless Error::Rollback === e
    end
  end
end