--- a/src/main/scala/RDB2RDFMain.scala Sat Dec 05 22:27:42 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala Sun Dec 06 02:09:32 2009 -0500
@@ -13,7 +13,7 @@
val uri = """[a-zA-Z0-9:]+""".r
val varr = """\?[a-zA-Z0-9]+""".r
-
+ val name = """[a-zA-Z_][a-zA-Z0-9_]*""".r
}
import MyParsers._
@@ -55,3 +55,46 @@
}
+case class Select(attributelist:AttributeList, tablelist:TableList, expression:Expression)
+
+case class AttributeList(attributes:List[NamedAttribute])
+case class NamedAttribute(fqattribute:FQAttribute, attribute:Attribute)
+case class FQAttribute(relation:Relation, attribute:Attribute)
+case class Attribute(n:Name)
+case class Relation(n:Name)
+case class TableList(tablealias:TableAlias, joins:List[Join])
+case class Join(tablealias:TableAlias, expression:Expression)
+case class TableAlias(rel:Relation, as:Relation)
+case class Expression(conjuncts:List[PrimaryExpression])
+sealed abstract class PrimaryExpression
+case class PrimaryExpressionEq(l:FQAttribute, r:RValue)
+case class PrimaryExpressionLt(l:FQAttribute, r:RValue)
+case class PrimaryExpressionNotNull(l:FQAttribute)
+sealed abstract class RValue
+case class RValueAttr(fqattribute:FQAttribute)
+case class RValueInt(i:Name)
+case class RValueString(i:Name)
+
+case class Name(s:String)
+
+
+case class Sql() extends JavaTokenParsers {
+
+ def select = "SELECT" ~ attributelist ~ "FROM" ~ tablelist ~ opt("WHERE" ~ expression)
+ def attributelist = repsep(namedattribute, ",")
+ def namedattribute = fqattribute ~ "AS" ~ attribute
+ def fqattribute = relation ~ "." ~ attribute
+ def attribute = """[a-zA-Z_]\w*""".r
+ def relation = """[a-zA-Z_]\w*""".r
+ def tablelist = tablealias ~ rep("INNER" ~ "JOIN" ~ tablealias ~ "ON" ~ expression)
+ def tablealias = relation ~ "AS" ~ relation
+ def expression = repsep(primaryexpression, "AND")
+ def primaryexpression = ( fqattribute ~ "=" ~ rvalue
+ | fqattribute ~ "<" ~ rvalue
+ | fqattribute ~ "IS" ~ "NOT" ~ "NULL" )
+ def rvalue = ( fqattribute
+ | """[0-9]+""".r
+ | "\"[^\"]*\"".r )
+
+}
+
--- a/src/test/scala/RDB2RDFTest.scala Sat Dec 05 22:27:42 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala Sun Dec 06 02:09:32 2009 -0500
@@ -20,5 +20,54 @@
println(a.parseAll(a.triplepatterns, e))
}
+ test("SQLbgp") {
+ val a = Sql()
+ val e = """
+SELECT emp.lastName AS empName, manager.lastName AS managName
+ FROM Employee AS emp
+ INNER JOIN Employee AS manager ON manager.id=emp.manager
+ WHERE emp.lastName IS NOT NULL AND manager.lastName IS NOT NULL
+"""
+ println(a.parseAll(a.select, e))
+ }
+
+ test("tup1") {
+ val a = Sql()
+ val e = """
+SELECT emp.lastName AS empName
+ FROM Employee AS emp
+ WHERE emp.manager=18 AND emp.lastName IS NOT NULL
+"""
+ println(a.parseAll(a.select, e))
+ }
+
+ test("litConst1") {
+ val a = Sql()
+ val e = """
+SELECT emp.lastName AS empName
+ FROM Employee AS emp
+ INNER JOIN Employee AS manager ON emp.manager=manager.id
+ AND manager.lastName="Johnson"
+WHERE emp.lastName IS NOT NULL
+"""
+ println(a.parseAll(a.select, e))
+ }
+
+ test("filter1") {
+ val a = Sql()
+ val e = """
+SELECT emp.lastName AS empName, grandManager.lastName AS grandManagName
+ FROM Employee AS emp
+ INNER JOIN Manage AS lower ON lower.manages=emp.id
+ INNER JOIN Employee AS manager ON manager.id=lower.manager
+ AND manager.birthday < emp.birthday
+ INNER JOIN Manage AS upper ON upper.manages=manager.id
+ INNER JOIN Employee AS grandManager ON grandManager.id=upper.manager
+ AND grandManager.birthday < manager.birthday
+ WHERE emp.lastName IS NOT NULL AND grandManager.lastName IS NOT NULL
+"""
+ println(a.parseAll(a.select, e))
+ }
+
}