--- a/src/main/scala/SparqlToSparqlToSql.scala Sun Feb 28 15:05:33 2010 -0500
+++ b/src/main/scala/SparqlToSparqlToSql.scala Sun Feb 28 15:07:28 2010 -0500
@@ -18,29 +18,38 @@
/* ... turn each triple pattern in the rule heads into a SQL pattern. */
rule.head.triplepatterns.foldLeft(allgps)((gps, triple) => {
+ def substituteVar (term:sparql.Term, label:String):(sparql.Term, List[sparql.Var], List[sql.NamedAttribute]) = {
+ term match {
+ case sparql.TermUri(u) => (term, List[sparql.Var](), List(
+ sql.NamedAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name(u.toString)),
+ sql.AttrAlias(sql.Name(label)))))
+ case sparql.TermVar(v) => (sparql.TermVar(sparql.Var(label)), List(sparql.Var(label)), List[sql.NamedAttribute]())
+ case sparql.TermLit(l) => (term, List[sparql.Var](), List(
+ sql.NamedAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name(l.toString)),
+ sql.AttrAlias(sql.Name(label)))))
+ }
+ }
+
+ /* Add constant for predicate into selects. */
/* Substituted SPARQL ?S and ?O into the triple pattern. */
- val s = sparql.Var("S");
- val o = sparql.Var("O")
+ val (s, sselects, sconsts) = substituteVar(triple.s, "S");
+ val (p, pselects, pconsts) = substituteVar(triple.p, "P");
+ val (o, oselects, oconsts) = substituteVar(triple.o, "O");
val select = sparql.Select(
- sparql.SparqlAttributeList(List(s, o)),
- sparql.TriplesBlock(
- List(sparql.TriplePattern(sparql.TermVar(s), triple.p, sparql.TermVar(o)))))
+ sparql.SparqlAttributeList(sselects ++ pselects ++ oselects),
+ sparql.TriplesBlock(List(sparql.TriplePattern(s, p, o)))
+ )
/* Convert to equivalent SQL SELECT. */
val asStem = sparql2sparql.SparqlToSparql(select, List(rule))
+ // println("triple: "+triple)
+ // println("asStem: "+asStem)
val sql.Select(sql.AttributeList(attributes), tablelist, expression) = sparql2sql.SparqlToSql(schema, asStem, stemUri, false, true)
-
- /* Add constant for predicate into selects. */
- val p = triple.p match {
- case sparql.TermUri(u) => sql.NamedAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name("<" + u.s + ">")),
- sql.AttrAlias(sql.Name("P")))
- case sparql.TermVar(v) => error("triplification algorithm doesn't handly variable predicate in " + triple)
- case sparql.TermLit(_) => error("illegal literal in predicate position of " + triple)
- }
+ // println("SQL: "+sql.Select(sql.AttributeList(attributes ++ sconsts ++ pconsts ++ oconsts), tablelist, expression))
/* Add resulting SELECT to list of graph patterns. */
- gps :+ sql.Select(sql.AttributeList(attributes + p), tablelist, expression)
+ gps :+ sql.Select(sql.AttributeList(attributes ++ sconsts ++ pconsts ++ oconsts), tablelist, expression)
})
})