Skip to content

systemxlabs/sqlparser-nom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

No packages published

Languages