Skip to content

Basic date/time support #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/sqlast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub enum Value {
HexStringLiteral(String),
/// Boolean value true or false
Boolean(bool),
/// Date literals
Date(String),
/// Time literals
Time(String),
/// Timestamp literals, which include both a date and time
Timestamp(String),
/// NULL value in insert statements,
Null,
}
Expand All @@ -28,6 +34,9 @@ impl ToString for Value {
Value::NationalStringLiteral(v) => format!("N'{}'", v),
Value::HexStringLiteral(v) => format!("X'{}'", v),
Value::Boolean(v) => v.to_string(),
Value::Date(v) => format!("DATE '{}'", escape_single_quote_string(v)),
Value::Time(v) => format!("TIME '{}'", escape_single_quote_string(v)),
Value::Timestamp(v) => format!("TIMESTAMP '{}'", escape_single_quote_string(v)),
Value::Null => "NULL".to_string(),
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,17 @@ impl Parser {
}
"CASE" => self.parse_case_expression(),
"CAST" => self.parse_cast_expression(),
"DATE" => Ok(ASTNode::SQLValue(Value::Date(self.parse_literal_string()?))),
"EXISTS" => self.parse_exists_expression(),
"EXTRACT" => self.parse_extract_expression(),
"NOT" => Ok(ASTNode::SQLUnary {
operator: SQLOperator::Not,
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
}),
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
"TIMESTAMP" => Ok(ASTNode::SQLValue(Value::Timestamp(
self.parse_literal_string()?,
))),
// Here `w` is a word, check if it's a part of a multi-part
// identifier, a function call, or a simple identifier:
_ => match self.peek_token() {
Expand Down
30 changes: 30 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,36 @@ fn parse_literal_string() {
one_statement_parses_to("SELECT x'deadBEEF'", "SELECT X'deadBEEF'");
}

#[test]
fn parse_literal_date() {
let sql = "SELECT DATE '1999-01-01'";
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLValue(Value::Date("1999-01-01".into())),
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_literal_time() {
let sql = "SELECT TIME '01:23:34'";
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLValue(Value::Time("01:23:34".into())),
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_literal_timestamp() {
let sql = "SELECT TIMESTAMP '1999-01-01 01:23:34'";
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLValue(Value::Timestamp("1999-01-01 01:23:34".into())),
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_simple_math_expr_plus() {
let sql = "SELECT a + b, 2 + a, 2.5 + a, a_f + b_f, 2 + a_f, 2.5 + a_f FROM c";
Expand Down