Refactor
This commit is contained in:
parent
7be1daacd3
commit
c018e45c3c
@ -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};
|
use sea_query::{MysqlQueryBuilder, PostgresQueryBuilder, QueryBuilder, SqliteQueryBuilder};
|
||||||
|
|
||||||
pub enum DatabaseConnection {
|
pub enum DatabaseConnection {
|
||||||
@ -116,9 +116,9 @@ impl DatabaseConnection {
|
|||||||
impl DatabaseBackend {
|
impl DatabaseBackend {
|
||||||
pub fn build<S>(&self, statement: &S) -> Statement
|
pub fn build<S>(&self, statement: &S) -> Statement
|
||||||
where
|
where
|
||||||
S: IntoStatement,
|
S: StatementBuilder,
|
||||||
{
|
{
|
||||||
statement.into_statement(self)
|
statement.build(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_query_builder(&self) -> Box<dyn QueryBuilder> {
|
pub fn get_query_builder(&self) -> Box<dyn QueryBuilder> {
|
||||||
|
@ -11,8 +11,8 @@ pub struct Statement {
|
|||||||
pub db_backend: DatabaseBackend,
|
pub db_backend: DatabaseBackend,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IntoStatement {
|
pub trait StatementBuilder {
|
||||||
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement;
|
fn build(&self, db_backend: &DatabaseBackend) -> Statement;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Statement {
|
impl Statement {
|
||||||
@ -66,8 +66,8 @@ macro_rules! build_any_stmt {
|
|||||||
|
|
||||||
macro_rules! build_query_stmt {
|
macro_rules! build_query_stmt {
|
||||||
($stmt: ty) => {
|
($stmt: ty) => {
|
||||||
impl IntoStatement for $stmt {
|
impl StatementBuilder for $stmt {
|
||||||
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement {
|
fn build(&self, db_backend: &DatabaseBackend) -> Statement {
|
||||||
let stmt = build_any_stmt!(self, db_backend);
|
let stmt = build_any_stmt!(self, db_backend);
|
||||||
Statement::from_string_values_tuple(*db_backend, stmt)
|
Statement::from_string_values_tuple(*db_backend, stmt)
|
||||||
}
|
}
|
||||||
@ -82,8 +82,8 @@ build_query_stmt!(sea_query::DeleteStatement);
|
|||||||
|
|
||||||
macro_rules! build_schema_stmt {
|
macro_rules! build_schema_stmt {
|
||||||
($stmt: ty) => {
|
($stmt: ty) => {
|
||||||
impl IntoStatement for $stmt {
|
impl StatementBuilder for $stmt {
|
||||||
fn into_statement(&self, db_backend: &DatabaseBackend) -> Statement {
|
fn build(&self, db_backend: &DatabaseBackend) -> Statement {
|
||||||
let stmt = build_any_stmt!(self, db_backend);
|
let stmt = build_any_stmt!(self, db_backend);
|
||||||
Statement::from_string(*db_backend, stmt)
|
Statement::from_string(*db_backend, stmt)
|
||||||
}
|
}
|
||||||
|
@ -102,11 +102,11 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn one(self, db: &DatabaseConnection) -> Result<Option<E::Model>, DbErr> {
|
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> {
|
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(
|
pub fn paginate(
|
||||||
@ -114,7 +114,7 @@ where
|
|||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
page_size: usize,
|
page_size: usize,
|
||||||
) -> Paginator<'_, SelectModel<E::Model>> {
|
) -> 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,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
|
) -> 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(
|
pub async fn all(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Vec<(E::Model, Option<F::Model>)>, DbErr> {
|
) -> 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,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
|
) -> 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(
|
pub async fn all(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Vec<(E::Model, Vec<F::Model>)>, DbErr> {
|
) -> 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))
|
Ok(consolidate_query_result::<E, F>(rows))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user