[tracker/parser] SPARQL: Support arbitrary expressions in STR



commit 0914a2bedcd7c3c63325132ab4f1e71c5d0e5e6f
Author: Jürg Billeter <j bitron ch>
Date:   Wed Aug 12 15:27:18 2009 +0200

    SPARQL: Support arbitrary expressions in STR

 src/libtracker-data/tracker-sparql-query.vala |   56 ++++++++++++++++---------
 1 files changed, 36 insertions(+), 20 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index 3f40bb7..32a1f4b 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -978,32 +978,48 @@ public class Tracker.SparqlQuery : Object {
 	}
 
 	void translate_str (StringBuilder sql) throws SparqlError {
-
 		expect (SparqlTokenType.STR);
 		expect (SparqlTokenType.OPEN_PARENS);
 
-		if (accept (SparqlTokenType.VAR)) {
-			string variable_name = get_last_string().substring(1);
-			var binding = var_map.lookup (variable_name);
-
-			check_binding (binding, variable_name);
-
-			if (binding.is_uri) {
-				sql.append_printf ("(SELECT \"rdfs:Resource\".\"Uri\" as \"STR\" FROM \"rdfs:Resource\" WHERE \"rdfs:Resource\".\"ID\" = \"%s_u\")", 
-				                   variable_name);
-			} else {
-				sql.append (get_sql_for_variable (get_last_string ().substring (1)));
-			}
-		} else if (accept (SparqlTokenType.IRI_REF)) {
-			sql.append ("?");
-			var binding = new LiteralBinding ();
-			binding.literal = get_last_string (1);
-			bindings.append (binding);
-		} else {
+		switch (current ()) {
+		case SparqlTokenType.IRI_REF:
+		case SparqlTokenType.PN_PREFIX:
+		case SparqlTokenType.COLON:
+			// handle IRI literals separately as it wouldn't work for unknown IRIs otherwise
 			sql.append ("?");
 			var binding = new LiteralBinding ();
-			binding.literal = parse_string_literal ();
+			bool is_var;
+			binding.literal = parse_var_or_term (null, out is_var);
 			bindings.append (binding);
+			break;
+		default:
+			long begin = sql.len;
+			switch (translate_expression (sql)) {
+			case DataType.STRING:
+				// nothing to convert
+				break;
+			case DataType.RESOURCE:
+				// ID => Uri
+				sql.insert (begin, "(SELECT Uri FROM \"rdfs:Resource\" WHERE ID = ");
+				sql.append (")");
+				break;
+			case DataType.BOOLEAN:
+				// 0/1 => false/true
+				sql.insert (begin, "CASE ");
+				sql.append (" WHEN 1 THEN 'true' WHEN 0 THEN 'false' ELSE NULL END");
+				break;
+			case DataType.DATETIME:
+				// ISO 8601 format
+				sql.insert (begin, "strftime (\"%%Y-%%m-%%dT%%H:%%M:%%SZ\", ");
+				sql.append (", \"unixepoch\")");
+				break;
+			default:
+				// let sqlite convert the expression to string
+				sql.insert (begin, "CAST (");
+				sql.append ("AS TEXT)");
+				break;
+			}
+			break;
 		}
 
 		expect (SparqlTokenType.CLOSE_PARENS);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]