+ ORDER (but neads cleanup)
authorEric Prud'hommeaux <eric@w3.org>
Sun, 13 Jun 2010 14:40:07 -0400
changeset 214 32e8f17bf5ce
parent 213 941993f6828e
child 218 7a169dfe5368
+ ORDER (but neads cleanup)
src/main/scala/SparqlToSql.scala
src/test/scala/SparqlToSparqlToSqlTest.scala
--- a/src/main/scala/SparqlToSql.scala	Sun Jun 13 09:56:13 2010 -0400
+++ b/src/main/scala/SparqlToSql.scala	Sun Jun 13 14:40:07 2010 -0400
@@ -1000,7 +1000,6 @@
    * @return an SQL query corresponding to sparquery
    */
   def apply (db:sql.DatabaseDesc, sparquery:sparql.Select, stem:StemURI, enforceForeignKeys:Boolean, concat:Boolean) : (sql.Select, Map[sparql.Assignable, SQL2RDFValueMapper]) = {
-    val sparql.Select(_, attrs, triples, _, _, _) = sparquery
 
     /** Create an object to hold our compilation state. */
     val initState = R2RState(
@@ -1021,7 +1020,7 @@
      */
     val attrlist:Set[sql.NamedAttribute] =
       // This foldLeft could be a map, if i could coerce to a set afterwards.
-      attrs.attributelist.foldLeft(Set[sql.NamedAttribute]())((attrs, v) => {
+      sparquery.attrs.attributelist.foldLeft(Set[sql.NamedAttribute]())((attrs, v) => {
 	val exp =
 	  if (concat)
 	    // generate CONCAT expression for keys.
@@ -1043,12 +1042,65 @@
 	case 1 => Some(r2rState.exprs.toList(0))
 	case _ => Some(sql.ExprConjunction(r2rState.exprs))
       },
-      List[sql.OrderElt](), sparquery.offset, sparquery.limit
+      sparquery.order.map((elt:sparql.OrderElt) => {
+	sql.OrderElt(elt.desc, xxx(r2rState.varmap, elt.expr))
+      }), sparquery.offset, sparquery.limit
     )
     // println("r2rState.varmap: " + r2rState.varmap)
     // println("select.expression: " + select.expression)
     (select.makePretty, r2rState.varmap)
   }
+
+/*
+ * vvv CLEAN THIS UP!!! vvv
+ */
+
+  def assignable2expr999(varmap:Map[sparql.Assignable, SQL2RDFValueMapper], rTerm:sparql.Term):sql.Expression = { // :sparql.Var
+    val r:sql.PrimaryExpression = rTerm match {
+      case sparql.TermUri(u) => error("not implemented: translating RDF URI to SQL: " + u) // :sparql.Uri
+      case sparql.TermVar(v) => sql.PrimaryExpressionAttr(varToAttribute(varmap, sparql.VarAssignable(v)))
+      case sparql.TermBNode(b) => sql.PrimaryExpressionAttr(varToAttribute(varmap, sparql.BNodeAssignable(b)))
+      case sparql.TermLit(sparql.Literal(rdf.RDFLiteral(lit,rdf.Datatype(dt)))) =>
+	sql.PrimaryExpressionTyped({
+	  dt.toString match {
+	    case "http://www.w3.org/2001/XMLSchema#string" => sql.Datatype.STRING
+	    case "http://www.w3.org/2001/XMLSchema#integer" => sql.Datatype.INTEGER
+	    case "http://www.w3.org/2001/XMLSchema#date" => sql.Datatype.DATE
+	    case _ => error("unable to translate to RDF literal SQL: \"" + lit + "\"^^<" + dt + ">")
+	  }
+	}, lit)
+    }
+    r
+  }
+
+  def xxx(varmap:Map[sparql.Assignable, SQL2RDFValueMapper], from:sparql.Expression) : sql.Expression = {
+    val l = from.conjuncts.map((conj) => {
+      conj match {
+	case sparql.SparqlTermExpression(sparql.TermVar(v:sparql.Var)) =>
+	  assignable2expr999(varmap, sparql.TermVar(v))
+	case sparql.SparqlTermExpression(sparql.TermBNode(b:sparql.BNode)) =>
+	  assignable2expr999(varmap, sparql.TermBNode(b))
+	case sparql.SparqlTermExpression(sparql.TermLit(l)) =>
+	  assignable2expr999(varmap, sparql.TermLit(l))
+	case sparql.SparqlTermExpression(sparql.TermUri(u:sparql.Uri)) =>
+	  assignable2expr999(varmap, sparql.TermUri(u))
+	case e:sparql.PrimaryExpression => yyy(varmap, e)
+      }})
+    if (l.size == 1)
+      l(0)
+    else
+      sql.ExprConjunction(l.toSet)
+  }
+
+  def yyy(varmap:Map[sparql.Assignable, SQL2RDFValueMapper], from:sparql.PrimaryExpression) : sql.Expression = {
+    from match {
+      case sparql.SparqlTermExpression(term) => assignable2expr999(varmap, term)
+      case sparql.PrimaryExpressionEq(l, r) => sql.RelationalExpressionEq(yyy(varmap, l), yyy(varmap, r))
+      case sparql.PrimaryExpressionGt(l, r) => sql.RelationalExpressionEq(yyy(varmap, l), yyy(varmap, r))
+      case sparql.PrimaryExpressionLt(l, r) => sql.RelationalExpressionEq(yyy(varmap, l), yyy(varmap, r))
+    }
+  }
+
 }
 
 /**
--- a/src/test/scala/SparqlToSparqlToSqlTest.scala	Sun Jun 13 09:56:13 2010 -0400
+++ b/src/test/scala/SparqlToSparqlToSqlTest.scala	Sun Jun 13 14:40:07 2010 -0400
@@ -583,6 +583,7 @@
                   AND R_productFeature7.productFeature=7
  WHERE R_product.propertyNum1 > 578
    AND R_product.label IS NOT NULL
+ORDER BY R_product.label
 LIMIT 10
 """).get
     val asStem = SparqlToSparql(bsbmQuery, List(db2bsbm))