Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

Re-exporting types for easier access by users of the library. Represents a database connection with its URL, type, and connection pool.

This struct provides a unified interface for interacting with different database systems supported by the rusticx library.

Implementations§

Source§

impl Connection

Source

pub fn new(url: &str) -> Result<Self, RusticxError>

Creates a new Connection instance based on the provided database URL.

This function determines the database type from the URL scheme and attempts to establish a connection using the appropriate driver.

§Arguments
  • url: The database connection string (e.g., “postgres://…”, “mysql://…”, “sqlite://…”).
§Returns

Returns a Result containing the initialized Connection on success, or a RusticxError if the URL is invalid or connection fails.

Source

pub fn create_table<T: SQLModel>(&self) -> Result<(), RusticxError>

Creates a table in the database based on the provided SQL model definition.

This function uses the SQLModel trait to generate the appropriate CREATE TABLE SQL statement for the current database type and executes it.

§Type Parameters
  • T: The type representing the SQL model, which must implement SQLModel.
§Returns

Returns Ok(()) on successful table creation, or a RusticxError if the SQL generation or execution fails.

Source

pub fn execute( &self, sql: &str, params: &[&(dyn ToSql + Sync + 'static)], ) -> Result<u64, RusticxError>

Executes a SQL command (INSERT, UPDATE, DELETE, CREATE, DROP, etc.) with the provided parameters.

This function is typically used for commands that do not return a result set. The number of affected rows (where applicable) is returned.

§Arguments
  • sql: The SQL query string to execute.
  • params: A slice of references to values to be used as query parameters. The specific type required depends on the database driver (e.g., &(dyn ToSql + Sync + 'static) for postgres).
§Returns

Returns a Result containing the number of rows affected on success, or a RusticxError if the execution fails. Note that the meaning and availability of “rows affected” can vary between database drivers.

§Errors

Returns a RusticxError::QueryError on database query execution failure or RusticxError::ConnectionError if the connection pool is not initialized.

Source

pub fn query_raw<T>( &self, sql: &str, params: &[&(dyn ToSql + Sync + 'static)], ) -> Result<Vec<T>, RusticxError>
where T: for<'de> Deserialize<'de> + Debug,

Executes a raw SQL query (typically SELECT) and returns the results as a vector of deserialized objects.

This function queries the database and attempts to map the rows from the result set into instances of the specified type T.

§Type Parameters
  • T: The target type to deserialize the rows into. Must implement serde::Deserialize<'de> and Debug.
§Arguments
  • sql: The SQL query string (e.g., “SELECT id, name FROM users WHERE age > $1”).
  • params: A slice of references to values to be used as query parameters. The specific type required depends on the database driver.
§Returns

Returns a Result containing a Vec<T> on success, where each element corresponds to a row from the result set, or a RusticxError if the query or deserialization fails.

§Errors

Returns a RusticxError::QueryError on database query execution failure, RusticxError::SerializationError if deserialization fails, or RusticxError::ConnectionError if the connection pool is not initialized.

Source

pub async fn transaction<F, R>( &self, transaction_fn: F, ) -> Result<R, RusticxError>
where F: FnOnce(&dyn TransactionExecutor) -> Result<R, RusticxError> + Send + 'static, R: Send + 'static,

Executes a database transaction using the provided transaction function.

This function manages the transaction lifecycle (begin, commit/rollback) and executes the code defined in the transaction_fn closure within the transaction’s scope. The closure receives a TransactionExecutor which allows performing database operations within the transaction.

§Type Parameters
  • F: The type of the closure that defines the transaction logic. Must implement FnOnce(&dyn TransactionExecutor) -> Result<R, RusticxError>, Send, and 'static.
  • R: The return type of the transaction function. Must implement Send and 'static.
§Arguments
  • transaction_fn: The closure containing the database operations to be executed within the transaction.
§Returns

Returns a Result containing the value R returned by the transaction function on successful commit, or a RusticxError if the transaction fails or is rolled back.

§Errors

Returns RusticxError::TransactionError on transaction management failures, or RusticxError::ConnectionError if the connection pool is not initialized.

Source

pub fn get_db_type(&self) -> &DatabaseType

Returns a reference to the database type of this connection.

§Returns

A reference to the DatabaseType enum indicating the connected database.

Trait Implementations§

Source§

impl Clone for Connection

Source§

fn clone(&self) -> Connection

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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