modeling relvars with no foreign key
authorEric Prud'hommeaux <bertails@w3.org>
Sun, 20 Dec 2009 09:05:28 -0500
changeset 69 ed5415596dfe
parent 68 a20cd55bacf1
child 70 7f903d5a115c
child 72 ef8cd60aa260
modeling relvars with no foreign key
src/main/scala/RDB2RDFMain.scala
src/main/scala/SQL.scala
src/test/scala/RDB2RDFTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Thu Dec 17 21:16:27 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Sun Dec 20 09:05:28 2009 -0500
@@ -19,6 +19,7 @@
   case class StringMapper(relaliasattr:RelAliasAttribute) extends SQL2RDFValueMapper(relaliasattr)
   case class IntMapper(relaliasattr:RelAliasAttribute) extends SQL2RDFValueMapper(relaliasattr)
   case class RDFNoder(relation:Relation, relaliasattr:RelAliasAttribute) extends SQL2RDFValueMapper(relaliasattr)
+  case class RDFBNoder(relation:Relation, relaliasattr:RelAliasAttribute) extends SQL2RDFValueMapper(relaliasattr)
 
   def relAliasFromS(s:S):RelAlias = {
     s match {
@@ -51,7 +52,10 @@
 
   def uriConstraint(state:R2RState, constrainMe:RelAliasAttribute, u:ObjUri):R2RState = {
     // println("equiv+= " + toString(constrainMe) + "=" + value)
-    R2RState(state.joins, state.varmap, state.exprs + PrimaryExpressionEq(constrainMe,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s))))    
+    //R2RState(state.joins, state.varmap, state.exprs + PrimaryExpressionEq(constrainMe,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s))))
+    val attr = Attribute(Name(u.attr.s))
+    val relvar = RelAliasAttribute(constrainMe.relalias, attr)
+    R2RState(state.joins, state.varmap, state.exprs + PrimaryExpressionEq(relvar,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s))))
   }
 
   def literalConstraint(state:R2RState, constrainMe:RelAliasAttribute, lit:SparqlLiteral, dt:SQLDatatype):R2RState = {
@@ -86,22 +90,30 @@
     */
     val reldesc = db.relationdescs(rel)
     if (state.varmap.contains(v)) {
+      /* The variable has already been bound. */
       if (varToAttribute(state.varmap, v) == constrainMe)
-	state /* Don't bother stipulating that foo.bar=foo.bar . */
+	/* Don't bother stipulating that foo.bar=foo.bar . */
+	state
       else
+	/* Constraint against the initial binding for this variable. */
 	R2RState(state.joins, state.varmap, state.exprs + PrimaryExpressionEq(varToAttribute(state.varmap, v), RValueAttr(constrainMe)))
     } else {
+      /* This is a new variable. */
       val binding = reldesc.primarykey match {
-	case Attribute(constrainMe.attribute.n) => 
+	case Some(Attribute(constrainMe.attribute.n)) => 
 	  RDFNoder(rel, constrainMe)
 	case _ => {
-	  reldesc.attributes(constrainMe.attribute) match {
-	    case ForeignKey(fkrel, fkattr) =>
-	      RDFNoder(rel, constrainMe)
-	    case Value(SQLDatatype("String")) =>
-	      StringMapper(constrainMe)
-	    case Value(SQLDatatype("Int")) =>
-	      IntMapper(constrainMe)
+	  if (reldesc.attributes.contains(constrainMe.attribute)) {
+	    reldesc.attributes(constrainMe.attribute) match {
+	      case ForeignKey(fkrel, fkattr) =>
+		RDFNoder(rel, constrainMe)
+	      case Value(SQLDatatype("String")) =>
+		StringMapper(constrainMe)
+	      case Value(SQLDatatype("Int")) =>
+		IntMapper(constrainMe)
+	    }
+	  } else {
+	    RDFBNoder(rel, constrainMe)
 	  }
 	}
       }
@@ -117,6 +129,7 @@
       case StringMapper(relalias) => "STRING: " + toString(relalias)
       case IntMapper(relalias) => "INT: " + toString(relalias)
       case RDFNoder(relation, relalias) => "RDFNoder: " + relation.n.s + ", " + toString(relalias)
+      case RDFBNoder(relation, relalias) => "RDFBNoder: " + relation.n.s + ", " + toString(relalias)
     }
   }
 
@@ -187,6 +200,7 @@
       case StringMapper(relalias) => relalias
       case IntMapper(relalias) => relalias
       case RDFNoder(relation, relalias) => relalias
+      case RDFBNoder(relation, relalias) => relalias
     }
   }
 
@@ -222,6 +236,7 @@
       case StringMapper(relalias) => relalias
       case IntMapper(relalias) => relalias
       case RDFNoder(relation, relalias) => relalias
+      case RDFBNoder(relation, relalias) => relalias
     }
     PrimaryExpressionNotNull(aattr)
   }
--- a/src/main/scala/SQL.scala	Thu Dec 17 21:16:27 2009 -0500
+++ b/src/main/scala/SQL.scala	Sun Dec 20 09:05:28 2009 -0500
@@ -74,7 +74,7 @@
 case class ForeignKey(rel:Relation, attr:Attribute) extends ValueDescription
 
 case class DatabaseDesc(relationdescs:Map[Relation,RelationDesc])
-case class RelationDesc(primarykey:Attribute, attributes:Map[Attribute, ValueDescription])
+case class RelationDesc(primarykey:Option[Attribute], attributes:Map[Attribute, ValueDescription])
 
 case class Sql() extends JavaTokenParsers {
 
--- a/src/test/scala/RDB2RDFTest.scala	Thu Dec 17 21:16:27 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala	Sun Dec 20 09:05:28 2009 -0500
@@ -7,7 +7,7 @@
 
   val db:DatabaseDesc = DatabaseDesc(
     Map(Relation("Employee") -> 
-	RelationDesc(Attribute("id"), 
+	RelationDesc(Option(Attribute("id")), 
 		     Map(Attribute("id") -> Value(SQLDatatype.INTEGER),
 			 Attribute("lastName") -> Value(SQLDatatype.STRING),
 			 Attribute("birthday") -> Value(SQLDatatype.INTEGER), // !!!
@@ -17,16 +17,15 @@
 
   val db2:DatabaseDesc = DatabaseDesc(
     Map(Relation("Employee") -> 
-	RelationDesc(Attribute("id"), 
+	RelationDesc(Option(Attribute("id")), 
 		     Map(Attribute("id") -> Value(SQLDatatype.INTEGER),
 			 Attribute("lastName") -> Value(SQLDatatype.STRING),
 			 Attribute("birthday") -> Value(SQLDatatype.INTEGER), // !!!
 			 Attribute("manager") -> Value(SQLDatatype.INTEGER),
 			 Attribute("address") -> Value(SQLDatatype.INTEGER))),
 	Relation("Manage") -> 
-	RelationDesc(Attribute("id"), // !!doesnotexist!!
-		     Map(Attribute("id") -> Value(SQLDatatype.INTEGER), // !!doesnotexist!!
-			 Attribute("manager") -> ForeignKey(Relation("Employee"), Attribute("id")), 
+	RelationDesc(None,
+		     Map(Attribute("manager") -> ForeignKey(Relation("Employee"), Attribute("id")), 
 			 Attribute("manages") -> ForeignKey(Relation("Employee"),  Attribute("id"))))
       ))