Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for sea-orm #280

Merged
merged 21 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Document the database drivers
  • Loading branch information
charleschege committed Oct 29, 2021
commit e4d115b5b0195b3654fcbbe41e8ce4de8f47ee96
21 changes: 21 additions & 0 deletions src/driver/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,43 @@ use std::{
},
};

/// Defines a database driver for the [MockDatabase]
#[derive(Debug)]
pub struct MockDatabaseConnector;

/// Defines a connection for the [MockDatabase]
#[derive(Debug)]
pub struct MockDatabaseConnection {
counter: AtomicUsize,
mocker: Mutex<Box<dyn MockDatabaseTrait>>,
}

/// A set of constraints for any type wanting to perform operations on the [MockDatabase]
pub trait MockDatabaseTrait: Send + Debug {
/// Execute a statement in the [MockDatabase]
fn execute(&mut self, counter: usize, stmt: Statement) -> Result<ExecResult, DbErr>;

/// Execute a SQL query in the [MockDatabase]
fn query(&mut self, counter: usize, stmt: Statement) -> Result<Vec<QueryResult>, DbErr>;

/// Create a transaction that can be committed atomically
fn begin(&mut self);

/// Commit a successful transaction atomically into the [MockDatabase]
fn commit(&mut self);

/// Roll back a transaction since errors were encountered
fn rollback(&mut self);

/// Get all logs from a [MockDatabase] and return a [Transaction]
fn drain_transaction_log(&mut self) -> Vec<Transaction>;

/// Get the backend being used in the [MockDatabase]
fn get_database_backend(&self) -> DbBackend;
}

impl MockDatabaseConnector {
/// Check if the database URI given and the [DatabaseBackend](crate::DatabaseBackend) selected are the same
#[allow(unused_variables)]
pub fn accepts(string: &str) -> bool {
#[cfg(feature = "sqlx-mysql")]
Expand All @@ -55,6 +66,7 @@ impl MockDatabaseConnector {
false
}

/// Cpnnect to the [MockDatabase]
#[allow(unused_variables)]
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
macro_rules! connect_mock_db {
Expand Down Expand Up @@ -82,6 +94,7 @@ impl MockDatabaseConnector {
}

impl MockDatabaseConnection {
/// Create a connection to the [MockDatabase]
pub fn new<M: 'static>(m: M) -> Self
where
M: MockDatabaseTrait,
Expand All @@ -96,29 +109,34 @@ impl MockDatabaseConnection {
&self.mocker
}

/// Get the [DatabaseBackend](crate::DatabaseBackend) being used by the [MockDatabase]
pub fn get_database_backend(&self) -> DbBackend {
self.mocker.lock().unwrap().get_database_backend()
}

/// Execute the SQL statement in the [MockDatabase]
pub fn execute(&self, statement: Statement) -> Result<ExecResult, DbErr> {
debug_print!("{}", statement);
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
self.mocker.lock().unwrap().execute(counter, statement)
}

/// Return one [QueryResult] if the query was successful
pub fn query_one(&self, statement: Statement) -> Result<Option<QueryResult>, DbErr> {
debug_print!("{}", statement);
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
let result = self.mocker.lock().unwrap().query(counter, statement)?;
Ok(result.into_iter().next())
}

/// Return all [QueryResult]s if the query was successful
pub fn query_all(&self, statement: Statement) -> Result<Vec<QueryResult>, DbErr> {
debug_print!("{}", statement);
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
self.mocker.lock().unwrap().query(counter, statement)
}

/// Return [QueryResult]s from a multi-query operation
pub fn fetch(
&self,
statement: &Statement,
Expand All @@ -129,14 +147,17 @@ impl MockDatabaseConnection {
}
}

/// Create a statement block of SQL statements that execute together.
pub fn begin(&self) {
self.mocker.lock().unwrap().begin()
}

/// Commit a transaction atomically to the database
pub fn commit(&self) {
self.mocker.lock().unwrap().commit()
}

/// Roll back a faulty transaction
pub fn rollback(&self) {
self.mocker.lock().unwrap().rollback()
}
Expand Down
2 changes: 2 additions & 0 deletions src/driver/sqlx_common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::DbErr;

/// Converts an [sqlx::error] execution error to a [DbErr]
pub fn sqlx_error_to_exec_err(err: sqlx::Error) -> DbErr {
DbErr::Exec(err.to_string())
}

/// Converts an [sqlx::error] query error to a [DbErr]
pub fn sqlx_error_to_query_err(err: sqlx::Error) -> DbErr {
DbErr::Query(err.to_string())
}
11 changes: 11 additions & 0 deletions src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ use crate::{

use super::sqlx_common::*;

/// Defines the [sqlx::mysql] connector
#[derive(Debug)]
pub struct SqlxMySqlConnector;

/// Defines a sqlx MySQL pool
#[derive(Debug, Clone)]
pub struct SqlxMySqlPoolConnection {
pool: MySqlPool,
}

impl SqlxMySqlConnector {
/// Check if the URI provided corresponds to `mysql://` for a MySQL database
pub fn accepts(string: &str) -> bool {
string.starts_with("mysql://") && string.parse::<MySqlConnectOptions>().is_ok()
}

/// Add configuration options for the MySQL database
pub async fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let mut opt = options
.url
Expand All @@ -48,12 +52,14 @@ impl SqlxMySqlConnector {
}

impl SqlxMySqlConnector {
/// Instantiate a sqlx pool connection to a [DatabaseConnection]
pub fn from_sqlx_mysql_pool(pool: MySqlPool) -> DatabaseConnection {
DatabaseConnection::SqlxMySqlPoolConnection(SqlxMySqlPoolConnection { pool })
}
}

impl SqlxMySqlPoolConnection {
/// Execute a [Statement] on a MySQL backend
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -70,6 +76,7 @@ impl SqlxMySqlPoolConnection {
}
}

/// Get one result from a SQL query. Returns [Option::None] if no match was found
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -89,6 +96,7 @@ impl SqlxMySqlPoolConnection {
}
}

/// Get the results of a query returning them as a Vec<[QueryResult]>
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -105,6 +113,7 @@ impl SqlxMySqlPoolConnection {
}
}

/// Stream the results of executing a SQL query
pub async fn stream(&self, stmt: Statement) -> Result<QueryStream, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -117,6 +126,7 @@ impl SqlxMySqlPoolConnection {
}
}

/// Bundle a set of SQL statements that execute together.
pub async fn begin(&self) -> Result<DatabaseTransaction, DbErr> {
if let Ok(conn) = self.pool.acquire().await {
DatabaseTransaction::new_mysql(conn).await
Expand All @@ -127,6 +137,7 @@ impl SqlxMySqlPoolConnection {
}
}

/// Create a MySQL transaction
pub async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
where
F: for<'b> FnOnce(
Expand Down
11 changes: 11 additions & 0 deletions src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ use crate::{

use super::sqlx_common::*;

/// Defines the [sqlx::postgres] connector
#[derive(Debug)]
pub struct SqlxPostgresConnector;

/// Defines a sqlx PostgreSQL pool
#[derive(Debug, Clone)]
pub struct SqlxPostgresPoolConnection {
pool: PgPool,
}

impl SqlxPostgresConnector {
/// Check if the URI provided corresponds to `postgres://` for a PostgreSQL database
pub fn accepts(string: &str) -> bool {
string.starts_with("postgres://") && string.parse::<PgConnectOptions>().is_ok()
}

/// Add configuration options for the MySQL database
pub async fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let mut opt = options
.url
Expand All @@ -48,12 +52,14 @@ impl SqlxPostgresConnector {
}

impl SqlxPostgresConnector {
/// Instantiate a sqlx pool connection to a [DatabaseConnection]
pub fn from_sqlx_postgres_pool(pool: PgPool) -> DatabaseConnection {
DatabaseConnection::SqlxPostgresPoolConnection(SqlxPostgresPoolConnection { pool })
}
}

impl SqlxPostgresPoolConnection {
/// Execute a [Statement] on a PostgreSQL backend
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -70,6 +76,7 @@ impl SqlxPostgresPoolConnection {
}
}

/// Get one result from a SQL query. Returns [Option::None] if no match was found
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -89,6 +96,7 @@ impl SqlxPostgresPoolConnection {
}
}

/// Get the results of a query returning them as a Vec<[QueryResult]>
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -105,6 +113,7 @@ impl SqlxPostgresPoolConnection {
}
}

/// Stream the results of executing a SQL query
pub async fn stream(&self, stmt: Statement) -> Result<QueryStream, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -117,6 +126,7 @@ impl SqlxPostgresPoolConnection {
}
}

/// Bundle a set of SQL statements that execute together.
pub async fn begin(&self) -> Result<DatabaseTransaction, DbErr> {
if let Ok(conn) = self.pool.acquire().await {
DatabaseTransaction::new_postgres(conn).await
Expand All @@ -127,6 +137,7 @@ impl SqlxPostgresPoolConnection {
}
}

/// Create a PostgreSQL transaction
pub async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
where
F: for<'b> FnOnce(
Expand Down
11 changes: 11 additions & 0 deletions src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ use crate::{

use super::sqlx_common::*;

/// Defines the [sqlx::sqlite] connector
#[derive(Debug)]
pub struct SqlxSqliteConnector;

/// Defines a sqlx SQLite pool
#[derive(Debug, Clone)]
pub struct SqlxSqlitePoolConnection {
pool: SqlitePool,
}

impl SqlxSqliteConnector {
/// Check if the URI provided corresponds to `sqlite:` for a SQLite database
pub fn accepts(string: &str) -> bool {
string.starts_with("sqlite:") && string.parse::<SqliteConnectOptions>().is_ok()
}

/// Add configuration options for the SQLite database
pub async fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
let mut opt = options
.url
Expand All @@ -53,12 +57,14 @@ impl SqlxSqliteConnector {
}

impl SqlxSqliteConnector {
/// Instantiate a sqlx pool connection to a [DatabaseConnection]
pub fn from_sqlx_sqlite_pool(pool: SqlitePool) -> DatabaseConnection {
DatabaseConnection::SqlxSqlitePoolConnection(SqlxSqlitePoolConnection { pool })
}
}

impl SqlxSqlitePoolConnection {
/// Execute a [Statement] on a SQLite backend
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -75,6 +81,7 @@ impl SqlxSqlitePoolConnection {
}
}

/// Get one result from a SQL query. Returns [Option::None] if no match was found
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -94,6 +101,7 @@ impl SqlxSqlitePoolConnection {
}
}

/// Get the results of a query returning them as a Vec<[QueryResult]>
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -110,6 +118,7 @@ impl SqlxSqlitePoolConnection {
}
}

/// Stream the results of executing a SQL query
pub async fn stream(&self, stmt: Statement) -> Result<QueryStream, DbErr> {
debug_print!("{}", stmt);

Expand All @@ -122,6 +131,7 @@ impl SqlxSqlitePoolConnection {
}
}

/// Bundle a set of SQL statements that execute together.
pub async fn begin(&self) -> Result<DatabaseTransaction, DbErr> {
if let Ok(conn) = self.pool.acquire().await {
DatabaseTransaction::new_sqlite(conn).await
Expand All @@ -132,6 +142,7 @@ impl SqlxSqlitePoolConnection {
}
}

/// Create a MySQL transaction
pub async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
where
F: for<'b> FnOnce(
Expand Down