[tracker/wip/carlosg/portal: 115/141] libtracker-data: Allow reducing set to the empty graph



commit 8850361c2fc9e75e103ee1de16ce5f89b062c351
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Jan 23 17:31:29 2020 +0100

    libtracker-data: Allow reducing set to the empty graph
    
    The default unnamed graph was always considered part of the default
    graph. Allow it to be an empty set, in order to implement proper
    filtering policies.

 src/libtracker-data/tracker-sparql.c | 47 +++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 12 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 5b3032870..7bde4e287 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -573,11 +573,12 @@ _append_variable_sql (TrackerSparql   *sparql,
 
 static gchar *
 build_properties_string_for_class (TrackerSparql *sparql,
-                                   TrackerClass  *class)
+                                   TrackerClass  *class,
+                                   gint          *n_properties_ret)
 {
        TrackerOntologies *ontologies;
        TrackerProperty **properties;
-       guint n_properties, i;
+       guint n_properties, i, count = 0;
        GString *str;
 
        ontologies = tracker_data_manager_get_ontologies (sparql->data_manager);
@@ -606,14 +607,18 @@ build_properties_string_for_class (TrackerSparql *sparql,
 
                g_string_append_printf (str, "\"%s\",",
                                        tracker_property_get_name (properties[i]));
+               count++;
        }
 
+       *n_properties_ret = count;
+
        return g_string_free (str, FALSE);
 }
 
 static gchar *
 build_properties_string (TrackerSparql   *sparql,
-                         TrackerProperty *property)
+                         TrackerProperty *property,
+                         gint            *n_properties)
 {
        if (tracker_property_get_multiple_values (property)) {
                GString *str;
@@ -621,12 +626,13 @@ build_properties_string (TrackerSparql   *sparql,
                str = g_string_new (NULL);
                g_string_append_printf (str, "\"%s\",",
                                        tracker_property_get_name (property));
+               *n_properties = 1;
                return g_string_free (str, FALSE);
        } else {
                TrackerClass *class;
 
                class = tracker_property_get_domain (property);
-               return build_properties_string_for_class (sparql, class);
+               return build_properties_string_for_class (sparql, class, n_properties);
        }
 }
 
@@ -668,7 +674,8 @@ tracker_sparql_get_effective_graphs (TrackerSparql *sparql)
 static void
 _append_union_graph_with_clause (TrackerSparql *sparql,
                                  const gchar   *table_name,
-                                 const gchar   *properties)
+                                 const gchar   *properties,
+                                 gint           n_properties)
 {
        gpointer graph_name, graph_id;
        GHashTable *graphs;
@@ -679,9 +686,23 @@ _append_union_graph_with_clause (TrackerSparql *sparql,
        _append_string_printf (sparql, "\"unionGraph_%s\"(ID, %s graph) AS (",
                               table_name, properties);
 
-       _append_string_printf (sparql,
-                              "SELECT ID, %s 0 AS graph FROM \"main\".\"%s\" ",
-                              properties, table_name);
+       if (g_hash_table_size (graphs) > 0) {
+               _append_string_printf (sparql,
+                                      "SELECT ID, %s 0 AS graph FROM \"main\".\"%s\" ",
+                                      properties, table_name);
+       } else {
+               gint i;
+
+               _append_string (sparql, "SELECT ");
+
+               for (i = 0; i < n_properties + 2; i++) {
+                       if (i > 0)
+                               _append_string (sparql, ", ");
+                       _append_string (sparql, "NULL ");
+               }
+
+               _append_string (sparql, "WHERE 0 ");
+       }
 
        g_hash_table_iter_init (&iter, graphs);
        while (g_hash_table_iter_next (&iter, &graph_name, &graph_id)) {
@@ -703,6 +724,7 @@ tracker_sparql_add_union_graph_subquery (TrackerSparql   *sparql,
        TrackerStringBuilder *old;
        const gchar *table_name;
        gchar *properties;
+       gint n_properties;
 
        table_name = tracker_property_get_table_name (property);
 
@@ -717,8 +739,8 @@ tracker_sparql_add_union_graph_subquery (TrackerSparql   *sparql,
        else
                _append_string (sparql, ", ");
 
-       properties = build_properties_string (sparql, property);
-       _append_union_graph_with_clause (sparql, table_name, properties);
+       properties = build_properties_string (sparql, property, &n_properties);
+       _append_union_graph_with_clause (sparql, table_name, properties, n_properties);
        g_free (properties);
 
        tracker_sparql_swap_builder (sparql, old);
@@ -731,6 +753,7 @@ tracker_sparql_add_union_graph_subquery_for_class (TrackerSparql *sparql,
        TrackerStringBuilder *old;
        const gchar *table_name;
        gchar *properties;
+       gint n_properties;
 
        table_name = tracker_class_get_name (class);
 
@@ -745,8 +768,8 @@ tracker_sparql_add_union_graph_subquery_for_class (TrackerSparql *sparql,
        else
                _append_string (sparql, ", ");
 
-       properties = build_properties_string_for_class (sparql, class);
-       _append_union_graph_with_clause (sparql, table_name, properties);
+       properties = build_properties_string_for_class (sparql, class, &n_properties);
+       _append_union_graph_with_clause (sparql, table_name, properties, n_properties);
        g_free (properties);
 
        tracker_sparql_swap_builder (sparql, old);


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