Support schema prefix when specifying table name
This commit is contained in:
parent
e76928f1cc
commit
b7408dda30
@ -3,7 +3,7 @@ use crate::{
|
|||||||
ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related, RelationBuilder,
|
ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related, RelationBuilder,
|
||||||
RelationTrait, RelationType, Select, Update, UpdateMany, UpdateOne,
|
RelationTrait, RelationType, Select, Update, UpdateMany, UpdateOne,
|
||||||
};
|
};
|
||||||
use sea_query::{Iden, IntoValueTuple};
|
use sea_query::{Alias, Iden, IntoIden, IntoTableRef, IntoValueTuple, TableRef};
|
||||||
pub use sea_strum::IntoEnumIterator as Iterable;
|
pub use sea_strum::IntoEnumIterator as Iterable;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
@ -12,10 +12,21 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait EntityName: IdenStatic + Default {
|
pub trait EntityName: IdenStatic + Default {
|
||||||
|
fn schema_name(&self) -> Option<&str> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn table_name(&self) -> &str;
|
fn table_name(&self) -> &str;
|
||||||
|
|
||||||
fn module_name(&self) -> &str {
|
fn module_name(&self) -> &str {
|
||||||
Self::table_name(self)
|
self.table_name()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_ref(&self) -> TableRef {
|
||||||
|
match self.schema_name() {
|
||||||
|
Some(schema) => (Alias::new(schema).into_iden(), self.into_iden()).into_table_ref(),
|
||||||
|
None => self.into_table_ref(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ impl Delete {
|
|||||||
{
|
{
|
||||||
let myself = DeleteOne {
|
let myself = DeleteOne {
|
||||||
query: DeleteStatement::new()
|
query: DeleteStatement::new()
|
||||||
.from_table(A::Entity::default().into_iden())
|
.from_table(A::Entity::default().table_ref())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
model: model.into_active_model(),
|
model: model.into_active_model(),
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{ActiveModelTrait, EntityTrait, IntoActiveModel, Iterable, QueryTrait};
|
use crate::{ActiveModelTrait, EntityName, EntityTrait, IntoActiveModel, Iterable, QueryTrait};
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
use sea_query::{InsertStatement, IntoIden};
|
use sea_query::InsertStatement;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Insert<A>
|
pub struct Insert<A>
|
||||||
@ -28,7 +28,7 @@ where
|
|||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
query: InsertStatement::new()
|
query: InsertStatement::new()
|
||||||
.into_table(A::Entity::default().into_iden())
|
.into_table(A::Entity::default().table_ref())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
columns: Vec::new(),
|
columns: Vec::new(),
|
||||||
model: PhantomData,
|
model: PhantomData,
|
||||||
|
@ -2,7 +2,7 @@ use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySe
|
|||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
pub use sea_query::JoinType;
|
pub use sea_query::JoinType;
|
||||||
use sea_query::{DynIden, IntoColumnRef, IntoIden, SeaRc, SelectStatement, SimpleExpr};
|
use sea_query::{DynIden, IntoColumnRef, SeaRc, SelectStatement, SimpleExpr};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Select<E>
|
pub struct Select<E>
|
||||||
@ -119,7 +119,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_from(mut self) -> Self {
|
fn prepare_from(mut self) -> Self {
|
||||||
self.query.from(E::default().into_iden());
|
self.query.from(E::default().table_ref());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ impl Update {
|
|||||||
{
|
{
|
||||||
let myself = UpdateOne {
|
let myself = UpdateOne {
|
||||||
query: UpdateStatement::new()
|
query: UpdateStatement::new()
|
||||||
.table(A::Entity::default().into_iden())
|
.table(A::Entity::default().table_ref())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
model,
|
model,
|
||||||
};
|
};
|
||||||
@ -75,7 +75,7 @@ impl Update {
|
|||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
UpdateMany {
|
UpdateMany {
|
||||||
query: UpdateStatement::new().table(entity.into_iden()).to_owned(),
|
query: UpdateStatement::new().table(entity.table_ref()).to_owned(),
|
||||||
entity: PhantomData,
|
entity: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user