[tracker/grouped-deletes: 1/4] libtracker-data: Make SparqlUriIsDescendant() Sql function take varargs



commit 7e1836925402cf59910007fe511cdb4f7e4639e1
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 7 18:42:52 2011 +0100

    libtracker-data: Make SparqlUriIsDescendant() Sql function take varargs
    
    Now the parameters are (parent1, parent2, ..., parentN, child), so
    it stays compatible with the older behavior.

 src/libtracker-data/tracker-db-interface-sqlite.c |   74 ++++++++++++++-------
 1 files changed, 50 insertions(+), 24 deletions(-)
---
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c
index 604684d..d090bc4 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -300,29 +300,13 @@ function_sparql_uri_is_parent (sqlite3_context *context,
 	sqlite3_result_int (context, match);
 }
 
-static void
-function_sparql_uri_is_descendant (sqlite3_context *context,
-                                   int              argc,
-                                   sqlite3_value   *argv[])
+static gboolean
+check_uri_is_descendant (const gchar *parent,
+                         guint        parent_len,
+                         const gchar *uri)
 {
-	const gchar *uri, *parent, *remaining;
+	const gchar *remaining;
 	gboolean match = FALSE;
-	guint parent_len;
-
-	if (argc != 2) {
-		sqlite3_result_error (context, "Invalid argument count", -1);
-		return;
-	}
-
-	parent = sqlite3_value_text (argv[0]);
-	uri = sqlite3_value_text (argv[1]);
-
-	if (!parent || !uri) {
-		sqlite3_result_error (context, "Invalid arguments", -1);
-		return;
-	}
-
-	parent_len = sqlite3_value_bytes (argv[0]);
 
 	/* Check only one argument, it's going to
 	 * be compared with the other anyway.
@@ -330,8 +314,7 @@ function_sparql_uri_is_descendant (sqlite3_context *context,
 
 	if (!(parent_len >= 7 && (parent[4] == ':' && parent[5] == '/' && parent[6] == '/'))) {
 		if (strstr (parent, "://") == NULL) {
-			sqlite3_result_int (context, FALSE);
-			return;
+			return FALSE;
 		}
 	}
 
@@ -354,6 +337,49 @@ function_sparql_uri_is_descendant (sqlite3_context *context,
 		}
 	}
 
+	return match;
+}
+
+static void
+function_sparql_uri_is_descendant (sqlite3_context *context,
+                                   int              argc,
+                                   sqlite3_value   *argv[])
+{
+	const gchar *child;
+	gboolean match = FALSE;
+	gint i;
+
+	/* fn:uri-is-descendant (parent1, parent2, ..., parentN, child) */
+
+	if (argc < 2) {
+		sqlite3_result_error (context, "Invalid argument count", -1);
+		return;
+	}
+
+	if (sqlite3_value_type (argv[argc-1]) != SQLITE_TEXT) {
+		sqlite3_result_error (context, "Invalid child", -1);
+		return;
+	}
+
+	if (sqlite3_value_type (argv[0]) != SQLITE_TEXT) {
+		sqlite3_result_error (context, "Invalid first parent", -1);
+		return;
+	}
+
+	child = sqlite3_value_text (argv[argc-1]);
+
+	for (i = 0; i < argc - 1 && match; i++) {
+		if (sqlite3_value_type (argv[i]) == SQLITE_TEXT) {
+			const gchar *parent = sqlite3_value_text (argv[i]);
+			guint parent_len = sqlite3_value_bytes (argv[i]);
+
+			if (!parent)
+				continue;
+
+			match = check_uri_is_descendant (parent, parent_len, child);
+		}
+	}
+
 	sqlite3_result_int (context, match);
 }
 
@@ -605,7 +631,7 @@ open_database (TrackerDBInterface  *db_interface,
 	                         db_interface, &function_sparql_uri_is_parent,
 	                         NULL, NULL);
 
-	sqlite3_create_function (db_interface->db, "SparqlUriIsDescendant", 2, SQLITE_ANY,
+	sqlite3_create_function (db_interface->db, "SparqlUriIsDescendant", -1, SQLITE_ANY,
 	                         db_interface, &function_sparql_uri_is_descendant,
 	                         NULL, NULL);
 



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