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



commit 55115f67a5f5b0d22214dd60c41297606613455f
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 5f678ab51..d7db36cc0 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -584,11 +584,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);
@@ -617,14 +618,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;
@@ -632,12 +637,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);
        }
 }
 
@@ -679,7 +685,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;
@@ -690,9 +697,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)) {
@@ -714,6 +735,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);
 
@@ -728,8 +750,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);
@@ -742,6 +764,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);
 
@@ -756,8 +779,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]