Support schema prefix when specifying table name

This commit is contained in:
Billy Chan 2021-08-11 16:25:27 +08:00 committed by Chris Tsang
parent e76928f1cc
commit b7408dda30
5 changed files with 21 additions and 10 deletions

View File

@ -3,7 +3,7 @@ use crate::{
ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related, RelationBuilder,
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;
use std::fmt::Debug;
@ -12,10 +12,21 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
}
pub trait EntityName: IdenStatic + Default {
fn schema_name(&self) -> Option<&str> {
None
}
fn table_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(),
}
}
}

View File

@ -65,7 +65,7 @@ impl Delete {
{
let myself = DeleteOne {
query: DeleteStatement::new()
.from_table(A::Entity::default().into_iden())
.from_table(A::Entity::default().table_ref())
.to_owned(),
model: model.into_active_model(),
};

View File

@ -1,6 +1,6 @@
use crate::{ActiveModelTrait, EntityTrait, IntoActiveModel, Iterable, QueryTrait};
use crate::{ActiveModelTrait, EntityName, EntityTrait, IntoActiveModel, Iterable, QueryTrait};
use core::marker::PhantomData;
use sea_query::{InsertStatement, IntoIden};
use sea_query::InsertStatement;
#[derive(Clone, Debug)]
pub struct Insert<A>
@ -28,7 +28,7 @@ where
pub(crate) fn new() -> Self {
Self {
query: InsertStatement::new()
.into_table(A::Entity::default().into_iden())
.into_table(A::Entity::default().table_ref())
.to_owned(),
columns: Vec::new(),
model: PhantomData,

View File

@ -2,7 +2,7 @@ use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySe
use core::fmt::Debug;
use core::marker::PhantomData;
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)]
pub struct Select<E>
@ -119,7 +119,7 @@ where
}
fn prepare_from(mut self) -> Self {
self.query.from(E::default().into_iden());
self.query.from(E::default().table_ref());
self
}
}

View File

@ -49,7 +49,7 @@ impl Update {
{
let myself = UpdateOne {
query: UpdateStatement::new()
.table(A::Entity::default().into_iden())
.table(A::Entity::default().table_ref())
.to_owned(),
model,
};
@ -75,7 +75,7 @@ impl Update {
E: EntityTrait,
{
UpdateMany {
query: UpdateStatement::new().table(entity.into_iden()).to_owned(),
query: UpdateStatement::new().table(entity.table_ref()).to_owned(),
entity: PhantomData,
}
}