Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Latest commit

History

56 Commits

Folders and files

NameName
Last commit message
Last commit date

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 ont2.a=t3.aleft join t4 ont3.b=t4.bwhere a > ((1+2) *3) and b < c group by a, c havingcount(*) >5order by a, b desclimit1, 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

About

A SQL query parser written using nom.

Topics

Resources

Stars

77 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages