Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TableRef::DatabaseSchemaTable #193

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,30 @@ impl QueryBuilder for PostgresQueryBuilder {
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
}
TableRef::DatabaseSchemaTable(database, schema, table) => {
database.prepare(sql, self.quote());
write!(sql, ".").unwrap();
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
}
TableRef::SchemaTableAlias(schema, table, alias) => {
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
}
_ => QueryBuilder::prepare_table_ref_common(self, table_ref, sql, collector)
TableRef::DatabaseSchemaTableAlias(database, schema, table, alias) => {
database.prepare(sql, self.quote());
write!(sql, ".").unwrap();
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
}
_ => QueryBuilder::prepare_table_ref_common(self, table_ref, sql, collector),
}
}
}
12 changes: 12 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ pub trait QueryBuilder: QuotedBuilder {
TableRef::SchemaTable(_, table) => {
table.prepare(sql, self.quote());
}
TableRef::DatabaseSchemaTable(database, _, table) => {
database.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
}
TableRef::TableAlias(iden, alias) => {
iden.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
Expand All @@ -441,6 +446,13 @@ pub trait QueryBuilder: QuotedBuilder {
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
}
TableRef::DatabaseSchemaTableAlias(database, _, table, alias) => {
database.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
}
TableRef::SubQuery(query, alias) => {
write!(sql, "(").unwrap();
self.prepare_select_statement(query, sql, collector);
Expand Down
22 changes: 22 additions & 0 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,28 @@ impl SelectStatement {
/// r#"SELECT `font_size` FROM `glyph`"#
/// );
/// ```
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Char::FontSize)
/// .from((Alias::new("database"), Char::Table, Glyph::Table))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `font_size` FROM `database`.`glyph`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "font_size" FROM "database"."character"."glyph""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `font_size` FROM `database`.`glyph`"#
/// );
/// ```
pub fn from<R>(&mut self, tbl_ref: R) -> &mut Self
where
R: IntoTableRef,
Expand Down
26 changes: 26 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@ pub trait IntoColumnRef {
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum TableRef {
/// Table identifier without any schema / database prefix
Table(DynIden),
/// Table identifier with schema (Postgres only) prefix
SchemaTable(DynIden, DynIden),
/// Table identifier with database and schema (Postgres only) prefix
DatabaseSchemaTable(DynIden, DynIden, DynIden),
/// Table identifier with alias
TableAlias(DynIden, DynIden),
/// Table identifier with schema (Postgres only) prefix and alias
SchemaTableAlias(DynIden, DynIden, DynIden),
/// Table identifier with database and schema (Postgres only) prefix and alias
DatabaseSchemaTableAlias(DynIden, DynIden, DynIden, DynIden),
/// Subquery with alias
SubQuery(SelectStatement, DynIden),
}

Expand Down Expand Up @@ -278,6 +287,17 @@ where
}
}

impl<S: 'static, T: 'static, U: 'static> IntoTableRef for (S, T, U)
where
S: IntoIden,
T: IntoIden,
U: IntoIden,
{
fn into_table_ref(self) -> TableRef {
TableRef::DatabaseSchemaTable(self.0.into_iden(), self.1.into_iden(), self.2.into_iden())
}
}

impl TableRef {
/// Add or replace the current alias
pub fn alias<A: 'static>(self, alias: A) -> Self
Expand All @@ -290,9 +310,15 @@ impl TableRef {
Self::SchemaTable(schema, table) => {
Self::SchemaTableAlias(schema, table, alias.into_iden())
}
Self::DatabaseSchemaTable(database, schema, table) => {
Self::DatabaseSchemaTableAlias(database, schema, table, alias.into_iden())
}
Self::SchemaTableAlias(schema, table, _) => {
Self::SchemaTableAlias(schema, table, alias.into_iden())
}
Self::DatabaseSchemaTableAlias(database, schema, table, _) => {
Self::DatabaseSchemaTableAlias(database, schema, table, alias.into_iden())
}
Self::SubQuery(statement, _) => Self::SubQuery(statement, alias.into_iden()),
}
}
Expand Down