--- a/src/main/scala/RDB2RDFMain.scala Tue Dec 15 01:02:16 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala Tue Dec 15 14:03:18 2009 -0500
@@ -29,7 +29,11 @@
def relAliasFromNode(u:ObjUri):RelAlias = {
val ObjUri(stem, rel, Attr(a), CellValue(v)) = u
- RelAlias(Name(a + v))
+ RelAlias(Name("R_" + a + v))
+ }
+
+ def relAliasFromLiteral(l:SparqlLiteral):RelAlias = {
+ RelAlias(Name("R_" + l.lit.lexicalForm))
}
def relAliasFromVar(vr:Var):RelAlias = {
@@ -39,10 +43,16 @@
def uriConstraint(u:ObjUri, pk:PrimaryKey):Expression = {
val relalias = relAliasFromNode(u)
- val ObjUri(stem, rel, attr, value) = u
val relaliasattr = RelAliasAttribute(relalias, pk.attr)
// println("equiv+= " + toString(relaliasattr) + "=" + value)
- Expression(List(PrimaryExpressionEq(relaliasattr,RValueTyped(SQLDatatype.INTEGER,Name(value.s)))))
+ Expression(List(PrimaryExpressionEq(relaliasattr,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s)))))
+ }
+
+ def literalConstraint(lit:SparqlLiteral, pk:PrimaryKey):Expression = {
+ val relalias = relAliasFromLiteral(lit)
+ val relaliasattr = RelAliasAttribute(relalias, pk.attr)
+ // println("equiv+= " + toString(attr) + "=" + lit)
+ Expression(List(PrimaryExpressionEq(relaliasattr,RValueTyped(SQLDatatype.INTEGER,Name(lit.lit.lexicalForm)))))
}
/** varConstraint
@@ -88,11 +98,6 @@
}
}
- def literalConstraint(lit:SparqlLiteral, attr:RelAliasAttribute):Expression = {
- // println("equiv+= " + toString(attr) + "=" + lit)
- Expression(List(PrimaryExpressionEq(attr,RValueTyped(SQLDatatype.INTEGER,Name(lit.lit.lexicalForm)))))
- }
-
def toString(relaliasattr:RelAliasAttribute) : String = {
relaliasattr.relalias.n.s + "." + relaliasattr.attribute.n.s
}
@@ -140,38 +145,59 @@
val target = db.relationdescs(rel).attributes(attr) match {
case ForeignKey(fkrel, fkattr) => {
o match {
- case OUri(u) => {
- val oRelAlias = relAliasFromNode(u)
+ case OLit(l) => {
// println(toString(objattr) + "->" + toString(RelAliasAttribute(oRelAlias, fkattr)))
// println(fkrel.n.s + " AS " + oRelAlias.n.s)
- val ret = Some(uriConstraint(u, pk))
+
+ val oRelAlias = relAliasFromLiteral(l)
+ val fkaliasattr = RelAliasAttribute(oRelAlias, fkattr)
+ val joinconstraint = PrimaryExpressionEq(fkaliasattr,RValueAttr(objattr))
+
+ val conjuncts = List(joinconstraint) ::: literalConstraint(l, pk).conjuncts
joined contains(oRelAlias) match {
case false => {
- joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), ret))
+ joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Some(Expression(conjuncts))))
+ joined = joined + oRelAlias
+ }
+ case true => null
+ }
+ }
+ case OUri(u) => {
+ // println(toString(objattr) + "->" + toString(RelAliasAttribute(oRelAlias, fkattr)))
+ // println(fkrel.n.s + " AS " + oRelAlias.n.s)
+
+ val oRelAlias = relAliasFromNode(u)
+ val fkaliasattr = RelAliasAttribute(oRelAlias, fkattr)
+ val joinconstraint = PrimaryExpressionEq(fkaliasattr,RValueAttr(objattr))
+
+ val conjuncts = List(joinconstraint) ::: uriConstraint(u, pk).conjuncts
+ joined contains(oRelAlias) match {
+ case false => {
+ joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Some(Expression(conjuncts))))
joined = joined + oRelAlias
}
case true => null
}
}
case OVar(v) => {
+ // println(toString(objattr) + "->" + toString(fkaliasattr))
+ // println(toString(binding))
+ // println(fkrel.n.s + " AS " + oRelAlias.n.s)
+
val oRelAlias = relAliasFromVar(v)
val fkaliasattr = RelAliasAttribute(oRelAlias, fkattr)
- // println(toString(objattr) + "->" + toString(fkaliasattr))
+ val joinconstraint = PrimaryExpressionEq(fkaliasattr,RValueAttr(objattr))
+
val binding = varConstraint(v, db, fkrel, oRelAlias, fkattr)
- varmap += v -> binding
- // println(toString(binding))
- // println(fkrel.n.s + " AS " + oRelAlias.n.s)
joined contains(oRelAlias) match {
case false => {
- joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias),
- Some(Expression(List(PrimaryExpressionEq(fkaliasattr,RValueAttr(objattr)))))))
+ joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Some(Expression(List(joinconstraint)))))
joined = joined + oRelAlias
}
case true => null
}
- }
- case OLit(l) => {
- literalConstraint(l, objattr) // !!! do something with constraint
+
+ varmap += v -> binding
}
}
}
@@ -185,7 +211,7 @@
varmap += v -> binding
// println(toString(binding))
}
- case OLit(l) => literalConstraint(l, objattr) // !!! do something with constraint
+ case OLit(l) => literalConstraint(l, pk)
}
}
}
--- a/src/test/scala/RDB2RDFTest.scala Tue Dec 15 01:02:16 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala Tue Dec 15 14:03:18 2009 -0500
@@ -15,7 +15,7 @@
Attribute("address") -> ForeignKey(Relation("Address"), Attribute("id"))))))
- test("transform max joins") {
+ test("?s <p> <x>") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
SELECT ?emp {
@@ -26,8 +26,25 @@
val sqlSelect = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.id AS A_emp
FROM Employee AS R_emp
- INNER JOIN Employee AS id18 ON id18.id=18
-""").get // !!! id18.id=R_emp.manager
+ INNER JOIN Employee AS R_id18 ON R_id18.id=R_emp.manager AND R_id18.id=18
+""").get
+ assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
+ true
+ }
+
+ test("?s <p> 18") {
+ val sparqlParser = Sparql()
+ val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+SELECT ?emp {
+?emp <http://hr.example/DB/Employee#manager> "18"^^<http://www.w3.org/2001/XMLSchema#integer>
+}
+""").get
+ val sqlParser = Sql()
+ val sqlSelect = sqlParser.parseAll(sqlParser.select, """
+SELECT R_emp.id AS A_emp
+ FROM Employee AS R_emp
+ INNER JOIN Employee AS R_18 ON R_18.id=R_emp.manager AND R_18.id=18
+""").get
assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
true
}