Sign in

    sqlparser

    Extensible SQL Lexer and Parser in MoonBit

    sql
    parser
    lexer
    Download zip
    Author
    Version
    0.3.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    78

    #Extensible SQL Lexer and Parser for MoonBit

    #Example

    #Todo

    Dialect

    pub trait Dialect {
    supports_string_literal_backslash_escape(Self) -> Bool
    supports_boolean_literals(Self) -> Bool
    supports_filter_during_aggregation(Self) -> Bool
    supports_within_after_array_aggregation(Self) -> Bool
    requires_column_types_in_create_table(Self) -> Bool
    supports_if_not_exists(Self) -> Bool
    supports_double_quoted_identifiers(Self) -> Bool
    parse_expr(Self, tokens : ArrayView[Token]) -> (Expr, ArrayView[Token])? raise ParserError
    parse_statement(Self, parser : Parser, tokens : ArrayView[Token]) -> (Statement, ArrayView[Token])? raise ParserError
    }

    LexerError

    type LexerError

    impl Show for LexerError

    ParserError

    type ParserError

    impl Show for ParserError

    SqlParserError

    pub suberror SqlParserError {
    LexerError(LexerError)
    ParserError(ParserError)
    }

    ANSI

    pub(all) struct ANSI {
    }

    ANSI SQL dialect - adheres to ANSI SQL standards
    impl Dialect for ANSI

    AlterTableOperation

    pub enum AlterTableOperation {
    DropColumn(String, Bool)
    }

    AlterTableStmt

    pub struct AlterTableStmt {
    table_name : ObjectName
    if_exists : Bool
    operation : AlterTableOperation
    }

    Assignment

    pub struct Assignment {
    column : String
    value : Expr
    }

    impl Eq for Assignment
    impl Show for Assignment

    BigQuery

    pub(all) struct BigQuery {
    }

    BigQuery dialect - supports Google BigQuery specific syntax
    impl Dialect for BigQuery

    BinaryOperator

    pub enum BinaryOperator {
    Eq
    Neq
    Lt
    Gt
    LtEq
    GtEq
    Spaceship
    Plus
    Minus
    Mul
    Div
    IntegerDiv
    Mod
    And
    Or
    }

    BinaryOperator::get_precedence

    fn BinaryOperator::get_precedence(self : BinaryOperator) -> Precedence

    CaseExpr

    pub struct CaseExpr {
    operand : Expr?
    when_then_clauses : Array[(Expr, Expr)]
    else_expr : Expr?
    }
    CASE [operand] WHEN THEN [...] [ELSE ] END
    impl Eq for CaseExpr
    impl Show for CaseExpr

    ClickHouse

    pub(all) struct ClickHouse {
    }

    ClickHouse dialect - supports ClickHouse specific syntax

    ColumnDef

    pub struct ColumnDef {
    name : String
    data_type : DataType
    options : Array[ColumnDefOption]
    }

    impl Eq for ColumnDef
    impl Show for ColumnDef

    ColumnDefOption

    pub enum ColumnDefOption {
    NotNull
    Unique
    Default(Expr)
    PrimaryKey
    }

    CreateTableDefinition

    pub enum CreateTableDefinition {
    Columns(Array[ColumnDef], Array[TableConstraint])
    AsQuery(QueryStmt)
    }

    CreateTableStmt

    pub struct CreateTableStmt {
    name : String
    if_not_exists : Bool
    definition : CreateTableDefinition
    }

    CreateViewStmt

    type CreateViewStmt

    DataType

    pub enum DataType {
    Integer
    Smallint
    Bigint
    Float(Int?)
    Real
    Double
    Char(Int)
    Varchar(Int)
    Text
    Boolean
    Timestamp
    Blob
    }

    impl Eq for DataType
    impl Show for DataType

    DatetimeUnit

    pub enum DatetimeUnit {
    Year
    Month
    Day
    Hour
    Minute
    Second
    }

    impl Eq for DatetimeUnit

    DeleteStmt

    pub struct DeleteStmt {
    table_name : ObjectName
    where_clause : Expr?
    }

    impl Eq for DeleteStmt
    impl Show for DeleteStmt

    DropTableStmt

    pub struct DropTableStmt {
    table_name : ObjectName
    if_exists : Bool
    }

    impl Eq for DropTableStmt

    DropViewStmt

    pub struct DropViewStmt {
    name : String
    }

    impl Eq for DropViewStmt

    DuckDB

    pub(all) struct DuckDB {
    }

    DuckDB dialect - supports DuckDB specific syntax
    impl Dialect for DuckDB

    DuplicateTreatment

    pub enum DuplicateTreatment {
    Distinct
    All
    }

    Expr

    pub enum Expr {
    Identifier(String)
    CompoundIdentifier(Array[String])
    Literal(Literal)
    BinaryOperation(Expr, BinaryOperator, Expr)
    UnaryOperation(UnaryOperator, Expr)
    FunctionCall(String, DuplicateTreatment?, Array[Expr])
    Wildcard
    Datetime(String)
    Interval(String, IntervalQualifier)
    Like(Bool, Expr, Expr)
    ILike(Bool, Expr, Expr)
    SubQuery(QueryStmt)
    Exists(Bool, QueryStmt)
    Between(Bool, Expr, Expr, Expr)
    Extract(PrimaryDatetimeField, Expr)
    Case(CaseExpr)
    InList(Bool, Expr, Array[Expr])
    InSubQuery(Bool, Expr, QueryStmt)
    Substring(Expr, Expr?, Expr?)
    PlaceHolder
    }

    impl Eq for Expr
    impl Show for Expr

    Generic

    pub(all) struct Generic {
    }

    Generic SQL dialect that supports the union of all other dialects This is the most permissive dialect, useful for parsing various SQL statements from different sources without strict dialect requirements
    impl Dialect for Generic

    InsertSource

    pub enum InsertSource {
    Values(Array[Array[Expr]])
    Query(QueryStmt)
    }

    impl Eq for InsertSource

    InsertStmt

    pub struct InsertStmt {
    table_name : ObjectName
    columns : Array[String]
    source : InsertSource
    }

    impl Eq for InsertStmt
    impl Show for InsertStmt

    IntervalQualifier

    pub enum IntervalQualifier {
    Single(PrimaryDatetimeField)
    Range(PrimaryDatetimeField, PrimaryDatetimeField)
    }

    Join

    pub struct Join {
    table_ref : TableRef
    join_operator : JoinOperator
    }

    impl Eq for Join
    impl Show for Join

    JoinConstraint

    pub enum JoinConstraint {
    On(Expr)
    Using(Array[String])
    None
    }

    JoinOperator

    pub enum JoinOperator {
    Join(JoinConstraint)
    Left(JoinConstraint)
    LeftOuter(JoinConstraint)
    Right(JoinConstraint)
    RightOuter(JoinConstraint)
    Full(JoinConstraint)
    FullOuter(JoinConstraint)
    Inner(JoinConstraint)
    Cross
    }

    impl Eq for JoinOperator

    Keyword

    pub(all) enum Keyword {
    Select
    From
    Where
    As
    Group
    Order
    By
    Asc
    Desc
    Nulls
    First
    Last
    Year
    Month
    Day
    Hour
    Minute
    Second
    Date
    Interval
    To
    Like
    Not
    ILike
    Exists
    Between
    And
    Or
    Extract
    Case
    When
    Then
    Else
    Having
    End
    In
    Join
    Left
    Right
    Full
    Outer
    Inner
    Cross
    On
    Using
    Limit
    Offset
    Create
    Table
    Integer
    Int
    Smallint
    Bigint
    Real
    Double
    Precision
    Char
    Character
    Varchar
    Varing
    Text
    Time
    Boolean
    Float
    Timestamp
    Blob
    Null
    Default
    Unique
    View
    Drop
    Distinct
    All
    Substring
    For
    Primary
    Key
    Foreign
    References
    Check
    Union
    Intersect
    Except
    Top
    Insert
    Into
    Values
    If
    Delete
    Update
    Set
    Truncate
    Alter
    Column
    Show
    Tables
    Columns
    Status
    Databases
    Div
    Lock
    Unlock
    Listen
    Notify
    }

    impl Eq for Keyword
    impl Show for Keyword

    Literal

    pub enum Literal {
    Integer(Int)
    Double(Double)
    String(String)
    Boolean(Bool)
    Null
    }

    impl Eq for Literal
    impl Show for Literal

    MySQL

    pub(all) struct MySQL {
    }

    impl Dialect for MySQL

    ObjectName

    pub struct ObjectName {
    parts : Array[String]
    }

    impl Eq for ObjectName
    impl Show for ObjectName

    OrderByExpr

    pub struct OrderByExpr {
    expr : Expr
    asc : Bool?
    nulls_first : Bool?
    }

    impl Eq for OrderByExpr
    impl Show for OrderByExpr

    Parser

    pub struct Parser {
    dialect : Dialect
    }

    Postgres

    pub(all) struct Postgres {
    }

    impl Dialect for Postgres

    Precedence

    pub(all) enum Precedence {
    PlusMinus
    MulDivMod
    Eq
    Like
    And
    Or
    Between
    UnaryNot
    }

    Precedence::value

    fn Precedence::value(self : Precedence) -> Int

    PrimaryDatetimeField

    type PrimaryDatetimeField

    Projection

    pub enum Projection {
    Wildcard
    UnamedExpr(Expr)
    AliasedExpr(Expr, String)
    }

    impl Eq for Projection
    impl Show for Projection

    QueryStmt

    pub struct QueryStmt {
    body : SetExpr
    order_by : Array[OrderByExpr]
    limit : Expr?
    offset : Expr?
    }

    impl Eq for QueryStmt
    impl Show for QueryStmt

    Redshift

    pub(all) struct Redshift {
    }

    Redshift dialect - supports Amazon Redshift specific syntax
    impl Dialect for Redshift

    SQLite

    pub(all) struct SQLite {
    }

    impl Dialect for SQLite

    SelectStmt

    type SelectStmt

    impl Eq for SelectStmt
    impl Show for SelectStmt

    SetExpr

    type SetExpr

    impl Eq for SetExpr
    impl Show for SetExpr

    SetOperator

    type SetOperator

    impl Eq for SetOperator
    impl Show for SetOperator

    ShowFilter

    pub enum ShowFilter {
    Like(String)
    Where(Expr)
    }

    impl Eq for ShowFilter
    impl Show for ShowFilter

    ShowStmt

    pub struct ShowStmt {
    show_type : ShowType
    object : ObjectName?
    filter : ShowFilter?
    }

    MySQL SHOW statement
    impl Eq for ShowStmt
    impl Show for ShowStmt

    ShowType

    pub enum ShowType {
    Tables
    Columns
    Status
    Databases
    }

    impl Eq for ShowType
    impl Show for ShowType

    Snowflake

    pub(all) struct Snowflake {
    }

    Snowflake dialect - supports Snowflake specific syntax

    Statement

    pub enum Statement {
    Query(QueryStmt)
    CreateTable(CreateTableStmt)
    CreateView(CreateViewStmt)
    DropView(DropViewStmt)
    DropTable(DropTableStmt)
    Insert(InsertStmt)
    Delete(DeleteStmt)
    Update(UpdateStmt)
    Truncate(TruncateStmt)
    AlterTable(AlterTableStmt)
    Show(ShowStmt)
    LockTables(Array[ObjectName])
    UnlockTables
    Listen(String)
    Notify(String, String?)
    }

    impl Eq for Statement
    impl Show for Statement

    Statements

    pub struct Statements {
    stmts : Array[Statement]
    }

    impl Eq for Statements
    impl Show for Statements

    Statements::op_get

    fn Statements::op_get(self : Statements, index : Int) -> Statement

    TableAlias

    pub struct TableAlias {
    name : String
    columns : Array[String]
    }

    impl Eq for TableAlias
    impl Show for TableAlias

    TableConstraint

    pub enum TableConstraint {
    Unique(Array[OrderByExpr])
    PrimaryKey(Array[OrderByExpr])
    ForeignKey(Array[OrderByExpr], ObjectName, Array[String])
    Check(Expr)
    }

    TableFactor

    pub enum TableFactor {
    Column(ObjectName, TableAlias?)
    SubQuery(QueryStmt, TableAlias?)
    }

    impl Eq for TableFactor
    impl Show for TableFactor

    TableRef

    pub struct TableRef {
    factor : TableFactor
    joins : Array[Join]
    }

    impl Eq for TableRef
    impl Show for TableRef

    Token

    pub(all) enum Token {
    Keyword(Keyword)
    Identifier(String)
    Number(String)
    StringLiteral(String)
    Boolean(Bool)
    Comma
    Semicolon
    Eq
    DoubleEq
    Neq
    Lt
    Gt
    LtEq
    GtEq
    Spaceship
    Plus
    Minus
    Mul
    Div
    Mod
    LBracket
    RBracket
    LBrace
    RBrace
    LParen
    RParen
    Period
    PlaceHolder
    Unknown(Char)
    Eof
    }

    impl Eq for Token
    impl Show for Token

    Top

    pub enum Top {
    Constant(Int)
    Expr(Expr)
    }

    impl Eq for Top
    impl Show for Top

    TruncateStmt

    pub struct TruncateStmt {
    table_name : ObjectName
    }

    impl Eq for TruncateStmt

    UnaryOperator

    pub enum UnaryOperator {
    Plus
    Minus
    Not
    }

    impl Eq for UnaryOperator

    UpdateStmt

    pub struct UpdateStmt {
    table_name : ObjectName
    assignments : Array[Assignment]
    where_clause : Expr?
    }

    impl Eq for UpdateStmt
    impl Show for UpdateStmt

    ViewColumnDef

    pub struct ViewColumnDef {
    name : String
    }

    impl Eq for ViewColumnDef

    parse

    fn parse(dialect? : Dialect, tokens : ArrayView[Token]) -> Array[Statement] raise ParserError

    parse_sql

    fn parse_sql(dialect? : Dialect, input : String) -> Statements raise SqlParserError

    pretty_print

    fn[T :
    Pretty
    ] pretty_print(obj : T) -> String

    structural_print

    fn[T : Show] structural_print(obj : T) -> String

    tokenize

    fn tokenize(dialect? : Dialect, input : String) -> Array[Token] raise LexerError