Skip to content

Instantly share code, notes, and snippets.

@songpp
Created April 2, 2011 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songpp/899312 to your computer and use it in GitHub Desktop.
Save songpp/899312 to your computer and use it in GitHub Desktop.
sql statement combinator parser
package par
import scala.util.parsing.combinator._
class SqlTokenParser extends JavaTokenParsers{
def ws : Parser[String] = """\s+""".r
def query : Parser[Any] = selectClouse ~ fromClouse
def subquerys : Parser[Any] = repsep(subquery,",")
def subquery : Parser[Any] = ws ~ "("~ ws ~ query ~ ws ~ ")"~ ws ~ opt(ident)
def column : Parser[Any] = "*" | ident | stringLiteral ~ opt(ws ~"as"~ ws ~ ident)
def columns : Parser[Any] = repsep(column,",")
def selectClouse : Parser[Any] = "select" ~ repsep( columns | subquerys ,",")
def tableNames : Parser[Any] = repsep(tableName,",")
def tableName : Parser[Any] = ident | "dual" ~ opt(ws ~ "as"~ ws ~ ident)
def fromClouse : Parser[Any] = "from" ~ repsep(tableNames | subquerys,",")
}
import java.io.{File,FileReader}
object ST extends SqlTokenParser {
def main(args: Array[String]) {
Console println new File(".").getAbsolutePath
val reader = new FileReader("./src/SQL")
println(parseAll(query, reader))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment