pub struct Archivist { /* private fields */ }Expand description
This keeps a live connection to the database and acts as your friend in getting and posting data.
For any queries that modify the DB, a transaction will be used, and if the user has not explicitly started one, a warning will be issued (though not an error). NB: If a transaction goes out of scope, it is rolled back.
All tables are accessible only through the Table enum.
Implementations§
Source§impl Archivist
impl Archivist
Sourcepub async fn new(
config_path: impl AsRef<Path>,
sql_setup_dir: impl AsRef<Path>,
) -> Result<Self, ARPAError>
pub async fn new( config_path: impl AsRef<Path>, sql_setup_dir: impl AsRef<Path>, ) -> Result<Self, ARPAError>
Initializes a new connection to the database.
§Errors
Fails if setup data is missing. Forwards errors from sqlx.
Sourcepub async fn start_transaction(&mut self) -> Result<(), ArchivistError>
pub async fn start_transaction(&mut self) -> Result<(), ArchivistError>
Starts a new transaction. Returns an error if there is a previous transaction still live.
§Errors
Fails if there is already a live transaction
Sourcepub async fn commit_transaction(&mut self) -> Result<(), ArchivistError>
pub async fn commit_transaction(&mut self) -> Result<(), ArchivistError>
Commits a currently live transaction. Returns an error if there is none present.
§Errors
Fails if there is no live transaction. Forwards errors from sqlx.
Sourcepub async fn rollback_transaction(&mut self) -> Result<(), ArchivistError>
pub async fn rollback_transaction(&mut self) -> Result<(), ArchivistError>
Undos a currently live transaction. Returns an error if there is none present.
§Errors
Fails if there is no live transaction. Forwards errors from sqlx.
Sourcepub async fn assert_exists<T: TableItem>(
&self,
id: i32,
) -> Result<(), ArchivistError>
pub async fn assert_exists<T: TableItem>( &self, id: i32, ) -> Result<(), ArchivistError>
Same as exists, but returns a result instead of an option.
§Errors
Fails if the id does not exist. Forwards errors from sqlx.
Sourcepub async fn assert_unique<T: TableItem>(
&self,
item: &T,
) -> Result<(), ArchivistError>
pub async fn assert_unique<T: TableItem>( &self, item: &T, ) -> Result<(), ArchivistError>
Returns an error if the provided item collides with anything.
§Errors
Fails if there is a collision. Forwards errors from sqlx.
Sourcepub async fn find<T: TableItem>(
&self,
condition: &str,
) -> Result<Option<T>, ArchivistError>
pub async fn find<T: TableItem>( &self, condition: &str, ) -> Result<Option<T>, ArchivistError>
Finds an item from T::TABLE, fulfilling a where-condition.
This is essentially just wrapping a query like select T from TABLE where CONDITION;.
Due to the flexibility in the condition parameter, this is not run
via any macro, and as such cannot be compile-time tested. Use
responsibly.
§Errors
Forwards errors from sqlx.
Sourcepub async fn insert<T: TableItem>(
&mut self,
item: T,
) -> Result<i32, ArchivistError>
pub async fn insert<T: TableItem>( &mut self, item: T, ) -> Result<i32, ArchivistError>
Adds a new entry to T::TABLE, making sure no unique fields are
duplicated.
Returns the id of the newly inserted item.
§Errors
Fails if there are collisions in the table. Forwards errors from sqlx.
Sourcepub async fn update<T: TableItem>(
&mut self,
id: i32,
value: &str,
) -> Result<(), ArchivistError>
pub async fn update<T: TableItem>( &mut self, id: i32, value: &str, ) -> Result<(), ArchivistError>
Update an entry with the given id in the given table. value in
this case is a string like number = 2, i.e. both the column and the
actual value.
Remember that string values need to be incased in single quotes.
Due to the flexibility in the value parameter, this is not run via
any macro, and as such cannot be compile-time tested. Use responsibly.
§Errors
Forwards errors from sqlx.
Sourcepub async fn update_from_cache<T: TableItem>(
&mut self,
cache: &T,
id: i32,
) -> Result<(), ArchivistError>
pub async fn update_from_cache<T: TableItem>( &mut self, cache: &T, id: i32, ) -> Result<(), ArchivistError>
Sourcepub async fn delete<T: TableItem>(
&mut self,
id: i32,
) -> Result<(), ArchivistError>
pub async fn delete<T: TableItem>( &mut self, id: i32, ) -> Result<(), ArchivistError>
Deletes an item from a table. Make sure you are providing the correct type, as there is no way of checking your intentions!
§Errors
Fails if id does not exist. Forwards errors from sqlx.
Sourcepub async fn get_special<T: TableItem, U>(
&self,
columns: &str,
condition: &str,
) -> Result<Option<U>, ArchivistError>
pub async fn get_special<T: TableItem, U>( &self, columns: &str, condition: &str, ) -> Result<Option<U>, ArchivistError>
Gets the indicated values from table, for one row if it meets
condition.
This may be preferred if you want only a specific value instead of the whole item, or a value that is not present in the rust-end struct, but is stored in the table (e.g. a password hash).
Due to the flexibility in the parameters, this is not run via any macro, and as such cannot be compile-time tested. Use responsibly.
§Errors
Forwards errors from sqlx.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Archivist
impl !RefUnwindSafe for Archivist
impl Send for Archivist
impl Sync for Archivist
impl Unpin for Archivist
impl !UnwindSafe for Archivist
Blanket Implementations§
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more