pub struct Namespace { /* private fields */ }Expand description
The part of a Store that one part of the bot uses.
Get one with Store::namespace. Clones share the same connection.
Implementations§
Source§impl Namespace
impl Namespace
Sourcepub async fn sql<R, F>(&self, f: F) -> Result<R, StoreError>
pub async fn sql<R, F>(&self, f: F) -> Result<R, StoreError>
Run f with the database connection, on Tokio’s blocking pool.
f gets the full rusqlite::Connection, so it can prepare
statements and start transactions. Calls from all namespaces of a store
run one at a time. A call that waits for the connection does not
occupy a thread of the blocking pool.
All namespaces of a store share this connection. The store sets its
connection-wide settings: the journal mode, the busy timeout, and the
authorizer, which Namespace::migrate uses while it runs. Do not
change these settings in f. For example, migrate removes an
authorizer that f installs.
§Errors
Returns StoreError::Sql if f returns an error, and
StoreError::Task if f panics.
Sourcepub async fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, StoreError>
pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, StoreError>
Get the value of key, decoded from JSON into T.
Returns None if the namespace has no value for key.
§Errors
Returns StoreError::Decode if the stored value does not decode
into T, and StoreError::Sql if SQLite cannot read it.
Sourcepub async fn set<T: Serialize + ?Sized>(
&self,
key: &str,
value: &T,
) -> Result<(), StoreError>
pub async fn set<T: Serialize + ?Sized>( &self, key: &str, value: &T, ) -> Result<(), StoreError>
Set the value of key to value, encoded as JSON.
A value that key already has is replaced.
§Errors
Returns StoreError::Encode if value does not encode as JSON, and
StoreError::Sql if SQLite cannot write it.
Sourcepub async fn delete(&self, key: &str) -> Result<bool, StoreError>
pub async fn delete(&self, key: &str) -> Result<bool, StoreError>
Delete the value of key.
Returns true if key had a value.
§Errors
Returns StoreError::Sql if SQLite cannot delete the value.
Sourcepub async fn keys(&self, prefix: &str) -> Result<Vec<String>, StoreError>
pub async fn keys(&self, prefix: &str) -> Result<Vec<String>, StoreError>
The keys that start with prefix, in byte order.
The comparison uses the bytes of prefix. Thus % and _ have no
special meaning, and a key that contains a NUL also matches. An empty
prefix gives all keys of the namespace.
§Errors
Returns StoreError::Sql if SQLite cannot read the keys.
Sourcepub async fn migrate(&self, steps: &[&str]) -> Result<(), StoreError>
pub async fn migrate(&self, steps: &[&str]) -> Result<(), StoreError>
Apply the steps of steps that the database does not have yet.
The store records how many steps each namespace has. A call applies only the steps after that number, in order, and records the new number. Thus it is safe to call this at each start of the bot with the full list.
A step can hold more than one SQL statement. All steps of one call run
in one transaction: if a step fails, the call applies no step. Thus a
step cannot use a statement that SQLite refuses in a transaction, for
example VACUUM. A step also cannot use BEGIN, COMMIT, END or
ROLLBACK, because these end the transaction of the call. The call
refuses such a step. SAVEPOINT, RELEASE and ROLLBACK TO are
permitted.
§Errors
Returns StoreError::Migration if a step fails or controls the
transaction, and
StoreError::UnknownMigrations if the database has more steps than
steps.