diff --git a/README.md b/README.md index a3e98003f..4f652d889 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ let sql = "SELECT a, b, 123, myfunc(b) \ WHERE a > b AND b < 100 \ ORDER BY a DESC, b"; -let dialect = GenericSqlDialect{}; // or AnsiSqlDialect, or your own dialect ... +let dialect = GenericDialect {}; // or AnsiDialect, or your own dialect ... -let ast = Parser::parse_sql(&dialect,sql.to_string()).unwrap(); +let ast = Parser::parse_sql(&dialect, sql.to_string()).unwrap(); println!("AST: {:?}", ast); ``` @@ -30,7 +30,7 @@ println!("AST: {:?}", ast); This outputs ```rust -AST: [SQLSelect(SQLQuery { ctes: [], body: Select(SQLSelect { distinct: false, projection: [UnnamedExpression(SQLIdentifier("a")), UnnamedExpression(SQLIdentifier("b")), UnnamedExpression(SQLValue(Long(123))), UnnamedExpression(SQLFunction { name: SQLObjectName(["myfunc"]), args: [SQLIdentifier("b")], over: None })], relation: Some(Table { name: SQLObjectName(["table_1"]), alias: None }), joins: [], selection: Some(SQLBinaryExpr { left: SQLBinaryExpr { left: SQLIdentifier("a"), op: Gt, right: SQLIdentifier("b") }, op: And, right: SQLBinaryExpr { left: SQLIdentifier("b"), op: Lt, right: SQLValue(Long(100)) } }), group_by: None, having: None }), order_by: Some([SQLOrderByExpr { expr: SQLIdentifier("a"), asc: Some(false) }, SQLOrderByExpr { expr: SQLIdentifier("b"), asc: None }]), limit: None })] +AST: [Query(Query { ctes: [], body: Select(Select { distinct: false, projection: [UnnamedExpr(Identifier("a")), UnnamedExpr(Identifier("b")), UnnamedExpr(Value(Long(123))), UnnamedExpr(Function(Function { name: ObjectName(["myfunc"]), args: [Identifier("b")], over: None, distinct: false }))], from: [TableWithJoins { relation: Table { name: ObjectName(["table_1"]), alias: None, args: [], with_hints: [] }, joins: [] }], selection: Some(BinaryOp { left: BinaryOp { left: Identifier("a"), op: Gt, right: Identifier("b") }, op: And, right: BinaryOp { left: Identifier("b"), op: Lt, right: Value(Long(100)) } }), group_by: [], having: None }), order_by: [OrderByExpr { expr: Identifier("a"), asc: Some(false) }, OrderByExpr { expr: Identifier("b"), asc: None }], limit: None, offset: None, fetch: None })] ``` ## Design diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index a42f02583..a8beabc84 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -1,8 +1,8 @@ -//! AST types specific to CREATE/ALTER variants of `SQLStatement` +//! AST types specific to CREATE/ALTER variants of [Statement] //! (commonly referred to as Data Definition Language, or DDL) use super::{DataType, Expr, Ident, ObjectName}; -/// An `ALTER TABLE` (`SQLStatement::SQLAlterTable`) operation +/// An `ALTER TABLE` (`Statement::AlterTable`) operation #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum AlterTableOperation { /// `ADD ` diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 66dfeaa07..12d3cc372 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -57,14 +57,14 @@ pub enum Expr { /// Identifier e.g. table name or column name Identifier(Ident), /// Unqualified wildcard (`*`). SQL allows this in limited contexts, such as: - /// - right after `SELECT` (which is represented as a [SQLSelectItem::Wildcard] instead) + /// - right after `SELECT` (which is represented as a [SelectItem::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 `* + *` Wildcard, /// Qualified wildcard, e.g. `alias.*` or `schema.table.*`. - /// (Same caveats apply to SQLQualifiedWildcard as to SQLWildcard.) + /// (Same caveats apply to `QualifiedWildcard` as to `Wildcard`.) QualifiedWildcard(Vec), /// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col` CompoundIdentifier(Vec), @@ -115,7 +115,7 @@ pub enum Expr { }, /// Nested expression e.g. `(foo > bar)` or `(1)` Nested(Box), - /// SQLValue + /// A literal value, such as string, number, date or NULL Value(Value), /// Scalar function call e.g. `LEFT(foo, 5)` Function(Function), @@ -320,12 +320,12 @@ impl FromStr for WindowFrameUnits { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum WindowFrameBound { - /// "CURRENT ROW" + /// `CURRENT ROW` CurrentRow, - /// " PRECEDING" or "UNBOUNDED PRECEDING" + /// ` PRECEDING` or `UNBOUNDED PRECEDING` Preceding(Option), - /// " FOLLOWING" or "UNBOUNDED FOLLOWING". This can only appear in - /// SQLWindowFrame::end_bound. + /// ` FOLLOWING` or `UNBOUNDED FOLLOWING`. This can only appear in + /// [WindowFrame::end_bound]. Following(Option), } @@ -610,7 +610,7 @@ impl ToString for Assignment { } } -/// SQL function +/// A function call #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Function { pub name: ObjectName, diff --git a/src/dialect/keywords.rs b/src/dialect/keywords.rs index 4fc103f37..3048fcd59 100644 --- a/src/dialect/keywords.rs +++ b/src/dialect/keywords.rs @@ -12,7 +12,7 @@ ///! This module defines /// 1) a list of constants for every keyword that -/// can appear in SQLWord::keyword: +/// can appear in [Word::keyword]: /// pub const KEYWORD = "KEYWORD" /// 2) an `ALL_KEYWORDS` array with every keyword in it /// This is not a list of *reserved* keywords: some of these can be diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index d7b7f6746..f99596691 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -28,7 +28,7 @@ pub trait Dialect: Debug { /// implementation, accepting "double quoted" ids is both ANSI-compliant /// and appropriate for most dialects (with the notable exception of /// MySQL, MS SQL, and sqlite). You can accept one of characters listed - /// in `SQLWord::matching_end_quote()` here + /// in `Word::matching_end_quote` here fn is_delimited_identifier_start(&self, ch: char) -> bool { ch == '"' } diff --git a/src/lib.rs b/src/lib.rs index a6b0423ad..156e20eb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,14 +14,14 @@ //! //! Example code: //! -//! This crate provides an ANSI:SQL 2011 lexer and parser that can parsed SQL into an Abstract -//! Syntax Tree (AST). +//! This crate provides an ANSI:SQL 2011 lexer and parser that can parse SQL +//! into an Abstract Syntax Tree (AST). //! //! ``` //! use sqlparser::dialect::GenericDialect; //! use sqlparser::parser::Parser; //! -//! let dialect = GenericDialect {}; // or AnsiSqlDialect +//! let dialect = GenericDialect {}; // or AnsiDialect //! //! let sql = "SELECT a, b, 123, myfunc(b) \ //! FROM table_1 \ diff --git a/src/parser.rs b/src/parser.rs index 2338cc754..30b98b7a1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -225,7 +225,7 @@ impl Parser { } _ => Ok(Expr::Identifier(w.as_ident())), }, - }, // End of Token::SQLWord + }, // End of Token::Word Token::Mult => Ok(Expr::Wildcard), tok @ Token::Minus | tok @ Token::Plus => { let op = if tok == Token::Plus { @@ -1697,7 +1697,7 @@ impl Parser { // ^ ^ ^ ^ // | | | | // | | | | - // | | | (4) belongs to a SQLSetExpr::Query inside the subquery + // | | | (4) belongs to a SetExpr::Query inside the subquery // | | (3) starts a derived table (subquery) // | (2) starts a nested join // (1) an additional set of parens around a nested join @@ -1882,7 +1882,7 @@ impl Parser { Ok(projections) } - /// Parse a comma-delimited list of SQL ORDER BY expressions + /// Parse a comma-delimited list of ORDER BY expressions pub fn parse_order_by_expr_list(&mut self) -> Result, ParserError> { let mut expr_list: Vec = vec![]; loop { diff --git a/src/test_utils.rs b/src/test_utils.rs index 547c6d366..1a1ab8b50 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -77,27 +77,27 @@ impl TestedDialects { only_statement } - /// Ensures that `sql` parses as a single SQLStatement, and is not modified + /// Ensures that `sql` parses as a single [Statement], and is not modified /// after a serialization round-trip. pub fn verified_stmt(&self, query: &str) -> Statement { self.one_statement_parses_to(query, query) } - /// Ensures that `sql` parses as a single SQLQuery, and is not modified + /// Ensures that `sql` parses as a single [Query], and is not modified /// after a serialization round-trip. pub fn verified_query(&self, sql: &str) -> Query { match self.verified_stmt(sql) { Statement::Query(query) => *query, - _ => panic!("Expected SQLQuery"), + _ => panic!("Expected Query"), } } - /// Ensures that `sql` parses as a single SQLSelect, and is not modified + /// Ensures that `sql` parses as a single [Select], and is not modified /// after a serialization round-trip. pub fn verified_only_select(&self, query: &str) -> Select { match self.verified_query(query).body { SetExpr::Select(s) => *s, - _ => panic!("Expected SQLSetExpr::Select"), + _ => panic!("Expected SetExpr::Select"), } }