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 33
33:       def connect(server)
34:         opts = server_opts(server)
35:         opts[:database] = ':memory:' if blank_object?(opts[:database])
36:         db = ::SQLite3::Database.new(opts[:database])
37:         db.busy_timeout(opts.fetch(:timeout, 5000))
38:         db.type_translation = true
39:         
40:         connection_pragmas.each{|s| log_yield(s){db.execute_batch(s)}}
41:         
42:         # Handle datetimes with Sequel.datetime_class
43:         prok = proc do |t,v|
44:           v = Time.at(v.to_i).iso8601 if UNIX_EPOCH_TIME_FORMAT.match(v)
45:           Sequel.database_to_application_timestamp(v)
46:         end
47:         db.translator.add_translator("timestamp", &prok)
48:         db.translator.add_translator("datetime", &prok)
49:         
50:         # Handle numeric values with BigDecimal
51:         prok = proc{|t,v| BigDecimal.new(v) rescue v}
52:         db.translator.add_translator("numeric", &prok)
53:         db.translator.add_translator("decimal", &prok)
54:         db.translator.add_translator("money", &prok)
55:         
56:         # Handle floating point values with Float
57:         prok = proc{|t,v| Float(v) rescue v}
58:         db.translator.add_translator("float", &prok)
59:         db.translator.add_translator("real", &prok)
60:         db.translator.add_translator("double precision", &prok)
61:         
62:         # Handle blob values with Sequel::SQL::Blob
63:         db.translator.add_translator("blob"){|t,v| ::Sequel::SQL::Blob.new(v)}
64: 
65:         class << db
66:           attr_reader :prepared_statements
67:         end
68:         db.instance_variable_set(:@prepared_statements, {})
69:         
70:         db
71:       end

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

[Source]

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

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 79
79:       def execute(sql, opts={}, &block)
80:         _execute(:select, sql, opts, &block)
81:       end

Drop any prepared statements on the connection when executing DDL. This is because prepared statements lock the table in such a way that you can‘t drop or alter the table while a prepared statement that references it still exists.

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 91
91:       def execute_ddl(sql, opts={})
92:         synchronize(opts[:server]) do |conn|
93:           conn.prepared_statements.values.each{|cps, s| cps.close}
94:           conn.prepared_statements.clear
95:           super
96:         end
97:       end

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

[Source]

    # File lib/sequel/adapters/sqlite.rb, line 84
84:       def execute_dui(sql, opts={})
85:         _execute(:update, sql, opts)
86:       end

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

[Source]

     # File lib/sequel/adapters/sqlite.rb, line 100
100:       def execute_insert(sql, opts={})
101:         _execute(:insert, sql, opts)
102:       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 105
105:       def single_value(sql, opts={})
106:         _execute(:single_value, sql, opts)
107:       end

[Validate]