[tracker/wip/carlosg/tracker-3.0-api-breaks: 31/92] tracker: Allow specifying connection on info/sparql CLI subcommands



commit eda6eeaadb5358ace24897add93e241353ea2f9e
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Dec 21 13:52:09 2019 +0100

    tracker: Allow specifying connection on info/sparql CLI subcommands
    
    This can be a local database (expressed as a database location),
    a DBus service (expressed as a service name), or a remote service
    (expressed as a URI base).
    
    This makes these tools able to work generically across connections,
    but one needs to be specified (eg. talking to tracker-miner-fs DBus
    name).

 src/tracker/tracker-info.c   | 43 ++++++++++++++++++++++++++++++++++-
 src/tracker/tracker-sparql.c | 54 +++++++++++++++++++++++++++++++++++---------
 2 files changed, 85 insertions(+), 12 deletions(-)
---
diff --git a/src/tracker/tracker-info.c b/src/tracker/tracker-info.c
index 17a0bee3a..784f67901 100644
--- a/src/tracker/tracker-info.c
+++ b/src/tracker/tracker-info.c
@@ -44,8 +44,23 @@ static gboolean plain_text_content;
 static gboolean resource_is_iri;
 static gboolean turtle;
 static gchar *url_property;
+static gchar *database_path;
+static gchar *dbus_service;
+static gchar *remote_service;
 
 static GOptionEntry entries[] = {
+       { "database", 'd', 0, G_OPTION_ARG_FILENAME, &database_path,
+         N_("Location of the database"),
+         N_("FILE")
+       },
+       { "dbus-service", 'b', 0, G_OPTION_ARG_STRING, &dbus_service,
+         N_("Connects to a DBus service"),
+         N_("DBus service name")
+       },
+       { "remote-service", 'r', 0, G_OPTION_ARG_STRING, &remote_service,
+         N_("Connects to a remote service"),
+         N_("Remote service URI")
+       },
        { "full-namespaces", 'f', 0, G_OPTION_ARG_NONE, &full_namespaces,
          N_("Show full namespaces (i.e. don’t use nie:title, use full URLs)"),
          NULL,
@@ -253,6 +268,32 @@ print_turtle (gchar               *urn,
        g_free (subject);
 }
 
+static TrackerSparqlConnection *
+create_connection (GError **error)
+{
+       if (database_path && !dbus_service && !remote_service) {
+               GFile *file;
+
+               file = g_file_new_for_commandline_arg (database_path);
+               return tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_READONLY,
+                                                     file, NULL, NULL, error);
+       } else if (dbus_service && !database_path && !remote_service) {
+               GDBusConnection *dbus_conn;
+
+               dbus_conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+               if (!dbus_conn)
+                       return NULL;
+
+               return tracker_sparql_connection_bus_new (dbus_service, dbus_conn, error);
+       } else if (remote_service && !database_path && !dbus_service) {
+               return tracker_sparql_connection_remote_new (remote_service);
+       } else {
+               /* TRANSLATORS: Those are commandline arguments */
+               g_printerr (_("Specify one --database, --dbus-service or --remote-service option"));
+               exit (EXIT_FAILURE);
+       }
+}
+
 static int
 info_run (void)
 {
@@ -261,7 +302,7 @@ info_run (void)
        GHashTable *prefixes;
        gchar **p;
 
-       connection = tracker_sparql_connection_get (NULL, &error);
+       connection = create_connection (&error);
 
        if (!connection) {
                g_printerr ("%s: %s\n",
diff --git a/src/tracker/tracker-sparql.c b/src/tracker/tracker-sparql.c
index 0b4d2679a..849f2e48d 100644
--- a/src/tracker/tracker-sparql.c
+++ b/src/tracker/tracker-sparql.c
@@ -98,9 +98,23 @@ static gchar *tree;
 static gchar *get_shorthand;
 static gchar *get_longhand;
 static gchar *search;
-static gchar *remote_url;
+static gchar *database_path;
+static gchar *dbus_service;
+static gchar *remote_service;
 
 static GOptionEntry entries[] = {
+       { "database", 'd', 0, G_OPTION_ARG_FILENAME, &database_path,
+         N_("Location of the database"),
+         N_("FILE")
+       },
+       { "dbus-service", 'b', 0, G_OPTION_ARG_STRING, &dbus_service,
+         N_("Connects to a DBus service"),
+         N_("DBus service name")
+       },
+       { "remote-service", 'r', 0, G_OPTION_ARG_STRING, &remote_service,
+         N_("Connects to a remote service"),
+         N_("Remote service URI")
+       },
        { "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
          N_("Path to use to run a query or update from file"),
          N_("FILE"),
@@ -149,13 +163,35 @@ static GOptionEntry entries[] = {
          N_("Returns the full namespace for a class."),
          N_("CLASS"),
        },
-       { "remote-service", 'r', 0, G_OPTION_ARG_STRING, &remote_url,
-         N_("Remote service to query to"),
-         N_("BASE_URL"),
-       },
        { NULL }
 };
 
+static TrackerSparqlConnection *
+create_connection (GError **error)
+{
+       if (database_path && !dbus_service && !remote_service) {
+               GFile *file;
+
+               file = g_file_new_for_commandline_arg (database_path);
+               return tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_READONLY,
+                                                     file, NULL, NULL, error);
+       } else if (dbus_service && !database_path && !remote_service) {
+               GDBusConnection *dbus_conn;
+
+               dbus_conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+               if (!dbus_conn)
+                       return NULL;
+
+               return tracker_sparql_connection_bus_new (dbus_service, dbus_conn, error);
+       } else if (remote_service && !database_path && !dbus_service) {
+               return tracker_sparql_connection_remote_new (remote_service);
+       } else {
+               /* TRANSLATORS: Those are commandline arguments */
+               g_printerr (_("Specify one --database, --dbus-service or --remote-service option"));
+               exit (EXIT_FAILURE);
+       }
+}
+
 GHashTable *
 tracker_sparql_get_prefixes (void)
 {
@@ -165,7 +201,7 @@ tracker_sparql_get_prefixes (void)
        GHashTable *retval;
        const gchar *query;
 
-       connection = tracker_sparql_connection_get (NULL, &error);
+       connection = create_connection (&error);
 
        if (!connection) {
                g_printerr ("%s: %s\n",
@@ -1075,11 +1111,7 @@ sparql_run (void)
        TrackerSparqlCursor *cursor;
        GError *error = NULL;
 
-       if (remote_url != NULL) {
-               connection = tracker_sparql_connection_remote_new (remote_url);
-       } else {
-               connection = tracker_sparql_connection_get (NULL, &error);
-       }
+       connection = create_connection (&error);
 
        if (!connection) {
                g_printerr ("%s: %s\n",


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