class Sequel::SQLite::Database

  1. lib/sequel/adapters/sqlite.rb
Superclass: Sequel::Database

Attributes

conversion_procs [R]

The conversion procs to use for this database

Public Class methods

new(opts = OPTS)
[show source]
    # File lib/sequel/adapters/sqlite.rb
101 def initialize(opts = OPTS)
102   super
103   @allow_regexp = typecast_value_boolean(opts[:setup_regexp_function])
104 end

Public Instance methods

allow_regexp?()

Whether this Database instance is setup to allow regexp matching. True if the :setup_regexp_function option was passed when creating the Database.

[show source]
    # File lib/sequel/adapters/sqlite.rb
158 def allow_regexp?
159   @allow_regexp
160 end
connect(server)

Connect to the database. Since SQLite is a file based database, available options are limited:

:database

database name (filename or ‘:memory:’ or file: URI)

:readonly

open database in read-only mode; useful for reading static data that you do not want to modify

:disable_dqs

disable double quoted strings in DDL and DML statements (requires SQLite 3.29.0+ and sqlite3 gem version 1.4.3+).

:timeout

how long to wait for the database to be available if it is locked, given in milliseconds (default is 5000)

:setup_regexp_function

enable use of Regexp objects with SQL

'REGEXP' operator. If the value is :cached or "cached",
caches the generated regexps, which can result in a memory
leak if dynamic regexps are used.  If the value is a Proc,
it will be called with a string for the regexp and a string
for the value to compare, and should return whether the regexp
matches.
:regexp_function_cache

If setting setup_regexp_function to cached, this

determines the cache to use.  It should either be a proc or a class, and it
defaults to +Hash+. You can use +ObjectSpace::WeakKeyMap+ on Ruby 3.3+ to
have the VM automatically remove regexps from the cache after they
are no longer used.
[show source]
    # File lib/sequel/adapters/sqlite.rb
128 def connect(server)
129   opts = server_opts(server)
130   opts[:database] = ':memory:' if blank_object?(opts[:database])
131   sqlite3_opts = {}
132   sqlite3_opts[:readonly] = typecast_value_boolean(opts[:readonly]) if opts.has_key?(:readonly)
133   # SEQUEL6: Make strict: true the default behavior
134   sqlite3_opts[:strict] = typecast_value_boolean(opts[:disable_dqs]) if opts.has_key?(:disable_dqs)
135   db = ::SQLite3::Database.new(opts[:database].to_s, sqlite3_opts)
136   db.busy_timeout(typecast_value_integer(opts.fetch(:timeout, 5000)))
137 
138   if USE_EXTENDED_RESULT_CODES
139     db.extended_result_codes = true
140   end
141   
142   connection_pragmas.each{|s| log_connection_yield(s, db){db.execute_batch(s)}}
143 
144   if typecast_value_boolean(opts[:setup_regexp_function])
145     setup_regexp_function(db, opts[:setup_regexp_function])
146   end
147   
148   class << db
149     attr_reader :prepared_statements
150   end
151   db.instance_variable_set(:@prepared_statements, {})
152   
153   db
154 end
disconnect_connection(c)

Disconnect given connections from the database.

[show source]
    # File lib/sequel/adapters/sqlite.rb
163 def disconnect_connection(c)
164   c.prepared_statements.each_value{|v| v.first.close}
165   c.close
166 end
execute(sql, opts=OPTS, &block)

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

[show source]
    # File lib/sequel/adapters/sqlite.rb
169 def execute(sql, opts=OPTS, &block)
170   _execute(:select, sql, opts, &block)
171 end
execute_ddl(sql, opts=OPTS)

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.

[show source]
    # File lib/sequel/adapters/sqlite.rb
181 def execute_ddl(sql, opts=OPTS)
182   synchronize(opts[:server]) do |conn|
183     conn.prepared_statements.values.each{|cps, s| cps.close}
184     conn.prepared_statements.clear
185     super
186   end
187 end
execute_dui(sql, opts=OPTS)

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

[show source]
    # File lib/sequel/adapters/sqlite.rb
174 def execute_dui(sql, opts=OPTS)
175   _execute(:update, sql, opts)
176 end
execute_insert(sql, opts=OPTS)
[show source]
    # File lib/sequel/adapters/sqlite.rb
189 def execute_insert(sql, opts=OPTS)
190   _execute(:insert, sql, opts)
191 end
freeze()
[show source]
    # File lib/sequel/adapters/sqlite.rb
193 def freeze
194   @conversion_procs.freeze
195   super
196 end
to_application_timestamp(s)

Handle Integer and Float arguments, since SQLite can store timestamps as integers and floats.

[show source]
    # File lib/sequel/adapters/sqlite.rb
199 def to_application_timestamp(s)
200   case s
201   when String
202     super
203   when Integer
204     super(Time.at(s).to_s)
205   when Float
206     super(DateTime.jd(s).to_s)
207   else
208     raise Sequel::Error, "unhandled type when converting to : #{s.inspect} (#{s.class.inspect})"
209   end
210 end