Class Sequel::SQLite::Database
In: lib/sequel/adapters/sqlite.rb
Parent: Sequel::Database

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

Methods

Included Modules

::Sequel::SQLite::DatabaseMethods

Constants

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

Public Instance methods

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 28
28:       def connect(server)
29:         opts = server_opts(server)
30:         opts[:database] = ':memory:' if blank_object?(opts[:database])
31:         db = ::SQLite3::Database.new(opts[:database])
32:         db.busy_timeout(opts.fetch(:timeout, 5000))
33:         db.type_translation = true
34:         
35:         # Handle datetime's with Sequel.datetime_class
36:         prok = proc do |t,v|
37:           v = Time.at(v.to_i).iso8601 if UNIX_EPOCH_TIME_FORMAT.match(v)
38:           Sequel.string_to_datetime(v)
39:         end
40:         db.translator.add_translator("timestamp", &prok)
41:         db.translator.add_translator("datetime", &prok)
42:         
43:         # Handle numeric values with BigDecimal
44:         prok = proc{|t,v| BigDecimal.new(v) rescue v}
45:         db.translator.add_translator("numeric", &prok)
46:         db.translator.add_translator("decimal", &prok)
47:         db.translator.add_translator("money", &prok)
48:         
49:         # Handle floating point values with Float
50:         prok = proc{|t,v| Float(v) rescue v}
51:         db.translator.add_translator("float", &prok)
52:         db.translator.add_translator("real", &prok)
53:         db.translator.add_translator("double precision", &prok)
54:         
55:         # Handle blob values with Sequel::SQL::Blob
56:         db.translator.add_translator("blob"){|t,v| ::Sequel::SQL::Blob.new(v)}
57:         
58:         db
59:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 62
62:       def dataset(opts = nil)
63:         SQLite::Dataset.new(self, opts)
64:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 77
77:       def execute(sql, opts={}, &block)
78:         _execute(sql, opts){|conn| conn.query(sql, opts[:arguments], &block)}
79:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 67
67:       def execute_dui(sql, opts={})
68:         _execute(sql, opts){|conn| conn.execute_batch(sql, opts[:arguments]); conn.changes}
69:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 72
72:       def execute_insert(sql, opts={})
73:         _execute(sql, opts){|conn| conn.execute(sql, opts[:arguments]); conn.last_insert_row_id}
74:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 82
82:       def single_value(sql, opts={})
83:         _execute(sql, opts){|conn| conn.get_first_value(sql, opts[:arguments])}
84:       end

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.

[Source]

     # File lib/sequel/adapters/sqlite.rb, line 89
 89:       def transaction(opts={})
 90:         synchronize(opts[:server]) do |conn|
 91:           return yield(conn) if conn.transaction_active?
 92:           begin
 93:             result = nil
 94:             log_info('Transaction.begin')
 95:             conn.transaction{result = yield(conn)}
 96:             result
 97:           rescue ::Exception => e
 98:             log_info('Transaction.rollback')
 99:             transaction_error(e, SQLite3::Exception)
100:           ensure
101:             log_info('Transaction.commit') unless e
102:           end
103:         end
104:       end

[Validate]