Skip to content

Add an AST visitor #601

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

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.22.0"
authors = ["Andy Grove <[email protected]>"]
homepage = "https://github.com/sqlparser-rs/sqlparser-rs"
documentation = "https://docs.rs/sqlparser/"
keywords = [ "ansi", "sql", "lexer", "parser" ]
keywords = ["ansi", "sql", "lexer", "parser"]
repository = "https://github.com/sqlparser-rs/sqlparser-rs"
license = "Apache-2.0"
include = [
Expand All @@ -32,6 +32,7 @@ serde = { version = "1.0", features = ["derive"], optional = true }
# of dev-dependencies because of
# https://github.com/rust-lang/cargo/issues/1596
serde_json = { version = "1.0", optional = true }
derive-visitor = { version = "0.3.0", optional = true }

[dev-dependencies]
simple_logger = "2.1"
Expand Down
46 changes: 25 additions & 21 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,58 @@ use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "derive-visitor")]
use derive_visitor::{Drive, DriveMut};

use crate::ast::ObjectName;

use super::value::escape_single_quote_string;

/// SQL data types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum DataType {
/// Fixed-length character type e.g. CHAR(10)
Char(Option<u64>),
Char(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Variable-length character type e.g. VARCHAR(10)
Varchar(Option<u64>),
Varchar(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Variable-length character type e.g. NVARCHAR(10)
Nvarchar(Option<u64>),
Nvarchar(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Uuid type
Uuid,
/// Large character object e.g. CLOB(1000)
Clob(u64),
Clob(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
/// Fixed-length binary type e.g. BINARY(10)
Binary(u64),
Binary(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
/// Variable-length binary type e.g. VARBINARY(10)
Varbinary(u64),
Varbinary(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
/// Large binary object e.g. BLOB(1000)
Blob(u64),
Blob(#[cfg_attr(feature = "derive-visitor", drive(skip))] u64),
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
Decimal(Option<u64>, Option<u64>),
Decimal(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>, #[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Floating point with optional precision e.g. FLOAT(8)
Float(Option<u64>),
Float(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Tiny integer with optional display width e.g. TINYINT or TINYINT(3)
TinyInt(Option<u64>),
TinyInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Unsigned tiny integer with optional display width e.g. TINYINT UNSIGNED or TINYINT(3) UNSIGNED
UnsignedTinyInt(Option<u64>),
UnsignedTinyInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Small integer with optional display width e.g. SMALLINT or SMALLINT(5)
SmallInt(Option<u64>),
SmallInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Unsigned small integer with optional display width e.g. SMALLINT UNSIGNED or SMALLINT(5) UNSIGNED
UnsignedSmallInt(Option<u64>),
UnsignedSmallInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Integer with optional display width e.g. INT or INT(11)
Int(Option<u64>),
Int(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
Integer(Option<u64>),
Integer(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Unsigned integer with optional display width e.g. INT UNSIGNED or INT(11) UNSIGNED
UnsignedInt(Option<u64>),
UnsignedInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Unsigned integer with optional display width e.g. INTGER UNSIGNED or INTEGER(11) UNSIGNED
UnsignedInteger(Option<u64>),
UnsignedInteger(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Big integer with optional display width e.g. BIGINT or BIGINT(20)
BigInt(Option<u64>),
BigInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Unsigned big integer with optional display width e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED
UnsignedBigInt(Option<u64>),
UnsignedBigInt(#[cfg_attr(feature = "derive-visitor", drive(skip))] Option<u64>),
/// Floating point e.g. REAL
Real,
/// Double e.g. DOUBLE PRECISION
Expand Down Expand Up @@ -96,9 +100,9 @@ pub enum DataType {
/// Arrays
Array(Box<DataType>),
/// Enums
Enum(Vec<String>),
Enum(#[cfg_attr(feature = "derive-visitor", drive(skip))] Vec<String>),
/// Set
Set(Vec<String>),
Set(#[cfg_attr(feature = "derive-visitor", drive(skip))] Vec<String>),
}

impl fmt::Display for DataType {
Expand Down
29 changes: 19 additions & 10 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,33 @@ use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "derive-visitor")]
use derive_visitor::{Drive, DriveMut};

use crate::ast::value::escape_single_quote_string;
use crate::ast::{display_comma_separated, display_separated, DataType, Expr, Ident, ObjectName};
use crate::tokenizer::Token;

/// An `ALTER TABLE` (`Statement::AlterTable`) operation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum AlterTableOperation {
/// `ADD <table_constraint>`
AddConstraint(TableConstraint),
/// `ADD [ COLUMN ] <column_def>`
AddColumn { column_def: ColumnDef },
/// `DROP CONSTRAINT [ IF EXISTS ] <name>`
DropConstraint {
if_exists: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] if_exists: bool,
name: Ident,
cascade: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] cascade: bool,
},
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
DropColumn {
column_name: Ident,
if_exists: bool,
cascade: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] if_exists: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] cascade: bool,
},
/// `RENAME TO PARTITION (partition=val)`
RenamePartitions {
Expand All @@ -51,12 +55,12 @@ pub enum AlterTableOperation {
},
/// Add Partitions
AddPartitions {
if_not_exists: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] if_not_exists: bool,
new_partitions: Vec<Expr>,
},
DropPartitions {
partitions: Vec<Expr>,
if_exists: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] if_exists: bool,
},
/// `RENAME [ COLUMN ] <old_column_name> TO <new_column_name>`
RenameColumn {
Expand Down Expand Up @@ -178,6 +182,7 @@ impl fmt::Display for AlterTableOperation {
/// An `ALTER COLUMN` (`Statement::AlterTable`) operation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum AlterColumnOperation {
/// `SET NOT NULL`
SetNotNull,
Expand Down Expand Up @@ -221,13 +226,14 @@ impl fmt::Display for AlterColumnOperation {
/// `ALTER TABLE ADD <constraint>` statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum TableConstraint {
/// `[ CONSTRAINT <name> ] { PRIMARY KEY | UNIQUE } (<columns>)`
Unique {
name: Option<Ident>,
columns: Vec<Ident>,
/// Whether this is a `PRIMARY KEY` or just a `UNIQUE` constraint
is_primary: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] is_primary: bool,
},
/// A referential integrity constraint (`[ CONSTRAINT <name> ] FOREIGN KEY (<columns>)
/// REFERENCES <foreign_table> (<referred_columns>)
Expand Down Expand Up @@ -297,6 +303,7 @@ impl fmt::Display for TableConstraint {
/// SQL column definition
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub struct ColumnDef {
pub name: Ident,
pub data_type: DataType,
Expand Down Expand Up @@ -332,6 +339,7 @@ impl fmt::Display for ColumnDef {
/// "column options," and we allow any column option to be named.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub struct ColumnOptionDef {
pub name: Option<Ident>,
pub option: ColumnOption,
Expand All @@ -347,6 +355,7 @@ impl fmt::Display for ColumnOptionDef {
/// TABLE` statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum ColumnOption {
/// `NULL`
Null,
Expand All @@ -356,7 +365,7 @@ pub enum ColumnOption {
Default(Expr),
/// `{ PRIMARY KEY | UNIQUE }`
Unique {
is_primary: bool,
#[cfg_attr(feature = "derive-visitor", drive(skip))] is_primary: bool,
},
/// A referential integrity constraint (`[FOREIGN KEY REFERENCES
/// <foreign_table> (<referred_columns>)
Expand All @@ -376,7 +385,7 @@ pub enum ColumnOption {
/// - ...
DialectSpecific(Vec<Token>),
CharacterSet(ObjectName),
Comment(String),
Comment(#[cfg_attr(feature = "derive-visitor", drive(skip))] String),
}

impl fmt::Display for ColumnOption {
Expand Down Expand Up @@ -433,7 +442,7 @@ fn display_constraint_name(name: &'_ Option<Ident>) -> impl fmt::Display + '_ {
///
/// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "derive-visitor", derive(Drive, DriveMut))]
pub enum ReferentialAction {
Restrict,
Cascade,
Expand Down
Loading