[tracker/wip/sam/resource: 8/8] squash! libtracker-sparql: Add TrackerResource class



commit 7c650362d674007c318bacb1283c653137f4299c
Author: Sam Thursfield <sam afuera me uk>
Date:   Sat Jun 11 01:44:16 2016 +0100

    squash! libtracker-sparql: Add TrackerResource class
    
    Fix TrackerResource SPARQL generation when full URIs are passed to
    tracker_resource_set_uri(). Prevously the necessary <> brackets were
    not being used, but they need to be when the URI is not a prefixed URI.

 src/libtracker-sparql/tracker-resource.c |   37 ++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 7 deletions(-)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 29b99fa..5d0e6a7 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -826,9 +826,32 @@ generate_sparql_deletes_foreach (gpointer key,
 }
 
 static void
-append_value_to_sparql_builder (const GValue *value,
-                                TrackerSparqlBuilder *builder)
+generate_sparql_uri_value (const char *uri_or_curie,
+                           GenerateSparqlData *data)
 {
+       /* The tracker_resource_set_uri() function accepts both URIs
+        * (such as http://example.com/) and compact URIs (such as nie:DataObject).
+        * We could expand them here, but since the tracker-store can understand them
+        * as-is we leave them be and the generated SPARQL is clearer as a result.
+        * We still need to attempt to expand them in order to know whether they need
+        * <> brackets around them.
+        */
+       char *prefix = g_uri_parse_scheme (uri_or_curie);
+
+       if (prefix && tracker_namespace_manager_has_prefix (data->namespaces, prefix)) {
+               /* It's a compact URI and we know the prefix */
+               tracker_sparql_builder_object (data->builder, uri_or_curie);
+       } else {
+               /* It's a full URI (or something invalid, but we can't really tell that here) */
+               tracker_sparql_builder_object_iri (data->builder, uri_or_curie);
+       }
+}
+
+static void
+generate_sparql_value (const GValue *value,
+                       GenerateSparqlData *data)
+{
+       TrackerSparqlBuilder *builder = data->builder;
        GType type = G_VALUE_TYPE (value);
        if (type == G_TYPE_BOOLEAN) {
                tracker_sparql_builder_object_boolean (builder, g_value_get_boolean (value));
@@ -871,7 +894,7 @@ append_value_to_sparql_builder (const GValue *value,
        } else if (type == G_TYPE_STRING) {
                tracker_sparql_builder_object_string (builder, g_value_get_string (value));
        } else if (type == TRACKER_TYPE_URI) {
-               tracker_sparql_builder_object (builder, g_value_get_string (value));
+               generate_sparql_uri_value (g_value_get_string (value), data);
        } else if (type == TRACKER_TYPE_RESOURCE) {
                TrackerResource *relation = TRACKER_RESOURCE (g_value_get_object (value));
                tracker_sparql_builder_object_iri (builder, tracker_resource_get_identifier (relation));
@@ -904,9 +927,9 @@ generate_sparql_inserts_foreach (gpointer key,
        g_free (full_property);
 
        if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY) {
-               g_ptr_array_foreach (g_value_get_boxed (value), (GFunc)append_value_to_sparql_builder, 
data->builder);
+               g_ptr_array_foreach (g_value_get_boxed (value), (GFunc)generate_sparql_value, data);
        } else {
-               append_value_to_sparql_builder (value, data->builder);
+               generate_sparql_value (value, data);
        }
 }
 
@@ -967,9 +990,9 @@ generate_sparql_update (TrackerResource *resource,
        if (type_value != NULL) {
                tracker_sparql_builder_predicate (builder, "a");
                if (G_VALUE_TYPE (type_value) == G_TYPE_PTR_ARRAY) {
-                       g_ptr_array_foreach (g_value_get_boxed (type_value), 
(GFunc)append_value_to_sparql_builder, builder);
+                       g_ptr_array_foreach (g_value_get_boxed (type_value), (GFunc)generate_sparql_value, 
data);
                } else {
-                       append_value_to_sparql_builder (type_value, builder);
+                       generate_sparql_value (type_value, data);
                }
        }
 


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