Class | SQLite3::Database |
In: |
lib/sqlite3/database.rb
ext/sqlite3/backup.c |
Parent: | Object |
call-seq: db.encoding
Fetch the encoding set on this database
new | -> | open |
busy_timeout= | -> | busy_timeout |
collations | [R] | |
results_as_hash | [RW] | A boolean that indicates whether rows in result sets should be returned as hashes or not. By default, rows are returned as arrays. |
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.
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.
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.
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.
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.
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.
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.
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.
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" )
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" )
Define a function named name with args. The arity of the block will be used as the arity for the function defined.
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.
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.
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.
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.
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.
Obtains the unique row ID of the last row to be inserted by this Database instance.
Loads an SQLite extension library from the named file. Extension loading must be enabled using db.enable_load_extension(1) prior to calling this API.
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.
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.
Returns true if the database has been open in readonly mode A helper to check before performing any operation
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.
Returns the total number of changes made to this database instance since it was opened.
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.
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.
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.)