[tracker/libtracker-sparql-porting: 7/45] tracker-sparql: Use new libtracker-sparql API



commit d1c6d48fb72b3b0c04be5f66c8bf9b55fbd4e0d6
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jul 19 17:07:47 2010 +0100

    tracker-sparql: Use new libtracker-sparql API

 src/tracker-utils/tracker-sparql.c |  301 +++++++++++++++---------------------
 1 files changed, 125 insertions(+), 176 deletions(-)
---
diff --git a/src/tracker-utils/tracker-sparql.c b/src/tracker-utils/tracker-sparql.c
index ad6e703..5de9358 100644
--- a/src/tracker-utils/tracker-sparql.c
+++ b/src/tracker-utils/tracker-sparql.c
@@ -1,5 +1,4 @@
 /*
- * Copyright (C) 2006, Jamie McCracken <jamiemcc gnome org>
  * Copyright (C) 2009, Nokia <ivan frade nokia com>
  *
  * This library is free software; you can redistribute it and/or
@@ -28,8 +27,7 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
-#include <libtracker-client/tracker-client.h>
-#include <libtracker-common/tracker-common.h>
+#include <libtracker-sparql/tracker-sparql.h>
 
 #define ABOUT	  \
 	"Tracker " PACKAGE_VERSION "\n"
@@ -97,14 +95,13 @@ static GOptionEntry   entries[] = {
 };
 
 static gchar *
-get_class_from_prefix (TrackerClient *client,
-                       const gchar   *prefix)
+get_class_from_prefix (TrackerSparqlConnection *connection,
+                       const gchar             *prefix)
 {
 	GError *error = NULL;
-	GPtrArray *results;
+	TrackerSparqlCursor *cursor;
 	const gchar *query;
-	gchar *found;
-	gint i;
+	gchar *found = NULL;
 
 	query = "SELECT ?prefix ?ns "
 		"WHERE {"
@@ -113,7 +110,7 @@ get_class_from_prefix (TrackerClient *client,
 		"}";
 
 	/* We have namespace prefix, get full name */
-	results = tracker_resources_sparql_query (client, query, &error);
+	cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
 	if (error) {
 		g_printerr ("%s, %s\n",
@@ -124,53 +121,29 @@ get_class_from_prefix (TrackerClient *client,
 		return NULL;
 	}
 
-	if (!results) {
+	if (!cursor) {
 		g_printerr ("%s\n",
 		            _("No namespace prefixes were found"));
 
 		return NULL;
 	}
 
-	for (i = 0, found = NULL; i < results->len && !found; i++) {
-		gchar **data;
-		gchar *class_prefix, *class_name;
+        while (tracker_sparql_cursor_next (cursor, NULL, NULL) && !found) {
+		const gchar *class_prefix, *class_name;
 
-		data = g_ptr_array_index (results, i);
-		class_prefix = data[0];
-		class_name = data[1];
+                class_prefix = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+                class_name = tracker_sparql_cursor_get_string (cursor, 1, NULL);
 
 		if (strcmp (class_prefix, prefix) == 0) {
 			found = g_strdup (class_name);
 		}
 	}
 
-	g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-	g_ptr_array_free (results, TRUE);
+        g_object_unref (cursor);
 
 	return found;
 }
 
-static void
-results_foreach (gpointer value,
-                 gpointer user_data)
-{
-	gchar **data;
-	gchar **p;
-	gint i;
-
-	data = value;
-
-	for (p = data, i = 0; *p; p++, i++) {
-		if (i == 0) {
-			g_print ("  %s", *p);
-		} else {
-			g_print (", %s", *p);
-		}
-	}
-
-	g_print ("\n");
-}
-
 static gboolean
 parse_list_notifies (const gchar  *option_name,
                      const gchar  *value,
@@ -186,13 +159,66 @@ parse_list_notifies (const gchar  *option_name,
         return TRUE;
 }
 
+static void
+print_cursor (TrackerSparqlCursor *cursor,
+              const gchar         *none_found,
+              const gchar         *heading,
+              gboolean             only_first_col)
+{
+        if (!cursor) {
+                g_print ("%s\n", none_found);
+        } else {
+                gint count = 0;
+
+                g_print ("%s:\n", heading);
+
+                if (only_first_col) {
+                        while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+                                g_print ("  %s\n",
+                                         tracker_sparql_cursor_get_string (cursor, 0, NULL));
+                                count++;
+                        }
+                } else {
+                        gint n_cols;
+
+                        n_cols = tracker_sparql_cursor_get_n_columns (cursor);
+
+                        while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+                                gint col;
+
+                                for (col = 0; col < n_cols; col++) {
+                                        if (col == 0) {
+                                                g_print ("  %s",
+                                                         tracker_sparql_cursor_get_string (cursor, col, NULL));
+                                        } else {
+                                                g_print (", %s",
+                                                         tracker_sparql_cursor_get_string (cursor, col, NULL));
+                                        }
+                                }
+
+                                g_print ("\n");
+
+                                count++;
+                        }
+                }
+
+                if (count == 0) {
+                        g_print ("  %s\n", _("None"));
+                }
+
+                g_print ("\n");
+
+                g_object_unref (cursor);
+        }
+}
+
 int
 main (int argc, char **argv)
 {
-	TrackerClient *client;
+	TrackerSparqlConnection *connection;
+        TrackerSparqlCursor *cursor;
 	GOptionContext *context;
 	GError *error = NULL;
-	GPtrArray *results;
 	const gchar *error_message;
 
 	setlocale (LC_ALL, "");
@@ -213,7 +239,7 @@ main (int argc, char **argv)
 		return EXIT_SUCCESS;
 	}
 
-	if (!list_classes && !list_class_prefixes && !list_properties && 
+	if (!list_classes && !list_class_prefixes && !list_properties &&
 	    !list_notifies && !search && !file && !query) {
 		error_message = _("An argument must be supplied");
 	} else if (file && query) {
@@ -237,11 +263,19 @@ main (int argc, char **argv)
 
 	g_option_context_free (context);
 
-	client = tracker_client_new (0, G_MAXINT);
+	g_type_init ();
 
-	if (!client) {
-		g_printerr ("%s\n",
-		            _("Could not establish a D-Bus connection to Tracker"));
+	if (!g_thread_supported ()) {
+		g_thread_init (NULL);
+	}
+
+	connection = tracker_sparql_connection_get (&error);
+
+	if (!connection) {
+		g_printerr ("%s: %s\n",
+		            _("Could not establish a connection to Tracker"),
+		            error ? error->message : _("No error given"));
+		g_clear_error (&error);
 		return EXIT_FAILURE;
 	}
 
@@ -249,33 +283,19 @@ main (int argc, char **argv)
 		const gchar *query;
 
 		query = "SELECT ?c WHERE { ?c a rdfs:Class }";
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
 		if (error) {
 			g_printerr ("%s, %s\n",
 			            _("Could not list classes"),
 			            error->message);
 			g_error_free (error);
-			g_object_unref (client);
+			g_object_unref (connection);
 
 			return EXIT_FAILURE;
 		}
 
-		if (!results) {
-			g_print ("%s\n",
-			         _("No classes were found"));
-		} else {
-			g_print (g_dngettext (NULL,
-			                      "Class: %d",
-			                      "Classes: %d",
-			                      results->len),
-			         results->len);
-			g_print ("\n");
-
-			g_ptr_array_foreach (results, results_foreach, NULL);
-			g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-			g_ptr_array_free (results, TRUE);
-		}
+                print_cursor (cursor, _("No classes were found"), _("Classes"), TRUE);
 	}
 
 	if (list_class_prefixes) {
@@ -287,33 +307,19 @@ main (int argc, char **argv)
 			"  tracker:prefix ?prefix "
 			"}";
 
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
 		if (error) {
 			g_printerr ("%s, %s\n",
 			            _("Could not list class prefixes"),
 			            error->message);
 			g_error_free (error);
-			g_object_unref (client);
+			g_object_unref (connection);
 
 			return EXIT_FAILURE;
 		}
 
-		if (!results) {
-			g_print ("%s\n",
-			         _("No class prefixes were found"));
-		} else {
-			g_print (g_dngettext (NULL,
-			                      "Prefix: %d",
-			                      "Prefixes: %d",
-			                      results->len),
-			         results->len);
-			g_print ("\n");
-
-			g_ptr_array_foreach (results, results_foreach, NULL);
-			g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-			g_ptr_array_free (results, TRUE);
-		}
+                print_cursor (cursor, _("No class prefixes were found"), _("Prefixes"), FALSE);
 	}
 
 	if (list_properties) {
@@ -336,7 +342,7 @@ main (int argc, char **argv)
 				            _("Could not find property for class prefix, "
 				              "e.g. :Resource in 'rdfs:Resource'"));
 				g_free (prefix);
-				g_object_unref (client);
+				g_object_unref (connection);
 
 				return EXIT_FAILURE;
 			}
@@ -344,12 +350,12 @@ main (int argc, char **argv)
 			property = g_strdup (p + 1);
 			*p = '\0';
 
-			class_name_no_property = get_class_from_prefix (client, prefix);
+			class_name_no_property = get_class_from_prefix (connection, prefix);
 			g_free (prefix);
 
 			if (!class_name_no_property) {
 				g_free (property);
-				g_object_unref (client);
+				g_object_unref (connection);
 
 				return EXIT_FAILURE;
 			}
@@ -366,7 +372,7 @@ main (int argc, char **argv)
 		                         "}",
 		                         class_name);
 
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 		g_free (query);
 		g_free (class_name);
 
@@ -375,26 +381,12 @@ main (int argc, char **argv)
 			            _("Could not list properties"),
 			            error->message);
 			g_error_free (error);
-			g_object_unref (client);
+			g_object_unref (connection);
 
 			return EXIT_FAILURE;
 		}
 
-		if (!results) {
-			g_print ("%s\n",
-			         _("No properties were found"));
-		} else {
-			g_print (g_dngettext (NULL,
-			                      "Property: %d",
-			                      "Properties: %d",
-			                      results->len),
-			         results->len);
-			g_print ("\n");
-
-			g_ptr_array_foreach (results, results_foreach, NULL);
-			g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-			g_ptr_array_free (results, TRUE);
-		}
+                print_cursor (cursor, _("No properties were found"), _("Properties"), TRUE);
 	}
 
 	if (list_notifies) {
@@ -417,31 +409,20 @@ main (int argc, char **argv)
 			                         list_notifies);
 		}
 
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 		g_free (query);
 
 		if (error) {
 			g_printerr ("%s, %s\n",
 			            _("Could not find notify classes"),
 			            error->message);
-			g_clear_error (&error);
-		} else {
-			if (!results) {
-				g_print ("%s\n",
-				         _("No notifies were found"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Notify: %d",
-				                      "Notifies: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
+			g_error_free (error);
+			g_object_unref (connection);
+
+			return EXIT_FAILURE;
 		}
+
+                print_cursor (cursor, _("No notifies were found"), _("Notifies"), TRUE);
 	}
 
 	if (search) {
@@ -454,32 +435,21 @@ main (int argc, char **argv)
 		                         "  FILTER regex (?c, \"%s\", \"i\") "
 		                         "}",
 		                         search);
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 		g_free (query);
 
 		if (error) {
 			g_printerr ("%s, %s\n",
 			            _("Could not search classes"),
 			            error->message);
-			g_clear_error (&error);
-		} else {
-			if (!results) {
-				g_print ("%s\n",
-				         _("No classes were found to match search term"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Class: %d",
-				                      "Classes: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
+			g_error_free (error);
+			g_object_unref (connection);
+
+			return EXIT_FAILURE;
 		}
 
+                print_cursor (cursor, _("No classes were found to match search term"), _("Classes"), TRUE);
+
 		/* Second list properties */
 		query = g_strdup_printf ("SELECT ?p "
 		                         "WHERE {"
@@ -488,31 +458,20 @@ main (int argc, char **argv)
 		                         "}",
 		                         search);
 
-		results = tracker_resources_sparql_query (client, query, &error);
+                cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 		g_free (query);
 
 		if (error) {
-			g_printerr ("  %s, %s\n",
+			g_printerr ("%s, %s\n",
 			            _("Could not search properties"),
 			            error->message);
-			g_clear_error (&error);
-		} else {
-			if (!results) {
-				g_print ("%s\n",
-				         _("No properties were found to match search term"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Property: %d",
-				                      "Properties: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
+			g_error_free (error);
+			g_object_unref (connection);
+
+			return EXIT_FAILURE;
 		}
+
+                print_cursor (cursor, _("No properties were found to match search term"), _("Properties"), TRUE);
 	}
 
 	if (file) {
@@ -526,7 +485,7 @@ main (int argc, char **argv)
 			            file,
 			            error->message);
 			g_error_free (error);
-			g_object_unref (client);
+			g_object_unref (connection);
 
 			return EXIT_FAILURE;
 		}
@@ -539,7 +498,7 @@ main (int argc, char **argv)
 			            error->message);
 			g_error_free (error);
 			g_free (path_in_utf8);
-			g_object_unref (client);
+			g_object_unref (connection);
 
 			return EXIT_FAILURE;
 		}
@@ -549,7 +508,7 @@ main (int argc, char **argv)
 
 	if (query) {
 		if (G_UNLIKELY (update)) {
-			results = tracker_resources_sparql_update_blank (client, query, &error);
+			tracker_sparql_connection_update (connection, query, NULL, &error);
 
 			if (error) {
 				g_printerr ("%s, %s\n",
@@ -560,6 +519,9 @@ main (int argc, char **argv)
 				return EXIT_FAILURE;
 			}
 
+                        g_print ("%s\n", _("Done"));
+
+#if 0
 			if (results) {
 				GPtrArray *insert;
 				GHashTable *solution;
@@ -576,7 +538,7 @@ main (int argc, char **argv)
 						g_hash_table_iter_init (&iter, solution);
 						n = 0;
 						while (g_hash_table_iter_next (&iter, &key, &value)) {
-							g_print ("%s%s: %s", 
+							g_print ("%s%s: %s",
 							         n > 0 ? ", " : "", 
 							         (const gchar *) key, 
 							         (const gchar *) value);
@@ -586,8 +548,9 @@ main (int argc, char **argv)
 					}
 				}
 			}
+#endif
 		} else {
-			results = tracker_resources_sparql_query (client, query, &error);
+                        cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
 			if (error) {
 				g_printerr ("%s, %s\n",
@@ -598,25 +561,11 @@ main (int argc, char **argv)
 				return EXIT_FAILURE;
 			}
 
-			if (!results) {
-				g_print ("%s\n",
-				         _("No results found matching your query"));
-			} else {
-				g_print (g_dngettext (NULL,
-				                      "Result: %d",
-				                      "Results: %d",
-				                      results->len),
-				         results->len);
-				g_print ("\n");
-
-				g_ptr_array_foreach (results, results_foreach, NULL);
-				g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-				g_ptr_array_free (results, TRUE);
-			}
+                        print_cursor (cursor, _("No results found matching your query"), _("Results"), FALSE);
 		}
 	}
 
-	g_object_unref (client);
+	g_object_unref (connection);
 
 	return EXIT_SUCCESS;
 }



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