Skip to main content

Archivist

Struct Archivist 

Source
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

Source

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.

Source

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

Source

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.

Source

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.

Source

pub async fn exists<T: TableItem>( &self, id: i32, ) -> Result<bool, ArchivistError>

Checks whether a row with id exists in table.

§Errors

Forwards errors from sqlx.

Source

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.

Source

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.

Source

pub async fn get<T: TableItem>(&self, id: i32) -> Result<T, ArchivistError>

Gets an item whose id you know.

§Errors

Forwards errors from sqlx.

Source

pub async fn get_all<T: TableItem>(&self) -> Result<Vec<T>, ArchivistError>

Gets all items from T::TABLE.

§Errors

Forwards errors from sqlx.

Source

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.

Source

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.

Source

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.

Source

pub async fn update_from_cache<T: TableItem>( &mut self, cache: &T, id: i32, ) -> Result<(), ArchivistError>

Updates all columns for a the row with the supplied id.

§Errors

Forwards errors from sqlx.

Source

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.

Source

pub async fn get_special<T: TableItem, U>( &self, columns: &str, condition: &str, ) -> Result<Option<U>, ArchivistError>
where for<'r> U: FromRow<'r, PgRow> + Send + Unpin,

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.

Source

pub const fn config(&self) -> &Config

The current configuration.

Trait Implementations§

Source§

impl Debug for Archivist

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,