Class: SQLite3::Database

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

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: FunctionProxy

Constant Summary

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

- (Object) SQLite3::Database.new(file, options = {})

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=).



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/sqlite3/database.c', line 41

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
  sqlite3RubyPtr ctx;
  VALUE file;
  VALUE opts;
  VALUE zvfs;
#ifdef HAVE_SQLITE3_OPEN_V2
  int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
#endif
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);

  rb_scan_args(argc, argv, "12", &file, &opts, &zvfs);
#if defined StringValueCStr
  StringValuePtr(file);
  rb_check_safe_obj(file);
#else
  Check_SafeStr(file);
#endif
  if(NIL_P(opts)) opts = rb_hash_new();
  else Check_Type(opts, T_HASH);

#ifdef HAVE_RUBY_ENCODING_H
  if(UTF16_LE_P(file) || UTF16_BE_P(file)) {
    status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
  } else {
#endif

    if(Qtrue == rb_hash_aref(opts, sym_utf16)) {
      status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
    } else {

#ifdef HAVE_RUBY_ENCODING_H
      if(!UTF8_P(file)) {
        file = rb_str_export_to_enc(file, rb_utf8_encoding());
      }
#endif

      if (Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("readonly")))) {
#ifdef HAVE_SQLITE3_OPEN_V2
        mode = SQLITE_OPEN_READONLY;
#else
        rb_raise(rb_eNotImpError, "sqlite3-ruby was compiled against a version of sqlite that does not support readonly databases");
#endif
      }
#ifdef HAVE_SQLITE3_OPEN_V2
      status = sqlite3_open_v2(
          StringValuePtr(file),
          &ctx->db,
          mode,
          NIL_P(zvfs) ? NULL : StringValuePtr(zvfs)
      );
#else
      status = sqlite3_open(
          StringValuePtr(file),
          &ctx->db
      );
#endif
    }

#ifdef HAVE_RUBY_ENCODING_H
  }
#endif

  CHECK(ctx->db, status)

  rb_iv_set(self, "@tracefunc", Qnil);
  rb_iv_set(self, "@authorizer", Qnil);
  rb_iv_set(self, "@encoding", Qnil);
  rb_iv_set(self, "@busy_handler", Qnil);
  rb_iv_set(self, "@collations", rb_hash_new());
  rb_iv_set(self, "@functions", rb_hash_new());
  rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash));
  rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation));
#ifdef HAVE_SQLITE3_OPEN_V2
  rb_iv_set(self, "@readonly", mode == SQLITE_OPEN_READONLY ? Qtrue : Qfalse);
#else
  rb_iv_set(self, "@readonly", Qfalse);
#endif

  if(rb_block_given_p()) {
    rb_yield(self);
    rb_funcall(self, rb_intern("close"), 0);
  }

  return self;
}

Instance Attribute Details

- (Object) collations (readonly)

Returns the value of attribute collations



36
37
38
# File 'lib/sqlite3/database.rb', line 36

def collations
  @collations
end

- (Object) results_as_hash

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



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

def results_as_hash
  @results_as_hash
end

- (Object) type_translation

:nodoc:



65
66
67
# File 'lib/sqlite3/database.rb', line 65

def type_translation
  @type_translation
end

Class Method Details

+ (Object) quote(string)

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.



47
48
49
# File 'lib/sqlite3/database.rb', line 47

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

Instance Method Details

- (Object) authorizer(&block)

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.



81
82
83
# File 'lib/sqlite3/database.rb', line 81

def authorizer( &block )
  self.authorizer = block
end

- (Object) set_authorizer=(auth)

Set the authorizer for this database. auth must respond to call, and call must take 5 arguments.

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



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'ext/sqlite3/database.c', line 570

static VALUE set_authorizer(VALUE self, VALUE authorizer)
{
  sqlite3RubyPtr ctx;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  status = sqlite3_set_authorizer(
      ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)self
  );

  CHECK(ctx->db, status);

  rb_iv_set(self, "@authorizer", authorizer);

  return self;
}

- (Object) busy_handler {|count| ... } - (Object) busy_handler(Class.new{)

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.

Overloads:

  • - (Object) busy_handler {|count| ... }

    Yields:

    • (count)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/sqlite3/database.c', line 235

static VALUE busy_handler(int argc, VALUE *argv, VALUE self)
{
  sqlite3RubyPtr ctx;
  VALUE block;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  rb_scan_args(argc, argv, "01", &block);

  if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();

  rb_iv_set(self, "@busy_handler", block);

  status = sqlite3_busy_handler(
      ctx->db, NIL_P(block) ? NULL : rb_sqlite3_busy_handler, (void *)self);

  CHECK(ctx->db, status);

  return self;
}

- (Object) busy_timeout=(ms) Also known as: busy_timeout

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.



599
600
601
602
603
604
605
606
607
608
# File 'ext/sqlite3/database.c', line 599

static VALUE set_busy_timeout(VALUE self, VALUE timeout)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  CHECK(ctx->db, sqlite3_busy_timeout(ctx->db, (int)NUM2INT(timeout)));

  return self;
}

- (Object) changes

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.



527
528
529
530
531
532
533
534
# File 'ext/sqlite3/database.c', line 527

static VALUE changes(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return INT2NUM(sqlite3_changes(ctx->db));
}

- (Object) close

Closes this database.



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/sqlite3/database.c', line 134

static VALUE sqlite3_rb_close(VALUE self)
{
  sqlite3RubyPtr ctx;
  sqlite3 * db;
  Data_Get_Struct(self, sqlite3Ruby, ctx);

  db = ctx->db;
  CHECK(db, sqlite3_close(ctx->db));

  ctx->db = NULL;

  return self;
}

- (Boolean) closed?

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

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
# File 'ext/sqlite3/database.c', line 152

static VALUE closed_p(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);

  if(!ctx->db) return Qtrue;

  return Qfalse;
}

- (Object) collation(name, comparator)

Add a collation with name name, and a comparator object. The comparator object should implement a method called "compare" that takes two parameters and returns an integer less than, equal to, or greater than 0.



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'ext/sqlite3/database.c', line 648

static VALUE collation(VALUE self, VALUE name, VALUE comparator)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  CHECK(ctx->db, sqlite3_create_collation(
        ctx->db,
        StringValuePtr(name),
        SQLITE_UTF8,
        (void *)comparator,
        NIL_P(comparator) ? NULL : rb_comparator_func));

  /* Make sure our comparator doesn't get garbage collected. */
  rb_hash_aset(rb_iv_get(self, "@collations"), name, comparator);

  return self;
}

- (Object) commit

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.



512
513
514
515
# File 'lib/sqlite3/database.rb', line 512

def commit
  execute "commit transaction"
  true
end

- (Boolean) complete?(sql)

Return true if the string is a valid (ie, parsable) SQL statement, and false otherwise.

Returns:

  • (Boolean)


513
514
515
516
517
518
519
# File 'ext/sqlite3/database.c', line 513

static VALUE complete_p(VALUE UNUSED(self), VALUE sql)
{
  if(sqlite3_complete(StringValuePtr(sql)))
    return Qtrue;

  return Qfalse;
}

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

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#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.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.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/sqlite3/database.rb', line 363

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

  factory = Class.new do
    def self.step( &block )
      define_method(:step, &block)
    end

    def self.finalize( &block )
      define_method(:finalize, &block)
    end
  end

  if block_given?
    factory.instance_eval(&block)
  else
    factory.class_eval do
      define_method(:step, step)
      define_method(:finalize, finalize)
    end
  end

  proxy = factory.new
  proxy.extend(Module.new {
    attr_accessor :ctx

    def step( *args )
      super(@ctx, *args)
    end

    def finalize
      super(@ctx)
    end
  })
  proxy.ctx = FunctionProxy.new
  define_aggregator(name, proxy)
end

- (Object) create_aggregate_handler(handler)

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.result = @total
  end
end

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


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/sqlite3/database.rb', line 447

def create_aggregate_handler( handler )
  proxy = Class.new do
    def initialize klass
      @klass = klass
      @fp    = FunctionProxy.new
    end

    def step( *args )
      instance.step(@fp, *args)
    end

    def finalize
      instance.finalize @fp
      @instance = nil
      @fp.result
    end

    private

    def instance
      @instance ||= @klass.new
    end
  end
  define_aggregator(handler.name, proxy.new(handler))
  self
end

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

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#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" )


318
319
320
321
322
323
324
325
# File 'lib/sqlite3/database.rb', line 318

def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
  define_function(name) do |*args|
    fp = FunctionProxy.new
    block.call(fp, *args)
    fp.result
  end
  self
end

- (Object) define_aggregator(name, aggregator)

Define an aggregate function named name using the object aggregator. aggregator must respond to step and finalize. step will be called with row information and finalize must return the return value for the aggregator function.



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'ext/sqlite3/database.c', line 437

static VALUE define_aggregator(VALUE self, VALUE name, VALUE aggregator)
{
  sqlite3RubyPtr ctx;
  int arity, status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  arity = sqlite3_obj_method_arity(aggregator, rb_intern("step"));

  status = sqlite3_create_function(
    ctx->db,
    StringValuePtr(name),
    arity,
    SQLITE_UTF8,
    (void *)aggregator,
    NULL,
    rb_sqlite3_step,
    rb_sqlite3_final
  );

  rb_iv_set(self, "@agregator", aggregator);

  CHECK(ctx->db, status);

  return self;
}

- (Object) define_function(name) {|args, ...| ... }

Define a function named name with args. The arity of the block will be used as the arity for the function defined.

Yields:

  • (args, ...)


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'ext/sqlite3/database.c', line 370

static VALUE define_function(VALUE self, VALUE name)
{
  sqlite3RubyPtr ctx;
  VALUE block;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  block = rb_block_proc();

  status = sqlite3_create_function(
    ctx->db,
    StringValuePtr(name),
    rb_proc_arity(block),
    SQLITE_UTF8,
    (void *)block,
    rb_sqlite3_func,
    NULL,
    NULL
  );

  CHECK(ctx->db, status);

  rb_hash_aset(rb_iv_get(self, "@functions"), name, block);

  return self;
}

- (Object) enable_load_extension(onoff)

Enable or disable extension loading.



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'ext/sqlite3/database.c', line 700

static VALUE enable_load_extension(VALUE self, VALUE onoff)
{
  sqlite3RubyPtr ctx;
  int onoffparam;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  if (Qtrue == onoff) {
    onoffparam = 1;
  } else if (Qfalse == onoff) {
    onoffparam = 0;
  } else {
    onoffparam = (int)NUM2INT(onoff);
  }

  CHECK(ctx->db, sqlite3_enable_load_extension(ctx->db, onoffparam));

  return self;
}

- (Object) encoding

Fetch the encoding set on this database



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'ext/sqlite3/database.c', line 747

static VALUE db_encoding(VALUE self)
{
  sqlite3RubyPtr ctx;
  VALUE enc;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  enc = rb_iv_get(self, "@encoding");

  if(NIL_P(enc)) {
    sqlite3_exec(ctx->db, "PRAGMA encoding", enc_cb, (void *)self, NULL);
  }

  return rb_iv_get(self, "@encoding");
}

- (Object) errcode

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



499
500
501
502
503
504
505
506
# File 'ext/sqlite3/database.c', line 499

static VALUE errcode_(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return INT2NUM((long)sqlite3_errcode(ctx->db));
}

- (Object) errmsg

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



485
486
487
488
489
490
491
492
# File 'ext/sqlite3/database.c', line 485

static VALUE errmsg(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return rb_str_new2(sqlite3_errmsg(ctx->db));
}

- (Object) execute(sql, bind_vars = [], *args, &block)

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sqlite3/database.rb', line 115

def execute sql, bind_vars = [], *args, &block
  # FIXME: This is a terrible hack and should be removed but is required
  # for older versions of rails
  hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/

  if bind_vars.nil? || !args.empty?
    if args.empty?
      bind_vars = []
    else
      bind_vars = [bind_vars] + args
    end

    warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
without using an array.  Please switch to passing bind parameters as an array.
Support for bind parameters as *args will be removed in 2.0.0.
    eowarn
  end

  prepare( sql ) do |stmt|
    stmt.bind_params(bind_vars)
    columns = stmt.columns
    stmt    = ResultSet.new(self, stmt).to_a if type_translation

    if block_given?
      stmt.each do |row|
        if @results_as_hash
          yield type_translation ? row : ordered_map_for(columns, row)
        else
          yield row
        end
      end
    else
      if @results_as_hash
        stmt.map { |row|
          h = type_translation ? row : ordered_map_for(columns, row)

          # FIXME UGH TERRIBLE HACK!
          h['unique'] = h['unique'].to_s if hack

          h
        }
      else
        stmt.to_a
      end
    end
  end
end

- (Object) execute2(sql, *bind_vars)

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.



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/sqlite3/database.rb', line 174

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

- (Object) execute_batch(sql, bind_vars = [], *args)

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.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/sqlite3/database.rb', line 195

def execute_batch( sql, bind_vars = [], *args )
  # FIXME: remove this stuff later
  unless [Array, Hash].include?(bind_vars.class)
    bind_vars = [bind_vars]
    warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
that are not a list of a hash.  Please switch to passing bind parameters as an
array or hash. Support for this behavior will be removed in version 2.0.0.
    eowarn
  end

  # FIXME: remove this stuff later
  if bind_vars.nil? || !args.empty?
    if args.empty?
      bind_vars = []
    else
      bind_vars = [nil] + args
    end

    warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
without using an array.  Please switch to passing bind parameters as an array.
Support for this behavior will be removed in version 2.0.0.
    eowarn
  end

  sql = sql.strip
  until sql.empty? do
    prepare( sql ) do |stmt|
      # FIXME: this should probably use sqlite3's api for batch execution
      # This implementation requires stepping over the results.
      if bind_vars.length == stmt.bind_parameter_count
        stmt.bind_params(bind_vars)
      end
      stmt.step
      sql = stmt.remainder.strip
    end
  end
  nil
end

- (Object) get_first_row(sql, *bind_vars)

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.



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

def get_first_row( sql, *bind_vars )
  execute( sql, *bind_vars ).first
end

- (Object) get_first_value(sql, *bind_vars)

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.



288
289
290
291
# File 'lib/sqlite3/database.rb', line 288

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

- (Object) interrupt

Interrupts the currently executing operation, causing it to abort.



469
470
471
472
473
474
475
476
477
478
# File 'ext/sqlite3/database.c', line 469

static VALUE interrupt(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  sqlite3_interrupt(ctx->db);

  return self;
}

- (Object) last_insert_row_id

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



263
264
265
266
267
268
269
270
# File 'ext/sqlite3/database.c', line 263

static VALUE last_insert_row_id(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return LL2NUM(sqlite3_last_insert_rowid(ctx->db));
}

- (Object) load_extension(file)

Loads an SQLite extension library from the named file. Extension loading must be enabled using db.enable_load_extension(true) prior to calling this API.



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/sqlite3/database.c', line 674

static VALUE load_extension(VALUE self, VALUE file)
{
  sqlite3RubyPtr ctx;
  int status;
  char *errMsg;
  VALUE errexp;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  status = sqlite3_load_extension(ctx->db, RSTRING_PTR(file), 0, &errMsg);
  if (status != SQLITE_OK)
  {
    errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
    sqlite3_free(errMsg);
    rb_exc_raise(errexp);
  }

  return self;
}

- (Object) prepare(sql)

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.



90
91
92
93
94
95
96
97
98
99
# File 'lib/sqlite3/database.rb', line 90

def prepare sql
  stmt = SQLite3::Statement.new( self, sql )
  return stmt unless block_given?

  begin
    yield stmt
  ensure
    stmt.close
  end
end

- (Object) query(sql, bind_vars = [], *args)

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.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/sqlite3/database.rb', line 247

def query( sql, bind_vars = [], *args )

  if bind_vars.nil? || !args.empty?
    if args.empty?
      bind_vars = []
    else
      bind_vars = [bind_vars] + args
    end

    warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
without using an array.  Please switch to passing bind parameters as an array.
Support for this will be removed in version 2.0.0.
    eowarn
  end

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

- (Boolean) readonly?

Returns true if the database has been open in readonly mode A helper to check before performing any operation

Returns:

  • (Boolean)


528
529
530
# File 'lib/sqlite3/database.rb', line 528

def readonly?
  @readonly
end

- (Object) rollback

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.



521
522
523
524
# File 'lib/sqlite3/database.rb', line 521

def rollback
  execute "rollback transaction"
  true
end

- (Object) total_changes

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



167
168
169
170
171
172
173
174
# File 'ext/sqlite3/database.c', line 167

static VALUE total_changes(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return INT2NUM((long)sqlite3_total_changes(ctx->db));
}

- (Object) trace {|sql| ... } - (Object) trace(Class.new{)

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

Overloads:

  • - (Object) trace {|sql| ... }

    Yields:

    • (sql)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/sqlite3/database.c', line 191

static VALUE trace(int argc, VALUE *argv, VALUE self)
{
  sqlite3RubyPtr ctx;
  VALUE block;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  rb_scan_args(argc, argv, "01", &block);

  if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();

  rb_iv_set(self, "@tracefunc", block);

  sqlite3_trace(ctx->db, NIL_P(block) ? NULL : tracefunc, (void *)self);

  return self;
}

- (Object) transaction(mode = :deferred)

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.



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/sqlite3/database.rb', line 490

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

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

  true
end

- (Boolean) transaction_active?

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

Returns:

  • (Boolean)


769
770
771
772
773
774
775
776
# File 'ext/sqlite3/database.c', line 769

static VALUE transaction_active_p(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return sqlite3_get_autocommit(ctx->db) ? Qfalse : Qtrue;
}

- (Object) translator

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.)



73
74
75
# File 'lib/sqlite3/database.rb', line 73

def translator
  @translator ||= Translator.new
end