A SQL query parser written using nom.

Overview

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

You might also like...
A small CLI tool to query ArcGIS REST API services, implemented in Rust. The server response is returned as pretty JSON.

A small CLI tool to query ArcGIS REST API services, implemented in Rust. The server response is returned as pretty JSON.

Track and query Cargo dependency graphs.

cargo-guppy: track and query dependency graphs This repository contains the source code for: guppy: a library for performing queries on Cargo dependen

Command line tool to query the Oxford Dictionaries API.
Command line tool to query the Oxford Dictionaries API.

oxd Oxd is a client library for the Oxford Dictionary API. It provides a series of structs modeling entries returned from the API, a function [get_ent

Leptos Query - a robust asynchronous state management library for Leptos,

Leptos Query is a robust asynchronous state management library for Leptos, providing simplified data fetching, integrated reactivity, server-side rendering support, and intelligent cache management.

The joker_query is a cute query builder, with Joker can implement most complex queries with sugar syntax
The joker_query is a cute query builder, with Joker can implement most complex queries with sugar syntax

joker_query The joker_query is most sugared query builder of Rust, with joker_query can implement most complex queries with sugar syntax Features − (O

A full featured, fast Command Line Argument Parser for Rust

clap Command Line Argument Parser for Rust It is a simple-to-use, efficient, and full-featured library for parsing command line arguments and subcomma

Docopt for Rust (command line argument parser).

THIS CRATE IS UNMAINTAINED This crate is unlikely to see significant future evolution. The primary reason to choose this crate for a new project is if

A minimal argument parser

Pieces An argument parser built with control in mind. Parsing The results you get are dependent on what order you parse in. If you want to say only pa

A full featured, fast Command Line Argument Parser for Rust

clap Command Line Argument Parser for Rust It is a simple-to-use, efficient, and full-featured library for parsing command line arguments and subcomma

Owner
SystemX Labs
Build system softwares for fun.
SystemX Labs
Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability and productivity.

Sleek: SQL Formatter ✨ Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability an

Nick Rempel 40 Apr 20, 2023
qsv - Performant CLI tool to query CSVs through SQL

qsv Performant CLI tool to query CSVs through SQL Installation After cloning the repository, you can install a binary locally using cargo install --pa

Dermot Haughey 3 Oct 28, 2021
Fgr - Find & Grep utility with SQL-like query language

fgr Find & Grep utility with SQL-like query language. Examples # Find all files with name equal to sample under the current directory: fgr -e name=sam

Igor 3 Dec 22, 2022
A language parser tool to build recursive descent top down parser.

lang-pt A language parser tool to generate recursive descent top down parser. Overview Parsers written for the languages like Javascript are often cus

Creative Forest 7 Jan 4, 2023
AskBend: SQL-based Knowledge Base Search and Completion using Databend

AskBend: SQL-based Knowledge Base Search and Completion using Databend AskBend is a Rust project that utilizes the power of Databend and OpenAI to cre

Databend Labs 87 Apr 7, 2023
Terminal based, feature rich, interactive SQL tool

datafusion-tui (dft) DataFusion-tui provides a feature rich terminal application, built with tui-rs, for using DataFusion (and eventually Ballista). I

null 49 Dec 24, 2022
This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects

This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects, such as tables, views, materialized views, functions, triggers, and indexes. The generated SQL statements can be useful for schema introspection, documentation, or migration purposes.

Tyr Chen 11 Apr 4, 2023
Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using ttf-parser and allsorts

rust-fontconfig Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser in order to parse .woff,

Felix Schütt 28 Oct 29, 2022
CLI to query for public transport connections in switzerland

Swiss Public Transport CLI This is a simple tool to search public transport connections from one station to another, possible with a via. It uses the

Sandro Covo 4 Apr 2, 2022
On-chain query aggregator/batcher in Terra.

Multicall On-chain query aggregator/batcher in Terra. Mainnet Code Id: 3758 Mainnet Address: terra1y60jx2jqh5qpmcnvgz3n0zg2p6ky4mr6ax2qa5 Testnet Code

SCB 10X 32 Sep 5, 2022