stephentu / scala-sql-parser Goto Github PK
View Code? Open in Web Editor NEWSQL parser written using Scala's parser combinator library
License: MIT License
SQL parser written using Scala's parser combinator library
License: MIT License
Hi there, would like to contribute to your project. Many databases support bitwise operations so I think this would be useful.
for example, the where clause parser:
def cmp_expr: Parser[SqlExpr] =
add_expr ~ rep(
("=" | "<>" | "!=" | "<" | "<=" | ">" | ">=") ~ add_expr ^^ {
case op ~ rhs => (op, rhs)
} |
"between" ~ add_expr ~ "and" ~ add_expr ^^ {
case op ~ a ~ _ ~ b => (op, a, b)
} |
opt("not") ~ "in" ~ "(" ~ (select | rep1sep(expr, ",")) ~ ")" ^^ {
case n ~ op ~ _ ~ a ~ _ => (op, a, n.isDefined)
} |
opt("not") ~ "like" ~ add_expr ^^ { case n ~ op ~ a => (op, a, n.isDefined) }
) ^^ {
case lhs ~ elems =>
elems.foldLeft(lhs) {
case (acc, (("=", rhs: SqlExpr))) => Eq(acc, rhs)
case (acc, (("<>", rhs: SqlExpr))) => Neq(acc, rhs)
case (acc, (("!=", rhs: SqlExpr))) => Neq(acc, rhs)
case (acc, (("<", rhs: SqlExpr))) => Lt(acc, rhs)
case (acc, (("<=", rhs: SqlExpr))) => Le(acc, rhs)
case (acc, ((">", rhs: SqlExpr))) => Gt(acc, rhs)
case (acc, ((">=", rhs: SqlExpr))) => Ge(acc, rhs)
case (acc, (("between", l: SqlExpr, r: SqlExpr))) => And(Ge(acc, l), Le(acc, r))
case (acc, (("in", e: Seq[_], n: Boolean))) => In(acc, e.asInstanceOf[Seq[SqlExpr]], n)
case (acc, (("in", s: SelectStmt, n: Boolean))) => In(acc, Seq(Subselect(s)), n)
case (acc, (("like", e: SqlExpr, n: Boolean))) => Like(acc, e, n)
}
} |
"not" ~> cmp_expr ^^ (Not(_)) |
"exists" ~> "(" ~> select <~ ")" ^^ { case s => Exists(Subselect(s)) }
could parser a simple sql "select * from user where user=20=20" which actually violates the sql grammar,here is test case:
"try sql" in {
val str = """ select * from user where user.age=20=20"""
val parser = new SQLParser
val r = parser.parse(str)
r should beSome
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.