Created
April 2, 2011 07:37
-
-
Save songpp/899312 to your computer and use it in GitHub Desktop.
sql statement combinator parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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