Class | Amalgalite::Database |
In: |
lib/amalgalite/database.rb
lib/amalgalite/database.rb |
Parent: | Object |
The encapsulation of a connection to an SQLite3 database.
Example opening and possibly creating a new database
db = Amalgalite::Database.new( "mydb.db" ) db.execute( "SELECT * FROM table" ) do |row| puts row end db.close
Open a database read only:
db = Amalgalite::Database.new( "mydb.db", "r" )
Open an in-memory database:
db = Amalgalite::MemoryDatabase.new
VALID_MODES | = | { "r" => Open::READONLY, "r+" => Open::READWRITE, "w+" => Open::READWRITE | Open::CREATE, } | list of valid modes for opening an Amalgalite::Database | |
VALID_MODES | = | { "r" => Open::READONLY, "r+" => Open::READWRITE, "w+" => Open::READWRITE | Open::CREATE, } | list of valid modes for opening an Amalgalite::Database |
aggregates | [R] | A list of the user defined aggregates |
aggregates | [R] | A list of the user defined aggregates |
api | [R] | the low level Amalgalite::SQLite3::Database |
api | [R] | the low level Amalgalite::SQLite3::Database |
functions | [R] | A list of the user defined functions |
functions | [R] | A list of the user defined functions |
profile_tap | [R] | An object that follows the ProfileTap protocol, or nil. By default this is nil |
profile_tap | [R] | An object that follows the ProfileTap protocol, or nil. By default this is nil |
trace_tap | [R] | An object that follows the TraceTap protocol, or nil. By default this is nil |
trace_tap | [R] | An object that follows the TraceTap protocol, or nil. By default this is nil |
type_map | [R] | An object that follows the TypeMap protocol, or nil. By default this is an instances of TypeMaps::DefaultMap |
type_map | [R] | An object that follows the TypeMap protocol, or nil. By default this is an instances of TypeMaps::DefaultMap |
Create a new Amalgalite database
The first parameter is the filename of the sqlite database. Specifying ":memory:" as the filename creates an in-memory database.
The second parameter is the standard file modes of how to open a file.
The modes are:
w+ is the default as this is how most databases will want to be utilized.
opts is a hash of available options for the database:
By default, databases are created with an encoding of utf8. Setting this to true and opening an already existing database has no effect.
NOTE Currently :utf16 is not supported by Amalgalite, it is planned for a later release
Create a new Amalgalite database
The first parameter is the filename of the sqlite database. Specifying ":memory:" as the filename creates an in-memory database.
The second parameter is the standard file modes of how to open a file.
The modes are:
w+ is the default as this is how most databases will want to be utilized.
opts is a hash of available options for the database:
By default, databases are created with an encoding of utf8. Setting this to true and opening an already existing database has no effect.
NOTE Currently :utf16 is not supported by Amalgalite, it is planned for a later release
Define an SQL aggregate function, these are functions like max(), min(), avg(), etc. SQL functions that would be used when a GROUP BY clause is in effect. See also ::Amalgalite::Aggregate.
A new instance of MyAggregateClass is created for each instance that the SQL aggregate is mentioned in SQL.
Define an SQL aggregate function, these are functions like max(), min(), avg(), etc. SQL functions that would be used when a GROUP BY clause is in effect. See also ::Amalgalite::Aggregate.
A new instance of MyAggregateClass is created for each instance that the SQL aggregate is mentioned in SQL.
Register a busy handler for this database connection, the handler MUST follow the to_proc protocol indicating that is will +respond_to?(:call)+. This is intrinsic to lambdas and blocks so those will work automatically.
This exposes the sqlite busy handler api to ruby.
The busy handler‘s _call(N)_ method may be invoked whenever an attempt is made to open a database table that another thread or process has locked. N will be the number of times the _call(N)_ method has been invoked during this locking event.
The handler may or maynot be called based upon what SQLite determins.
If the handler returns nil or false then no more busy handler calls will be made in this lock event and you are probably going to see an SQLite::Error in your immediately future in another process or in another piece of code.
If the handler returns non-nil or non-false then another attempt will be made to obtain the lock, lather, rinse, repeat.
If an Exception happens in a busy handler, it will be the same as if the busy handler had returned nil or false. The exception itself will not be propogated further.
Register a busy handler for this database connection, the handler MUST follow the to_proc protocol indicating that is will +respond_to?(:call)+. This is intrinsic to lambdas and blocks so those will work automatically.
This exposes the sqlite busy handler api to ruby.
The busy handler‘s _call(N)_ method may be invoked whenever an attempt is made to open a database table that another thread or process has locked. N will be the number of times the _call(N)_ method has been invoked during this locking event.
The handler may or maynot be called based upon what SQLite determins.
If the handler returns nil or false then no more busy handler calls will be made in this lock event and you are probably going to see an SQLite::Error in your immediately future in another process or in another piece of code.
If the handler returns non-nil or non-false then another attempt will be made to obtain the lock, lather, rinse, repeat.
If an Exception happens in a busy handler, it will be the same as if the busy handler had returned nil or false. The exception itself will not be propogated further.
register a callback to be exposed as an SQL function. There are multiple ways to register this function:
See also ::Amalgalite::Function
register a callback to be exposed as an SQL function. There are multiple ways to register this function:
See also ::Amalgalite::Function
Register a progress handler for this database connection, the handler MUST follow the to_proc protocol indicating that is will +respond_to?(:call)+. This is intrinsic to lambdas and blocks so those will work automatically.
This exposes the sqlite progress handler api to ruby.
The progress handler‘s _call()_ method may be invoked ever N SQLite op codes. If the progress handler returns anything that can evaluate to true then current running sqlite statement is terminated at the earliest oppportunity.
You can use this to be notified that a thread is still processingn a request.
Register a progress handler for this database connection, the handler MUST follow the to_proc protocol indicating that is will +respond_to?(:call)+. This is intrinsic to lambdas and blocks so those will work automatically.
This exposes the sqlite progress handler api to ruby.
The progress handler‘s _call()_ method may be invoked ever N SQLite op codes. If the progress handler returns anything that can evaluate to true then current running sqlite statement is terminated at the earliest oppportunity.
You can use this to be notified that a thread is still processingn a request.
Execute a single SQL statement.
If called with a block and there are result rows, then they are iteratively yielded to the block.
If no block is passed, then all the results are returned as an arrayfields instance. This is an array with field name access.
If no block is passed, and there are no results, then an empty Array is returned.
On an error an exception is thrown
This is just a wrapper around the preparation of an Amalgalite Statement and iterating over the results.
Execute a single SQL statement.
If called with a block and there are result rows, then they are iteratively yielded to the block.
If no block is passed, then all the results are returned as an arrayfields instance. This is an array with field name access.
If no block is passed, and there are no results, then an empty Array is returned.
On an error an exception is thrown
This is just a wrapper around the preparation of an Amalgalite Statement and iterating over the results.
Execute a batch of statements, this will execute all the sql in the given string until no more sql can be found in the string. It will bind the same parameters to each statement. All data that would be returned from all of the statements is thrown away.
All statements to be executed in the batch must be terminated with a ’;’ Returns the number of statements executed
Execute a batch of statements, this will execute all the sql in the given string until no more sql can be found in the string. It will bind the same parameters to each statement. All data that would be returned from all of the statements is thrown away.
All statements to be executed in the batch must be terminated with a ’;’ Returns the number of statements executed
Execute a sql statment, and only return the first row of results. This is a shorthand method when you only want a single row of results from a query. If there is no result, then return an empty array
It is in all other was, exactly like execute()
Execute a sql statment, and only return the first row of results. This is a shorthand method when you only want a single row of results from a query. If there is no result, then return an empty array
It is in all other was, exactly like execute()
Execute an sql statement, and return only the first column of the first row. If there is no result, return nil.
It is in all other ways, exactly like first_row_from()
Execute an sql statement, and return only the first column of the first row. If there is no result, return nil.
It is in all other ways, exactly like first_row_from()
import_csv_to_table() takes 2 required arguments, and a hash of options. The first argument is the path to a CSV, the second is the table in which to load the data. The options has is a subset of those used by CSV
This may also be an Array. If that is the case then the array is used as the fields in the CSV and the fields in the table in which to insert. If this is set to an Array, it is assumed that all rows in the csv will be inserted.
import_csv_to_table() takes 2 required arguments, and a hash of options. The first argument is the path to a CSV, the second is the table in which to load the data. The options has is a subset of those used by CSV
This may also be an Array. If that is the case then the array is used as the fields in the CSV and the fields in the table in which to insert. If this is set to an Array, it is assumed that all rows in the csv will be inserted.
Cause another thread with a handle on this database to be interrupted and return at the earliest opportunity as interrupted. It is not safe to call this method if the database might be closed before interrupt! returns.
Cause another thread with a handle on this database to be interrupted and return at the earliest opportunity as interrupted. It is not safe to call this method if the database might be closed before interrupt! returns.
Prepare a statement for execution
If called with a block, the statement is yielded to the block and the statement is closed when the block is done.
db.prepare( "SELECT * FROM table WHERE c = ?" ) do |stmt| list_of_c_values.each do |c| stmt.execute( c ) do |row| puts "when c = #{c} : #{row.inspect}" end end end
Or without a block:
stmt = db.prepare( "INSERT INTO t1(x, y, z) VALUES ( :
Prepare a statement for execution
If called with a block, the statement is yielded to the block and the statement is closed when the block is done.
db.prepare( "SELECT * FROM table WHERE c = ?" ) do |stmt| list_of_c_values.each do |c| stmt.execute( c ) do |row| puts "when c = #{c} : #{row.inspect}" end end end
Or without a block:
stmt = db.prepare( "INSERT INTO t1(x, y, z) VALUES ( :
Register a profile tap.
Registering a profile tap means that the obj registered will have its profile method called with an Integer and a String parameter every time a profile event happens. The Integer is the number of nanoseconds it took for the String (SQL) to execute in wall-clock time.
That is, every time a profile event happens in SQLite the following is invoked:
obj.profile( str, int )
For instance:
db.profile_tap = Amalgalite::ProfileTap.new( logger, 'debug' )
This will register an instance of ProfileTap, which wraps an logger object. On each profile event the ProfileTap#profile method will be called which in turn will call <tt>logger.debug<tt> with a formatted string containing the String and Integer from the profile event.
db.profile_tap = nil
This will unregister the profile tap
Register a profile tap.
Registering a profile tap means that the obj registered will have its profile method called with an Integer and a String parameter every time a profile event happens. The Integer is the number of nanoseconds it took for the String (SQL) to execute in wall-clock time.
That is, every time a profile event happens in SQLite the following is invoked:
obj.profile( str, int )
For instance:
db.profile_tap = Amalgalite::ProfileTap.new( logger, 'debug' )
This will register an instance of ProfileTap, which wraps an logger object. On each profile event the ProfileTap#profile method will be called which in turn will call <tt>logger.debug<tt> with a formatted string containing the String and Integer from the profile event.
db.profile_tap = nil
This will unregister the profile tap
Release a savepoint. This is similar to a commit but only for savepoints. All savepoints up the savepoint stack and include the name savepoint being released are ‘committed’ to the transaction. There are several ways of thinking about release and they are all detailed in the sqlite documentation: sqlite.org/lang_savepoint.html
Release a savepoint. This is similar to a commit but only for savepoints. All savepoints up the savepoint stack and include the name savepoint being released are ‘committed’ to the transaction. There are several ways of thinking about release and they are all detailed in the sqlite documentation: sqlite.org/lang_savepoint.html
Remove an aggregate from use in the database. Since the same aggregate may be refistered more than once with different arity, you may specify the arity, or the aggregate class, or nil. If nil is used for the arity then Amalgalite does its best to remove all aggregates of the given name
Remove an aggregate from use in the database. Since the same aggregate may be refistered more than once with different arity, you may specify the arity, or the aggregate class, or nil. If nil is used for the arity then Amalgalite does its best to remove all aggregates of the given name
Remove a function from use in the database. Since the same function may be registered more than once with different arity, you may specify the arity, or the function object, or nil. If nil is used for the arity, then Amalgalite does its best to remove all functions of given name.
Remove a function from use in the database. Since the same function may be registered more than once with different arity, you may specify the arity, or the function object, or nil. If nil is used for the arity, then Amalgalite does its best to remove all functions of given name.
replicate_to() takes a single argument, either a String or an Amalgalite::Database. It returns the replicated database object. If given a String, it will truncate that database if it already exists.
Replicate the current database to another location, this can be used for a number of purposes:
replicate_to() takes a single argument, either a String or an Amalgalite::Database. It returns the replicated database object. If given a String, it will truncate that database if it already exists.
Replicate the current database to another location, this can be used for a number of purposes:
Rollback to a savepoint. The transaction is not cancelled, the transaction is restarted.
Rollback to a savepoint. The transaction is not cancelled, the transaction is restarted.
Much of the following documentation is para-phrased from sqlite.org/lang_savepoint.html
Savepoints are a method of creating transactions, similar to transaction except that they may be nested.
If no block is passed in then you are on your own.
Much of the following documentation is para-phrased from sqlite.org/lang_savepoint.html
Savepoints are a method of creating transactions, similar to transaction except that they may be nested.
If no block is passed in then you are on your own.
Returns a Schema object containing the table and column structure of the database.
Returns a Schema object containing the table and column structure of the database.
Register a trace tap.
Registering a trace tap measn that the obj registered will have its trace method called with a string parameter at various times. If the object doesn‘t respond to the trace method then write will be called.
For instance:
db.trace_tap = Amalgalite::TraceTap.new( logger, 'debug' )
This will register an instance of TraceTap, which wraps an logger object. On each trace event the TraceTap#trace method will be called, which in turn will call the logger.debug method
db.trace_tap = $stderr
This will register the $stderr io stream as a trace tap. Every time a trace event happens then $stderr.write( msg ) will be called.
db.trace_tap = nil
This will unregistere the trace tap
Register a trace tap.
Registering a trace tap measn that the obj registered will have its trace method called with a string parameter at various times. If the object doesn‘t respond to the trace method then write will be called.
For instance:
db.trace_tap = Amalgalite::TraceTap.new( logger, 'debug' )
This will register an instance of TraceTap, which wraps an logger object. On each trace event the TraceTap#trace method will be called, which in turn will call the logger.debug method
db.trace_tap = $stderr
This will register the $stderr io stream as a trace tap. Every time a trace event happens then $stderr.write( msg ) will be called.
db.trace_tap = nil
This will unregistere the trace tap
Begin a transaction. The valid transaction types are:
DEFERRED: | no read or write locks are created until the first statement is executed that requries a read or a write |
IMMEDIATE: | a readlock is obtained immediately so that no other process can write to the database |
EXCLUSIVE: | a read+write lock is obtained, no other proces can read or write to the database |
As a convenience, these are constants available in the Database::TransactionBehavior class.
Amalgalite Transactions are database level transactions, just as SQLite‘s are.
If a block is passed in, then when the block exits, it is guaranteed that either ‘COMMIT’ or ‘ROLLBACK’ has been executed.
If any exception happens during the transaction that is caught by Amalgalite, then a ‘ROLLBACK’ is issued when the block closes.
If no exception happens during the transaction then a ‘COMMIT’ is issued upon leaving the block.
If no block is passed in then you are on your own.
Nesting a transaaction via the transaction method are no-ops. If you call transaction within a transaction, no new transaction is started, the current one is just continued.
True nexted transactions are available through the savepoint method.
Begin a transaction. The valid transaction types are:
DEFERRED: | no read or write locks are created until the first statement is executed that requries a read or a write |
IMMEDIATE: | a readlock is obtained immediately so that no other process can write to the database |
EXCLUSIVE: | a read+write lock is obtained, no other proces can read or write to the database |
As a convenience, these are constants available in the Database::TransactionBehavior class.
Amalgalite Transactions are database level transactions, just as SQLite‘s are.
If a block is passed in, then when the block exits, it is guaranteed that either ‘COMMIT’ or ‘ROLLBACK’ has been executed.
If any exception happens during the transaction that is caught by Amalgalite, then a ‘ROLLBACK’ is issued when the block closes.
If no exception happens during the transaction then a ‘COMMIT’ is issued upon leaving the block.
If no block is passed in then you are on your own.
Nesting a transaaction via the transaction method are no-ops. If you call transaction within a transaction, no new transaction is started, the current one is just continued.
True nexted transactions are available through the savepoint method.