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
impl Connection
Sourcepub fn new(url: &str) -> Result<Self, RusticxError>
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.
Sourcepub fn create_table<T: SQLModel>(&self) -> Result<(), RusticxError>
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 implementSQLModel.
§Returns
Returns Ok(()) on successful table creation, or a RusticxError
if the SQL generation or execution fails.
Sourcepub fn execute(
&self,
sql: &str,
params: &[&(dyn ToSql + Sync + 'static)],
) -> Result<u64, RusticxError>
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.
Sourcepub fn query_raw<T>(
&self,
sql: &str,
params: &[&(dyn ToSql + Sync + 'static)],
) -> Result<Vec<T>, RusticxError>where
T: for<'de> Deserialize<'de> + Debug,
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 implementserde::Deserialize<'de>andDebug.
§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.
Sourcepub 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,
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 implementFnOnce(&dyn TransactionExecutor) -> Result<R, RusticxError>,Send, and'static.R: The return type of the transaction function. Must implementSendand'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.
Sourcepub fn get_db_type(&self) -> &DatabaseType
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
impl Clone for Connection
Source§fn clone(&self) -> Connection
fn clone(&self) -> Connection
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more