Skip to content

Commit

Permalink
Merge pull request #186 from josh-codes/sea-query-change-type-into-ta…
Browse files Browse the repository at this point in the history
…ble-ref

removed `'static` and inplace, added the type `IntoTableRef`.
  • Loading branch information
tyt2y3 authored Nov 17, 2021
2 parents e05a851 + 27d0150 commit 82ab40a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,27 @@ impl QueryBuilder for PostgresQueryBuilder {
_ => QueryBuilder::prepare_simple_expr_common(self, simple_expr, sql, collector),
}
}

fn prepare_table_ref(
&self,
table_ref: &TableRef,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
match table_ref {
TableRef::SchemaTable(schema, table) => {
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)
}
}
}
11 changes: 11 additions & 0 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use super::*;

impl TableBuilder for PostgresQueryBuilder {
fn prepare_table_ref(&self, table_ref: &TableRef, sql: &mut SqlWriter) {
match table_ref {
TableRef::SchemaTable(schema, table) => {
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
table.prepare(sql, self.quote());
}
_ => TableBuilder::prepare_table_ref_common(self, table_ref, sql),
}
}

fn prepare_column_def(&self, column_def: &ColumnDef, sql: &mut SqlWriter) {
column_def.name.prepare(sql, '"');

Expand Down
17 changes: 11 additions & 6 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,24 +414,29 @@ pub trait QueryBuilder: QuotedBuilder {
table_ref: &TableRef,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
QueryBuilder::prepare_table_ref_common(self, table_ref, sql, collector);
}

fn prepare_table_ref_common(
&self,
table_ref: &TableRef,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
match table_ref {
TableRef::Table(iden) => {
iden.prepare(sql, self.quote());
}
TableRef::SchemaTable(schema, table) => {
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
TableRef::SchemaTable(_, table) => {
table.prepare(sql, self.quote());
}
TableRef::TableAlias(iden, alias) => {
iden.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
}
TableRef::SchemaTableAlias(schema, table, alias) => {
schema.prepare(sql, self.quote());
write!(sql, ".").unwrap();
TableRef::SchemaTableAlias(_, table, alias) => {
table.prepare(sql, self.quote());
write!(sql, " AS ").unwrap();
alias.prepare(sql, self.quote());
Expand Down
23 changes: 20 additions & 3 deletions src/backend/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder {
write!(sql, "IF NOT EXISTS ").unwrap();
}

if let Some(table) = &create.table {
table.prepare(sql, self.quote());
if let Some(table_ref) = &create.table {
self.prepare_table_ref(table_ref, sql);
}

write!(sql, " ( ").unwrap();
Expand Down Expand Up @@ -48,6 +48,23 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder {
}
}

/// Translate [`TableRef`] into SQL statement.
fn prepare_table_ref(&self, table_ref: &TableRef, sql: &mut SqlWriter) {
self.prepare_table_ref_common(table_ref, sql);
}

fn prepare_table_ref_common(&self, table_ref: &TableRef, sql: &mut SqlWriter) {
match table_ref {
TableRef::Table(table) => {
table.prepare(sql, self.quote());
}
TableRef::SchemaTable(_, table) => {
table.prepare(sql, self.quote());
}
_ => panic!("Not supported"),
}
}

/// Translate [`ColumnDef`] into SQL statement.
fn prepare_column_def(&self, column_def: &ColumnDef, sql: &mut SqlWriter);

Expand Down Expand Up @@ -86,7 +103,7 @@ pub trait TableBuilder: IndexBuilder + ForeignKeyBuilder + QuotedBuilder {
if !first {
write!(sql, ", ").unwrap();
}
table.prepare(sql, self.quote());
self.prepare_table_ref(table, sql);
false
});

Expand Down
12 changes: 6 additions & 6 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,15 @@ impl SelectStatement {
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `font_size` FROM `character`.`glyph`"#
/// r#"SELECT `font_size` FROM `glyph`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "font_size" FROM "character"."glyph""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `font_size` FROM `character`.`glyph`"#
/// r#"SELECT `font_size` FROM `glyph`"#
/// );
/// ```
pub fn from<R>(&mut self, tbl_ref: R) -> &mut Self
Expand Down Expand Up @@ -550,15 +550,15 @@ impl SelectStatement {
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `font_size` FROM `character`.`glyph`"#
/// r#"SELECT `font_size` FROM `glyph`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "font_size" FROM "character"."glyph""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `font_size` FROM `character`.`glyph`"#
/// r#"SELECT `font_size` FROM `glyph`"#
/// );
/// ```
pub fn from_schema<S: 'static, T: 'static>(&mut self, schema: S, table: T) -> &mut Self
Expand Down Expand Up @@ -609,15 +609,15 @@ impl SelectStatement {
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `alias`.`character` FROM `font`.`character` AS `alias`"#
/// r#"SELECT `alias`.`character` FROM `character` AS `alias`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "alias"."character" FROM "font"."character" AS "alias""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT `alias`.`character` FROM `font`.`character` AS `alias`"#
/// r#"SELECT `alias`.`character` FROM `character` AS `alias`"#
/// );
/// ```
pub fn from_as<R, A>(&mut self, tbl_ref: R, alias: A) -> &mut Self
Expand Down
2 changes: 1 addition & 1 deletion src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{types::*, value::*};
/// Specification of a table column
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub(crate) table: Option<DynIden>,
pub(crate) table: Option<TableRef>,
pub(crate) name: DynIden,
pub(crate) types: Option<ColumnType>,
pub(crate) spec: Vec<ColumnSpec>,
Expand Down
12 changes: 6 additions & 6 deletions src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use crate::{
/// ```
#[derive(Debug, Clone)]
pub struct TableCreateStatement {
pub(crate) table: Option<DynIden>,
pub(crate) table: Option<TableRef>,
pub(crate) columns: Vec<ColumnDef>,
pub(crate) options: Vec<TableOpt>,
pub(crate) partitions: Vec<TablePartition>,
Expand Down Expand Up @@ -135,11 +135,11 @@ impl TableCreateStatement {
}

/// Set table name
pub fn table<T: 'static>(&mut self, table: T) -> &mut Self
pub fn table<T>(&mut self, table: T) -> &mut Self
where
T: Iden,
T: IntoTableRef,
{
self.table = Some(SeaRc::new(table));
self.table = Some(table.into_table_ref());
self
}

Expand Down Expand Up @@ -267,8 +267,8 @@ impl TableCreateStatement {
self
}

pub fn get_table_name(&self) -> Option<String> {
self.table.as_ref().map(|table| table.to_string())
pub fn get_table_name(&self) -> Option<&TableRef> {
self.table.as_ref()
}

pub fn get_columns(&self) -> &Vec<ColumnDef> {
Expand Down
6 changes: 3 additions & 3 deletions src/table/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{backend::SchemaBuilder, prepare::*, types::*, SchemaStatementBuilder
/// ```
#[derive(Debug, Clone)]
pub struct TableDropStatement {
pub(crate) tables: Vec<DynIden>,
pub(crate) tables: Vec<TableRef>,
pub(crate) options: Vec<TableDropOpt>,
pub(crate) if_exists: bool,
}
Expand Down Expand Up @@ -58,9 +58,9 @@ impl TableDropStatement {
/// Set table name
pub fn table<T: 'static>(&mut self, table: T) -> &mut Self
where
T: Iden,
T: IntoTableRef,
{
self.tables.push(SeaRc::new(table));
self.tables.push(table.into_table_ref());
self
}

Expand Down

0 comments on commit 82ab40a

Please sign in to comment.