[tracker/sparql_coalesce] Implemented tracker:coalesce SPARQL convenience function



commit 8001707af89d76f5deb2fee8f005fd8dc4d1a3a8
Author: Philip Van Hoof <philip codeminded be>
Date:   Wed Dec 16 11:55:16 2009 +0100

    Implemented tracker:coalesce SPARQL convenience function
    
    Example usage:
    
    SELECT tracker:coalesce (?a, ?b) {
       ?s a nie:FileDataObject ;
          nfo:fileName ?b .
       ?x a nie:InformationElement ;
          nie:isStoredAs ?s ;
          nie:title ?a
    }

 src/libtracker-data/tracker-sparql-query.vala |   19 +++++++++++++++
 src/libtracker-db/tracker-db-manager.c        |   30 +++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index f9fa768..37f7dfd 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -241,6 +241,7 @@ public class Tracker.SparqlQuery : Object {
 
 	const string FN_NS = "http://www.w3.org/2005/xpath-functions#";;
 	const string FTS_NS = "http://www.tracker-project.org/ontologies/fts#";;
+	const string TRACKER_NS = "http://www.tracker-project.org/ontologies/tracker#";;
 
 	string query_string;
 	bool update_extensions;
@@ -1352,6 +1353,20 @@ public class Tracker.SparqlQuery : Object {
 		expect (SparqlTokenType.CLOSE_PARENS);
 	}
 
+
+	void translate_coalesce (StringBuilder sql) throws SparqlError {
+		bool first = true;
+		sql.append ("SparqlCoalesce(");
+		do {
+			if (!first)
+				sql.append (", ");
+			else 
+				first = false;
+			translate_expression_as_string (sql);
+		} while (accept (SparqlTokenType.COMMA));
+		sql.append (")");
+	}
+
 	PropertyType translate_function (StringBuilder sql, string uri) throws SparqlError {
 		if (uri == FN_NS + "contains") {
 			// fn:contains('A','B') => 'A' GLOB '*B*'
@@ -1398,6 +1413,10 @@ public class Tracker.SparqlQuery : Object {
 			sql.append (")");
 
 			return PropertyType.BOOLEAN;
+		} else if (uri == TRACKER_NS + "coalesce") {
+			// tracker:coalesce('A',null) => 'A' 
+			translate_coalesce (sql);
+			return PropertyType.STRING;
 		} else if (uri == FTS_NS + "rank") {
 			bool is_var;
 			string v = parse_var_or_term (null, out is_var);
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 823216a..d6c2a00 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -379,6 +379,32 @@ function_sparql_regex (TrackerDBInterface *interface,
 	return result;
 }
 
+
+static GValue
+function_sparql_coalesce (TrackerDBInterface *interface,
+                          gint                argc,
+                          GValue              values[])
+{
+	gchar   *result_str = NULL;
+	GValue   result = { 0, };
+	gboolean ret;
+	gint     i;
+
+	for (i = 0; i < argc; i++) {
+		gchar *value;
+		value = g_value_get_string (&values[i]);
+		if (!result_str && !tracker_is_blank_string (value)) {
+			result_str = g_strstrip (value);
+			break;
+		}
+	}
+
+	g_value_init (&result, G_TYPE_STRING);
+	g_value_set_string (&result, result_str);
+
+	return result;
+}
+
 static gchar *
 function_uncompress_string (const gchar *ptr,
                             gint         size,
@@ -679,6 +705,10 @@ db_set_params (TrackerDBInterface *iface,
 		                                             "SparqlRegex",
 		                                             function_sparql_regex,
 		                                             3);
+		tracker_db_interface_sqlite_create_function (iface,
+		                                             "SparqlCoalesce",
+		                                             function_sparql_coalesce,
+		                                             -1);
 
 		tracker_db_interface_sqlite_create_function (iface,
 		                                             "uncompress",



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