From 87a576563b8e8612e24efea2bf02cc7a484fec18 Mon Sep 17 00:00:00 2001 From: Andy Grove Date: Mon, 3 Sep 2018 10:31:04 -0600 Subject: [PATCH] Save --- README.md | 19 +++++++++++++++++-- examples/parse_select.rs | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 examples/parse_select.rs diff --git a/README.md b/README.md index ee3790de..d3e627b6 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,21 @@ The goal of this project is to build a SQL lexer and parser capable of parsing ANSI SQL:2011 (or 2016 if I can get access to the specification for free). -The current code was copied from DataFusion and has some non-standard SQL support and only a subset of ANSI SQL currently. +The current code is capable of parsing some simple SELECT and CREATE TABLE statements. -The current code is capable of parsing some simple SELECT and CREATE TABLE statements. \ No newline at end of file +```rust +let sql = "SELECT a, b, 123, myfunc(b) \ + FROM table_1 \ + WHERE a > b AND b < 100 \ + ORDER BY a DESC, b"; + +let ast = Parser::parse_sql(sql.to_string()).unwrap(); + +println!("AST: {:?}", ast); +``` + +This outputs + +```rust +AST: SQLSelect { projection: [SQLIdentifier("a"), SQLIdentifier("b"), SQLLiteralLong(123), SQLFunction { id: "myfunc", args: [SQLIdentifier("b")] }], relation: Some(SQLIdentifier("table_1")), selection: Some(SQLBinaryExpr { left: SQLBinaryExpr { left: SQLIdentifier("a"), op: Gt, right: SQLIdentifier("b") }, op: And, right: SQLBinaryExpr { left: SQLIdentifier("b"), op: Lt, right: SQLLiteralLong(100) } }), order_by: Some([SQLOrderBy { expr: SQLIdentifier("a"), asc: false }, SQLOrderBy { expr: SQLIdentifier("b"), asc: true }]), group_by: None, having: None, limit: None } +``` \ No newline at end of file diff --git a/examples/parse_select.rs b/examples/parse_select.rs new file mode 100644 index 00000000..fb22caac --- /dev/null +++ b/examples/parse_select.rs @@ -0,0 +1,15 @@ +extern crate sqlparser; + +use sqlparser::sqlparser::*; + +fn main() { + + let sql = "SELECT a, b, 123, myfunc(b) \ + FROM table_1 \ + WHERE a > b AND b < 100 \ + ORDER BY a DESC, b"; + + let ast = Parser::parse_sql(sql.to_string()).unwrap(); + + println!("AST: {:?}", ast); +} \ No newline at end of file