pub struct Transaction<'db> { /* private fields */ }Expand description
A RAII wrapper around transactions.
The transaction can be created using Connection::transaction, and finalized using commit or rollback. If no finalization method is used, the transaction will automatically be rolled back when it is dropped. For convenience, Transaction derefs to Connection.
Note that transactions affect the entire database connection. If other threads need to be prevented from interfering with a transaction, Connection::lock should be used.
This interface is optional. It’s permitted to execute BEGIN, COMMIT, and ROLLBACK statements directly, without using this interface. This interface
Implementations§
Source§impl<'db> Transaction<'db>
impl<'db> Transaction<'db>
Sourcepub fn commit(self) -> Result<&'db Connection>
pub fn commit(self) -> Result<&'db Connection>
Consumes the Transaction, committing it.
Sourcepub fn rollback(self) -> Result<&'db Connection>
pub fn rollback(self) -> Result<&'db Connection>
Consumes the Transaction, rolling it back.
Sourcepub fn savepoint(&mut self) -> Result<Transaction<'_>>
pub fn savepoint(&mut self) -> Result<Transaction<'_>>
Create a savepoint for the current transaction. This functions identically to a transaction, but committing or rolling back will only affect statements since the savepoint was created.
Methods from Deref<Target = Connection>§
Sourcepub unsafe fn as_mut_ptr(&self) -> *mut sqlite3
pub unsafe fn as_mut_ptr(&self) -> *mut sqlite3
Get the underlying SQLite handle.
§Safety
Using the returned pointer may cause undefined behavior in other, safe code.
Sourcepub fn load_extension(&self, path: &str, entry: Option<&str>) -> Result<()>
pub fn load_extension(&self, path: &str, entry: Option<&str>) -> Result<()>
Load the extension at the given path, optionally providing a specific entry point.
§Safety
Loading libraries can cause undefined behavior in safe code. It is the caller’s responsibility to ensure that the extension is compatible with sqlite3_ext. In particular, the caller must verify that the extension being loaded (and the entry point being invoked) is actually an SQLite extension (created with sqlite3_ext or otherwise). Never invoke this method with untrusted user-specified data.
If extension loading is not enabled when this method is called and SQLite is at least version 3.13.0, then extension loading will temporarily be enabled before loading the extension, and disabled afterwards. On older versions of SQLite, extension loading must be manually enabled using unsafe ffi functions before this method can be used, see sqlite3_enable_load_extension for details.
Requires SQLite 3.8.7.
Sourcepub fn db_config_defensive(&self, enable: bool) -> Result<()>
pub fn db_config_defensive(&self, enable: bool) -> Result<()>
Enable or disable the “defensive” flag for the database.
See SQLITE_DBCONFIG_DEFENSIVE for details.
Requires SQLite 3.26.0. On earlier versions, this method is a no-op.
Sourcepub fn dump_prepared_statements(&self)
pub fn dump_prepared_statements(&self)
Prints the text of all currently prepared statements to stderr. Intended for debugging.
Sourcepub fn create_overloaded_function(
&self,
name: &str,
opts: &FunctionOptions,
) -> Result<()>
pub fn create_overloaded_function( &self, name: &str, opts: &FunctionOptions, ) -> Result<()>
Create a stub function that always fails.
This API makes sure a global version of a function with a particular name and number of parameters exists. If no such function exists before this API is called, a new function is created. The implementation of the new function always causes an exception to be thrown. So the new function is not good for anything by itself. Its only purpose is to be a placeholder function that can be overloaded by a virtual table.
For more information, see vtab::FindFunctionVTab.
Sourcepub fn create_scalar_function<F>(
&self,
name: &str,
opts: &FunctionOptions,
func: F,
) -> Result<()>
pub fn create_scalar_function<F>( &self, name: &str, opts: &FunctionOptions, func: F, ) -> Result<()>
Create a new scalar function. The function will be invoked with a Context and an array of ValueRef objects. The function is required to set its output using Context::set_result. If no result is set, SQL NULL is returned. If the function returns an Err value, the SQL statement will fail, even if a result had been set.
The passed function can be a closure, however the lifetime of the closure must be
'static due to limitations in the Rust borrow checker. The
Self::create_scalar_function_object function is an alternative that allows using an
alternative lifetime.
§Compatibility
On versions of SQLite earlier than 3.7.3, this function will leak the function and all bound variables. This is because these versions of SQLite did not provide the ability to specify a destructor function.
Sourcepub fn create_scalar_function_object<'db, F>(
&'db self,
name: &str,
opts: &FunctionOptions,
func: F,
) -> Result<()>where
F: ScalarFunction<'db>,
pub fn create_scalar_function_object<'db, F>(
&'db self,
name: &str,
opts: &FunctionOptions,
func: F,
) -> Result<()>where
F: ScalarFunction<'db>,
Create a new scalar function using a struct. This function is identical to
Self::create_scalar_function, but uses a trait object instead of a closure. This enables
creating scalar functions that maintain references with a lifetime smaller than 'static.
Sourcepub fn create_legacy_aggregate_function<U, F: LegacyAggregateFunction<U>>(
&self,
name: &str,
opts: &FunctionOptions,
user_data: U,
) -> Result<()>
pub fn create_legacy_aggregate_function<U, F: LegacyAggregateFunction<U>>( &self, name: &str, opts: &FunctionOptions, user_data: U, ) -> Result<()>
Create a new aggregate function which cannot be used as a window function.
In general, you should use create_aggregate_function instead, which provides all of the same features as legacy aggregate functions but also support WINDOW.
§Compatibility
On versions of SQLite earlier than 3.7.3, this function will leak the user data. This is because these versions of SQLite did not provide the ability to specify a destructor function.
Sourcepub fn create_aggregate_function<U, F: AggregateFunction<U>>(
&self,
name: &str,
opts: &FunctionOptions,
user_data: U,
) -> Result<()>
pub fn create_aggregate_function<U, F: AggregateFunction<U>>( &self, name: &str, opts: &FunctionOptions, user_data: U, ) -> Result<()>
Create a new aggregate function.
§Compatibility
Window functions require SQLite 3.25.0. On earlier versions of SQLite, this function will automatically fall back to create_legacy_aggregate_function.
Sourcepub fn remove_function(&self, name: &str, n_args: i32) -> Result<()>
pub fn remove_function(&self, name: &str, n_args: i32) -> Result<()>
Remove an application-defined scalar or aggregate function. The name and n_args parameters must match the values used when the function was created.
Sourcepub fn create_collation<F: Fn(&str, &str) -> Ordering>(
&self,
name: &str,
func: F,
) -> Result<()>
pub fn create_collation<F: Fn(&str, &str) -> Ordering>( &self, name: &str, func: F, ) -> Result<()>
Register a new collating sequence.
Sourcepub fn set_collation_needed_func<F: Fn(&str)>(&self, func: F) -> Result<()>
pub fn set_collation_needed_func<F: Fn(&str)>(&self, func: F) -> Result<()>
Register a callback for when SQLite needs a collation sequence. The function will be invoked when a collation sequence is needed, and create_collation can be used to provide the needed sequence.
Note: the provided function and any captured variables will be leaked. SQLite does not provide any facilities for cleaning up this data.
Sourcepub fn lock(&self) -> SQLiteMutexGuard<'_, Connection>
pub fn lock(&self) -> SQLiteMutexGuard<'_, Connection>
Locks the mutex associated with this database connection. If multiple SQLite APIs need to be used and there is a chance that this Connection may be used from multiple threads, this method should be used to lock the Connection to the calling thread. The returned mutex guard will unlock the mutex when it is dropped, and derefs to the original connection.
This method has no effect if SQLite is not operating in serialized threading mode.
Sourcepub fn prepare_first<'a>(
&self,
sql: &'a str,
) -> Result<(Option<Statement>, &'a str)>
pub fn prepare_first<'a>( &self, sql: &'a str, ) -> Result<(Option<Statement>, &'a str)>
Prepare some SQL for execution. This method will return the prepared statement and a slice containing the portion of the original input which was after the first SQL statement.
Sourcepub fn prepare(&self, sql: &str) -> Result<Statement>
pub fn prepare(&self, sql: &str) -> Result<Statement>
Prepare some SQL for execution. This method will return Err(SQLITE_MISUSE) if the input string does not contain any SQL statements.
Sourcepub fn query<P>(&self, sql: &str, params: P) -> Result<Statement>where
P: Params,
pub fn query<P>(&self, sql: &str, params: P) -> Result<Statement>where
P: Params,
Convenience method to prepare a query and bind it with values. See Statement::query.
Sourcepub fn query_row<P, R, F>(&self, sql: &str, params: P, f: F) -> Result<R>
pub fn query_row<P, R, F>(&self, sql: &str, params: P, f: F) -> Result<R>
Convenience method for self.prepare(sql)?.query_row(params, f). See
Statement::query_row.
Sourcepub fn execute<P: Params>(&self, sql: &str, params: P) -> Result<i64>
pub fn execute<P: Params>(&self, sql: &str, params: P) -> Result<i64>
Convenience method for self.prepare(sql)?.execute(params). See Statement::execute.
Sourcepub fn insert<P: Params>(&self, sql: &str, params: P) -> Result<i64>
pub fn insert<P: Params>(&self, sql: &str, params: P) -> Result<i64>
Convenience method for self.prepare(sql)?.insert(params). See Statement::insert.
Sourcepub fn transaction(&self, tt: TransactionType) -> Result<Transaction<'_>>
pub fn transaction(&self, tt: TransactionType) -> Result<Transaction<'_>>
Starts a new transaction with the specified behavior.