Skip to main content

DatabaseAdapter

Trait DatabaseAdapter 

Source
pub trait DatabaseAdapter: Send + Sync {
Show 13 methods // Required methods fn name(&self) -> &str; fn backend(&self) -> DatabaseBackend; fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<QueryResult>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fetch_all<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<Vec<JsonValue>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fetch_one<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<Option<JsonValue>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn ping<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn as_any(&self) -> &dyn Any; fn clone_box(&self) -> Box<dyn DatabaseAdapter>; // Provided methods fn query(&self) -> QueryBuilder { ... } fn type_backend(&self) -> DatabaseBackend { ... } fn begin_transaction<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait { ... } fn commit<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait { ... } fn rollback<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait { ... }
}
Expand description

Unified database adapter trait

This trait provides a common interface for all database backends, enabling RustF to work with PostgreSQL, MySQL, and SQLite through the same API.

Required Methods§

Source

fn name(&self) -> &str

Get the name of this database connection

Source

fn backend(&self) -> DatabaseBackend

Get the database backend type

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<QueryResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a query that modifies data (INSERT, UPDATE, DELETE)

§Arguments
  • sql - The SQL query to execute
  • params - Parameters for the query
§Returns
  • Ok(QueryResult) - Result with affected rows and last insert ID
  • Err(Error) - If the query fails
Source

fn fetch_all<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<Vec<JsonValue>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch all rows from a SELECT query

§Arguments
  • sql - The SQL SELECT query
  • params - Parameters for the query
§Returns
  • Ok(Vec<JsonValue>) - All matching rows as JSON values
  • Err(Error) - If the query fails
Source

fn fetch_one<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, params: Vec<SqlValue>, ) -> Pin<Box<dyn Future<Output = Result<Option<JsonValue>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Fetch a single row from a SELECT query

§Arguments
  • sql - The SQL SELECT query
  • params - Parameters for the query
§Returns
  • Ok(Some(JsonValue)) - The first matching row
  • Ok(None) - If no rows match
  • Err(Error) - If the query fails
Source

fn ping<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Test database connectivity

§Returns
  • Ok(true) - Database is connected and responding
  • Err(Error) - Connection test failed
Source

fn as_any(&self) -> &dyn Any

Get the underlying connection pool as Any for downcasting

This allows code that knows the specific database type to access the underlying sqlx pool directly when needed.

Source

fn clone_box(&self) -> Box<dyn DatabaseAdapter>

Clone the adapter into a boxed trait object

Provided Methods§

Source

fn query(&self) -> QueryBuilder

Create a new query builder for this database

§Returns

A QueryBuilder configured for this database’s SQL dialect

Source

fn type_backend(&self) -> DatabaseBackend

Get the database backend as the type system’s DatabaseBackend

Source

fn begin_transaction<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Begin a transaction (optional, can return error for unsupported databases)

§Returns
  • Ok(()) - Transaction started
  • Err(Error) - If transactions aren’t supported or fail to start
Source

fn commit<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Commit a transaction

Source

fn rollback<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Rollback a transaction

Trait Implementations§

Source§

impl Clone for Box<dyn DatabaseAdapter>

Extension trait for Arc to enable cloning

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§