Code Monkey home page Code Monkey logo

sqlparser-nom's Introduction

sqlparser-nom

A SQL query parser written using nom.

  • Query
    • Select
    • From
    • Where
    • Order by
    • Limit
    • CTE
    • Group by
    • Having
    • Aggregate
    • Window
  • Pratt Parsing
  • Friendly error info

Example

More examples can be found in tests.

select a, count(*)
from (select * from t1) as t2
join t3 on t2.a = t3.a
left join t4 on t3.b = t4.b
where a > ((1 + 2) * 3) and b < c 
group by a, c 
having count(*) > 5
order by a, b desc 
limit 1, 2

output ast:

SelectStatement {
    body: Select {
        projection: [
            UnnamedExpr(
                ColumnRef {
                    database: None,
                    table: None,
                    column: Ident {
                        value: "a",
                    },
                },
            ),
            UnnamedExpr(
                Function {
                    name: Ident {
                        value: "count",
                    },
                    distinct: false,
                    args: [
                        Wildcard,
                    ],
                },
            ),
        ],
        from: Some(
            Join {
                op: LeftOuter,
                condition: On(
                    BinaryOp {
                        left: ColumnRef {
                            database: None,
                            table: Some(
                                Ident {
                                    value: "t3",
                                },
                            ),
                            column: Ident {
                                value: "b",
                            },
                        },
                        op: Eq,
                        right: ColumnRef {
                            database: None,
                            table: Some(
                                Ident {
                                    value: "t4",
                                },
                            ),
                            column: Ident {
                                value: "b",
                            },
                        },
                    },
                ),
                left: Join {
                    op: Inner,
                    condition: On(
                        BinaryOp {
                            left: ColumnRef {
                                database: None,
                                table: Some(
                                    Ident {
                                        value: "t2",
                                    },
                                ),
                                column: Ident {
                                    value: "a",
                                },
                            },
                            op: Eq,
                            right: ColumnRef {
                                database: None,
                                table: Some(
                                    Ident {
                                        value: "t3",
                                    },
                                ),
                                column: Ident {
                                    value: "a",
                                },
                            },
                        },
                    ),
                    left: Subquery {
                        subquery: SelectStatement {
                            body: Select {
                                projection: [
                                    Wildcard,
                                ],
                                from: Some(
                                    BaseTable {
                                        name: TableName {
                                            database: None,
                                            table: Ident {
                                                value: "t1",
                                            },
                                        },
                                        alias: None,
                                    },
                                ),
                                selection: None,
                                group_by: [],
                                having: None,
                            },
                            order_by: [],
                            limit: None,
                            offset: None,
                        },
                        alias: Some(
                            Ident {
                                value: "t2",
                            },
                        ),
                    },
                    right: BaseTable {
                        name: TableName {
                            database: None,
                            table: Ident {
                                value: "t3",
                            },
                        },
                        alias: None,
                    },
                },
                right: BaseTable {
                    name: TableName {
                        database: None,
                        table: Ident {
                            value: "t4",
                        },
                    },
                    alias: None,
                },
            },
        ),
        selection: Some(
            BinaryOp {
                left: BinaryOp {
                    left: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "a",
                        },
                    },
                    op: Gt,
                    right: BinaryOp {
                        left: BinaryOp {
                            left: Literal(
                                UnsignedInteger(
                                    1,
                                ),
                            ),
                            op: Add,
                            right: Literal(
                                UnsignedInteger(
                                    2,
                                ),
                            ),
                        },
                        op: Mul,
                        right: Literal(
                            UnsignedInteger(
                                3,
                            ),
                        ),
                    },
                },
                op: And,
                right: BinaryOp {
                    left: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "b",
                        },
                    },
                    op: Lt,
                    right: ColumnRef {
                        database: None,
                        table: None,
                        column: Ident {
                            value: "c",
                        },
                    },
                },
            },
        ),
        group_by: [
            ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "a",
                },
            },
            ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "c",
                },
            },
        ],
        having: Some(
            BinaryOp {
                left: Function {
                    name: Ident {
                        value: "count",
                    },
                    distinct: false,
                    args: [
                        Wildcard,
                    ],
                },
                op: Gt,
                right: Literal(
                    UnsignedInteger(
                        5,
                    ),
                ),
            },
        ),
    },
    order_by: [
        OrderByExpr {
            expr: ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "a",
                },
            },
            asc: None,
        },
        OrderByExpr {
            expr: ColumnRef {
                database: None,
                table: None,
                column: Ident {
                    value: "b",
                },
            },
            asc: Some(
                false,
            ),
        },
    ],
    limit: Some(
        Literal(
            UnsignedInteger(
                1,
            ),
        ),
    ),
    offset: Some(
        Literal(
            UnsignedInteger(
                2,
            ),
        ),
    ),
}

References

sqlparser-nom's People

Contributors

lewiszlw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sqlparser-nom's Issues

License?

Do you have a license for this library? I'm looking at forking this (or heavily copying) to adapt to work with a very SQL-Like language CQL2 with a custom AST. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.