SQLiteDriver

Struct SQLiteDriver 

Source
pub struct SQLiteDriver {
    pub name: String,
    pub options: SQLiteDriverOptions,
    pub table: String,
    pub database: Connection,
}
Expand description

SQLite database driver for storing and managing JSON data.

The SQLiteDriver provides methods for interacting with an SQLite database, including adding, retrieving, updating, and deleting JSON data in a specified table.

It abstracts the database operations and allows the user to interact with the database as if it were a key-value store, with the data being stored as serialised JSON.

§Fields

  • name: The name of the SQLite database file.
  • options: Configuration options for the SQLite driver, including the database file name and table name.
  • table: The name of the table in the SQLite database to operate on.
  • database: The connection to the SQLite database.

Fields§

§name: String

The name of the SQLite database file.

§options: SQLiteDriverOptions

Configuration options for the SQLite driver, including the database file and table name

§table: String

The name of the table in the SQLite database to operate on.

§database: Connection

The connection to the SQLite database.

Implementations§

Source§

impl SQLiteDriver

Source

pub fn new(options: Option<SQLiteDriverOptions>) -> Result<Self>

Creates a new instance of the SQLiteDriver with the provided options. If no options are provided, it defaults to using json.sqlite as the database file and json as the table name.

§Parameters
  • options: Optional configuration options for the SQLite database.
§Returns

A Result containing either the SQLiteDriver instance or an error.

Source

pub fn prepare(&self, table: &str) -> Result<()>

Prepares the SQLite database by creating the table if it doesn’t already exist.

§Parameters
  • table: The name of the table to create.
§Returns

A Result indicating success or failure.

Source

pub fn add(&self, key: &str, value: f64) -> Result<f64>

Adds a value to an existing entry or creates a new entry if it doesn’t exist. The value is added to the current value of the entry (if it exists).

§Parameters
  • key: The key for the entry to update.
  • value: The value to add to the current entry.
§Returns

The new value after adding value to the existing entry, or an error if the value is not finite (e.g., NaN or infinity).

Source

pub fn all(&self) -> Result<Vec<(String, Value)>>

Retrieves all data entries from the database as a vector of tuples.

§Returns

A Result containing a vector of tuples where each tuple consists of a key (String) and a corresponding value (serde_json::Value).

Source

pub fn delete(&self, key: &str) -> Result<bool>

Deletes a specific entry by key. If the key refers to a nested value, it will remove the nested field within the JSON data.

§Parameters
  • key: The key of the entry to delete.
§Returns

A Result indicating whether the deletion was successful.

Source

pub fn delete_all(&self) -> Result<bool>

Deletes all entries in the database.

§Returns

A Result indicating whether the deletion was successful.

Source

pub fn get<T>(&self, key: &str) -> Result<Option<T>>

Retrieves the value for a given key, potentially deserialising it into the specified type.

§Parameters
  • key: The key of the entry to retrieve.
§Returns

A Result containing an Option of the deserialised value, or an error if the deserialisation fails.

Source

pub fn has(&self, key: &str) -> Result<bool>

Checks if a given key exists in the database.

§Parameters
  • key: The key to check.
§Returns

A Result containing a boolean indicating whether the key exists.

Source

pub fn pull<T>(&self, key: &str, value: T) -> Result<Vec<T>>

Removes a specific value from an array stored at the given key.

§Parameters
  • key: The key of the entry where the array is stored.
  • value: The value to remove from the array.
§Returns

A Result containing the updated array after removal.

Source

pub fn push<T>(&self, key: &str, value: T) -> Result<Vec<T>>

Appends a value to an array stored at the given key.

§Parameters
  • key: The key of the entry where the array is stored.
  • value: The value to append to the array.
§Returns

A Result containing the updated array after the value is appended.

Source

pub fn set<T>(&self, key: &str, value: T) -> Result<()>
where T: Serialize,

Sets or updates the value for a given key in the database.

§Parameters
  • key: The key for the entry.
  • value: The value to store, which will be serialised into JSON.
§Returns

A Result containing the value that was set.

Source

pub fn subtract(&self, key: &str, value: f64) -> Result<f64>

Subtracts a value from an existing entry. If the entry does not exist, it initialises it with the result.

§Parameters
  • key: The key of the entry to subtract from.
  • value: The value to subtract from the current value.
§Returns

The new value after subtraction.

Trait Implementations§

Source§

impl Debug for SQLiteDriver

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, 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, 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.