This commit is contained in:
Chris Tsang 2021-07-17 20:34:45 +08:00
parent 7be1daacd3
commit c018e45c3c
3 changed files with 16 additions and 16 deletions

View File

@ -1,4 +1,4 @@
use crate::{error::*, ExecResult, IntoStatement, QueryResult, Statement};
use crate::{error::*, ExecResult, QueryResult, Statement, StatementBuilder};
use sea_query::{MysqlQueryBuilder, PostgresQueryBuilder, QueryBuilder, SqliteQueryBuilder};
pub enum DatabaseConnection {
@ -116,9 +116,9 @@ impl DatabaseConnection {
impl DatabaseBackend {
pub fn build<S>(&self, statement: &S) -> Statement
where
S: IntoStatement,
S: StatementBuilder,
{
statement.into_statement(self)
statement.build(self)
}
pub fn get_query_builder(&self) -> Box<dyn QueryBuilder> {

View File

@ -11,8 +11,8 @@ pub struct Statement {
pub db_backend: DatabaseBackend,
}
pub trait IntoStatement {
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement;
pub trait StatementBuilder {
fn build(&self, db_backend: &DatabaseBackend) -> Statement;
}
impl Statement {
@ -66,8 +66,8 @@ macro_rules! build_any_stmt {
macro_rules! build_query_stmt {
($stmt: ty) => {
impl IntoStatement for $stmt {
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement {
impl StatementBuilder for $stmt {
fn build(&self, db_backend: &DatabaseBackend) -> Statement {
let stmt = build_any_stmt!(self, db_backend);
Statement::from_string_values_tuple(*db_backend, stmt)
}
@ -82,8 +82,8 @@ build_query_stmt!(sea_query::DeleteStatement);
macro_rules! build_schema_stmt {
($stmt: ty) => {
impl IntoStatement for $stmt {
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement {
impl StatementBuilder for $stmt {
fn build(&self, db_backend: &DatabaseBackend) -> Statement {
let stmt = build_any_stmt!(self, db_backend);
Statement::from_string(*db_backend, stmt)
}

View File

@ -102,11 +102,11 @@ where
}
pub async fn one(self, db: &DatabaseConnection) -> Result<Option<E::Model>, DbErr> {
self.into_model::<E::Model>().one(db).await
self.into_model().one(db).await
}
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<E::Model>, DbErr> {
self.into_model::<E::Model>().all(db).await
self.into_model().all(db).await
}
pub fn paginate(
@ -114,7 +114,7 @@ where
db: &DatabaseConnection,
page_size: usize,
) -> Paginator<'_, SelectModel<E::Model>> {
self.into_model::<E::Model>().paginate(db, page_size)
self.into_model().paginate(db, page_size)
}
}
@ -146,14 +146,14 @@ where
self,
db: &DatabaseConnection,
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
self.into_model::<E::Model, F::Model>().one(db).await
self.into_model().one(db).await
}
pub async fn all(
self,
db: &DatabaseConnection,
) -> Result<Vec<(E::Model, Option<F::Model>)>, DbErr> {
self.into_model::<E::Model, F::Model>().all(db).await
self.into_model().all(db).await
}
}
@ -185,14 +185,14 @@ where
self,
db: &DatabaseConnection,
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
self.into_model::<E::Model, F::Model>().one(db).await
self.into_model().one(db).await
}
pub async fn all(
self,
db: &DatabaseConnection,
) -> Result<Vec<(E::Model, Vec<F::Model>)>, DbErr> {
let rows = self.into_model::<E::Model, F::Model>().all(db).await?;
let rows = self.into_model().all(db).await?;
Ok(consolidate_query_result::<E, F>(rows))
}
}