made joins have optional constraint expressions
authorEric Prud'hommeaux <bertails@w3.org>
Fri, 11 Dec 2009 13:41:19 -0500
changeset 16 658eef0e7116
parent 15 878e0bbdc2ec
child 17 f08e5d0a1540
made joins have optional constraint expressions
src/main/scala/RDB2RDFMain.scala
src/main/scala/SQL.scala
src/test/scala/SQLTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Thu Dec 10 22:35:15 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Fri Dec 11 13:41:19 2009 -0500
@@ -73,10 +73,10 @@
       // List[Join](), 
       List(
 	Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),
-	     Expression(List(
+	     Some(Expression(List(
 	       PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),
 				   RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))
-		      ))
+		      )))
       ), 
       // Expression(List()), 
       Expression(List(
--- a/src/main/scala/SQL.scala	Thu Dec 10 22:35:15 2009 -0500
+++ b/src/main/scala/SQL.scala	Fri Dec 11 13:41:19 2009 -0500
@@ -12,7 +12,7 @@
 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 Join(tablealias:TableAlias, expression:Option[Expression])
 case class TableAlias(rel:Relation, as:Relation)
 case class Expression(conjuncts:List[PrimaryExpression])
 sealed abstract class PrimaryExpression
@@ -54,12 +54,15 @@
     """[a-zA-Z_]\w*""".r ^^ { x => Relation(Name(x)) }
 
   def tablelist:Parser[TableList] =
-    tablealias ~ rep("INNER" ~ "JOIN" ~ tablealias ~ "ON" ~ expression) ^^
+    tablealias ~ rep("INNER" ~ "JOIN" ~ tablealias ~ opt(optexpr)) ^^
     { case tablealias ~ repjoins =>
-        val joins:List[Join] = repjoins map { case "INNER"~"JOIN"~tablealias~"ON"~expression => Join(tablealias, expression) }
+        val joins:List[Join] = repjoins map { case "INNER"~"JOIN"~tablealias~optexpr => Join(tablealias, optexpr) }
         TableList(tablealias, joins)
     }
 
+  def optexpr:Parser[Expression] =
+    "ON" ~ expression ^^ { case "ON"~expression => expression }
+
   def tablealias:Parser[TableAlias] =
     relation ~ "AS" ~ relation ^^
     { case rel1 ~ "AS" ~ rel2 => TableAlias(rel1, rel2) }
--- a/src/test/scala/SQLTest.scala	Thu Dec 10 22:35:15 2009 -0500
+++ b/src/test/scala/SQLTest.scala	Fri Dec 11 13:41:19 2009 -0500
@@ -13,7 +13,7 @@
             INNER JOIN Employee AS manager ON manager.id=emp.manager
  WHERE emp.lastName IS NOT NULL AND manager.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager")))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -37,7 +37,7 @@
                                        AND manager.lastName="Johnson"
 WHERE emp.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))), PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),RValueString(Name("Johnson")))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))), PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),RValueString(Name("Johnson"))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -54,7 +54,7 @@
                                          AND grandManager.birthday < manager.birthday
  WHERE emp.lastName IS NOT NULL AND grandManager.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))),Attribute(Name("grandManagName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Manage")),Relation(Name("lower"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("lower")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("id")))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("birthday")))))))), Join(TableAlias(Relation(Name("Manage")),Relation(Name("upper"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("upper")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id")))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("grandManager"))),Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("grandManager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("upper")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("grandManager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday")))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))),Attribute(Name("grandManagName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Manage")),Relation(Name("lower"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("lower")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("birthday"))))))))), Join(TableAlias(Relation(Name("Manage")),Relation(Name("upper"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("upper")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("grandManager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("grandManager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("upper")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("grandManager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }