| Class | Sequel::SQLite::Database |
| In: |
lib/sequel/adapters/sqlite.rb
|
| Parent: | Sequel::Database |
| UNIX_EPOCH_TIME_FORMAT | = | /\A\d+\z/.freeze |
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).
# 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: db
66: end
Return instance of Sequel::SQLite::Dataset with the given options.
# File lib/sequel/adapters/sqlite.rb, line 69
69: def dataset(opts = nil)
70: SQLite::Dataset.new(self, opts)
71: end
Run the given SQL with the given arguments and yield each row.
# File lib/sequel/adapters/sqlite.rb, line 84
84: def execute(sql, opts={})
85: _execute(opts) do |conn|
86: begin
87: yield(result = log_yield(sql, opts[:arguments]){conn.query(sql, opts.fetch(:arguments, []))})
88: ensure
89: result.close if result
90: end
91: end
92: end
Run the given SQL with the given arguments and return the number of changed rows.
# File lib/sequel/adapters/sqlite.rb, line 74
74: def execute_dui(sql, opts={})
75: _execute(opts){|conn| log_yield(sql, opts[:arguments]){conn.execute_batch(sql, opts.fetch(:arguments, []))}; conn.changes}
76: end
Run the given SQL with the given arguments and return the last inserted row id.
# File lib/sequel/adapters/sqlite.rb, line 79
79: def execute_insert(sql, opts={})
80: _execute(opts){|conn| log_yield(sql, opts[:arguments]){conn.execute(sql, opts.fetch(:arguments, []))}; conn.last_insert_row_id}
81: end
Run the given SQL with the given arguments and return the first value of the first row.
# File lib/sequel/adapters/sqlite.rb, line 95
95: def single_value(sql, opts={})
96: _execute(opts){|conn| log_yield(sql, opts[:arguments]){conn.get_first_value(sql, opts.fetch(:arguments, []))}}
97: end