QueryTrait
This commit is contained in:
parent
ba1eef1671
commit
89c743d5a7
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
ActiveModelOf, ActiveModelTrait, ColumnTrait, Insert, ModelTrait, OneOrManyActiveModel,
|
||||
PrimaryKeyOfModel, PrimaryKeyTrait, SelectHelper, RelationBuilder, RelationTrait, RelationType,
|
||||
Select,
|
||||
PrimaryKeyOfModel, PrimaryKeyTrait, RelationBuilder, RelationTrait, RelationType, Select,
|
||||
SelectHelper,
|
||||
};
|
||||
use sea_query::{Iden, IntoValueTuple};
|
||||
use std::fmt::Debug;
|
||||
@ -120,7 +120,7 @@ pub trait EntityTrait: EntityName {
|
||||
fn insert_many<A, I>(models: I) -> Insert<A>
|
||||
where
|
||||
A: ActiveModelTrait + ActiveModelOf<Self>,
|
||||
I: IntoIterator<Item = A>
|
||||
I: IntoIterator<Item = A>,
|
||||
{
|
||||
Insert::new().many(models)
|
||||
}
|
||||
@ -129,7 +129,7 @@ pub trait EntityTrait: EntityName {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests_cfg::cake;
|
||||
use crate::{EntityTrait, Val};
|
||||
use crate::{EntityTrait, QueryTrait, Val};
|
||||
use sea_query::PostgresQueryBuilder;
|
||||
|
||||
#[test]
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{EntityTrait, Identity, IntoIdentity, Iterable, SelectHelper, Select};
|
||||
use crate::{EntityTrait, Identity, IntoIdentity, Iterable, Select, SelectHelper};
|
||||
use core::marker::PhantomData;
|
||||
use sea_query::{Iden, IntoIden, JoinType};
|
||||
use std::fmt::Debug;
|
||||
|
@ -2,7 +2,6 @@ mod connector;
|
||||
mod database;
|
||||
mod driver;
|
||||
pub mod entity;
|
||||
mod operation;
|
||||
mod query;
|
||||
pub mod tests_cfg;
|
||||
mod util;
|
||||
@ -11,7 +10,6 @@ pub use connector::*;
|
||||
pub use database::*;
|
||||
pub use driver::*;
|
||||
pub use entity::*;
|
||||
pub use operation::*;
|
||||
pub use query::*;
|
||||
|
||||
pub use sea_orm_macros::{
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{ActiveModelOf, ActiveModelTrait, EntityTrait, Iterable, Statement};
|
||||
use crate::{ActiveModelOf, ActiveModelTrait, EntityTrait, Iterable, QueryTrait};
|
||||
use core::marker::PhantomData;
|
||||
use sea_query::{InsertStatement, IntoIden, QueryBuilder};
|
||||
use sea_query::{InsertStatement, IntoIden};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Insert<A>
|
||||
@ -65,35 +65,31 @@ where
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a mutable ref to the query builder
|
||||
pub fn query(&mut self) -> &mut InsertStatement {
|
||||
impl<A> QueryTrait for Insert<A>
|
||||
where
|
||||
A: ActiveModelTrait,
|
||||
{
|
||||
type QueryStatementBuilder = InsertStatement;
|
||||
|
||||
fn query(&mut self) -> &mut InsertStatement {
|
||||
&mut self.query
|
||||
}
|
||||
|
||||
/// Get an immutable ref to the query builder
|
||||
pub fn as_query(&self) -> &InsertStatement {
|
||||
fn as_query(&self) -> &InsertStatement {
|
||||
&self.query
|
||||
}
|
||||
|
||||
/// Take ownership of the query builder
|
||||
pub fn into_query(self) -> InsertStatement {
|
||||
fn into_query(self) -> InsertStatement {
|
||||
self.query
|
||||
}
|
||||
|
||||
/// Build the query as [`Statement`]
|
||||
pub fn build<B>(&self, builder: B) -> Statement
|
||||
where
|
||||
B: QueryBuilder,
|
||||
{
|
||||
self.as_query().build(builder).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests_cfg::cake;
|
||||
use crate::{Insert, Val};
|
||||
use crate::{Insert, QueryTrait, Val};
|
||||
use sea_query::PostgresQueryBuilder;
|
||||
|
||||
#[test]
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ColumnTrait, EntityTrait, Iterable, ModelTrait, PrimaryKeyOfModel, SelectHelper, Related,
|
||||
Select, SelectTwo,
|
||||
ColumnTrait, EntityTrait, Iterable, ModelTrait, PrimaryKeyOfModel, Related, Select,
|
||||
SelectHelper, SelectTwo,
|
||||
};
|
||||
pub use sea_query::JoinType;
|
||||
|
||||
|
@ -6,6 +6,7 @@ mod join;
|
||||
mod json;
|
||||
mod result;
|
||||
mod select;
|
||||
mod traits;
|
||||
|
||||
// pub use combine::*;
|
||||
pub use helper::*;
|
||||
@ -15,3 +16,4 @@ pub use join::*;
|
||||
pub use json::*;
|
||||
pub use result::*;
|
||||
pub use select::*;
|
||||
pub use traits::*;
|
||||
|
23
src/query/traits.rs
Normal file
23
src/query/traits.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use crate::Statement;
|
||||
use sea_query::{QueryBuilder, QueryStatementBuilder};
|
||||
|
||||
pub trait QueryTrait {
|
||||
type QueryStatementBuilder: QueryStatementBuilder;
|
||||
|
||||
/// Get a mutable ref to the query builder
|
||||
fn query(&mut self) -> &mut Self::QueryStatementBuilder;
|
||||
|
||||
/// Get an immutable ref to the query builder
|
||||
fn as_query(&self) -> &Self::QueryStatementBuilder;
|
||||
|
||||
/// Take ownership of the query builder
|
||||
fn into_query(self) -> Self::QueryStatementBuilder;
|
||||
|
||||
/// Build the query as [`Statement`]
|
||||
fn build<B>(&self, builder: B) -> Statement
|
||||
where
|
||||
B: QueryBuilder,
|
||||
{
|
||||
self.as_query().build(builder).into()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user