-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
842e6d3
commit 160b7bd
Showing
12 changed files
with
245 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
use crate::*; | ||
|
||
impl ViewBuilder for MysqlQueryBuilder {} | ||
impl ViewBuilder for MysqlQueryBuilder { | ||
fn prepare_view_rename_statement(&self, rename: &ViewRenameStatement, sql: &mut SqlWriter) { | ||
write!(sql, "RENAME TABLE ").unwrap(); | ||
if let Some(from_name) = &rename.from_name { | ||
from_name.prepare(sql, self.quote()); | ||
} | ||
write!(sql, " TO ").unwrap(); | ||
if let Some(to_name) = &rename.to_name { | ||
to_name.prepare(sql, self.quote()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
use crate::*; | ||
|
||
impl ViewBuilder for PostgresQueryBuilder {} | ||
impl ViewBuilder for PostgresQueryBuilder { | ||
fn prepare_view_create_befor_view(&self, create: &ViewCreateStatement, sql: &mut SqlWriter) { | ||
if create.or_replace { | ||
write!(sql, " OR REPLACE").unwrap(); | ||
} | ||
|
||
if create.temporary { | ||
write!(sql, " TEMPORARY").unwrap(); | ||
} | ||
|
||
if create.recursive { | ||
write!(sql, " RECURSIVE").unwrap(); | ||
} | ||
} | ||
|
||
fn prepare_view_rename_statement(&self, rename: &ViewRenameStatement, sql: &mut SqlWriter) { | ||
write!(sql, "ALTER VIEW ").unwrap(); | ||
if let Some(from_name) = &rename.from_name { | ||
from_name.prepare(sql, self.quote()); | ||
} | ||
write!(sql, " TO ").unwrap(); | ||
if let Some(to_name) = &rename.to_name { | ||
to_name.prepare(sql, self.quote()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
use crate::*; | ||
|
||
impl ViewBuilder for SqliteQueryBuilder { | ||
fn prepare_view_rename_statement(&self, rename: &ViewRenameStatement, sql: &mut SqlWriter) {} | ||
fn prepare_view_create_befor_view(&self, create: &ViewCreateStatement, sql: &mut SqlWriter) { | ||
if create.temporary { | ||
write!(sql, " TEMPORARY").unwrap(); | ||
} | ||
} | ||
|
||
fn prepare_view_create_after_view(&self, create: &ViewCreateStatement, sql: &mut SqlWriter) { | ||
if create.if_not_exists { | ||
write!(sql, " IF NOT EXISTS").unwrap(); | ||
} | ||
} | ||
|
||
fn prepare_view_create_opt_update(&self, _opt: &ViewCreateOpt, _sql: &mut SqlWriter) { | ||
// SQLite does not support view create options | ||
} | ||
|
||
fn prepare_view_rename_statement(&self, _rename: &ViewRenameStatement, _sql: &mut SqlWriter) { | ||
// SQLite does not support rename view | ||
} | ||
|
||
/// Translate [`ViewDropStatement`] into SQL statement. | ||
fn prepare_view_drop_statement(&self, drop: &ViewDropStatement, sql: &mut SqlWriter) { | ||
write!(sql, "DROP VIEW ").unwrap(); | ||
|
||
if drop.if_exists { | ||
write!(sql, "IF EXISTS ").unwrap(); | ||
} | ||
self.prepare_view_ref(&drop.views[0], sql); | ||
} | ||
|
||
fn prepare_view_drop_opt(&self, _drop_opt: &ViewDropOpt, _sql: &mut dyn std::fmt::Write) { | ||
// SQLite does not support view drop options | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,108 @@ | ||
use crate::*; | ||
|
||
pub trait ViewBuilder: QueryBuilder + QuotedBuilder { | ||
fn prepare_view_create_statement(&self, create: &ViewCreateStatement, sql: &mut SqlWriter) {} | ||
/// Translate [`ViewCreateStatement`] into SQL statement. | ||
fn prepare_view_create_statement(&self, create: &ViewCreateStatement, sql: &mut SqlWriter) { | ||
write!(sql, "CREATE").unwrap(); | ||
self.prepare_view_create_befor_view(create, sql); | ||
|
||
fn prepare_view_drop_statement(&self, drop: &ViewDropStatement, sql: &mut SqlWriter) {} | ||
write!(sql, " VIEW ").unwrap(); | ||
self.prepare_view_create_after_view(create, sql); | ||
|
||
fn prepare_view_rename_statement(&self, rename: &ViewRenameStatement, sql: &mut SqlWriter) {} | ||
if let Some(view_ref) = &create.view { | ||
self.prepare_view_ref(view_ref, sql); | ||
} | ||
|
||
if !create.columns.is_empty() { | ||
write!(sql, " ( ").unwrap(); | ||
create.columns.iter().fold(true, |first, column| { | ||
if !first { | ||
write!(sql, ", ").unwrap(); | ||
} | ||
column.prepare(sql, self.quote()); | ||
false | ||
}); | ||
write!(sql, " ) ").unwrap(); | ||
} | ||
|
||
write!(sql, " AS ").unwrap(); | ||
// self.prepare_select_statement(create.query, sql); | ||
if let Some(opt) = &create.opt { | ||
self.prepare_view_create_opt_update(opt, sql); | ||
} | ||
} | ||
|
||
fn prepare_view_create_befor_view(&self, _create: &ViewCreateStatement, _sql: &mut SqlWriter) {} | ||
|
||
fn prepare_view_create_after_view(&self, _create: &ViewCreateStatement, _sql: &mut SqlWriter) {} | ||
|
||
fn prepare_view_create_opt_update(&self, opt: &ViewCreateOpt, sql: &mut SqlWriter) { | ||
write!(sql, " WITH ").unwrap(); | ||
match opt { | ||
ViewCreateOpt::Cascade => write!(sql, "CASCADED").unwrap(), | ||
ViewCreateOpt::Local => write!(sql, "LOCAL").unwrap(), | ||
} | ||
write!(sql, " CHECK OPTION").unwrap(); | ||
} | ||
|
||
fn prepare_view_rename_statement(&self, _rename: &ViewRenameStatement, _sql: &mut SqlWriter); | ||
|
||
/// Translate [`ViewDropStatement`] into SQL statement. | ||
fn prepare_view_drop_statement(&self, drop: &ViewDropStatement, sql: &mut SqlWriter) { | ||
write!(sql, "DROP VIEW ").unwrap(); | ||
|
||
if drop.if_exists { | ||
write!(sql, "IF EXISTS ").unwrap(); | ||
} | ||
|
||
drop.views.iter().fold(true, |first, view| { | ||
if !first { | ||
write!(sql, ", ").unwrap(); | ||
} | ||
self.prepare_view_ref(view, sql); | ||
false | ||
}); | ||
|
||
let mut view_drop_opt = String::new(); | ||
for drop_opt in drop.options.iter() { | ||
write!(&mut view_drop_opt, " ").unwrap(); | ||
self.prepare_view_drop_opt(drop_opt, &mut view_drop_opt); | ||
} | ||
write!(sql, "{}", view_drop_opt.trim_end()).unwrap(); | ||
} | ||
|
||
/// Translate [`ViewDropOpt`] into SQL statement. | ||
fn prepare_view_drop_opt(&self, drop_opt: &ViewDropOpt, sql: &mut dyn std::fmt::Write) { | ||
write!( | ||
sql, | ||
"{}", | ||
match drop_opt { | ||
ViewDropOpt::Restrict => "RESTRICT", | ||
ViewDropOpt::Cascade => "CASCADE", | ||
} | ||
) | ||
.unwrap(); | ||
} | ||
|
||
/// Translate [`TableRef`] into SQL statement. | ||
fn prepare_view_ref(&self, table_ref: &TableRef, sql: &mut SqlWriter) { | ||
match table_ref { | ||
TableRef::Table(table) => { | ||
table.prepare(sql, self.quote()); | ||
} | ||
TableRef::SchemaTable(schema, table) => { | ||
schema.prepare(sql, self.quote()); | ||
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()); | ||
} | ||
_ => panic!("Not supported"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.