Expand description
Parse SQL into an AST
This crate provides an lexer and parser that can parse SQL into an Abstract Syntax Tree (AST). Currently primarily focused on MariaDB/Mysql.
Example code:
use qusql_parse::{SQLDialect, SQLArguments, ParseOptions, parse_statement, Issues};
let options = ParseOptions::new()
.dialect(SQLDialect::MariaDB)
.arguments(SQLArguments::QuestionMark)
.warn_unquoted_identifiers(true);
let sql = "SELECT `monkey`,
FROM `t1` LEFT JOIN `t2` ON `t2`.`id` = `t1.two`
WHERE `t1`.`id` = ?";
let mut issues = Issues::new(sql);
let ast = parse_statement(sql, &mut issues, &options);
println!("{}", issues);
println!("AST: {:#?}", ast);Macros§
- issue_
ice - Construct an “Internal compiler error” issue, containing the current file and line
- issue_
todo - Construct an “Not yet implemented” issue, containing the current file and line
Structs§
- AddColumn
- ADD COLUMN specification
- AddForeign
Key - Add a foreign key
- AddIndex
- Add an index
- AddTable
Constraint - PostgreSQL: ADD CONSTRAINT … UNIQUE/PRIMARY KEY/CHECK
- Aggregate
Function Call Expression - Algorithm
- ALGORITHM specification
- Alter
Column - ALTER COLUMN specification
- Alter
Operator - ALTER OPERATOR statement (PostgreSQL)
- Alter
Operator Class - ALTER OPERATOR CLASS statement (PostgreSQL)
- Alter
Operator Family - ALTER OPERATOR FAMILY statement (PostgreSQL)
- Alter
Role - ALTER ROLE statement (PostgreSQL)
- Alter
Schema - ALTER SCHEMA statement (PostgreSQL)
- Alter
Table - Represent an alter table statement
- Alter
Type - ALTER TYPE statement (PostgreSQL)
- ArgExpression
- Input argument to query, the first argument is the occurrence number of the argument
- Array
Expression - PostgreSQL ARRAY[…] literal expression
- Array
Subscript Expression - Array subscript or slice expression: expr[idx] or expr[lower:upper]
- Auto
Increment - AUTO_INCREMENT specification
- Begin
- Begin statement
- Between
Expression - Between expression (expr BETWEEN low AND high)
- Binary
Expression - Expression with binary operator
- Block
- Block statement, for example in stored procedures
- Bool
Expression - Literal bool expression “TRUE” or “FALSE”
- Call
- CALL statement — invokes a stored procedure
- Case
Expression - Case expression
- Case
Statement - Case statement
- Cast
Expression - Cast expression
- Change
- CHANGE specification
- Char
Function Expression - CHAR(N,… [USING charset_name]) expression
- Close
Cursor - MariaDB/MySQL CLOSE cursor statement.
CLOSE cursor_name - Commit
- Commit statement
- Compound
Query - Compound query statement
- Compound
Query Branch - Right hand side branch of a compound query
- Convert
Expression - Convert expression (CONVERT(expr, type) or CONVERT(expr USING charset))
- Copy
From - A PostgreSQL
COPY ... FROMstatement. - CopyTo
- A PostgreSQL
COPY ... TOstatement. - Create
Constraint Trigger - Represent a create constraint trigger statement
- Create
Database - Create
Domain - CREATE DOMAIN statement (PostgreSQL)
- Create
Extension - CREATE EXTENSION statement (PostgreSQL)
- Create
Function - Representation of Create Function Statement
- Create
Index - Create
Operator - CREATE OPERATOR statement (PostgreSQL)
- Create
Operator Class - CREATE OPERATOR CLASS statement (PostgreSQL)
- Create
Operator Family - CREATE OPERATOR FAMILY statement (PostgreSQL)
- Create
Procedure - Representation of a CREATE PROCEDURE statement
- Create
Role - CREATE ROLE statement (PostgreSQL)
- Create
Schema - CREATE SCHEMA statement (PostgreSQL)
- Create
Sequence - CREATE SEQUENCE statement (PostgreSQL)
- Create
Server - CREATE SERVER statement (PostgreSQL)
- Create
Table - Represent a create table statement
- Create
Table As - Create
Table Partition Of - CREATE TABLE name PARTITION OF parent_table (PostgreSQL)
- Create
Trigger - Represent a create trigger statement
- Create
Type Enum - Create
View - Represent a create view statement
- Data
Type - Type of data
- Declare
Cursor - PostgreSQL DECLARE cursor statement
- Declare
Cursor Maria Db - MariaDB/MySQL DECLARE cursor statement inside a stored procedure/function.
DECLARE name CURSOR FOR select - Declare
Handler - MariaDB/MySQL DECLARE handler statement inside a stored procedure/function.
DECLARE CONTINUE|EXIT HANDLER FOR condition statement - Declare
Variable - MariaDB/MySQL DECLARE variable statement inside a stored procedure/function.
DECLARE name data_type [DEFAULT expr] - Default
Expression - Literal DEFAULT expression
- Delete
- Represent a delete statement
- Disable
RowLevel Security - PostgreSQL: DISABLE ROW LEVEL SECURITY
- Disable
Rule - PostgreSQL: DISABLE RULE rule_name
- Disable
Trigger - PostgreSQL: DISABLE TRIGGER { trigger_name | ALL | USER }
- Do
- Do statement
- Drop
Column - DROP COLUMN specification
- Drop
Database - Represent a drop database statement
- Drop
Domain - Represent a drop domain statement
- Drop
Event - Represent a drop event statement
- Drop
Extension - Represent a drop extension statement (PostgreSQL)
- Drop
Foreign Key - DROP FOREIGN KEY specification
- Drop
Function - Represent a drop function statement
- Drop
Function Arg - Drop
Index - Represent a drop index statement.
- Drop
Operator - Drop
Operator Class - Represent a drop operator class statement (PostgreSQL)
- Drop
Operator Family - Represent a drop operator family statement (PostgreSQL)
- Drop
Operator Item - Represent a drop operator statement (PostgreSQL)
- Drop
Primary Key - DROP PRIMARY KEY specification
- Drop
Procedure - Represent a drop procedure statement
- Drop
Sequence - Represent a drop sequence statement (PostgreSQL)
- Drop
Server - Represent a drop server statement
- Drop
Table - Represent a drop table statement
- Drop
Trigger - Represent a drop trigger statement
- Drop
View - Represent a drop view statement
- Enable
RowLevel Security - PostgreSQL: ENABLE ROW LEVEL SECURITY
- Enable
Rule - PostgreSQL: ENABLE [ REPLICA | ALWAYS ] RULE rule_name
- Enable
Trigger - PostgreSQL: ENABLE [ REPLICA | ALWAYS ] TRIGGER { trigger_name | ALL | USER }
- End
- End statement
- Exists
Expression - Exists expression
- Explain
- PostgreSQL EXPLAIN statement
- Extract
Expression - Extract expression
- Fetch
- Representation of a FETCH clause in VALUES
- Fetch
Cursor - MariaDB/MySQL FETCH cursor statement.
FETCH [NEXT] [FROM] cursor INTO var, ... - Field
Access Expression - PostgreSQL composite type field access: (expr).field
- Float
Expression - Literal floating point expression
- Flush
- Parse mariadb FLUSH statement.
- Force
RowLevel Security - PostgreSQL: FORCE ROW LEVEL SECURITY
- Foreign
KeyOn - Action to perform on events on foreign keys
- Fragment
- Function
Body - Body of a CREATE FUNCTION AS clause
- Function
Call Expression - Function call expression,
- Function
Param - A single function parameter
- Group
Concat Expression - Group contat expression
- Identifier
- Simple identifier in code it derefs to its string value
- Identifier
Expression - Identifier pointing to column
- If
- If statement
- IfCondition
- Condition in if statement
- InExpression
- In expression
- Include
Clause - Index
Col - Specify a column for an index, together with a with
- Index
Hint - Insert
Replace - Representation of Insert or Replace Statement
- Insert
Replace OnDuplicate KeyUpdate - Insert
Replace Set - Insert
Replace SetPair - Integer
Expression - Literal integer expression
- Interval
- Interior of
Type::Interval. - Interval
Expression - Time Interval
- Invalid
- Invalid statement produced after recovering from parse error
- Invalid
Expression - Invalid expression, returned on recovery of a parse error
- IsExpression
- Is expression
- Issue
- An issue encountered during parsing, or later stages
- Issue
Handle - Issues
- Iterate
- MariaDB/MySQL ITERATE label statement.
ITERATE label - Kill
- Represent a MySQL
KILLstatement - Leave
- MariaDB/MySQL LEAVE label statement.
LEAVE label - List
Hack Expression - Literal LIST expression
- Lock
- Represent a MySQL
LOCKstatement - Lock
Member - Locking
- Loop
- MariaDB/MySQL LOOP body END LOOP construct.
[label:] LOOP body END LOOP [label] - Match
Against Expression - Full-text MATCH … AGAINST expression
- Member
OfExpression - Member of expression
- Modify
Column - Modify a column
- NoForce
RowLevel Security - PostgreSQL: NO FORCE ROW LEVEL SECURITY
- Null
Expression - Literal NULL expression
- OnConflict
- Open
Cursor - MariaDB/MySQL OPEN cursor statement.
OPEN cursor_name - OwnerTo
- OWNER TO specification
- Parse
Options - Options used when parsing sql
- Partition
By - PARTITION BY clause, appended to a CREATE TABLE statement
- Prepare
- PostgreSQL PREPARE statement
- Qualified
Name - Quantifier
Expression - PostgreSQL ANY / SOME / ALL quantifier expression.
- Refresh
Materialized View - PostgreSQL REFRESH MATERIALIZED VIEW statement
- Rename
Column - RENAME COLUMN specification
- Rename
Constraint - RENAME CONSTRAINT specification
- Rename
Index - RENAME INDEX specification
- Rename
Table - Represent a rename table statement
- Rename
To - RENAME TO specification
- Repeat
- MariaDB/MySQL REPEAT body UNTIL condition END REPEAT construct.
[label:] REPEAT body UNTIL condition END REPEAT [label] - Replica
Identity - PostgreSQL: REPLICA IDENTITY { DEFAULT | USING INDEX
| FULL | NOTHING } - Return
- Return statement
- Role
Membership - Role membership clause in CREATE ROLE
- RowExpression
- A row/tuple constructor expression:
(expr1, expr2, ...) - SString
- A string with attached span
- Select
- Representation of select Statement
- Select
Expr - Value in select
- Set
- Show
Character Set - Parse result for
SHOW CHARACTER SET/SHOW CHARSET - Show
Collation - Parse result for
SHOW COLLATION - Show
Columns - Parse result for
SHOW COLUMNS/SHOW FIELDS - Show
Create Database - Parse result for
SHOW CREATE DATABASE - Show
Create Table - Parse result for
SHOW CREATE TABLE - Show
Create View - Parse result for
SHOW CREATE VIEW - Show
Databases - Parse result for
SHOW DATABASES - Show
Engines - Parse result for
SHOW ENGINES - Show
Process List - Parse result for
SHOW PROCESSLIST/SHOW FULL PROCESSLIST - Show
Status - Parse result for
SHOW STATUS - Show
Tables - Parse result for
SHOW TABLESvariants - Show
Variables - Parse result for
SHOW VARIABLES - Signal
- Return statement
- Start
Transaction - Start transaction statement
- Stdin
- Stdin statement, used to represent input from stdin after a COPY statement
- Subquery
Expression - Subquery expression
- Table
ToTable - Timestamp
- Timestamp
AddExpression - Timestampadd call
- Timestamp
Diff Expression - Timestampdiff call
- Trigger
Reference - Trim
Expression - TRIM([{BOTH|LEADING|TRAILING} [remstr] FROM] str) expression
- Truncate
Table - Truncate
Table Spec - Represent a truncate table statement
- Type
Cast Expression - PostgreSQL-style typecast expression (expr::type)
- Unary
Expression - Expression with a unary (prefix) operator
- Unlock
- Represent a MySQL
UNLOCKstatement - Update
- Representation of replace Statement
- User
Variable Expression - User variable expression (@variable_name)
- Validate
Constraint - PostgreSQL: VALIDATE CONSTRAINT constraint_name
- Values
- Representation of a VALUES statement
- Variable
Expression - Variable expression
- When
- When part of CASE
- When
Statement - When part of case statement
- While
- MariaDB/MySQL WHILE condition DO body END WHILE construct.
[label:] WHILE condition DO body END WHILE [label] - Window
Clause - Window
Frame - Window frame clause: ROWS/RANGE/GROUPS { frame_start | BETWEEN frame_start AND frame_end }
- Window
Function Call Expression - A window function call expression
- Window
Spec - When part of CASE
- With
Block - With
Option - A single
key = valueoption inside aWITH (...)clause onCREATE INDEX. - With
Query - Represent a with query statement
Enums§
- After
Event - Enum for constraint trigger events
- Alter
Algorithm - Alter
Column Action - Enum of alterations to perform on a column
- Alter
Lock - Alter
Operator Action - ALTER OPERATOR actions (OWNER TO, SET SCHEMA, SET (…))
- Alter
Operator Class Action - Alter
Operator Family Action - Actions for ALTER OPERATOR FAMILY (ADD, DROP, RENAME TO, OWNER TO, SET SCHEMA)
- Alter
Role Action - Actions that can be performed in ALTER ROLE
- Alter
Role Value - Parameters value for ALTER ROLE SET/RESET
- Alter
Schema Action - Alter
Specification - Enum of alterations to perform on a table
- Alter
Table Owner - Owner value for ALTER TABLE OWNER TO
- Alter
Type Action - ALTER TYPE actions
- Attribute
Action - Attribute actions for composite types
- Binary
Operator - Binary operator to apply
- Cascade
OrRestrict - Drop cascade or restrict option (PostgreSQL)
- Compound
Operator - Set operator used between compound query branches
- Compound
Quantifier - Quantifier for a compound-query operator
- Copy
Column List - A column list in a COPY option: either
*(all columns) or( col, ... ). - Copy
Header Value - Value for the
HEADERoption: a boolean orMATCH. - Copy
Location - Where to read from / write to in a COPY statement.
- Copy
Option - A single
WITH ( ... )option in a COPY statement. - Copy
Source - The source / destination of a COPY: either a table (with optional column list)
or a parenthesised query (only valid with
TO). - Create
Algorithm - Special algorithm used for table creation
- Create
Database Option - Create
Definition - Definition in create table
- Create
Index Option - Create
Option - Options for create statement
- Cursor
Hold - Hold behaviour of a declared cursor
- Cursor
Scroll - Scroll behaviour of a declared cursor
- Cursor
Sensitivity - Sensitivity of a declared cursor
- Data
Type Property - A property on a datatype
- Deferrable
- Whether the trigger is deferrable
- Delete
Flag - Flags for deletion
- DoBody
- Body of a DO statement
- Domain
Constraint - Domain constraint for CREATE DOMAIN
- Drop
Function ArgMode - Explain
Format - PostgreSQL EXPLAIN output format
- Explain
Option - A single option in a parenthesized EXPLAIN (…) list
- Expression
- Representation of an expression
- Fetch
Direction - Direction for FETCH clause (FIRST or NEXT)
- Flush
Option - Foreign
KeyMatch - MATCH type for a foreign key reference
- Foreign
KeyOn Action - Action to take on event for foreign key
- Foreign
KeyOn Type - When to take a foreign key action
- Function
- Function to execute
- Function
Characteristic - Characteristic of a function
- Function
Language - Language of a function
- Function
Parallel - Parallel safety level of a function
- Function
Param Direction - Direction of a function argument
- Handler
Action - Action part of a MariaDB/MySQL handler declaration.
- Handler
Condition - Condition part of a MariaDB/MySQL handler declaration.
- Identifier
Part - Part of a full identifier
- Identity
Option - Identity restart option for TRUNCATE (PostgreSQL)
- Index
ColExpr - Column or expression specification for an index
- Index
Hint For - Index
Hint Type - Index
Hint Use - Index
Option - Option on an index
- Index
Type - Type of index to add
- Initially
- Initial timing of the constraint trigger
- Insert
Replace Flag - Flags for insert
- Insert
Replace Type - Interval
Field - A single field qualifier for an
INTERVALtype. - Is
- Type of is expression
- Join
Specification - Specification for join
- Join
Type - Type of join
- Json
Table Column - JSON_TABLE column definition
- Json
Table OnError Empty - JSON_TABLE ON EMPTY/ERROR behavior
- Kill
Type - Left
Operator Type - Left operator type for ALTER OPERATOR (explicitly stores NONE span or DataType)
- Level
- Level of an issues
- Lock
Strength - Lock strength for locking
- Lock
Type - Lock
Wait - Match
Mode - Mode for MATCH … AGAINST
- Materialized
Hint - PostgreSQL CTE materialization hint
- OnCommit
Action - Action for ON COMMIT clause on temporary tables
- OnConflict
Action - OnConflict
Target - Operator
Class Item - Operator
Class Operator Option - Operator
Family Drop Item - Item to drop from an operator family (OPERATOR or FUNCTION)
- Operator
Family Item - Item to add to an operator family (OPERATOR or FUNCTION)
- Operator
Option - Options for CREATE OPERATOR
- Operator
Ref - Reference to an operator
- Order
Flag - Ordering direction
- Partition
Bound Expr - A single value in a partition bound specification: an expression, MINVALUE, or MAXVALUE
- Partition
Bound Spec - The partition bound specification for CREATE TABLE … PARTITION OF
- Partition
Method - The partitioning method for PARTITION BY
- Partition
OfBound - The bound clause of a PARTITION OF statement
- Quantifier
- Which row quantifier keyword was used
- Range
Subtype - Subtype for a built-in PostgreSQL range or multirange type.
- Replica
Identity Option - Options for REPLICA IDENTITY
- Role
Membership Type - Role membership type for CREATE ROLE
- Role
Option - Role option for CREATE ROLE / ALTER ROLE
- SQLArguments
- What kinds or arguments
- SQLDialect
- What sql diarect to parse as
- Select
Flag - Flags specified after SELECT
- Sequence
Option - Sequence option for CREATE SEQUENCE / ALTER SEQUENCE
- Signal
Condition Information Name - Statement
- SQL statement
- Table
Constraint Type - Table constraint types for ADD CONSTRAINT
- Table
Function Name - The name of a table-valued function in a FROM clause.
- Table
Option - Options on created table
- Table
Reference - Reference to table in select
- Time
Unit - Units of time
- Trigger
Event - On what event to fire the trigger
- Trigger
Name - Trigger name variants for ENABLE/DISABLE TRIGGER
- Trigger
Reference Direction - Trigger
Time - When to fire the trigger
- Trim
Direction - Direction for TRIM()
- Type
- Type of datatype
- Unary
Operator - Unary operator to apply
- Update
Flag - Flags specified after “UPDATE”
- Using
Index Method - Variable
- Function to execute
- Window
Frame Bound - One bound in a window frame clause
- Window
Frame Mode - Window frame mode: ROWS, RANGE, or GROUPS
Traits§
- OptSpanned
- Compute an optional byte span of an ast fragment
- Spanned
- Compute byte span of an ast fragment
Functions§
- parse_
statement - Parse a single statement, A statement may be returned even if there where parse errors. The statement is free of errors if no Error issues are added to issues
- parse_
statements - Parse multiple statements, return an Vec of Statements even if there are parse errors. The statements are free of errors if no Error issues are added to issues
Type Aliases§
- Span
- Byte span of ast fragment