Crate sql_parse

Source
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 sql_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§

AlterTable
Represent an alter table statement
CreateFunction
Representation of Create Function Statement
CreateTable
Represent a create table statement
CreateTrigger
Represent a create trigger statement
CreateView
Represent a create view statement
DataType
Type of data
Delete
Represent a delete statement
DropDatabase
Represent a drop database statement
DropEvent
Represent a drop event statement
DropFunction
Represent a drop function statement
DropIndex
Represent a drop index statement.
DropProcedure
Represent a drop procedure statement
DropServer
Represent a drop server statement
DropTable
Represent a drop table statement
DropTrigger
Represent a drop trigger statement
DropView
Represent a drop view statement
ForeignKeyOn
Action to perform on events on foreign keys
Fragment
Identifier
Simple identifier in code it derefs to its string value
IndexCol
Specify a column for an index, together with a with
IndexHint
InsertReplace
Representation of Insert or Replace Statement
InsertReplaceOnDuplicateKeyUpdate
InsertReplaceSet
InsertReplaceSetPair
Issue
An issue encountered during parsing, or later stages
IssueHandle
Issues
OnConflict
ParseOptions
Options used when parsing sql
QualifiedName
RenameTable
Represent a rename table statement
SString
A string with attached span
Select
Representation of select Statement
SelectExpr
Value in select
TableToTable
TruncateTable
Represent a truncate table statement
Union
Union statement
UnionWith
Right hand side of a union expression
Update
Representation of replace Statement
When
When part of CASE
WithBlock
WithQuery
Represent a with query statement

Enums§

AlterColumnAction
Enum of alterations to perform on a column
AlterSpecification
Enum of alterations to perform on a table
BinaryOperator
Binary operator to apply
CreateAlgorithm
Special algorithm used for table creation
CreateDefinition
Definition in create table
CreateOption
Options for create statement
DataTypeProperty
A property on a datatype
DeleteFlag
Flags for deletion
Expression
Representation of an expression
ForeignKeyOnAction
Action to take on event for foreign key
ForeignKeyOnType
When to take a foreign key action
Function
Function to execute
IdentifierPart
Part of a full identifier
IndexHintFor
IndexHintType
IndexHintUse
IndexOption
Option on an index
IndexType
Type of index to add
InsertReplaceFlag
Flags for insert
InsertReplaceType
Is
Type of is expression
JoinSpecification
Specification for join
JoinType
Type of join
Level
Level of an issues
OnConflictAction
OnConflictTarget
SQLArguments
What kinds or arguments
SQLDialect
What sql diarect to parse as
SelectFlag
Flags specified after SELECT
Statement
SQL statement
TableOption
Options on created table
TableReference
Reference to table in select
Type
Type of datatype
UnaryOperator
Unary operator to apply
UnionType
Type of union to perform
UpdateFlag
Flags specified after “UPDATE”
Variable
Function to execute

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