Skip to content

Commit

Permalink
Add TableRef::DatabaseTable
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Nov 29, 2021
1 parent ff10e85 commit 1f2c7db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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::DatabaseTable(db, table) => {
db.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::DatabaseTableAlias(db, table, alias) => {
db.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
5 changes: 5 additions & 0 deletions src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder {
TableRef::SchemaTable(_, table) => {
table.prepare(sql, self.quote());
}
TableRef::DatabaseTable(db, table) => {
db.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
}
_ => panic!("Not supported"),
}
}
Expand Down
15 changes: 15 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 (for Postgres) prefix
SchemaTable(DynIden, DynIden),
/// Table identifier with database (for MySQL and SQLite) prefix
DatabaseTable(DynIden, DynIden),
/// Table identifier with alias
TableAlias(DynIden, DynIden),
/// Table identifier with schema (for Postgres) prefix and alias
SchemaTableAlias(DynIden, DynIden, DynIden),
/// Table identifier with database (for MySQL and SQLite) prefix and alias
DatabaseTableAlias(DynIden, DynIden, DynIden),
/// Subquery with alias
SubQuery(SelectStatement, DynIden),
}

Expand Down Expand Up @@ -290,9 +299,15 @@ impl TableRef {
Self::SchemaTable(schema, table) => {
Self::SchemaTableAlias(schema, table, alias.into_iden())
}
Self::DatabaseTable(schema, table) => {
Self::DatabaseTableAlias(schema, table, alias.into_iden())
}
Self::SchemaTableAlias(schema, table, _) => {
Self::SchemaTableAlias(schema, table, alias.into_iden())
}
Self::DatabaseTableAlias(schema, table, _) => {
Self::DatabaseTableAlias(schema, table, alias.into_iden())
}
Self::SubQuery(statement, _) => Self::SubQuery(statement, alias.into_iden()),
}
}
Expand Down

0 comments on commit 1f2c7db

Please sign in to comment.