SQLite3 is a wrapper around the popular database sqlite.
For an example of usage, see SQLite3::Database.
# File lib/sqlite3/version.rb, line 18 def self.const_missing(name) return super unless name == :Version warn("#{caller[0]}: SQLite::Version will be removed in sqlite3-ruby version 2.0.0 ") if $VERBOSE VersionProxy end
static VALUE libversion(VALUE UNUSED(klass))
{
return INT2NUM(sqlite3_libversion_number());
}
# File lib/sqlite3/database.rb, line 449 def initialize handler @handler = handler @fp = FunctionProxy.new end
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.
# File lib/sqlite3/database.rb, line 505 def commit execute "commit transaction" true end
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" )
# File lib/sqlite3/database.rb, line 447 def create_aggregate_handler( handler ) proxy = Class.new do def initialize handler @handler = handler @fp = FunctionProxy.new end def step( *args ) @handler.step(@fp, *args) end def finalize @handler.finalize @fp @fp.result end end define_aggregator(handler.name, proxy.new(handler.new)) self end
# File lib/sqlite3/database.rb, line 458 def finalize @handler.finalize @fp @fp.result end
Returns true if the database has been open in readonly mode A
helper to check before performing any operation
# File lib/sqlite3/database.rb, line 521 def readonly? @readonly end
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.
# File lib/sqlite3/database.rb, line 514 def rollback execute "rollback transaction" true end
# File lib/sqlite3/database.rb, line 454 def step( *args ) @handler.step(@fp, *args) end
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.
# File lib/sqlite3/database.rb, line 483 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
# File lib/sqlite3/database.rb, line 573 def ordered_map_for columns, row h = Hash[*columns.zip(row).flatten] row.each_with_index { |r, i| h[i] = r } h end