Class: SQLite3::Database

Inherits:
Object
  • Object
show all
Includes:
Pragmas
Defined in:
lib/sqlite3/database.rb

Overview

The Database class encapsulates a single connection to a SQLite3 database. Its usage is very straightforward:

require 'sqlite3'

SQLite3::Database.new( "data.db" ) do |db|
  db.execute( "select * from table" ) do |row|
    p row
  end
end

It wraps the lower-level methods provides by the selected driver, and includes the Pragmas module for access to various pragma convenience methods.

The Database class provides type translation services as well, by which the SQLite3 data types (which are all represented as strings) may be converted into their corresponding types (as defined in the schemas for their tables). This translation only occurs when querying data from the database–insertions and updates are all still typeless.

Furthermore, the Database class has been designed to work well with the ArrayFields module from Ara Howard. If you require the ArrayFields module before performing a query, and if you have not enabled results as hashes, then the results will all be indexible by field name.

Defined Under Namespace

Classes: AggregateDefinitionProxy, FunctionProxy

Constant Summary

Constants included from Pragmas

Pragmas::SYNCHRONOUS_MODES, Pragmas::TEMP_STORE_MODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pragmas

#auto_vacuum, #auto_vacuum=, #cache_size, #cache_size=, #database_list, #default_cache_size, #default_cache_size=, #default_synchronous, #default_synchronous=, #default_temp_store, #default_temp_store=, #foreign_key_list, #full_column_names, #full_column_names=, #index_info, #index_list, #integrity_check, #parser_trace, #parser_trace=, #schema_cookie, #schema_cookie=, #synchronous, #synchronous=, #table_info, #temp_store, #temp_store=, #user_cookie, #user_cookie=, #vdbe_trace, #vdbe_trace=

Constructor Details

#initialize(file_name, options = {}) ⇒ Database

Create a new Database object that opens the given file. If utf16 is true, the filename is interpreted as a UTF-16 encoded string.

By default, the new database will return result rows as arrays (#results_as_hash) and has type translation disabled (#type_translation=).



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sqlite3/database.rb', line 70

def initialize( file_name, options={} ) # :yields: db
  utf16 = options.fetch(:utf16, false)
  load_driver( options[:driver] )

  @statement_factory = options[:statement_factory] || Statement

  result, @handle = @driver.open( file_name, utf16 )
  Error.check( result, self, "could not open database" )

  @closed = false
  @results_as_hash = options.fetch(:results_as_hash,false)
  @type_translation = options.fetch(:type_translation,false)
  @translator = nil
  @transaction_active = false
  
  if block_given?
    begin
      yield self
    ensure
      self.close
    end
  end
end

Instance Attribute Details

#driverObject (readonly)

A reference to the underlying SQLite3 driver used by this database.



55
56
57
# File 'lib/sqlite3/database.rb', line 55

def driver
  @driver
end

#handleObject (readonly)

The low-level opaque database handle that this object wraps.



52
53
54
# File 'lib/sqlite3/database.rb', line 52

def handle
  @handle
end

#results_as_hashObject

A boolean that indicates whether rows in result sets should be returned as hashes or not. By default, rows are returned as arrays.



59
60
61
# File 'lib/sqlite3/database.rb', line 59

def results_as_hash
  @results_as_hash
end

#type_translationObject

A boolean indicating whether or not type translation is enabled for this database.



63
64
65
# File 'lib/sqlite3/database.rb', line 63

def type_translation
  @type_translation
end

Class Method Details

.quote(string) ⇒ Object

Quotes the given string, making it safe to use in an SQL statement. It replaces all instances of the single-quote character with two single-quote characters. The modified string is returned.



45
46
47
# File 'lib/sqlite3/database.rb', line 45

def quote( string )
  string.gsub( /'/, "''" )
end

Instance Method Details

#authorizer(data = nil, &block) ⇒ Object

Installs (or removes) a block that will be invoked for every access to the database. If the block returns 0 (or nil), the statement is allowed to proceed. Returning 1 causes an authorization error to occur, and returning 2 causes the access to be silently denied.



149
150
151
152
# File 'lib/sqlite3/database.rb', line 149

def authorizer( data=nil, &block )
  result = @driver.set_authorizer( @handle, data, &block )
  Error.check( result, self )
end

#busy_handler(data = nil, &block) ⇒ Object

Register a busy handler with this database instance. When a requested resource is busy, this handler will be invoked. If the handler returns false, the operation will be aborted; otherwise, the resource will be requested again.

The handler will be invoked with the name of the resource that was busy, and the number of times it has been retried.

See also the mutually exclusive #busy_timeout.



315
316
317
318
# File 'lib/sqlite3/database.rb', line 315

def busy_handler( data=nil, &block ) # :yields: data, retries
  result = @driver.busy_handler( @handle, data, &block )
  Error.check( result, self )
end

#busy_timeout(ms) ⇒ Object

Indicates that if a request for a resource terminates because that resource is busy, SQLite should sleep and retry for up to the indicated number of milliseconds. By default, SQLite does not retry busy resources. To restore the default behavior, send 0 as the ms parameter.

See also the mutually exclusive #busy_handler.



327
328
329
330
# File 'lib/sqlite3/database.rb', line 327

def busy_timeout( ms )
  result = @driver.busy_timeout( @handle, ms )
  Error.check( result, self )
end

#changesObject

Returns the number of changes made to this database instance by the last operation performed. Note that a “delete from table” without a where clause will not affect this value.



291
292
293
# File 'lib/sqlite3/database.rb', line 291

def changes
  @driver.changes( @handle )
end

#closeObject

Closes this database.



124
125
126
127
128
129
130
# File 'lib/sqlite3/database.rb', line 124

def close
  unless @closed
    result = @driver.close( @handle )
    Error.check( result, self )
  end
  @closed = true
end

#closed?Boolean

Returns true if this database instance has been closed (see #close).

Returns:

  • (Boolean)


133
134
135
# File 'lib/sqlite3/database.rb', line 133

def closed?
  @closed
end

#commitObject

Commits the current transaction. If there is no current transaction, this will cause an error to be raised. This returns true, in order to allow it to be used in idioms like abort? and rollback or commit.



586
587
588
589
590
# File 'lib/sqlite3/database.rb', line 586

def commit
  execute "commit transaction"
  @transaction_active = false
  true
end

#complete?(string, utf16 = false) ⇒ Boolean

Return true if the string is a valid (ie, parsable) SQL statement, and false otherwise. If utf16 is true, then the string is a UTF-16 character string.

Returns:

  • (Boolean)


97
98
99
# File 'lib/sqlite3/database.rb', line 97

def complete?( string, utf16=false )
  @driver.complete?( string, utf16 )
end

#create_aggregate(name, arity, step = nil, finalize = nil, text_rep = Constants::TextRep::ANY, &block) ⇒ Object

Creates a new aggregate function for use in SQL statements. Aggregate functions are functions that apply over every row in the result set, instead of over just a single row. (A very common aggregate function is the “count” function, for determining the number of rows that match a query.)

The new function will be added as name, with the given arity. (For variable arity functions, use -1 for the arity.)

The step parameter must be a proc object that accepts as its first parameter a FunctionProxy instance (representing the function invocation), with any subsequent parameters (up to the function’s arity). The step callback will be invoked once for each row of the result set.

The finalize parameter must be a proc object that accepts only a single parameter, the FunctionProxy instance representing the current function invocation. It should invoke FunctionProxy#set_result to store the result of the function.

Example:

db.create_aggregate( "lengths", 1 ) do
  step do |func, value|
    func[ :total ] ||= 0
    func[ :total ] += ( value ? value.length : 0 )
  end

  finalize do |func|
    func.set_result( func[ :total ] || 0 )
  end
end

puts db.get_first_value( "select lengths(name) from table" )

See also #create_aggregate_handler for a more object-oriented approach to aggregate functions.



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/sqlite3/database.rb', line 411

def create_aggregate( name, arity, step=nil, finalize=nil,
  text_rep=Constants::TextRep::ANY, &block )
# begin
  if block
    proxy = AggregateDefinitionProxy.new
    proxy.instance_eval(&block)
    step ||= proxy.step_callback
    finalize ||= proxy.finalize_callback
  end

  step_callback = proc do |func,*args|
    ctx = @driver.aggregate_context( func )
    unless ctx[:__error]
      begin
        step.call( FunctionProxy.new( @driver, func, ctx ),
          *args.map{|v| Value.new(self,v)} )
      rescue Exception => e
        ctx[:__error] = e
      end
    end
  end

  finalize_callback = proc do |func|
    ctx = @driver.aggregate_context( func )
    unless ctx[:__error]
      begin
        finalize.call( FunctionProxy.new( @driver, func, ctx ) )
      rescue Exception => e
        @driver.result_error( func,
          "#{e.message} (#{e.class})", -1 )
      end
    else
      e = ctx[:__error]
      @driver.result_error( func,
        "#{e.message} (#{e.class})", -1 )
    end
  end

  result = @driver.create_function( @handle, name, arity, text_rep, nil,
    nil, step_callback, finalize_callback )
  Error.check( result, self )

  self
end

#create_aggregate_handler(handler) ⇒ Object

This is another approach to creating an aggregate function (see #create_aggregate). Instead of explicitly specifying the name, callbacks, arity, and type, you specify a factory object (the “handler”) that knows how to obtain all of that information. The handler should respond to the following messages:

arity

corresponds to the arity parameter of #create_aggregate. This message is optional, and if the handler does not respond to it, the function will have an arity of -1.

name

this is the name of the function. The handler must implement this message.

new

this must be implemented by the handler. It should return a new instance of the object that will handle a specific invocation of the function.

The handler instance (the object returned by the new message, described above), must respond to the following messages:

step

this is the method that will be called for each step of the aggregate function’s evaluation. It should implement the same signature as the step callback for #create_aggregate.

finalize

this is the method that will be called to finalize the aggregate function’s evaluation. It should implement the same signature as the finalize callback for #create_aggregate.

Example:

class LengthsAggregateHandler
  def self.arity; 1; end

  def initialize
    @total = 0
  end

  def step( ctx, name )
    @total += ( name ? name.length : 0 )
  end

  def finalize( ctx )
    ctx.set_result( @total )
  end
end

db.create_aggregate_handler( LengthsAggregateHandler )
puts db.get_first_value( "select lengths(name) from A" )


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/sqlite3/database.rb', line 502

def create_aggregate_handler( handler )
  arity = -1
  text_rep = Constants::TextRep::ANY

  arity = handler.arity if handler.respond_to?(:arity)
  text_rep = handler.text_rep if handler.respond_to?(:text_rep)
  name = handler.name

  step = proc do |func,*args|
    ctx = @driver.aggregate_context( func )
    unless ctx[ :__error ]
      ctx[ :handler ] ||= handler.new
      begin
        ctx[ :handler ].step( FunctionProxy.new( @driver, func, ctx ),
          *args.map{|v| Value.new(self,v)} )
      rescue Exception, StandardError => e
        ctx[ :__error ] = e
      end
    end
  end

  finalize = proc do |func|
    ctx = @driver.aggregate_context( func )
    unless ctx[ :__error ]
      ctx[ :handler ] ||= handler.new
      begin
        ctx[ :handler ].finalize( FunctionProxy.new( @driver, func, ctx ) )
      rescue Exception => e
        ctx[ :__error ] = e
      end
    end

    if ctx[ :__error ]
      e = ctx[ :__error ]
      @driver.sqlite3_result_error( func, "#{e.message} (#{e.class})", -1 )
    end
  end

  result = @driver.create_function( @handle, name, arity, text_rep, nil,
    nil, step, finalize )
  Error.check( result, self )

  self
end

#create_function(name, arity, text_rep = Constants::TextRep::ANY, &block) ⇒ Object

Creates a new function for use in SQL statements. It will be added as name, with the given arity. (For variable arity functions, use -1 for the arity.)

The block should accept at least one parameter–the FunctionProxy instance that wraps this function invocation–and any other arguments it needs (up to its arity).

The block does not return a value directly. Instead, it will invoke the FunctionProxy#set_result method on the func parameter and indicate the return value that way.

Example:

db.create_function( "maim", 1 ) do |func, value|
  if value.nil?
    func.result = nil
  else
    func.result = value.split(//).sort.join
  end
end

puts db.get_first_value( "select maim(name) from table" )


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/sqlite3/database.rb', line 355

def create_function( name, arity, text_rep=Constants::TextRep::ANY,
  &block ) # :yields: func, *args
# begin
  callback = proc do |func,*args|
    begin
      block.call( FunctionProxy.new( @driver, func ),
        *args.map{|v| Value.new(self,v)} )
    rescue StandardError, Exception => e
      @driver.result_error( func,
        "#{e.message} (#{e.class})", -1 )
    end
  end

  result = @driver.create_function( @handle, name, arity, text_rep, nil,
    callback, nil, nil )
  Error.check( result, self )

  self
end

#errcodeObject

Return an integer representing the last error to have occurred with this database.



109
110
111
# File 'lib/sqlite3/database.rb', line 109

def errcode
  @driver.errcode( @handle )
end

#errmsg(utf16 = false) ⇒ Object

Return a string describing the last error to have occurred with this database.



103
104
105
# File 'lib/sqlite3/database.rb', line 103

def errmsg( utf16=false )
  @driver.errmsg( @handle, utf16 )
end

#execute(sql, *bind_vars) ⇒ Object

Executes the given SQL statement. If additional parameters are given, they are treated as bind variables, and are bound to the placeholders in the query.

Note that if any of the values passed to this are hashes, then the key/value pairs are each bound separately, with the key being used as the name of the placeholder to bind the value to.

The block is optional. If given, it will be invoked for each row returned by the query. Otherwise, any results are accumulated into an array and returned wholesale.

See also #execute2, #query, and #execute_batch for additional ways of executing statements.



186
187
188
189
190
191
192
193
194
195
# File 'lib/sqlite3/database.rb', line 186

def execute( sql, *bind_vars )
  prepare( sql ) do |stmt|
    result = stmt.execute( *bind_vars )
    if block_given?
      result.each { |row| yield row }
    else
      return result.inject( [] ) { |arr,row| arr << row; arr }
    end
  end
end

#execute2(sql, *bind_vars) ⇒ Object

Executes the given SQL statement, exactly as with #execute. However, the first row returned (either via the block, or in the returned array) is always the names of the columns. Subsequent rows correspond to the data from the result set.

Thus, even if the query itself returns no rows, this method will always return at least one row–the names of the columns.

See also #execute, #query, and #execute_batch for additional ways of executing statements.



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/sqlite3/database.rb', line 207

def execute2( sql, *bind_vars )
  prepare( sql ) do |stmt|
    result = stmt.execute( *bind_vars )
    if block_given?
      yield result.columns
      result.each { |row| yield row }
    else
      return result.inject( [ result.columns ] ) { |arr,row|
        arr << row; arr }
    end
  end
end

#execute_batch(sql, *bind_vars) ⇒ Object

Executes all SQL statements in the given string. By contrast, the other means of executing queries will only execute the first statement in the string, ignoring all subsequent statements. This will execute each one in turn. The same bind parameters, if given, will be applied to each statement.

This always returns nil, making it unsuitable for queries that return rows.



228
229
230
231
232
233
234
235
236
237
# File 'lib/sqlite3/database.rb', line 228

def execute_batch( sql, *bind_vars )
  sql = sql.strip
  until sql.empty? do
    prepare( sql ) do |stmt|
      stmt.execute( *bind_vars )
      sql = stmt.remainder.strip
    end
  end
  nil
end

#get_first_row(sql, *bind_vars) ⇒ Object

A convenience method for obtaining the first row of a result set, and discarding all others. It is otherwise identical to #execute.

See also #get_first_value.



267
268
269
270
# File 'lib/sqlite3/database.rb', line 267

def get_first_row( sql, *bind_vars )
  execute( sql, *bind_vars ) { |row| return row }
  nil
end

#get_first_value(sql, *bind_vars) ⇒ Object

A convenience method for obtaining the first value of the first row of a result set, and discarding all other values and rows. It is otherwise identical to #execute.

See also #get_first_row.



277
278
279
280
# File 'lib/sqlite3/database.rb', line 277

def get_first_value( sql, *bind_vars )
  execute( sql, *bind_vars ) { |row| return row[0] }
  nil
end

#interruptObject

Interrupts the currently executing operation, causing it to abort.



302
303
304
# File 'lib/sqlite3/database.rb', line 302

def interrupt
  @driver.interrupt( @handle )
end

#last_insert_row_idObject

Obtains the unique row ID of the last row to be inserted by this Database instance.



284
285
286
# File 'lib/sqlite3/database.rb', line 284

def last_insert_row_id
  @driver.last_insert_rowid( @handle )
end

#prepare(sql) ⇒ Object

Returns a Statement object representing the given SQL. This does not execute the statement; it merely prepares the statement for execution.

The Statement can then be executed using Statement#execute.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sqlite3/database.rb', line 159

def prepare( sql )
  stmt = @statement_factory.new( self, sql )
  if block_given?
    begin
      yield stmt
    ensure
      stmt.close
    end
  else
    return stmt
  end
end

#query(sql, *bind_vars) ⇒ Object

This is a convenience method for creating a statement, binding paramters to it, and calling execute:

result = db.query( "select * from foo where a=?", 5 )
# is the same as
result = db.prepare( "select * from foo where a=?" ).execute( 5 )

You must be sure to call close on the ResultSet instance that is returned, or you could have problems with locks on the table. If called with a block, close will be invoked implicitly when the block terminates.



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/sqlite3/database.rb', line 250

def query( sql, *bind_vars )
  result = prepare( sql ).execute( *bind_vars )
  if block_given?
    begin
      yield result
    ensure
      result.close
    end
  else
    return result
  end
end

#rollbackObject

Rolls the current transaction back. If there is no current transaction, this will cause an error to be raised. This returns true, in order to allow it to be used in idioms like abort? and rollback or commit.



596
597
598
599
600
# File 'lib/sqlite3/database.rb', line 596

def rollback
  execute "rollback transaction"
  @transaction_active = false
  true
end

#total_changesObject

Returns the total number of changes made to this database instance since it was opened.



297
298
299
# File 'lib/sqlite3/database.rb', line 297

def total_changes
  @driver.total_changes( @handle )
end

#trace(data = nil, &block) ⇒ Object

Installs (or removes) a block that will be invoked for every SQL statement executed. The block receives a two parameters: the data argument, and the SQL statement executed. If the block is nil, any existing tracer will be uninstalled.



141
142
143
# File 'lib/sqlite3/database.rb', line 141

def trace( data=nil, &block )
  @driver.trace( @handle, data, &block )
end

#transaction(mode = :deferred) ⇒ Object

Begins a new transaction. Note that nested transactions are not allowed by SQLite, so attempting to nest a transaction will result in a runtime exception.

The mode parameter may be either :deferred (the default), :immediate, or :exclusive.

If a block is given, the database instance is yielded to it, and the transaction is committed when the block terminates. If the block raises an exception, a rollback will be performed instead. Note that if a block is given, #commit and #rollback should never be called explicitly or you’ll get an error when the block terminates.

If a block is not given, it is the caller’s responsibility to end the transaction explicitly, either by calling #commit, or by calling #rollback.



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/sqlite3/database.rb', line 563

def transaction( mode = :deferred )
  execute "begin #{mode.to_s} transaction"
  @transaction_active = true

  if block_given?
    abort = false
    begin
      yield self
    rescue ::Object
      abort = true
      raise
    ensure
      abort and rollback or commit
    end
  end

  true
end

#transaction_active?Boolean

Returns true if there is a transaction active, and false otherwise.

Returns:

  • (Boolean)


603
604
605
# File 'lib/sqlite3/database.rb', line 603

def transaction_active?
  @transaction_active
end

#translatorObject

Return the type translator employed by this database instance. Each database instance has its own type translator; this allows for different type handlers to be installed in each instance without affecting other instances. Furthermore, the translators are instantiated lazily, so that if a database does not use type translation, it will not be burdened by the overhead of a useless type translator. (See the Translator class.)



119
120
121
# File 'lib/sqlite3/database.rb', line 119

def translator
  @translator ||= Translator.new
end