diff --git a/examples/cli.rs b/examples/cli.rs index f288fd138..77a0b5014 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -19,7 +19,7 @@ use simple_logger; use std::fs; use sqlparser::dialect::*; -use sqlparser::sqlparser::Parser; +use sqlparser::parser::Parser; fn main() { simple_logger::init().unwrap(); @@ -30,10 +30,10 @@ fn main() { ); let dialect: Box = match std::env::args().nth(2).unwrap_or_default().as_ref() { - "--ansi" => Box::new(AnsiSqlDialect {}), + "--ansi" => Box::new(AnsiDialect {}), "--postgres" => Box::new(PostgreSqlDialect {}), "--ms" => Box::new(MsSqlDialect {}), - "--generic" | "" => Box::new(GenericSqlDialect {}), + "--generic" | "" => Box::new(GenericDialect {}), s => panic!("Unexpected parameter: {}", s), }; diff --git a/examples/parse_select.rs b/examples/parse_select.rs index d79ab26ed..539d91652 100644 --- a/examples/parse_select.rs +++ b/examples/parse_select.rs @@ -12,8 +12,8 @@ #![warn(clippy::all)] -use sqlparser::dialect::GenericSqlDialect; -use sqlparser::sqlparser::*; +use sqlparser::dialect::GenericDialect; +use sqlparser::parser::*; fn main() { let sql = "SELECT a, b, 123, myfunc(b) \ @@ -21,7 +21,7 @@ fn main() { WHERE a > b AND b < 100 \ ORDER BY a DESC, b"; - let dialect = GenericSqlDialect {}; + let dialect = GenericDialect {}; let ast = Parser::parse_sql(&dialect, sql.to_string()).unwrap(); diff --git a/src/sqlast/sqltype.rs b/src/ast/data_type.rs similarity index 58% rename from src/sqlast/sqltype.rs rename to src/ast/data_type.rs index 5b63f1db4..4aede8590 100644 --- a/src/sqlast/sqltype.rs +++ b/src/ast/data_type.rs @@ -10,11 +10,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::SQLObjectName; +use super::ObjectName; /// SQL data types #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLType { +pub enum DataType { /// Fixed-length character type e.g. CHAR(10) Char(Option), /// Variable-length character type e.g. VARCHAR(10) @@ -60,44 +60,44 @@ pub enum SQLType { /// Bytea Bytea, /// Custom type such as enums - Custom(SQLObjectName), + Custom(ObjectName), /// Arrays - Array(Box), + Array(Box), } -impl ToString for SQLType { +impl ToString for DataType { fn to_string(&self) -> String { match self { - SQLType::Char(size) => format_type_with_optional_length("char", size), - SQLType::Varchar(size) => format_type_with_optional_length("character varying", size), - SQLType::Uuid => "uuid".to_string(), - SQLType::Clob(size) => format!("clob({})", size), - SQLType::Binary(size) => format!("binary({})", size), - SQLType::Varbinary(size) => format!("varbinary({})", size), - SQLType::Blob(size) => format!("blob({})", size), - SQLType::Decimal(precision, scale) => { + DataType::Char(size) => format_type_with_optional_length("char", size), + DataType::Varchar(size) => format_type_with_optional_length("character varying", size), + DataType::Uuid => "uuid".to_string(), + DataType::Clob(size) => format!("clob({})", size), + DataType::Binary(size) => format!("binary({})", size), + DataType::Varbinary(size) => format!("varbinary({})", size), + DataType::Blob(size) => format!("blob({})", size), + DataType::Decimal(precision, scale) => { if let Some(scale) = scale { format!("numeric({},{})", precision.unwrap(), scale) } else { format_type_with_optional_length("numeric", precision) } } - SQLType::Float(size) => format_type_with_optional_length("float", size), - SQLType::SmallInt => "smallint".to_string(), - SQLType::Int => "int".to_string(), - SQLType::BigInt => "bigint".to_string(), - SQLType::Real => "real".to_string(), - SQLType::Double => "double".to_string(), - SQLType::Boolean => "boolean".to_string(), - SQLType::Date => "date".to_string(), - SQLType::Time => "time".to_string(), - SQLType::Timestamp => "timestamp".to_string(), - SQLType::Interval => "interval".to_string(), - SQLType::Regclass => "regclass".to_string(), - SQLType::Text => "text".to_string(), - SQLType::Bytea => "bytea".to_string(), - SQLType::Array(ty) => format!("{}[]", ty.to_string()), - SQLType::Custom(ty) => ty.to_string(), + DataType::Float(size) => format_type_with_optional_length("float", size), + DataType::SmallInt => "smallint".to_string(), + DataType::Int => "int".to_string(), + DataType::BigInt => "bigint".to_string(), + DataType::Real => "real".to_string(), + DataType::Double => "double".to_string(), + DataType::Boolean => "boolean".to_string(), + DataType::Date => "date".to_string(), + DataType::Time => "time".to_string(), + DataType::Timestamp => "timestamp".to_string(), + DataType::Interval => "interval".to_string(), + DataType::Regclass => "regclass".to_string(), + DataType::Text => "text".to_string(), + DataType::Bytea => "bytea".to_string(), + DataType::Array(ty) => format!("{}[]", ty.to_string()), + DataType::Custom(ty) => ty.to_string(), } } } diff --git a/src/sqlast/ddl.rs b/src/ast/ddl.rs similarity index 89% rename from src/sqlast/ddl.rs rename to src/ast/ddl.rs index e39e533be..3f7c42819 100644 --- a/src/sqlast/ddl.rs +++ b/src/ast/ddl.rs @@ -1,6 +1,6 @@ //! AST types specific to CREATE/ALTER variants of `SQLStatement` //! (commonly referred to as Data Definition Language, or DDL) -use super::{Expr, SQLIdent, SQLObjectName, SQLType}; +use super::{DataType, Expr, Ident, ObjectName}; /// An `ALTER TABLE` (`SQLStatement::SQLAlterTable`) operation #[derive(Debug, Clone, PartialEq, Hash)] @@ -8,7 +8,7 @@ pub enum AlterTableOperation { /// `ADD ` AddConstraint(TableConstraint), /// TODO: implement `DROP CONSTRAINT ` - DropConstraint { name: SQLIdent }, + DropConstraint { name: Ident }, } impl ToString for AlterTableOperation { @@ -26,22 +26,22 @@ impl ToString for AlterTableOperation { pub enum TableConstraint { /// `[ CONSTRAINT ] { PRIMARY KEY | UNIQUE } ()` Unique { - name: Option, - columns: Vec, + name: Option, + columns: Vec, /// Whether this is a `PRIMARY KEY` or just a `UNIQUE` constraint is_primary: bool, }, /// A referential integrity constraint (`[ CONSTRAINT ] FOREIGN KEY () /// REFERENCES ()`) ForeignKey { - name: Option, - columns: Vec, - foreign_table: SQLObjectName, - referred_columns: Vec, + name: Option, + columns: Vec, + foreign_table: ObjectName, + referred_columns: Vec, }, /// `[ CONSTRAINT ] CHECK ()` Check { - name: Option, + name: Option, expr: Box, }, } @@ -82,14 +82,14 @@ impl ToString for TableConstraint { /// SQL column definition #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLColumnDef { - pub name: SQLIdent, - pub data_type: SQLType, - pub collation: Option, +pub struct ColumnDef { + pub name: Ident, + pub data_type: DataType, + pub collation: Option, pub options: Vec, } -impl ToString for SQLColumnDef { +impl ToString for ColumnDef { fn to_string(&self) -> String { format!( "{} {}{}", @@ -122,7 +122,7 @@ impl ToString for SQLColumnDef { /// "column options," and we allow any column option to be named. #[derive(Debug, Clone, PartialEq, Hash)] pub struct ColumnOptionDef { - pub name: Option, + pub name: Option, pub option: ColumnOption, } @@ -153,8 +153,8 @@ pub enum ColumnOption { /// A referential integrity constraint (`[FOREIGN KEY REFERENCES /// ()`). ForeignKey { - foreign_table: SQLObjectName, - referred_columns: Vec, + foreign_table: ObjectName, + referred_columns: Vec, }, // `CHECK ()` Check(Expr), @@ -187,7 +187,7 @@ impl ToString for ColumnOption { } } -fn format_constraint_name(name: &Option) -> String { +fn format_constraint_name(name: &Option) -> String { name.as_ref() .map(|name| format!("CONSTRAINT {} ", name)) .unwrap_or_default() diff --git a/src/sqlast/mod.rs b/src/ast/mod.rs similarity index 76% rename from src/sqlast/mod.rs rename to src/ast/mod.rs index 34efb0f4c..cac2ccffb 100644 --- a/src/sqlast/mod.rs +++ b/src/ast/mod.rs @@ -12,23 +12,23 @@ //! SQL Abstract Syntax Tree (AST) types +mod data_type; mod ddl; +mod operator; mod query; -mod sql_operator; -mod sqltype; mod value; use std::ops::Deref; +pub use self::data_type::DataType; pub use self::ddl::{ - AlterTableOperation, ColumnOption, ColumnOptionDef, SQLColumnDef, TableConstraint, + AlterTableOperation, ColumnDef, ColumnOption, ColumnOptionDef, TableConstraint, }; +pub use self::operator::{BinaryOperator, UnaryOperator}; pub use self::query::{ - Cte, Fetch, Join, JoinConstraint, JoinOperator, SQLOrderByExpr, SQLQuery, SQLSelect, - SQLSelectItem, SQLSetExpr, SQLSetOperator, SQLValues, TableAlias, TableFactor, TableWithJoins, + Cte, Fetch, Join, JoinConstraint, JoinOperator, OrderByExpr, Query, Select, SelectItem, + SetExpr, SetOperator, TableAlias, TableFactor, TableWithJoins, Values, }; -pub use self::sql_operator::{SQLBinaryOperator, SQLUnaryOperator}; -pub use self::sqltype::SQLType; pub use self::value::{SQLDateTimeField, Value}; /// Like `vec.join(", ")`, but for any types implementing ToString. @@ -45,7 +45,7 @@ where } /// Identifier name, in the originally quoted form (e.g. `"id"`) -pub type SQLIdent = String; +pub type Ident = String; /// An SQL expression of any type. /// @@ -55,76 +55,76 @@ pub type SQLIdent = String; #[derive(Debug, Clone, PartialEq, Hash)] pub enum Expr { /// Identifier e.g. table name or column name - SQLIdentifier(SQLIdent), + Identifier(Ident), /// Unqualified wildcard (`*`). SQL allows this in limited contexts, such as: /// - right after `SELECT` (which is represented as a [SQLSelectItem::Wildcard] instead) /// - or as part of an aggregate function, e.g. `COUNT(*)`, /// /// ...but we currently also accept it in contexts where it doesn't make /// sense, such as `* + *` - SQLWildcard, + Wildcard, /// Qualified wildcard, e.g. `alias.*` or `schema.table.*`. /// (Same caveats apply to SQLQualifiedWildcard as to SQLWildcard.) - SQLQualifiedWildcard(Vec), + QualifiedWildcard(Vec), /// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col` - SQLCompoundIdentifier(Vec), + CompoundIdentifier(Vec), /// `IS NULL` expression - SQLIsNull(Box), + IsNull(Box), /// `IS NOT NULL` expression - SQLIsNotNull(Box), + IsNotNull(Box), /// `[ NOT ] IN (val1, val2, ...)` - SQLInList { + InList { expr: Box, list: Vec, negated: bool, }, /// `[ NOT ] IN (SELECT ...)` - SQLInSubquery { + InSubquery { expr: Box, - subquery: Box, + subquery: Box, negated: bool, }, /// ` [ NOT ] BETWEEN AND ` - SQLBetween { + Between { expr: Box, negated: bool, low: Box, high: Box, }, /// Binary operation e.g. `1 + 1` or `foo > bar` - SQLBinaryOp { + BinaryOp { left: Box, - op: SQLBinaryOperator, + op: BinaryOperator, right: Box, }, /// Unary operation e.g. `NOT foo` - SQLUnaryOp { - op: SQLUnaryOperator, + UnaryOp { op: UnaryOperator, expr: Box }, + /// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))` + Cast { expr: Box, + data_type: DataType, }, - /// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))` - SQLCast { expr: Box, data_type: SQLType }, - SQLExtract { + Extract { field: SQLDateTimeField, expr: Box, }, /// `expr COLLATE collation` - SQLCollate { + Collate { expr: Box, - collation: SQLObjectName, + collation: ObjectName, }, /// Nested expression e.g. `(foo > bar)` or `(1)` - SQLNested(Box), + Nested(Box), /// SQLValue - SQLValue(Value), + Value(Value), /// Scalar function call e.g. `LEFT(foo, 5)` - SQLFunction(SQLFunction), + Function(Function), /// `CASE [] WHEN THEN ... [ELSE ] END` /// /// Note we only recognize a complete single expression as ``, /// not `< 0` nor `1, 2, 3` as allowed in a `` per /// - SQLCase { + Case { operand: Option>, conditions: Vec, results: Vec, @@ -132,22 +132,22 @@ pub enum Expr { }, /// An exists expression `EXISTS(SELECT ...)`, used in expressions like /// `WHERE EXISTS (SELECT ...)`. - SQLExists(Box), + Exists(Box), /// A parenthesized subquery `(SELECT ...)`, used in expression like /// `SELECT (subquery) AS x` or `WHERE (subquery) = x` - SQLSubquery(Box), + Subquery(Box), } impl ToString for Expr { fn to_string(&self) -> String { match self { - Expr::SQLIdentifier(s) => s.to_string(), - Expr::SQLWildcard => "*".to_string(), - Expr::SQLQualifiedWildcard(q) => q.join(".") + ".*", - Expr::SQLCompoundIdentifier(s) => s.join("."), - Expr::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()), - Expr::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()), - Expr::SQLInList { + Expr::Identifier(s) => s.to_string(), + Expr::Wildcard => "*".to_string(), + Expr::QualifiedWildcard(q) => q.join(".") + ".*", + Expr::CompoundIdentifier(s) => s.join("."), + Expr::IsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()), + Expr::IsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()), + Expr::InList { expr, list, negated, @@ -157,7 +157,7 @@ impl ToString for Expr { if *negated { "NOT " } else { "" }, comma_separated_string(list) ), - Expr::SQLInSubquery { + Expr::InSubquery { expr, subquery, negated, @@ -167,7 +167,7 @@ impl ToString for Expr { if *negated { "NOT " } else { "" }, subquery.to_string() ), - Expr::SQLBetween { + Expr::Between { expr, negated, low, @@ -179,32 +179,32 @@ impl ToString for Expr { low.to_string(), high.to_string() ), - Expr::SQLBinaryOp { left, op, right } => format!( + Expr::BinaryOp { left, op, right } => format!( "{} {} {}", left.as_ref().to_string(), op.to_string(), right.as_ref().to_string() ), - Expr::SQLUnaryOp { op, expr } => { + Expr::UnaryOp { op, expr } => { format!("{} {}", op.to_string(), expr.as_ref().to_string()) } - Expr::SQLCast { expr, data_type } => format!( + Expr::Cast { expr, data_type } => format!( "CAST({} AS {})", expr.as_ref().to_string(), data_type.to_string() ), - Expr::SQLExtract { field, expr } => { + Expr::Extract { field, expr } => { format!("EXTRACT({} FROM {})", field.to_string(), expr.to_string()) } - Expr::SQLCollate { expr, collation } => format!( + Expr::Collate { expr, collation } => format!( "{} COLLATE {}", expr.as_ref().to_string(), collation.to_string() ), - Expr::SQLNested(ast) => format!("({})", ast.as_ref().to_string()), - Expr::SQLValue(v) => v.to_string(), - Expr::SQLFunction(f) => f.to_string(), - Expr::SQLCase { + Expr::Nested(ast) => format!("({})", ast.as_ref().to_string()), + Expr::Value(v) => v.to_string(), + Expr::Function(f) => f.to_string(), + Expr::Case { operand, conditions, results, @@ -225,21 +225,21 @@ impl ToString for Expr { } s + " END" } - Expr::SQLExists(s) => format!("EXISTS ({})", s.to_string()), - Expr::SQLSubquery(s) => format!("({})", s.to_string()), + Expr::Exists(s) => format!("EXISTS ({})", s.to_string()), + Expr::Subquery(s) => format!("({})", s.to_string()), } } } /// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`) #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLWindowSpec { +pub struct WindowSpec { pub partition_by: Vec, - pub order_by: Vec, - pub window_frame: Option, + pub order_by: Vec, + pub window_frame: Option, } -impl ToString for SQLWindowSpec { +impl ToString for WindowSpec { fn to_string(&self) -> String { let mut clauses = vec![]; if !self.partition_by.is_empty() { @@ -277,39 +277,39 @@ impl ToString for SQLWindowSpec { /// Specifies the data processed by a window function, e.g. /// `RANGE UNBOUNDED PRECEDING` or `ROWS BETWEEN 5 PRECEDING AND CURRENT ROW`. #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLWindowFrame { - pub units: SQLWindowFrameUnits, - pub start_bound: SQLWindowFrameBound, +pub struct WindowFrame { + pub units: WindowFrameUnits, + pub start_bound: WindowFrameBound, /// The right bound of the `BETWEEN .. AND` clause. - pub end_bound: Option, + pub end_bound: Option, // TBD: EXCLUDE } #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLWindowFrameUnits { +pub enum WindowFrameUnits { Rows, Range, Groups, } -impl ToString for SQLWindowFrameUnits { +impl ToString for WindowFrameUnits { fn to_string(&self) -> String { match self { - SQLWindowFrameUnits::Rows => "ROWS".to_string(), - SQLWindowFrameUnits::Range => "RANGE".to_string(), - SQLWindowFrameUnits::Groups => "GROUPS".to_string(), + WindowFrameUnits::Rows => "ROWS".to_string(), + WindowFrameUnits::Range => "RANGE".to_string(), + WindowFrameUnits::Groups => "GROUPS".to_string(), } } } -impl FromStr for SQLWindowFrameUnits { +impl FromStr for WindowFrameUnits { type Err = ParserError; fn from_str(s: &str) -> Result { match s { - "ROWS" => Ok(SQLWindowFrameUnits::Rows), - "RANGE" => Ok(SQLWindowFrameUnits::Range), - "GROUPS" => Ok(SQLWindowFrameUnits::Groups), + "ROWS" => Ok(WindowFrameUnits::Rows), + "RANGE" => Ok(WindowFrameUnits::Range), + "GROUPS" => Ok(WindowFrameUnits::Groups), _ => Err(ParserError::ParserError(format!( "Expected ROWS, RANGE, or GROUPS, found: {}", s @@ -319,7 +319,7 @@ impl FromStr for SQLWindowFrameUnits { } #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLWindowFrameBound { +pub enum WindowFrameBound { /// "CURRENT ROW" CurrentRow, /// " PRECEDING" or "UNBOUNDED PRECEDING" @@ -329,14 +329,14 @@ pub enum SQLWindowFrameBound { Following(Option), } -impl ToString for SQLWindowFrameBound { +impl ToString for WindowFrameBound { fn to_string(&self) -> String { match self { - SQLWindowFrameBound::CurrentRow => "CURRENT ROW".to_string(), - SQLWindowFrameBound::Preceding(None) => "UNBOUNDED PRECEDING".to_string(), - SQLWindowFrameBound::Following(None) => "UNBOUNDED FOLLOWING".to_string(), - SQLWindowFrameBound::Preceding(Some(n)) => format!("{} PRECEDING", n), - SQLWindowFrameBound::Following(Some(n)) => format!("{} FOLLOWING", n), + WindowFrameBound::CurrentRow => "CURRENT ROW".to_string(), + WindowFrameBound::Preceding(None) => "UNBOUNDED PRECEDING".to_string(), + WindowFrameBound::Following(None) => "UNBOUNDED FOLLOWING".to_string(), + WindowFrameBound::Preceding(Some(n)) => format!("{} PRECEDING", n), + WindowFrameBound::Following(Some(n)) => format!("{} FOLLOWING", n), } } } @@ -344,91 +344,91 @@ impl ToString for SQLWindowFrameBound { /// A top-level statement (SELECT, INSERT, CREATE, etc.) #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLStatement { +pub enum Statement { /// SELECT - SQLQuery(Box), + Query(Box), /// INSERT - SQLInsert { + Insert { /// TABLE - table_name: SQLObjectName, + table_name: ObjectName, /// COLUMNS - columns: Vec, + columns: Vec, /// A SQL query that specifies what to insert - source: Box, + source: Box, }, - SQLCopy { + Copy { /// TABLE - table_name: SQLObjectName, + table_name: ObjectName, /// COLUMNS - columns: Vec, + columns: Vec, /// VALUES a vector of values to be copied values: Vec>, }, /// UPDATE - SQLUpdate { + Update { /// TABLE - table_name: SQLObjectName, + table_name: ObjectName, /// Column assignments - assignments: Vec, + assignments: Vec, /// WHERE selection: Option, }, /// DELETE - SQLDelete { + Delete { /// FROM - table_name: SQLObjectName, + table_name: ObjectName, /// WHERE selection: Option, }, /// CREATE VIEW - SQLCreateView { + CreateView { /// View name - name: SQLObjectName, - columns: Vec, - query: Box, + name: ObjectName, + columns: Vec, + query: Box, materialized: bool, - with_options: Vec, + with_options: Vec, }, /// CREATE TABLE - SQLCreateTable { + CreateTable { /// Table name - name: SQLObjectName, + name: ObjectName, /// Optional schema - columns: Vec, + columns: Vec, constraints: Vec, - with_options: Vec, + with_options: Vec, external: bool, file_format: Option, location: Option, }, /// ALTER TABLE - SQLAlterTable { + AlterTable { /// Table name - name: SQLObjectName, + name: ObjectName, operation: AlterTableOperation, }, /// DROP TABLE - SQLDrop { - object_type: SQLObjectType, + Drop { + object_type: ObjectType, if_exists: bool, - names: Vec, + names: Vec, cascade: bool, }, /// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...` - SQLStartTransaction { modes: Vec }, + StartTransaction { modes: Vec }, /// `SET TRANSACTION ...` - SQLSetTransaction { modes: Vec }, + SetTransaction { modes: Vec }, /// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]` - SQLCommit { chain: bool }, + Commit { chain: bool }, /// `ROLLBACK [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]` - SQLRollback { chain: bool }, + Rollback { chain: bool }, } -impl ToString for SQLStatement { +impl ToString for Statement { fn to_string(&self) -> String { match self { - SQLStatement::SQLQuery(s) => s.to_string(), - SQLStatement::SQLInsert { + Statement::Query(s) => s.to_string(), + Statement::Insert { table_name, columns, source, @@ -440,7 +440,7 @@ impl ToString for SQLStatement { s += &source.to_string(); s } - SQLStatement::SQLCopy { + Statement::Copy { table_name, columns, values, @@ -463,7 +463,7 @@ impl ToString for SQLStatement { s += "\n\\."; s } - SQLStatement::SQLUpdate { + Statement::Update { table_name, assignments, selection, @@ -478,7 +478,7 @@ impl ToString for SQLStatement { } s } - SQLStatement::SQLDelete { + Statement::Delete { table_name, selection, } => { @@ -488,7 +488,7 @@ impl ToString for SQLStatement { } s } - SQLStatement::SQLCreateView { + Statement::CreateView { name, columns, query, @@ -515,7 +515,7 @@ impl ToString for SQLStatement { query.to_string(), ) } - SQLStatement::SQLCreateTable { + Statement::CreateTable { name, columns, constraints, @@ -546,10 +546,10 @@ impl ToString for SQLStatement { } s } - SQLStatement::SQLAlterTable { name, operation } => { + Statement::AlterTable { name, operation } => { format!("ALTER TABLE {} {}", name.to_string(), operation.to_string()) } - SQLStatement::SQLDrop { + Statement::Drop { object_type, if_exists, names, @@ -561,7 +561,7 @@ impl ToString for SQLStatement { comma_separated_string(names), if *cascade { " CASCADE" } else { "" }, ), - SQLStatement::SQLStartTransaction { modes } => format!( + Statement::StartTransaction { modes } => format!( "START TRANSACTION{}", if modes.is_empty() { "".into() @@ -569,7 +569,7 @@ impl ToString for SQLStatement { format!(" {}", comma_separated_string(modes)) } ), - SQLStatement::SQLSetTransaction { modes } => format!( + Statement::SetTransaction { modes } => format!( "SET TRANSACTION{}", if modes.is_empty() { "".into() @@ -577,10 +577,10 @@ impl ToString for SQLStatement { format!(" {}", comma_separated_string(modes)) } ), - SQLStatement::SQLCommit { chain } => { + Statement::Commit { chain } => { format!("COMMIT{}", if *chain { " AND CHAIN" } else { "" },) } - SQLStatement::SQLRollback { chain } => { + Statement::Rollback { chain } => { format!("ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },) } } @@ -589,9 +589,9 @@ impl ToString for SQLStatement { /// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLObjectName(pub Vec); +pub struct ObjectName(pub Vec); -impl ToString for SQLObjectName { +impl ToString for ObjectName { fn to_string(&self) -> String { self.0.join(".") } @@ -599,12 +599,12 @@ impl ToString for SQLObjectName { /// SQL assignment `foo = expr` as used in SQLUpdate #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLAssignment { - pub id: SQLIdent, +pub struct Assignment { + pub id: Ident, pub value: Expr, } -impl ToString for SQLAssignment { +impl ToString for Assignment { fn to_string(&self) -> String { format!("{} = {}", self.id, self.value.to_string()) } @@ -612,15 +612,15 @@ impl ToString for SQLAssignment { /// SQL function #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLFunction { - pub name: SQLObjectName, +pub struct Function { + pub name: ObjectName, pub args: Vec, - pub over: Option, + pub over: Option, // aggregate functions may specify eg `COUNT(DISTINCT x)` pub distinct: bool, } -impl ToString for SQLFunction { +impl ToString for Function { fn to_string(&self) -> String { let mut s = format!( "{}({}{})", @@ -662,7 +662,7 @@ impl ToString for FileFormat { } } -use crate::sqlparser::ParserError; +use crate::parser::ParserError; use std::str::FromStr; impl FromStr for FileFormat { type Err = ParserError; @@ -686,27 +686,27 @@ impl FromStr for FileFormat { } #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLObjectType { +pub enum ObjectType { Table, View, } -impl SQLObjectType { +impl ObjectType { fn to_string(&self) -> String { match self { - SQLObjectType::Table => "TABLE".into(), - SQLObjectType::View => "VIEW".into(), + ObjectType::Table => "TABLE".into(), + ObjectType::View => "VIEW".into(), } } } #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLOption { - pub name: SQLIdent, +pub struct SqlOption { + pub name: Ident, pub value: Value, } -impl ToString for SQLOption { +impl ToString for SqlOption { fn to_string(&self) -> String { format!("{} = {}", self.name.to_string(), self.value.to_string()) } diff --git a/src/ast/operator.rs b/src/ast/operator.rs new file mode 100644 index 000000000..32626e4f1 --- /dev/null +++ b/src/ast/operator.rs @@ -0,0 +1,71 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Unary operators +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum UnaryOperator { + Plus, + Minus, + Not, +} + +impl ToString for UnaryOperator { + fn to_string(&self) -> String { + match self { + UnaryOperator::Plus => "+".to_string(), + UnaryOperator::Minus => "-".to_string(), + UnaryOperator::Not => "NOT".to_string(), + } + } +} + +/// Binary operators +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum BinaryOperator { + Plus, + Minus, + Multiply, + Divide, + Modulus, + Gt, + Lt, + GtEq, + LtEq, + Eq, + NotEq, + And, + Or, + Like, + NotLike, +} + +impl ToString for BinaryOperator { + fn to_string(&self) -> String { + match self { + BinaryOperator::Plus => "+".to_string(), + BinaryOperator::Minus => "-".to_string(), + BinaryOperator::Multiply => "*".to_string(), + BinaryOperator::Divide => "/".to_string(), + BinaryOperator::Modulus => "%".to_string(), + BinaryOperator::Gt => ">".to_string(), + BinaryOperator::Lt => "<".to_string(), + BinaryOperator::GtEq => ">=".to_string(), + BinaryOperator::LtEq => "<=".to_string(), + BinaryOperator::Eq => "=".to_string(), + BinaryOperator::NotEq => "<>".to_string(), + BinaryOperator::And => "AND".to_string(), + BinaryOperator::Or => "OR".to_string(), + BinaryOperator::Like => "LIKE".to_string(), + BinaryOperator::NotLike => "NOT LIKE".to_string(), + } + } +} diff --git a/src/sqlast/query.rs b/src/ast/query.rs similarity index 87% rename from src/sqlast/query.rs rename to src/ast/query.rs index e8d0cd08a..2d09d821c 100644 --- a/src/sqlast/query.rs +++ b/src/ast/query.rs @@ -15,13 +15,13 @@ use super::*; /// The most complete variant of a `SELECT` query expression, optionally /// including `WITH`, `UNION` / other set operations, and `ORDER BY`. #[derive(Debug, Clone, PartialEq, Hash)] -pub struct SQLQuery { +pub struct Query { /// WITH (common table expressions, or CTEs) pub ctes: Vec, /// SELECT or UNION / EXCEPT / INTECEPT - pub body: SQLSetExpr, + pub body: SetExpr, /// ORDER BY - pub order_by: Vec, + pub order_by: Vec, /// `LIMIT { | ALL }` pub limit: Option, /// `OFFSET { ROW | ROWS }` @@ -30,7 +30,7 @@ pub struct SQLQuery { pub fetch: Option, } -impl ToString for SQLQuery { +impl ToString for Query { fn to_string(&self) -> String { let mut s = String::new(); if !self.ctes.is_empty() { @@ -57,30 +57,30 @@ impl ToString for SQLQuery { /// A node in a tree, representing a "query body" expression, roughly: /// `SELECT ... [ {UNION|EXCEPT|INTERSECT} SELECT ...]` #[derive(Debug, Clone, PartialEq, Hash)] -pub enum SQLSetExpr { +pub enum SetExpr { /// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations) - Select(Box), + Select(Box