Merge pull request #245 from nickolay/cleanups

Minor code cleanups
This commit is contained in:
Nickolay Ponomarev 2020-07-29 02:13:16 +03:00 committed by GitHub
commit 1337820c06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 56 deletions

View file

@ -299,7 +299,7 @@ impl fmt::Display for Expr {
results, results,
else_result, else_result,
} => { } => {
f.write_str("CASE")?; write!(f, "CASE")?;
if let Some(operand) = operand { if let Some(operand) = operand {
write!(f, " {}", operand)?; write!(f, " {}", operand)?;
} }
@ -310,7 +310,7 @@ impl fmt::Display for Expr {
if let Some(else_result) = else_result { if let Some(else_result) = else_result {
write!(f, " ELSE {}", else_result)?; write!(f, " ELSE {}", else_result)?;
} }
f.write_str(" END") write!(f, " END")
} }
Expr::Exists(s) => write!(f, "EXISTS ({})", s), Expr::Exists(s) => write!(f, "EXISTS ({})", s),
Expr::Subquery(s) => write!(f, "({})", s), Expr::Subquery(s) => write!(f, "({})", s),
@ -626,8 +626,7 @@ impl fmt::Display for Statement {
} => { } => {
write!(f, "UPDATE {}", table_name)?; write!(f, "UPDATE {}", table_name)?;
if !assignments.is_empty() { if !assignments.is_empty() {
write!(f, " SET ")?; write!(f, " SET {}", display_comma_separated(assignments))?;
write!(f, "{}", display_comma_separated(assignments))?;
} }
if let Some(selection) = selection { if let Some(selection) = selection {
write!(f, " WHERE {}", selection)?; write!(f, " WHERE {}", selection)?;
@ -652,26 +651,19 @@ impl fmt::Display for Statement {
materialized, materialized,
with_options, with_options,
} => { } => {
write!(f, "CREATE")?; write!(
f,
if *or_replace { "CREATE {or_replace}{materialized}VIEW {name}",
write!(f, " OR REPLACE")?; or_replace = if *or_replace { "OR REPLACE " } else { "" },
} materialized = if *materialized { "MATERIALIZED " } else { "" },
name = name
if *materialized { )?;
write!(f, " MATERIALIZED")?;
}
write!(f, " VIEW {}", name)?;
if !with_options.is_empty() { if !with_options.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_options))?; write!(f, " WITH ({})", display_comma_separated(with_options))?;
} }
if !columns.is_empty() { if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?; write!(f, " ({})", display_comma_separated(columns))?;
} }
write!(f, " AS {}", query) write!(f, " AS {}", query)
} }
Statement::CreateTable { Statement::CreateTable {
@ -716,7 +708,6 @@ impl fmt::Display for Statement {
if *without_rowid { if *without_rowid {
write!(f, " WITHOUT ROWID")?; write!(f, " WITHOUT ROWID")?;
} }
if *external { if *external {
write!( write!(
f, f,
@ -757,22 +748,15 @@ impl fmt::Display for Statement {
columns, columns,
unique, unique,
if_not_exists, if_not_exists,
} => { } => write!(
write!( f,
f, "CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns})",
"CREATE{}INDEX{}{} ON {}({}", unique = if *unique { "UNIQUE " } else { "" },
if *unique { " UNIQUE " } else { " " }, if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
if *if_not_exists { name = name,
" IF NOT EXISTS " table_name = table_name,
} else { columns = display_separated(columns, ",")
" " ),
},
name,
table_name,
display_separated(columns, ",")
)?;
write!(f, ");")
}
Statement::AlterTable { name, operation } => { Statement::AlterTable { name, operation } => {
write!(f, "ALTER TABLE {} {}", name, operation) write!(f, "ALTER TABLE {} {}", name, operation)
} }
@ -793,13 +777,13 @@ impl fmt::Display for Statement {
local, local,
variable, variable,
value, value,
} => { } => write!(
f.write_str("SET ")?; f,
if *local { "SET{local} {variable} = {value}",
f.write_str("LOCAL ")?; local = if *local { " LOCAL" } else { "" },
} variable = variable,
write!(f, "{} = {}", variable, value) value = value
} ),
Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable), Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable),
Statement::ShowColumns { Statement::ShowColumns {
extended, extended,
@ -807,14 +791,13 @@ impl fmt::Display for Statement {
table_name, table_name,
filter, filter,
} => { } => {
f.write_str("SHOW ")?; write!(
if *extended { f,
f.write_str("EXTENDED ")?; "SHOW {extended}{full}COLUMNS FROM {table_name}",
} extended = if *extended { "EXTENDED " } else { "" },
if *full { full = if *full { "FULL " } else { "" },
f.write_str("FULL ")?; table_name = table_name,
} )?;
write!(f, "COLUMNS FROM {}", table_name)?;
if let Some(filter) = filter { if let Some(filter) = filter {
write!(f, " {}", filter)?; write!(f, " {}", filter)?;
} }
@ -843,7 +826,6 @@ impl fmt::Display for Statement {
Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name), Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name),
Statement::Assert { condition, message } => { Statement::Assert { condition, message } => {
write!(f, "ASSERT {}", condition)?; write!(f, "ASSERT {}", condition)?;
if let Some(m) = message { if let Some(m) = message {
write!(f, " AS {}", m)?; write!(f, " AS {}", m)?;
} }
@ -941,7 +923,7 @@ impl fmt::Display for FileFormat {
PARQUET => "PARQUET", PARQUET => "PARQUET",
AVRO => "AVRO", AVRO => "AVRO",
RCFILE => "RCFILE", RCFILE => "RCFILE",
JSONFILE => "TEXTFILE", JSONFILE => "JSONFILE",
}) })
} }
} }

View file

@ -3152,7 +3152,7 @@ fn ensure_multiple_dialects_are_tested() {
#[test] #[test]
fn parse_create_index() { fn parse_create_index() {
let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age);"; let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age)";
let ident_vec = vec![Ident::new("name"), Ident::new("age")]; let ident_vec = vec![Ident::new("name"), Ident::new("age")];
match verified_stmt(sql) { match verified_stmt(sql) {
Statement::CreateIndex { Statement::CreateIndex {

View file

@ -474,14 +474,12 @@ fn parse_execute() {
); );
let stmt = pg_and_generic().verified_stmt("EXECUTE a(1, 't')"); let stmt = pg_and_generic().verified_stmt("EXECUTE a(1, 't')");
#[cfg(feature = "bigdecimal")]
assert_eq!( assert_eq!(
stmt, stmt,
Statement::Execute { Statement::Execute {
name: "a".into(), name: "a".into(),
parameters: vec![ parameters: vec![
Expr::Value(Value::Number(bigdecimal::BigDecimal::from(1))), Expr::Value(number("1")),
Expr::Value(Value::SingleQuotedString("t".to_string())) Expr::Value(Value::SingleQuotedString("t".to_string()))
], ],
} }