Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 2 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl CreateTableBuilder {
}

pub fn build(self) -> Statement {
Statement::CreateTable(CreateTable {
CreateTable {
or_replace: self.or_replace,
temporary: self.temporary,
external: self.external,
Expand Down Expand Up @@ -484,7 +484,8 @@ impl CreateTableBuilder {
refresh_mode: self.refresh_mode,
initialize: self.initialize,
require_user: self.require_user,
})
}
.into()
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub mod helpers;
mod operator;
mod query;
mod spans;
mod statement_from_impls;
pub use spans::Spanned;

mod trigger;
Expand Down Expand Up @@ -3029,14 +3030,6 @@ impl Display for Set {
}
}

/// Convert a `Set` into a `Statement`.
/// Convenience function, instead of writing `Statement::Set(Set::Set...{...})`
impl From<Set> for Statement {
fn from(set: Set) -> Self {
Statement::Set(set)
}
}

/// A representation of a `WHEN` arm with all the identifiers catched and the statements to execute
/// for the arm.
///
Expand Down
232 changes: 232 additions & 0 deletions src/ast/statement_from_impls.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this I'm thinking it could make more sense to have the impls right next to the struct definitions (similar to how we do for display impls) vs a dedicated file for it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the different module as the main one is ever more massive, so I thought it best not to add even more to it. That being said, if you prefer avoiding specialised sub modules but keep the monolitic one I can copy everything back into it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think we can move the impls close to the associated struct definitions, indeed it would be nice to split the large files but I imagine splitting based on e.g. statement types or similar dimension grouping related structs in the same file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 9ef033f

Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

//! Implementations of the `From` trait to convert from various
//! AST nodes to `Statement` nodes.
use crate::ast::{
AlterSchema, AlterType, CaseStatement, CreateConnector, CreateDomain, CreateFunction,
CreateIndex, CreateServerStatement, CreateTable, CreateTrigger, CreateUser, Delete,
DenyStatement, DropDomain, DropTrigger, ExportData, Function, IfStatement, Insert,
OpenStatement, PrintStatement, Query, RaiseStatement, RenameTable, ReturnStatement, Set,
ShowCharset, ShowObjects, Statement, Use, VacuumStatement, WhileStatement,
};
#[cfg(not(feature = "std"))]
use alloc::{
boxed::Box,
format,
string::{String, ToString},
vec,
vec::Vec,
};

impl From<Set> for Statement {
fn from(s: Set) -> Self {
Self::Set(s)
}
}

impl From<Query> for Statement {
fn from(q: Query) -> Self {
Box::new(q).into()
}
}

impl From<Box<Query>> for Statement {
fn from(q: Box<Query>) -> Self {
Self::Query(q)
}
}

impl From<Insert> for Statement {
fn from(i: Insert) -> Self {
Self::Insert(i)
}
}

impl From<CaseStatement> for Statement {
fn from(c: CaseStatement) -> Self {
Self::Case(c)
}
}

impl From<IfStatement> for Statement {
fn from(i: IfStatement) -> Self {
Self::If(i)
}
}

impl From<WhileStatement> for Statement {
fn from(w: WhileStatement) -> Self {
Self::While(w)
}
}

impl From<RaiseStatement> for Statement {
fn from(r: RaiseStatement) -> Self {
Self::Raise(r)
}
}

impl From<Function> for Statement {
fn from(f: Function) -> Self {
Self::Call(f)
}
}

impl From<OpenStatement> for Statement {
fn from(o: OpenStatement) -> Self {
Self::Open(o)
}
}

impl From<Delete> for Statement {
fn from(d: Delete) -> Self {
Self::Delete(d)
}
}

impl From<CreateTable> for Statement {
fn from(c: CreateTable) -> Self {
Self::CreateTable(c)
}
}

impl From<CreateIndex> for Statement {
fn from(c: CreateIndex) -> Self {
Self::CreateIndex(c)
}
}

impl From<CreateServerStatement> for Statement {
fn from(c: CreateServerStatement) -> Self {
Self::CreateServer(c)
}
}

impl From<CreateConnector> for Statement {
fn from(c: CreateConnector) -> Self {
Self::CreateConnector(c)
}
}

impl From<AlterSchema> for Statement {
fn from(a: AlterSchema) -> Self {
Self::AlterSchema(a)
}
}

impl From<AlterType> for Statement {
fn from(a: AlterType) -> Self {
Self::AlterType(a)
}
}

impl From<DropDomain> for Statement {
fn from(d: DropDomain) -> Self {
Self::DropDomain(d)
}
}

impl From<ShowCharset> for Statement {
fn from(s: ShowCharset) -> Self {
Self::ShowCharset(s)
}
}

impl From<ShowObjects> for Statement {
fn from(s: ShowObjects) -> Self {
Self::ShowObjects(s)
}
}

impl From<Use> for Statement {
fn from(u: Use) -> Self {
Self::Use(u)
}
}

impl From<CreateFunction> for Statement {
fn from(c: CreateFunction) -> Self {
Self::CreateFunction(c)
}
}

impl From<CreateTrigger> for Statement {
fn from(c: CreateTrigger) -> Self {
Self::CreateTrigger(c)
}
}

impl From<DropTrigger> for Statement {
fn from(d: DropTrigger) -> Self {
Self::DropTrigger(d)
}
}

impl From<DenyStatement> for Statement {
fn from(d: DenyStatement) -> Self {
Self::Deny(d)
}
}

impl From<CreateDomain> for Statement {
fn from(c: CreateDomain) -> Self {
Self::CreateDomain(c)
}
}

impl From<RenameTable> for Statement {
fn from(r: RenameTable) -> Self {
vec![r].into()
}
}

impl From<Vec<RenameTable>> for Statement {
fn from(r: Vec<RenameTable>) -> Self {
Self::RenameTable(r)
}
}

impl From<PrintStatement> for Statement {
fn from(p: PrintStatement) -> Self {
Self::Print(p)
}
}

impl From<ReturnStatement> for Statement {
fn from(r: ReturnStatement) -> Self {
Self::Return(r)
}
}

impl From<ExportData> for Statement {
fn from(e: ExportData) -> Self {
Self::ExportData(e)
}
}

impl From<CreateUser> for Statement {
fn from(c: CreateUser) -> Self {
Self::CreateUser(c)
}
}

impl From<VacuumStatement> for Statement {
fn from(v: VacuumStatement) -> Self {
Self::Vacuum(v)
}
}
10 changes: 6 additions & 4 deletions src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,13 @@ impl MsSqlDialect {
parser.prev_token();
}

Ok(Statement::If(IfStatement {
Ok(IfStatement {
if_block,
else_block,
elseif_blocks: Vec::new(),
end_token: None,
}))
}
.into())
}

/// Parse `CREATE TRIGGER` for [MsSql]
Expand All @@ -251,7 +252,7 @@ impl MsSqlDialect {
parser.expect_keyword_is(Keyword::AS)?;
let statements = Some(parser.parse_conditional_statements(&[Keyword::END])?);

Ok(Statement::CreateTrigger(CreateTrigger {
Ok(CreateTrigger {
or_alter,
or_replace: false,
is_constraint: false,
Expand All @@ -269,7 +270,8 @@ impl MsSqlDialect {
statements_as: true,
statements,
characteristics: None,
}))
}
.into())
}

/// Parse a sequence of statements, optionally separated by semicolon.
Expand Down