From 72e8b1f9d6920ebad9e18f4257da17a68cc6d9b2 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 29 Jun 2021 03:02:14 +0800 Subject: [PATCH 1/3] Docs --- src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 35571f42..571706d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,47 @@ -//! # Select +//!
+//! +//! +//! +//!

SeaORM

+//! +//!

+//! 🐚 An async & dynamic ORM for Rust +//!

+//! +//! Built with 🔥 by 🌊🦀🐚 +//! +//!
+//! +//! # SeaORM +//! +//! Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic +//! API to make working with databases in Rust a first-class experience. +//! +//! ```ignore +//! This is an early WIP of SeaORM, and is not yet published. See [example](examples/sqlx-mysql/src) for demo usage. +//! ``` +//! +//! ## Features +//! +//! 1. Async +//! +//! Relying on SQLx, SeaORM is a new library with async support from day 1. +//! +//! 2. Dynamic +//! +//! Built upon SeaQuery, a dynamic query builder, SeaORM allows you to build complex queries without 'fighting the ORM'. +//! +//! 3. Testable +//! +//! Use mock connections to write unit tests for your logic. +//! +//! 4. API oriented +//! +//! Quickly build search models that help you join, filter, sort and paginate data in APIs. +//! +//! # A quick taste of SeaORM +//! +//! ## Select //! ``` //! # use sea_orm::{DbConn, entity::*, query::*, tests_cfg::*}; //! # async fn function(db: &DbConn) -> Result<(), QueryErr> { @@ -28,7 +71,7 @@ //! # Ok(()) //! # } //! ``` -//! # Insert +//! ## Insert //! ``` //! # use sea_orm::{DbConn, entity::*, query::*, tests_cfg::*}; //! # async fn function(db: &DbConn) -> Result<(), ExecErr> { @@ -68,7 +111,7 @@ //! # Ok(()) //! # } //! ``` -//! # Update +//! ## Update //! ``` //! # use sea_orm::{DbConn, entity::*, query::*, tests_cfg::*}; //! # @@ -98,7 +141,7 @@ //! # Ok(()) //! # } //! ``` -//! # Delete +//! ## Delete //! ``` //! # use sea_orm::{DbConn, entity::*, query::*, tests_cfg::*}; //! # From 3e7220aad0c91f3c6de64433e0c923b01139f581 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 29 Jun 2021 16:59:40 +0800 Subject: [PATCH 2/3] Add SchemaBuilderBackend --- src/database/connection.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 95911c29..9727125b 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -1,6 +1,7 @@ use crate::{ExecErr, ExecResult, QueryErr, QueryResult, Statement, Transaction}; use sea_query::{ - MysqlQueryBuilder, PostgresQueryBuilder, QueryStatementBuilder, SqliteQueryBuilder, + MysqlQueryBuilder, PostgresQueryBuilder, QueryStatementBuilder, SchemaStatementBuilder, + SqliteQueryBuilder, }; use std::{error::Error, fmt}; @@ -22,6 +23,12 @@ pub enum QueryBuilderBackend { Sqlite, } +pub enum SchemaBuilderBackend { + MySql, + Postgres, + Sqlite, +} + #[derive(Debug)] pub struct ConnectionErr; @@ -70,6 +77,18 @@ impl DatabaseConnection { } } + pub fn get_schema_builder_backend(&self) -> SchemaBuilderBackend { + match self { + #[cfg(feature = "sqlx-mysql")] + DatabaseConnection::SqlxMySqlPoolConnection(_) => SchemaBuilderBackend::MySql, + #[cfg(feature = "sqlx-sqlite")] + DatabaseConnection::SqlxSqlitePoolConnection(_) => SchemaBuilderBackend::Sqlite, + #[cfg(feature = "mock")] + DatabaseConnection::MockDatabaseConnection(_) => SchemaBuilderBackend::Postgres, + DatabaseConnection::Disconnected => panic!("Disconnected"), + } + } + pub async fn execute(&self, stmt: Statement) -> Result { match self { #[cfg(feature = "sqlx-mysql")] @@ -139,3 +158,17 @@ impl QueryBuilderBackend { .into() } } + +impl SchemaBuilderBackend { + pub fn build(&self, statement: &S) -> Statement + where + S: SchemaStatementBuilder, + { + match self { + Self::MySql => statement.build(MysqlQueryBuilder), + Self::Postgres => statement.build(PostgresQueryBuilder), + Self::Sqlite => statement.build(SqliteQueryBuilder), + } + .into() + } +} From 5714bbe915b49c9ada8f38a8a8eda6b9c71607c7 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 29 Jun 2021 16:59:45 +0800 Subject: [PATCH 3/3] cargo fmt --- examples/sqlx-mysql/src/select.rs | 11 +++------ src/lib.rs | 38 +++++++++++++++---------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/examples/sqlx-mysql/src/select.rs b/examples/sqlx-mysql/src/select.rs index 0fe9c921..c344bae6 100644 --- a/examples/sqlx-mysql/src/select.rs +++ b/examples/sqlx-mysql/src/select.rs @@ -66,10 +66,7 @@ async fn find_all(db: &DbConn) -> Result<(), QueryErr> { async fn find_together(db: &DbConn) -> Result<(), QueryErr> { print!("find cakes and fruits: "); - let both = Cake::find() - .find_also_related(Fruit) - .all(db) - .await?; + let both = Cake::find().find_also_related(Fruit).all(db).await?; println!(); for bb in both.iter() { @@ -144,10 +141,8 @@ async fn count_fruits_by_cake(db: &DbConn) -> Result<(), QueryErr> { async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> { print!("find cakes and fillings: "); - let both: Vec<(cake::Model, Vec)> = Cake::find() - .find_with_related(Filling) - .all(db) - .await?; + let both: Vec<(cake::Model, Vec)> = + Cake::find().find_with_related(Filling).all(db).await?; println!(); for bb in both.iter() { diff --git a/src/lib.rs b/src/lib.rs index 571706d6..3214dc89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,46 +1,46 @@ //!
-//! +//! //! -//! +//! //!

SeaORM

-//! +//! //!

//! 🐚 An async & dynamic ORM for Rust //!

-//! +//! //! Built with 🔥 by 🌊🦀🐚 -//! +//! //!
-//! +//! //! # SeaORM -//! -//! Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic +//! +//! Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic //! API to make working with databases in Rust a first-class experience. -//! +//! //! ```ignore //! This is an early WIP of SeaORM, and is not yet published. See [example](examples/sqlx-mysql/src) for demo usage. //! ``` -//! +//! //! ## Features -//! +//! //! 1. Async -//! +//! //! Relying on SQLx, SeaORM is a new library with async support from day 1. -//! +//! //! 2. Dynamic -//! +//! //! Built upon SeaQuery, a dynamic query builder, SeaORM allows you to build complex queries without 'fighting the ORM'. -//! +//! //! 3. Testable -//! +//! //! Use mock connections to write unit tests for your logic. -//! +//! //! 4. API oriented -//! +//! //! Quickly build search models that help you join, filter, sort and paginate data in APIs. //! //! # A quick taste of SeaORM -//! +//! //! ## Select //! ``` //! # use sea_orm::{DbConn, entity::*, query::*, tests_cfg::*};