[tracker/wip/carlosg/graphs: 11/12] libtracker-sparql: Allow shortened graph forms in TrackerResource



commit 771989eabe5f5771108ec1fcea172e69b0d4dd5d
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Apr 29 18:54:21 2020 +0200

    libtracker-sparql: Allow shortened graph forms in TrackerResource
    
    When printing to SPARQL we would only accept graph URIs in their full
    form. Allow expanding prefixes, so it is possible to pass graphs with
    a prefix (as long as it is covered by the namespace manager).
    
    This allows the use of eg. "tracker:Documents", instead of
    "https://tracker.api.gnome.org/.../Documents";.

 src/libtracker-sparql/tracker-resource.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 8bbe3ba46..26b2d00a8 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -1300,7 +1300,7 @@ tracker_resource_print_turtle (TrackerResource         *self,
 typedef struct {
        TrackerNamespaceManager *namespaces;
        GString *string;
-       const char *graph_id;
+       char *graph_id;
        GList *done_list;
 } GenerateSparqlData;
 
@@ -1519,7 +1519,7 @@ tracker_resource_print_sparql_update (TrackerResource         *resource,
                                       const char              *graph_id)
 {
        TrackerResourcePrivate *priv;
-       GenerateSparqlData context;
+       GenerateSparqlData context = { 0, };
 
        g_return_val_if_fail (TRACKER_IS_RESOURCE (resource), "");
 
@@ -1535,7 +1535,9 @@ tracker_resource_print_sparql_update (TrackerResource         *resource,
 
        context.namespaces = namespaces;
        context.string = g_string_new (NULL);
-       context.graph_id = graph_id;
+
+       if (graph_id)
+               context.graph_id = tracker_namespace_manager_expand_uri (namespaces, graph_id);
 
        /* Resources can be recursive, and may have repeated or even cyclic
         * relationships. This list keeps track of what we already processed.
@@ -1553,18 +1555,19 @@ tracker_resource_print_sparql_update (TrackerResource         *resource,
 
        /* Finally insert the data */
        g_string_append (context.string, "INSERT DATA {\n");
-       if (graph_id) {
-               g_string_append_printf (context.string, "GRAPH <%s> {\n", graph_id);
+       if (context.graph_id) {
+               g_string_append_printf (context.string, "GRAPH <%s> {\n", context.graph_id);
        }
 
        generate_sparql_insert_pattern (resource, &context);
 
-       if (graph_id) {
+       if (context.graph_id) {
                g_string_append (context.string, "}\n");
        }
        g_string_append (context.string, "};\n");
 
        g_list_free (context.done_list);
+       g_free (context.graph_id);
        context.done_list = NULL;
 
        return g_string_free (context.string, FALSE);


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