[tracker] tracker-sparql: Added command line options to make using SPARQL easier



commit 660d34b33e12037a97fb721961592dd5ec5dd65c
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Sep 8 08:40:27 2009 +0100

    tracker-sparql: Added command line options to make using SPARQL easier
    
    Added:
    --list-classes
    --list-class-prefixes
    --list-properties
    
    This allows people to quickly find out what classes we support, the
    prefix they can use for them (to make life easier) and the properties
    associated with a class. The class prefix can be used to get
    properties too.

 src/tracker-utils/tracker-sparql.c |  303 +++++++++++++++++++++++++++++++-----
 1 files changed, 267 insertions(+), 36 deletions(-)
---
diff --git a/src/tracker-utils/tracker-sparql.c b/src/tracker-utils/tracker-sparql.c
index bfbb6d6..fdfc20e 100644
--- a/src/tracker-utils/tracker-sparql.c
+++ b/src/tracker-utils/tracker-sparql.c
@@ -1,7 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
- * Copyright (C) 2008, Nokia
+ * Copyright (C) 2009, Nokia
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -30,10 +30,14 @@
 #include <glib/gi18n.h>
 
 #include <libtracker/tracker.h>
+#include <libtracker-common/tracker-common.h>
 
-static gchar	     *file;
-static gchar	     *query;
-static gboolean	      update;
+static gchar	*file;
+static gchar	*query;
+static gboolean	 update;
+static gboolean  list_classes;
+static gboolean  list_class_prefixes;
+static gchar    *list_properties;
 
 static GOptionEntry   entries[] = {
 	{ "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
@@ -48,9 +52,75 @@ static GOptionEntry   entries[] = {
 	  N_("SPARQL update extensions"),
 	  N_("SPARQL"),
 	},
+	{ "list-classes", 'c', 0, G_OPTION_ARG_NONE, &list_classes,
+	  N_("Retrieve classes"),
+	  NULL,
+	},
+	{ "list-class-prefixes", 'n', 0, G_OPTION_ARG_NONE, &list_class_prefixes,
+	  N_("Retrieve class prefixes"),
+	  NULL,
+	},
+	{ "list-properties", 'p', 0, G_OPTION_ARG_STRING, &list_properties,
+	  N_("Retrieve properties for a class (e.g. http://www.w3.org/2000/01/rdf-schema#Resource)"),
+	  N_("NAMESPACE"),
+	},
 	{ NULL }
 };
 
+static gchar *
+get_class_from_prefix (TrackerClient *client,
+		       const gchar   *prefix)
+{
+	GError *error = NULL;
+	GPtrArray *results;
+	const gchar *query;
+	gchar *found;
+	gint i;
+	
+	query = "SELECT ?prefix ?ns "
+		"WHERE {"
+		"  ?ns a tracker:Namespace ;"
+		"  tracker:prefix ?prefix "
+		"}";
+	
+	/* We have namespace prefix, get full name */
+	results = tracker_resources_sparql_query (client, query, &error);
+	
+	if (error) {
+		g_printerr ("%s, %s\n",
+			    _("Could not get namespace prefixes"),
+			    error->message);
+		g_error_free (error);
+
+		return NULL;
+	}
+	
+	if (!results) {
+		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;
+
+		data = g_ptr_array_index (results, i);
+		class_prefix = data[0];
+		class_name = data[1];
+
+		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);
+
+	return found;
+}
+
 static void
 results_foreach (gpointer value,
 		 gpointer user_data)
@@ -79,6 +149,7 @@ main (int argc, char **argv)
 	GOptionContext *context;
 	GError *error = NULL;
 	GPtrArray *results;
+	gboolean success;
 
 	setlocale (LC_ALL, "");
 
@@ -91,11 +162,12 @@ main (int argc, char **argv)
 	g_option_context_add_main_entries (context, entries, NULL);
 	g_option_context_parse (context, &argc, &argv, NULL);
 
-	if ((!file && !query) || (file && query)) {
+	if (!list_classes && !list_class_prefixes && !list_properties &&
+	    ((!file && !query) || (file && query))) {
 		gchar *help;
 
 		g_printerr ("%s\n\n",
-			    _("Either path or query needs to be specified"));
+			    _("Either a file or query needs to be specified"));
 
 		help = g_option_context_get_help (context, TRUE, NULL);
 		g_option_context_free (context);
@@ -115,6 +187,157 @@ main (int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
+	if (list_classes) {
+		const gchar *query;
+
+		query = "SELECT ?cl WHERE { ?cl a rdfs:Class }";
+
+		results = tracker_resources_sparql_query (client, query, &error);
+
+		if (error) {
+			g_printerr ("%s, %s\n",
+				    _("Could not list classes"),
+				    error->message);
+			g_error_free (error);
+			tracker_disconnect (client);
+
+			return EXIT_FAILURE;
+		}
+
+		if (!results) {
+			g_print ("%s\n",
+				 _("No classes were found"));
+		} else {
+			g_print (tracker_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);
+		}
+	}
+
+	if (list_class_prefixes) {
+		const gchar *query;
+
+		query = "SELECT ?prefix ?ns "
+			"WHERE {"
+			"  ?ns a tracker:Namespace ;"
+			"  tracker:prefix ?prefix "
+			"}";
+
+		results = tracker_resources_sparql_query (client, query, &error);
+
+		if (error) {
+			g_printerr ("%s, %s\n",
+				    _("Could not list class prefixes"),
+				    error->message);
+			g_error_free (error);
+			tracker_disconnect (client);
+
+			return EXIT_FAILURE;
+		}
+
+		if (!results) {
+			g_print ("%s\n",
+				 _("No class prefixes were found"));
+		} else {
+			g_print (tracker_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);
+		}
+	}
+
+	if (list_properties) {
+		gchar *query;
+		gchar *class_name;
+
+		if (g_str_has_prefix (list_properties, "http://";)) {
+			/* We have full class name */
+			class_name = g_strdup (list_properties);
+		} else {
+			gchar *p;
+			gchar *prefix, *property;
+			gchar *class_name_no_property;
+
+			prefix = g_strdup (list_properties);
+			p = strchr (prefix, '#');
+			
+			if (!p) {
+				g_printerr ("%s\n", 
+					    _("Could not find property for class prefix, "
+					      "e.g. #Resource in 'rdfs#Resource'"));
+				g_free (prefix);
+				tracker_disconnect (client);
+				return EXIT_FAILURE;
+			}
+
+			property = g_strdup (p + 1);
+			*p = '\0';
+
+			class_name_no_property = get_class_from_prefix (client, prefix);
+			g_free (prefix);
+
+			if (!class_name_no_property) {
+				g_free (property);
+				tracker_disconnect (client);
+				return EXIT_FAILURE;
+			}
+
+			class_name = g_strconcat (class_name_no_property, property, NULL);
+			g_free (class_name_no_property);
+			g_free (property);
+		}
+	
+		query = g_strdup_printf ("SELECT ?prop "
+					 "WHERE {"
+					 "  ?prop a rdf:Property ;"
+					 "  rdfs:domain <%s>"
+					 "}",
+					 class_name);
+
+		results = tracker_resources_sparql_query (client, query, &error);
+		g_free (query);
+		g_free (class_name);
+
+		if (error) {
+			g_printerr ("%s, %s\n",
+				    _("Could not list properties"),
+				    error->message);
+			g_error_free (error);
+			tracker_disconnect (client);
+
+			return EXIT_FAILURE;
+		}
+
+		if (!results) {
+			g_print ("%s\n",
+				 _("No properties were found"));
+		} else {
+			g_print (tracker_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);
+		}
+	}
+
 	if (file) {
 		gchar *path_in_utf8;
 		gsize size;
@@ -147,41 +370,49 @@ main (int argc, char **argv)
 		g_free (path_in_utf8);
 	}
 
-	if (G_UNLIKELY (update)) {
-		tracker_resources_sparql_update (client, query, &error);
-		results = NULL;
-	} else {
-		results = tracker_resources_sparql_query (client, query, &error);
-	}
-
-	if (error) {
+	if (query) {
 		if (G_UNLIKELY (update)) {
-			g_printerr ("%s, %s\n",
-				    _("Could not run update"),
-				    error->message);
+			tracker_resources_sparql_update (client, query, &error);
+
+			if (error) {
+				g_printerr ("%s, %s\n",
+					    _("Could not run update"),
+					    error->message);
+				g_error_free (error);
+				
+				return FALSE;
+			}
 		} else {
-			g_printerr ("%s, %s\n",
-				    _("Could not run query"),
-				    error->message);
-		}
-
-		g_error_free (error);
-		tracker_disconnect (client);
-
-		return EXIT_FAILURE;
-	}
-
-	if (!update) {
-		if (!results) {
-			g_print ("%s\n",
-				 _("No results found matching your query"));
-		} else {
-			g_ptr_array_foreach (results, results_foreach, NULL);
-			g_ptr_array_free (results, TRUE);
+			results = tracker_resources_sparql_query (client, query, &error);
+
+			if (error) {
+				g_printerr ("%s, %s\n",
+					    _("Could not run query"),
+					    error->message);
+				g_error_free (error);
+				
+				return FALSE;
+			}
+			
+			if (!results) {
+				g_print ("%s\n",
+					 _("No results found matching your query"));
+			} else {
+				g_print (tracker_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);
+			}
 		}
 	}
 
 	tracker_disconnect (client);
 
-	return EXIT_SUCCESS;
+	return success ? EXIT_SUCCESS : EXIT_FAILURE;
 }



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