2 releases

Uses old Rust 2015

0.1.0 Mar 17, 2024
0.0.2 Apr 16, 2024
0.0.1 Apr 15, 2024

#215 in Database interfaces

Download history 84/week @ 2024-03-11 45/week @ 2024-03-18 14/week @ 2024-04-01 288/week @ 2024-04-15 5/week @ 2024-04-22

307 downloads per month

Apache-2.0 OR MIT

435KB
11K SLoC

sqlparser-mysql Rust Coverage Status

A SQL parser for MySQL with nom. Written in Rust.

Project Status

Please note that this repository is currently under active development. As such, it is subject to potentially disruptive changes without prior notice. We are working hard to improve the project and make it more robust. However, during this phase, we might introduce significant modifications to the codebase, features, and functionality.

We encourage users and contributors to keep this in mind when using, forking, or contributing to this project. Your understanding and patience are greatly appreciated as we continue to evolve this project.

Stay tuned for updates, and feel free to reach out or contribute to the development process!

Disclaimer

This project is in a pre-release state. It may contain incomplete features, bugs, and unexpected behaviors. Use it at your own risk.

Quick Start

Example parsing SQL

use sqlparser_mysql::parser::Parser;
use sqlparser_mysql::parser::ParseConfig;

let config = ParseConfig::default();
let sql = "SELECT a, b, 123, myfunc(b) \
            FROM table_1 \
            WHERE a > b AND b < 100 \
            ORDER BY a DESC, b";
// parse to a Statement
let ast = Parser::parse(&config, sql).unwrap();

println!("AST: {:#?}", ast);

The output should be:

AST: Select(
    SelectStatement {
        tables: [
            Table {
                name: "table_1",
                alias: None,
                schema: None,
            },
        ],
        distinct: false,
        fields: [
            Col(
                Column {
                    name: "a",
                    alias: None,
                    table: None,
                    function: None,
                },
            ),
            Col(
                Column {
                    name: "b",
                    alias: None,
                    table: None,
                    function: None,
                },
            ),
            Value(
                Literal(
                    LiteralExpression {
                        value: Integer(
                            123,
                        ),
                        alias: None,
                    },
                ),
            ),
            Col(
                Column {
                    name: "myfunc(b)",
                    alias: None,
                    table: None,
                    function: Some(
                        Generic(
                            "myfunc",
                            FunctionArguments {
                                arguments: [
                                    Column(
                                        Column {
                                            name: "b",
                                            alias: None,
                                            table: None,
                                            function: None,
                                        },
                                    ),
                                ],
                            },
                        ),
                    ),
                },
            ),
        ],
        join: [],
        where_clause: Some(
            LogicalOp(
                ConditionTree {
                    operator: And,
                    left: ComparisonOp(
                        ConditionTree {
                            operator: Greater,
                            left: Base(
                                Field(
                                    Column {
                                        name: "a",
                                        alias: None,
                                        table: None,
                                        function: None,
                                    },
                                ),
                            ),
                            right: Base(
                                Field(
                                    Column {
                                        name: "b",
                                        alias: None,
                                        table: None,
                                        function: None,
                                    },
                                ),
                            ),
                        },
                    ),
                    right: ComparisonOp(
                        ConditionTree {
                            operator: Less,
                            left: Base(
                                Field(
                                    Column {
                                        name: "b",
                                        alias: None,
                                        table: None,
                                        function: None,
                                    },
                                ),
                            ),
                            right: Base(
                                Literal(
                                    Integer(
                                        100,
                                    ),
                                ),
                            ),
                        },
                    ),
                },
            ),
        ),
        group_by: None,
        order: Some(
            OrderClause {
                columns: [
                    (
                        Column {
                            name: "a",
                            alias: None,
                            table: None,
                            function: None,
                        },
                        Desc,
                    ),
                    (
                        Column {
                            name: "b",
                            alias: None,
                            table: None,
                            function: None,
                        },
                        Asc,
                    ),
                ],
            },
        ),
        limit: None,
    },
)

Creating SQL text from AST

use sqlparser_mysql::parser::Parser;
use sqlparser_mysql::parser::ParseConfig;

let sql = "SELECT a FROM table_1";
let config = ParseConfig::default();

// parse to a Statement
let ast = Parser::parse(&config, sql).unwrap();

// The original SQL text can be generated from the AST
assert_eq!(ast.to_string(), sql);

Supported Statements

Data Definition Statements

MySQL Doc

  • Atomic Data Definition Statement Support
  • ALTER DATABASE Statement
  • ALTER EVENT Statement
  • ALTER FUNCTION Statement
  • ALTER INSTANCE Statement
  • ALTER LOGFILE GROUP Statement
  • ALTER PROCEDURE Statement
  • ALTER SERVER Statement
  • ALTER TABLE Statement
  • ALTER TABLESPACE Statement
  • ALTER VIEW Statement
  • CREATE DATABASE Statement
  • CREATE EVENT Statement
  • CREATE FUNCTION Statement
  • CREATE INDEX Statement
  • CREATE LOGFILE GROUP Statement
  • CREATE PROCEDURE and CREATE FUNCTION Statements
  • CREATE SERVER Statement
  • CREATE SPATIAL REFERENCE SYSTEM Statement
  • CREATE TABLE Statement
  • CREATE TABLESPACE Statement
  • CREATE TRIGGER Statement
  • CREATE VIEW Statement
  • DROP DATABASE Statement
  • DROP EVENT Statement
  • DROP FUNCTION Statement --> "DROP PROCEDURE and DROP FUNCTION Statements" && "DROP FUNCTION Statement for Loadable Functions"
  • DROP INDEX Statement
  • DROP LOGFILE GROUP Statement
  • DROP PROCEDURE and DROP FUNCTION Statements
  • DROP SERVER Statement
  • DROP SPATIAL REFERENCE SYSTEM Statement
  • DROP TABLE Statement
  • DROP TABLESPACE Statement
  • DROP TRIGGER Statement
  • DROP VIEW Statement
  • RENAME TABLE Statement
  • TRUNCATE TABLE Statement

Database Administration Statements

  • SET Statements

Dependencies

~1.4–2.2MB
~45K SLoC