Support SHOW <var> and SET <var>

This commit is contained in:
Nikhil Benesch 2019-08-14 14:59:55 -04:00
parent f64928e994
commit e1ded184f8
No known key found for this signature in database
GPG key ID: FCF98542083C5A69
4 changed files with 168 additions and 9 deletions

View file

@ -16,6 +16,7 @@
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
use sqlparser::parser::ParserError;
use sqlparser::test_utils::*;
#[test]
@ -251,6 +252,102 @@ PHP ₱ USD $
//assert_eq!(sql, ast.to_string());
}
#[test]
fn parse_set() {
let stmt = pg_and_generic().verified_stmt("SET a = b");
assert_eq!(
stmt,
Statement::SetVariable {
local: false,
variable: "a".into(),
value: SetVariableValue::Ident("b".into()),
}
);
let stmt = pg_and_generic().verified_stmt("SET a = 'b'");
assert_eq!(
stmt,
Statement::SetVariable {
local: false,
variable: "a".into(),
value: SetVariableValue::Literal(Value::SingleQuotedString("b".into())),
}
);
let stmt = pg_and_generic().verified_stmt("SET a = 0");
assert_eq!(
stmt,
Statement::SetVariable {
local: false,
variable: "a".into(),
value: SetVariableValue::Literal(Value::Long(0)),
}
);
let stmt = pg_and_generic().verified_stmt("SET a = DEFAULT");
assert_eq!(
stmt,
Statement::SetVariable {
local: false,
variable: "a".into(),
value: SetVariableValue::Ident("DEFAULT".into()),
}
);
let stmt = pg_and_generic().verified_stmt("SET LOCAL a = b");
assert_eq!(
stmt,
Statement::SetVariable {
local: true,
variable: "a".into(),
value: SetVariableValue::Ident("b".into()),
}
);
pg_and_generic().one_statement_parses_to("SET a TO b", "SET a = b");
pg_and_generic().one_statement_parses_to("SET SESSION a = b", "SET a = b");
assert_eq!(
pg_and_generic().parse_sql_statements("SET"),
Err(ParserError::ParserError(
"Expected identifier, found: EOF".to_string()
)),
);
assert_eq!(
pg_and_generic().parse_sql_statements("SET a b"),
Err(ParserError::ParserError(
"Expected equals sign or TO, found: b".to_string()
)),
);
assert_eq!(
pg_and_generic().parse_sql_statements("SET a ="),
Err(ParserError::ParserError(
"Expected variable value, found: EOF".to_string()
)),
);
}
#[test]
fn parse_show() {
let stmt = pg_and_generic().verified_stmt("SHOW a");
assert_eq!(
stmt,
Statement::ShowVariable {
variable: "a".into()
}
);
let stmt = pg_and_generic().verified_stmt("SHOW ALL");
assert_eq!(
stmt,
Statement::ShowVariable {
variable: "ALL".into()
}
)
}
fn pg() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {})],