pub struct Sql {
pub(crate) dbfile: PathBuf,
pool: RwLock<Option<Pool>>,
is_encrypted: RwLock<Option<bool>>,
pub(crate) config_cache: RwLock<HashMap<String, Option<String>>>,
}
Expand description
A wrapper around the underlying Sqlite3 object.
Fields§
§dbfile: PathBuf
Database file path
pool: RwLock<Option<Pool>>
SQL connection pool.
is_encrypted: RwLock<Option<bool>>
None if the database is not open, true if it is open with passphrase and false if it is open without a passphrase.
config_cache: RwLock<HashMap<String, Option<String>>>
Cache of config
table.
Implementations§
source§impl Sql
impl Sql
async fn set_db_version(&self, version: i32) -> Result<()>
fn set_db_version_trans( transaction: &mut Transaction<'_>, version: i32, ) -> Result<()>
async fn set_db_version_in_cache(&self, version: i32) -> Result<()>
async fn execute_migration(&self, query: &str, version: i32) -> Result<()>
source§impl Sql
impl Sql
sourcepub async fn check_passphrase(&self, passphrase: String) -> Result<bool>
pub async fn check_passphrase(&self, passphrase: String) -> Result<bool>
Tests SQLCipher passphrase.
Returns true if passphrase is correct, i.e. the database is new or can be unlocked with this passphrase, and false if the database is already encrypted with another passphrase or corrupted.
Fails if database is already open.
sourcepub async fn is_open(&self) -> bool
pub async fn is_open(&self) -> bool
Checks if there is currently a connection to the underlying Sqlite database.
sourcepub(crate) async fn is_encrypted(&self) -> Option<bool>
pub(crate) async fn is_encrypted(&self) -> Option<bool>
Returns true if the database is encrypted.
If database is not open, returns None
.
sourcepub(crate) async fn import(&self, path: &Path, passphrase: String) -> Result<()>
pub(crate) async fn import(&self, path: &Path, passphrase: String) -> Result<()>
Imports the database from a separate file with the given passphrase.
async fn try_open( &self, context: &Context, dbfile: &Path, passphrase: String, ) -> Result<()>
sourcepub async fn run_migrations(&self, context: &Context) -> Result<()>
pub async fn run_migrations(&self, context: &Context) -> Result<()>
Updates SQL schema to the latest version.
sourcepub async fn open(&self, context: &Context, passphrase: String) -> Result<()>
pub async fn open(&self, context: &Context, passphrase: String) -> Result<()>
Opens the provided database and runs any necessary migrations. If a database is already open, this will return an error.
sourcepub async fn change_passphrase(&self, passphrase: String) -> Result<()>
pub async fn change_passphrase(&self, passphrase: String) -> Result<()>
Changes the passphrase of encrypted database.
The database must already be encrypted and the passphrase cannot be empty. It is impossible to turn encrypted database into unencrypted and vice versa this way, use import/export for this.
sourceasync fn call<'a, F, R>(&'a self, query_only: bool, function: F) -> Result<R>
async fn call<'a, F, R>(&'a self, query_only: bool, function: F) -> Result<R>
Allocates a connection and calls function
with the connection.
If query_only
is true, allocates read-only connection,
otherwise allocates write connection.
Returns the result of the function.
sourcepub async fn call_write<'a, F, R>(&'a self, function: F) -> Result<R>
pub async fn call_write<'a, F, R>(&'a self, function: F) -> Result<R>
Allocates a connection and calls given function, assuming it does write queries, with the connection.
Returns the result of the function.
sourcepub async fn execute(
&self,
query: &str,
params: impl Params + Send,
) -> Result<usize>
pub async fn execute( &self, query: &str, params: impl Params + Send, ) -> Result<usize>
Execute query
assuming it is a write query, returning the number of affected rows.
sourcepub async fn insert(
&self,
query: &str,
params: impl Params + Send,
) -> Result<i64>
pub async fn insert( &self, query: &str, params: impl Params + Send, ) -> Result<i64>
Executes the given query, returning the last inserted row ID.
sourcepub async fn query_map<T, F, G, H>(
&self,
sql: &str,
params: impl Params + Send,
f: F,
g: G,
) -> Result<H>
pub async fn query_map<T, F, G, H>( &self, sql: &str, params: impl Params + Send, f: F, g: G, ) -> Result<H>
Prepares and executes the statement and maps a function over the resulting rows. Then executes the second function over the returned iterator and returns the result of that function.
sourcepub async fn count(
&self,
query: &str,
params: impl Params + Send,
) -> Result<usize>
pub async fn count( &self, query: &str, params: impl Params + Send, ) -> Result<usize>
Used for executing SELECT COUNT
statements only. Returns the resulting count.
sourcepub async fn exists(
&self,
sql: &str,
params: impl Params + Send,
) -> Result<bool>
pub async fn exists( &self, sql: &str, params: impl Params + Send, ) -> Result<bool>
Used for executing SELECT COUNT
statements only. Returns true
, if the count is at least
one, false
otherwise.
sourcepub async fn query_row<T, F>(
&self,
query: &str,
params: impl Params + Send,
f: F,
) -> Result<T>
pub async fn query_row<T, F>( &self, query: &str, params: impl Params + Send, f: F, ) -> Result<T>
Execute a query which is expected to return one row.
sourcepub async fn transaction<G, H>(&self, callback: G) -> Result<H>
pub async fn transaction<G, H>(&self, callback: G) -> Result<H>
Execute the function inside a transaction assuming that it does write queries.
If the function returns an error, the transaction will be rolled back. If it does not return an error, the transaction will be committed.
sourcepub async fn table_exists(&self, name: &str) -> Result<bool>
pub async fn table_exists(&self, name: &str) -> Result<bool>
Query the database if the requested table already exists.
sourcepub async fn col_exists(&self, table_name: &str, col_name: &str) -> Result<bool>
pub async fn col_exists(&self, table_name: &str, col_name: &str) -> Result<bool>
Check if a column exists in a given table.
sourcepub async fn query_row_optional<T, F>(
&self,
sql: &str,
params: impl Params + Send,
f: F,
) -> Result<Option<T>>
pub async fn query_row_optional<T, F>( &self, sql: &str, params: impl Params + Send, f: F, ) -> Result<Option<T>>
Execute a query which is expected to return zero or one row.
sourcepub async fn query_get_value<T>(
&self,
query: &str,
params: impl Params + Send,
) -> Result<Option<T>>where
T: FromSql + Send + 'static,
pub async fn query_get_value<T>(
&self,
query: &str,
params: impl Params + Send,
) -> Result<Option<T>>where
T: FromSql + Send + 'static,
Executes a query which is expected to return one row and one
column. If the query does not return any rows, returns Ok(None)
.
sourcepub async fn set_raw_config(&self, key: &str, value: Option<&str>) -> Result<()>
pub async fn set_raw_config(&self, key: &str, value: Option<&str>) -> Result<()>
Set private configuration options.
Setting None
deletes the value. On failure an error message
will already have been logged.
sourcepub async fn get_raw_config(&self, key: &str) -> Result<Option<String>>
pub async fn get_raw_config(&self, key: &str) -> Result<Option<String>>
Get configuration options from the database.
sourcepub async fn set_raw_config_int(&self, key: &str, value: i32) -> Result<()>
pub async fn set_raw_config_int(&self, key: &str, value: i32) -> Result<()>
Sets configuration for the given key to 32-bit signed integer value.
sourcepub async fn get_raw_config_int(&self, key: &str) -> Result<Option<i32>>
pub async fn get_raw_config_int(&self, key: &str) -> Result<Option<i32>>
Returns 32-bit signed integer configuration value for the given key.
sourcepub async fn get_raw_config_u32(&self, key: &str) -> Result<Option<u32>>
pub async fn get_raw_config_u32(&self, key: &str) -> Result<Option<u32>>
Returns 32-bit unsigned integer configuration value for the given key.
sourcepub async fn get_raw_config_bool(&self, key: &str) -> Result<bool>
pub async fn get_raw_config_bool(&self, key: &str) -> Result<bool>
Returns boolean configuration value for the given key.
sourcepub async fn set_raw_config_bool(&self, key: &str, value: bool) -> Result<()>
pub async fn set_raw_config_bool(&self, key: &str, value: bool) -> Result<()>
Sets configuration for the given key to boolean value.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Sql
impl !RefUnwindSafe for Sql
impl Send for Sql
impl Sync for Sql
impl Unpin for Sql
impl !UnwindSafe for Sql
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.