Skip to content

Commit

Permalink
Merge pull request #292 from SeaQL/returning
Browse files Browse the repository at this point in the history
Returning
  • Loading branch information
tyt2y3 authored Nov 17, 2021
2 parents aff988b + 42404eb commit 0deeddd
Show file tree
Hide file tree
Showing 14 changed files with 884 additions and 195 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ jobs:
name: Examples
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
path: [basic, actix_example, actix4_example, axum_example, rocket_example]
Expand All @@ -312,6 +313,7 @@ jobs:
if: ${{ (needs.init.outputs.run-partial == 'true' && needs.init.outputs.run-issues == 'true') }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
path: [86, 249, 262, 319, 324]
Expand Down Expand Up @@ -350,6 +352,7 @@ jobs:
env:
DATABASE_URL: "sqlite::memory:"
strategy:
fail-fast: false
matrix:
runtime: [async-std, actix, tokio]
tls: [native-tls, rustls]
Expand Down Expand Up @@ -392,6 +395,7 @@ jobs:
env:
DATABASE_URL: "mysql://root:@localhost"
strategy:
fail-fast: false
matrix:
version: [8.0, 5.7]
runtime: [async-std, actix, tokio]
Expand Down Expand Up @@ -452,8 +456,9 @@ jobs:
env:
DATABASE_URL: "mysql://root:@localhost"
strategy:
fail-fast: false
matrix:
version: [10.6]
version: [10.6, 10.5, 10.4]
runtime: [async-std, actix, tokio]
tls: [native-tls]
services:
Expand Down Expand Up @@ -512,8 +517,9 @@ jobs:
env:
DATABASE_URL: "postgres://root:root@localhost"
strategy:
fail-fast: false
matrix:
version: [13.3, 12.7, 11.12, 10.17, 9.6.22]
version: [13, 12, 11, 10, 9]
runtime: [tokio]
tls: [native-tls]
services:
Expand Down
6 changes: 6 additions & 0 deletions src/database/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub trait ConnectionTrait<'a>: Sync {
T: Send,
E: std::error::Error + Send;

/// Check if the connection supports `RETURNING` syntax on insert and update
fn support_returning(&self) -> bool {
let db_backend = self.get_database_backend();
db_backend.support_returning()
}

/// Check if the connection is a test connection for the Mock database
fn is_mock_connection(&self) -> bool {
false
Expand Down
5 changes: 5 additions & 0 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ impl DbBackend {
Self::Sqlite => Box::new(SqliteQueryBuilder),
}
}

/// Check if the database supports `RETURNING` syntax on insert and update
pub fn support_returning(&self) -> bool {
matches!(self, Self::Postgres)
}
}

#[cfg(test)]
Expand Down
11 changes: 8 additions & 3 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
//! Relying on [SQLx](https://github.com/launchbadge/sqlx), SeaORM is a new library with async support from day 1.
//!
//! ```
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*, DatabaseConnection, DbBackend, MockDatabase, Transaction, IntoMockRow};
//! # use sea_orm::{error::*, tests_cfg::*, *};
//! #
//! # #[smol_potat::main]
//! # #[cfg(feature = "mock")]
//! # pub async fn main() -> Result<(), DbErr> {
//! #
//! # let db = MockDatabase::new(DbBackend::Postgres)
//! # .append_query_results(vec![
//! # vec![cake::Model {
Expand All @@ -19,7 +24,7 @@
//! # .into_mock_row()],
//! # ])
//! # .into_connection();
//! # let _: Result<(), DbErr> = smol::block_on(async {
//! #
//! // execute multiple queries in parallel
//! let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
//! futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?;
Expand Down Expand Up @@ -53,7 +58,7 @@
//! # ]
//! # );
//! # Ok(())
//! # });
//! # }
//! ```
//!
//! 2. Dynamic
Expand Down
12 changes: 7 additions & 5 deletions src/driver/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub struct MockDatabaseConnector;
/// Defines a connection for the [MockDatabase]
#[derive(Debug)]
pub struct MockDatabaseConnection {
counter: AtomicUsize,
execute_counter: AtomicUsize,
query_counter: AtomicUsize,
mocker: Mutex<Box<dyn MockDatabaseTrait>>,
}

Expand Down Expand Up @@ -100,7 +101,8 @@ impl MockDatabaseConnection {
M: MockDatabaseTrait,
{
Self {
counter: AtomicUsize::new(0),
execute_counter: AtomicUsize::new(0),
query_counter: AtomicUsize::new(0),
mocker: Mutex::new(Box::new(m)),
}
}
Expand All @@ -117,22 +119,22 @@ impl MockDatabaseConnection {
/// 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);
let counter = self.execute_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 counter = self.query_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);
let counter = self.query_counter.fetch_add(1, Ordering::SeqCst);
self.mocker.lock().unwrap().query(counter, statement)
}

Expand Down
Loading

0 comments on commit 0deeddd

Please sign in to comment.