Skip to content
Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Given that the parser produces a typed AST, any changes to the AST will technica
Check https://github.com/ballista-compute/sqlparser-rs/commits/main for undocumented changes.


## [0.8.0] 2020-03-21

### Added
* Add support for `TRY_CAST` syntax (#299) - Thanks @seddonm1!

## [0.8.0] 2020-02-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sqlparser"
description = "Extensible SQL Lexer and Parser with support for ANSI SQL:2011"
version = "0.8.1-alpha.0"
version = "0.9.1-alpha.0"
authors = ["Andy Grove <andygrove73@gmail.com>"]
homepage = "https://github.com/ballista-compute/sqlparser-rs"
documentation = "https://docs.rs/sqlparser/"
Expand Down
76 changes: 69 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use self::query::{
Values, With,
};
pub use self::value::{DateTimeField, Value};
use std::option::Option::Some;

struct DisplaySeparated<'a, T>
where
Expand Down Expand Up @@ -201,6 +202,12 @@ pub enum Expr {
expr: Box<Expr>,
data_type: DataType,
},
/// TRY_CAST an expression to a different data type e.g. `TRY_CAST(foo AS VARCHAR(123))`
// this differs from CAST in the choice of how to implement invalid conversions
TryCast {
expr: Box<Expr>,
data_type: DataType,
},
/// EXTRACT(DateTimeField FROM <expr>)
Extract {
field: DateTimeField,
Expand Down Expand Up @@ -253,6 +260,8 @@ pub enum Expr {
Subquery(Box<Query>),
/// The `LISTAGG` function `SELECT LISTAGG(...) WITHIN GROUP (ORDER BY ...)`
ListAgg(ListAgg),
/// `?` Parameter Mark
ParameterMark(u32),
}

impl fmt::Display for Expr {
Expand Down Expand Up @@ -309,6 +318,7 @@ impl fmt::Display for Expr {
}
}
Expr::Cast { expr, data_type } => write!(f, "CAST({} AS {})", expr, data_type),
Expr::TryCast { expr, data_type } => write!(f, "TRY_CAST({} AS {})", expr, data_type),
Expr::Extract { field, expr } => write!(f, "EXTRACT({} FROM {})", field, expr),
Expr::Collate { expr, collation } => write!(f, "{} COLLATE {}", expr, collation),
Expr::Nested(ast) => write!(f, "({})", ast),
Expand Down Expand Up @@ -355,6 +365,7 @@ impl fmt::Display for Expr {

write!(f, ")")
}
Expr::ParameterMark(_) => write!(f, "?"),
}
}
}
Expand Down Expand Up @@ -549,6 +560,8 @@ pub enum Statement {
assignments: Vec<Assignment>,
/// WHERE
selection: Option<Expr>,
/// `LIMIT { <N> | ALL }`
limit: Option<Expr>,
},
/// DELETE
Delete {
Expand Down Expand Up @@ -640,6 +653,10 @@ pub enum Statement {
///
/// Note: this is a PostgreSQL-specific statement.
ShowVariable { variable: Vec<Ident> },
/// USE <variable>
///
/// Note: this is a MySQL-specific statement.
UseDatabase { variable: Ident },
/// SHOW COLUMNS
///
/// Note: this is a MySQL-specific statement.
Expand All @@ -651,12 +668,27 @@ pub enum Statement {
},
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
StartTransaction { modes: Vec<TransactionMode> },
/// `SET TRANSACTION ...`
SetTransaction { modes: Vec<TransactionMode> },
/// `SET [SESSION] TRANSACTION ...`
SetTransaction {
session: bool,
modes: Vec<TransactionMode>,
},
/// SET NAMES <variable>
///
/// Note: this is a MySQL-specific statement.
SetNames { variable: Ident },
/// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
Commit { chain: bool },
/// `SAVEPOINT identifier`
Savepoint { variable: Ident },
/// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
Rollback { chain: bool },
/// `ROLLBACK [WORK] TO [SAVEPOINT] identifier`
Rollback {
chain: bool,
savepoint: Option<Ident>,
},
/// RELEASE SAVEPOINT identifier
Release { variable: Ident },
/// CREATE SCHEMA
CreateSchema {
schema_name: ObjectName,
Expand Down Expand Up @@ -869,6 +901,7 @@ impl fmt::Display for Statement {
table_name,
assignments,
selection,
limit,
} => {
write!(f, "UPDATE {}", table_name)?;
if !assignments.is_empty() {
Expand All @@ -877,6 +910,9 @@ impl fmt::Display for Statement {
if let Some(selection) = selection {
write!(f, " WHERE {}", selection)?;
}
if let Some(ref limit) = limit {
write!(f, " LIMIT {}", limit)?;
}
Ok(())
}
Statement::Delete {
Expand Down Expand Up @@ -1151,6 +1187,10 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::UseDatabase { variable } => {
write!(f, "USE {}", variable)?;
Ok(())
}
Statement::ShowColumns {
extended,
full,
Expand All @@ -1176,18 +1216,38 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::SetTransaction { modes } => {
write!(f, "SET TRANSACTION")?;
Statement::SetTransaction { session, modes } => {
write!(
f,
"SET{} TRANSACTION",
if *session { " SESSION" } else { "" }
)?;
if !modes.is_empty() {
write!(f, " {}", display_comma_separated(modes))?;
}
Ok(())
}
Statement::SetNames { variable } => {
write!(f, "SET NAMES {}", variable)?;
Ok(())
}
Statement::Commit { chain } => {
write!(f, "COMMIT{}", if *chain { " AND CHAIN" } else { "" },)
}
Statement::Rollback { chain } => {
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
Statement::Savepoint { variable } => {
write!(f, "SAVEPOINT {}", variable)?;
Ok(())
}
Statement::Rollback { chain, savepoint } => {
if let Some(savepoint) = savepoint {
write!(f, "ROLLBACK TO SAVEPOINT {}", savepoint)
} else {
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
}
}
Statement::Release { variable } => {
write!(f, "RELEASE SAVEPOINT {}", variable)?;
Ok(())
}
Statement::CreateSchema {
schema_name,
Expand Down Expand Up @@ -1539,6 +1599,7 @@ impl fmt::Display for TransactionIsolationLevel {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ShowStatementFilter {
Like(String),
ILike(String),
Where(Expr),
}

Expand All @@ -1547,6 +1608,7 @@ impl fmt::Display for ShowStatementFilter {
use ShowStatementFilter::*;
match self {
Like(pattern) => write!(f, "LIKE '{}'", value::escape_single_quote_string(pattern)),
ILike(pattern) => write!(f, "ILIKE {}", value::escape_single_quote_string(pattern)),
Where(expr) => write!(f, "WHERE {}", expr),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum BinaryOperator {
Or,
Like,
NotLike,
ILike,
NotILike,
BitwiseOr,
BitwiseAnd,
BitwiseXor,
Expand Down Expand Up @@ -100,6 +102,8 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::Or => "OR",
BinaryOperator::Like => "LIKE",
BinaryOperator::NotLike => "NOT LIKE",
BinaryOperator::ILike => "ILIKE",
BinaryOperator::NotILike => "NOT ILIKE",
BinaryOperator::BitwiseOr => "|",
BinaryOperator::BitwiseAnd => "&",
BinaryOperator::BitwiseXor => "^",
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ define_keywords!(
IDENTITY,
IF,
IGNORE,
ILIKE,
IN,
INDEX,
INDICATOR,
Expand Down Expand Up @@ -291,6 +292,7 @@ define_keywords!(
MONTH,
MSCK,
MULTISET,
NAMES,
NATIONAL,
NATURAL,
NCHAR,
Expand Down Expand Up @@ -456,6 +458,7 @@ define_keywords!(
TRIM_ARRAY,
TRUE,
TRUNCATE,
TRY_CAST,
UESCAPE,
UNBOUNDED,
UNCOMMITTED,
Expand All @@ -465,6 +468,7 @@ define_keywords!(
UNNEST,
UPDATE,
UPPER,
USE,
USER,
USING,
UUID,
Expand Down
2 changes: 2 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl Dialect for MySqlDialect {
|| ('A'..='Z').contains(&ch)
|| ch == '_'
|| ch == '$'
|| ch == '@'
|| ch == '?'
|| ('\u{0080}'..='\u{ffff}').contains(&ch)
}

Expand Down
Loading