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

Database class for PostgreSQL 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_core/adapters/sqlite.rb, line 28
28:       def connect(server)
29:         opts = server_opts(server)
30:         opts[:database] = ':memory:' if opts[:database].blank?
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:           v.to_sequel_time
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:         db
56:       end

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

[Source]

    # File lib/sequel_core/adapters/sqlite.rb, line 59
59:       def dataset(opts = nil)
60:         SQLite::Dataset.new(self, opts)
61:       end

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

[Source]

    # File lib/sequel_core/adapters/sqlite.rb, line 74
74:       def execute(sql, opts={}, &block)
75:         _execute(sql, opts){|conn| conn.query(sql, opts[:arguments], &block)}
76:       end

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

[Source]

    # File lib/sequel_core/adapters/sqlite.rb, line 64
64:       def execute_dui(sql, opts={})
65:         _execute(sql, opts){|conn| conn.execute_batch(sql, opts[:arguments]); conn.changes}
66:       end

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

[Source]

    # File lib/sequel_core/adapters/sqlite.rb, line 69
69:       def execute_insert(sql, opts={})
70:         _execute(sql, opts){|conn| conn.execute(sql, opts[:arguments]); conn.last_insert_row_id}
71:       end

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

[Source]

    # File lib/sequel_core/adapters/sqlite.rb, line 79
79:       def single_value(sql, opts={})
80:         _execute(sql, opts){|conn| conn.get_first_value(sql, opts[:arguments])}
81:       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_core/adapters/sqlite.rb, line 86
 86:       def transaction(server=nil, &block)
 87:         synchronize(server) do |conn|
 88:           return yield(conn) if conn.transaction_active?
 89:           begin
 90:             result = nil
 91:             log_info('Transaction.begin')
 92:             conn.transaction{result = yield(conn)}
 93:             result
 94:           rescue ::Exception => e
 95:             log_info('Transaction.rollback')
 96:             transaction_error(e, SQLite3::Exception)
 97:           ensure
 98:             log_info('Transaction.commit') unless e
 99:           end
100:         end
101:       end

[Validate]