Class: Sequel::SQLite::Database

Inherits:
Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel/adapters/sqlite.rb

Overview

Database class for PostgreSQL databases used with Sequel and the ruby-sqlite3 driver.

Constant Summary collapse

UNIX_EPOCH_TIME_FORMAT =
/\A\d+\z/.freeze

Constants included from DatabaseMethods

Sequel::SQLite::DatabaseMethods::AUTO_VACUUM, Sequel::SQLite::DatabaseMethods::SYNCHRONOUS, Sequel::SQLite::DatabaseMethods::TABLES_FILTER, Sequel::SQLite::DatabaseMethods::TEMP_STORE, Sequel::SQLite::DatabaseMethods::TYPES

Constants inherited from Database

Database::ADAPTERS, Database::AUTOINCREMENT, Database::CASCADE, Database::COMMA_SEPARATOR, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK, Database::TYPES, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED

Instance Attribute Summary

Attributes inherited from Database

#default_schema, #loggers, #opts, #pool, #prepared_statements

Instance Method Summary collapse

Methods included from DatabaseMethods

#alter_table, #alter_table_sql, #auto_vacuum, #auto_vacuum=, #pragma_get, #pragma_set, #synchronous, #synchronous=, #tables, #temp_store, #temp_store=

Methods inherited from Database

#<<, #>>, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #call, #column_definition_sql, #column_list_sql, #column_references_sql, connect, #constraint_definition_sql, #create_or_replace_view, #create_table, #create_table!, #create_table_sql_list, #create_view, #default_index_name, #disconnect, #drop_column, #drop_index, #drop_index_sql, #drop_table, #drop_table_sql, #drop_view, #execute_ddl, #fetch, #filter_expr, #from, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, #identifier_output_method, identifier_output_method, #identifier_output_method=, identifier_output_method=, #index_definition_sql, #index_list_sql_list, #initialize, #inspect, #literal, #log_info, #logger, #logger=, #multi_threaded?, #on_delete_clause, #query, #quote_identifier, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #quote_schema_table, #rename_column, #rename_table, #rename_table_sql, #schema, #schema_utility_dataset, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, upcase_identifiers=, #upcase_identifiers=, #upcase_identifiers?, #uri, #url

Methods included from Metaprogramming

#meta_def

Constructor Details

This class inherits a constructor from Sequel::Database

Instance Method Details

#connect(server) ⇒ Object

Connect to the database. Since SQLite is a file based database, the only options available are :database (to specify the database name), and :timeout, to specify how long to wait for the database to be available if it is locked, given in milliseconds (default is 5000).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sequel/adapters/sqlite.rb', line 28

def connect(server)
  opts = server_opts(server)
  opts[:database] = ':memory:' if blank_object?(opts[:database])
  db = ::SQLite3::Database.new(opts[:database])
  db.busy_timeout(opts.fetch(:timeout, 5000))
  db.type_translation = true
  
  # Handle datetime's with Sequel.datetime_class
  prok = proc do |t,v|
    v = Time.at(v.to_i).iso8601 if UNIX_EPOCH_TIME_FORMAT.match(v)
    Sequel.string_to_datetime(v)
  end
  db.translator.add_translator("timestamp", &prok)
  db.translator.add_translator("datetime", &prok)
  
  # Handle numeric values with BigDecimal
  prok = proc{|t,v| BigDecimal.new(v) rescue v}
  db.translator.add_translator("numeric", &prok)
  db.translator.add_translator("decimal", &prok)
  db.translator.add_translator("money", &prok)
  
  # Handle floating point values with Float
  prok = proc{|t,v| Float(v) rescue v}
  db.translator.add_translator("float", &prok)
  db.translator.add_translator("real", &prok)
  db.translator.add_translator("double precision", &prok)
  
  # Handle blob values with Sequel::SQL::Blob
  db.translator.add_translator("blob"){|t,v| ::Sequel::SQL::Blob.new(v)}
  
  db
end

#dataset(opts = nil) ⇒ Object

Return instance of Sequel::SQLite::Dataset with the given options.



62
63
64
# File 'lib/sequel/adapters/sqlite.rb', line 62

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

#execute(sql, opts = {}, &block) ⇒ Object

Run the given SQL with the given arguments and yield each row.



77
78
79
# File 'lib/sequel/adapters/sqlite.rb', line 77

def execute(sql, opts={}, &block)
  _execute(sql, opts){|conn| conn.query(sql, opts[:arguments], &block)}
end

#execute_dui(sql, opts = {}) ⇒ Object

Run the given SQL with the given arguments and return the number of changed rows.



67
68
69
# File 'lib/sequel/adapters/sqlite.rb', line 67

def execute_dui(sql, opts={})
  _execute(sql, opts){|conn| conn.execute_batch(sql, opts[:arguments]); conn.changes}
end

#execute_insert(sql, opts = {}) ⇒ Object

Run the given SQL with the given arguments and return the last inserted row id.



72
73
74
# File 'lib/sequel/adapters/sqlite.rb', line 72

def execute_insert(sql, opts={})
  _execute(sql, opts){|conn| conn.execute(sql, opts[:arguments]); conn.last_insert_row_id}
end

#single_value(sql, opts = {}) ⇒ Object

Run the given SQL with the given arguments and return the first value of the first row.



82
83
84
# File 'lib/sequel/adapters/sqlite.rb', line 82

def single_value(sql, opts={})
  _execute(sql, opts){|conn| conn.get_first_value(sql, opts[:arguments])}
end

#transaction(opts = {}) ⇒ Object

Use the native driver transaction method if there isn’t already a transaction in progress on the connection, always yielding a connection inside a transaction transaction.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sequel/adapters/sqlite.rb', line 89

def transaction(opts={})
  unless opts.is_a?(Hash)
    Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})") 
    opts = {:server=>opts}
  end
  synchronize(opts[:server]) do |conn|
    return yield(conn) if conn.transaction_active?
    begin
      result = nil
      log_info('Transaction.begin')
      conn.transaction{result = yield(conn)}
      result
    rescue ::Exception => e
      log_info('Transaction.rollback')
      transaction_error(e, SQLite3::Exception)
    ensure
      log_info('Transaction.commit') unless e
    end
  end
end