Merge pull request #98 from benesch/col-names

Support column names in more places
This commit is contained in:
Nikhil Benesch 2019-06-04 00:00:07 -04:00 committed by GitHub
commit 4c2722280d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 17 deletions

View file

@ -25,7 +25,7 @@ use std::ops::Deref;
pub use self::ddl::{AlterTableOperation, TableConstraint};
pub use self::query::{
Cte, Fetch, Join, JoinConstraint, JoinOperator, SQLOrderByExpr, SQLQuery, SQLSelect,
SQLSelectItem, SQLSetExpr, SQLSetOperator, SQLValues, TableFactor,
SQLSelectItem, SQLSetExpr, SQLSetOperator, SQLValues, TableAlias, TableFactor,
};
pub use self::sqltype::SQLType;
pub use self::value::Value;
@ -377,6 +377,7 @@ pub enum SQLStatement {
SQLCreateView {
/// View name
name: SQLObjectName,
columns: Vec<SQLIdent>,
query: Box<SQLQuery>,
materialized: bool,
with_options: Vec<SQLOption>,
@ -474,6 +475,7 @@ impl ToString for SQLStatement {
}
SQLStatement::SQLCreateView {
name,
columns,
query,
materialized,
with_options,
@ -484,12 +486,18 @@ impl ToString for SQLStatement {
} else {
"".into()
};
let columns = if !columns.is_empty() {
format!(" ({})", comma_separated_string(columns))
} else {
"".into()
};
format!(
"CREATE{} VIEW {}{} AS {}",
"CREATE{} VIEW {}{}{} AS {}",
modifier,
name.to_string(),
with_options,
query.to_string()
columns,
query.to_string(),
)
}
SQLStatement::SQLCreateTable {

View file

@ -202,7 +202,7 @@ impl ToString for SQLSelectItem {
pub enum TableFactor {
Table {
name: SQLObjectName,
alias: Option<SQLIdent>,
alias: Option<TableAlias>,
/// Arguments of a table-valued function, as supported by Postgres
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
/// will also be parsed as `args`.
@ -213,7 +213,7 @@ pub enum TableFactor {
Derived {
lateral: bool,
subquery: Box<SQLQuery>,
alias: Option<SQLIdent>,
alias: Option<TableAlias>,
},
}
@ -231,7 +231,7 @@ impl ToString for TableFactor {
s += &format!("({})", comma_separated_string(args))
};
if let Some(alias) = alias {
s += &format!(" AS {}", alias);
s += &format!(" AS {}", alias.to_string());
}
if !with_hints.is_empty() {
s += &format!(" WITH ({})", comma_separated_string(with_hints));
@ -249,7 +249,7 @@ impl ToString for TableFactor {
}
s += &format!("({})", subquery.to_string());
if let Some(alias) = alias {
s += &format!(" AS {}", alias);
s += &format!(" AS {}", alias.to_string());
}
s
}
@ -257,6 +257,22 @@ impl ToString for TableFactor {
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct TableAlias {
pub name: SQLIdent,
pub columns: Vec<SQLIdent>,
}
impl ToString for TableAlias {
fn to_string(&self) -> String {
let mut s = self.name.clone();
if !self.columns.is_empty() {
s += &format!(" ({})", comma_separated_string(&self.columns));
}
s
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Join {
pub relation: TableFactor,

View file

@ -775,7 +775,7 @@ impl Parser {
// Many dialects support `OR REPLACE` | `OR ALTER` right after `CREATE`, but we don't (yet).
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
let name = self.parse_object_name()?;
// Parenthesized "output" columns list could be handled here.
let columns = self.parse_parenthesized_column_list(Optional)?;
let with_options = if self.parse_keyword("WITH") {
self.parse_with_options()?
} else {
@ -786,6 +786,7 @@ impl Parser {
// Optional `WITH [ CASCADED | LOCAL ] CHECK OPTION` is widely supported here.
Ok(SQLStatement::SQLCreateView {
name,
columns,
query,
materialized,
with_options,
@ -1207,6 +1208,23 @@ impl Parser {
}
}
/// Parse `AS identifier` when the AS is describing a table-valued object,
/// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
/// the alias is allowed to optionally name the columns in the table, in
/// addition to the table itself.
pub fn parse_optional_table_alias(
&mut self,
reserved_kwds: &[&str],
) -> Result<Option<TableAlias>, ParserError> {
match self.parse_optional_alias(reserved_kwds)? {
Some(name) => {
let columns = self.parse_parenthesized_column_list(Optional)?;
Ok(Some(TableAlias { name, columns }))
}
None => Ok(None),
}
}
/// Parse one or more identifiers with the specified separator between them
pub fn parse_list_of_ids(&mut self, separator: &Token) -> Result<Vec<SQLIdent>, ParserError> {
let mut idents = vec![];
@ -1490,7 +1508,7 @@ impl Parser {
if self.consume_token(&Token::LParen) {
let subquery = Box::new(self.parse_query()?);
self.expect_token(&Token::RParen)?;
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(TableFactor::Derived {
lateral,
subquery,
@ -1506,7 +1524,7 @@ impl Parser {
} else {
vec![]
};
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
// MSSQL-specific table hints:
let mut with_hints = vec![];
if self.parse_keyword("WITH") {