Skip to content
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

Add manually implemented AST visitor #758

Closed
wants to merge 5 commits into from
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
85 changes: 84 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::String, vec::Vec};
use core::fmt;
use core::ops::ControlFlow;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

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

/// An `ALTER TABLE` (`Statement::AlterTable`) operation
Expand Down Expand Up @@ -200,6 +203,35 @@ impl fmt::Display for AlterTableOperation {
}
}

impl Visit for AlterTableOperation {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self {
AlterTableOperation::DropConstraint { .. }
| AlterTableOperation::DropColumn { .. }
| AlterTableOperation::DropPrimaryKey
| AlterTableOperation::RenameColumn { .. }
| AlterTableOperation::RenameTable { .. }
| AlterTableOperation::RenameConstraint { .. } => ControlFlow::Continue(()),
AlterTableOperation::AddConstraint(c) => c.visit(visitor),
AlterTableOperation::AddColumn { column_def, .. } => column_def.visit(visitor),
AlterTableOperation::RenamePartitions {
old_partitions,
new_partitions,
} => {
old_partitions.visit(visitor)?;
new_partitions.visit(visitor)
}
AlterTableOperation::AddPartitions { new_partitions, .. } => {
new_partitions.visit(visitor)
}
AlterTableOperation::DropPartitions { partitions, .. } => partitions.visit(visitor),
AlterTableOperation::ChangeColumn { options, .. } => options.visit(visitor),

AlterTableOperation::AlterColumn { op, .. } => op.visit(visitor),
}
}
}

/// An `ALTER COLUMN` (`Statement::AlterTable`) operation
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -242,6 +274,18 @@ impl fmt::Display for AlterColumnOperation {
}
}

impl Visit for AlterColumnOperation {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self {
AlterColumnOperation::SetNotNull
| AlterColumnOperation::DropNotNull
| AlterColumnOperation::DropDefault => ControlFlow::Continue(()),
AlterColumnOperation::SetDefault { value } => value.visit(visitor),
AlterColumnOperation::SetDataType { using, .. } => using.visit(visitor),
}
}
}

/// A table-level constraint, specified in a `CREATE TABLE` or an
/// `ALTER TABLE ADD <constraint>` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -400,6 +444,18 @@ impl fmt::Display for TableConstraint {
}
}

impl Visit for TableConstraint {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self {
TableConstraint::Unique { .. }
| TableConstraint::ForeignKey { .. }
| TableConstraint::Index { .. }
| TableConstraint::FulltextOrSpatial { .. } => ControlFlow::Continue(()),
TableConstraint::Check { expr, .. } => expr.visit(visitor),
}
}
}

/// Representation whether a definition can can contains the KEY or INDEX keywords with the same
/// meaning.
///
Expand Down Expand Up @@ -479,6 +535,12 @@ impl fmt::Display for ColumnDef {
}
}

impl Visit for ColumnDef {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
self.options.visit(visitor)
}
}

/// An optionally-named `ColumnOption`: `[ CONSTRAINT <name> ] <column-option>`.
///
/// Note that implementations are substantially more permissive than the ANSI
Expand Down Expand Up @@ -508,6 +570,12 @@ impl fmt::Display for ColumnOptionDef {
}
}

impl Visit for ColumnOptionDef {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
self.option.visit(visitor)
}
}

/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
/// TABLE` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -580,6 +648,21 @@ impl fmt::Display for ColumnOption {
}
}

impl Visit for ColumnOption {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self {
ColumnOption::Null
| ColumnOption::NotNull
| ColumnOption::Unique { .. }
| ColumnOption::ForeignKey { .. }
| ColumnOption::DialectSpecific(_)
| ColumnOption::CharacterSet(_)
| ColumnOption::Comment(_) => ControlFlow::Continue(()),
ColumnOption::Default(e) | ColumnOption::Check(e) => e.visit(visitor),
}
}
}

fn display_constraint_name(name: &'_ Option<Ident>) -> impl fmt::Display + '_ {
struct ConstraintName<'a>(&'a Option<Ident>);
impl<'a> fmt::Display for ConstraintName<'a> {
Expand Down
Loading