[tracker/wip/carlosg/serialize-api: 5/22] libtracker-data: Add query to tell whether a query may be serialized to RDF




commit 82b1c5a45dad1770a08688f642b34a26dcba4ae8
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Nov 21 00:01:22 2021 +0100

    libtracker-data: Add query to tell whether a query may be serialized to RDF
    
    These are basically DESCRIBE and CONSTRUCT queries, which are guaranteed
    to return data in a format that is fully serializable.

 src/libtracker-data/tracker-sparql.c | 34 ++++++++++++++++++++++++++++++++++
 src/libtracker-data/tracker-sparql.h |  2 ++
 2 files changed, 36 insertions(+)
---
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index f6d42228a..b7112a940 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -9942,3 +9942,37 @@ tracker_sparql_make_langstring (const gchar *str,
 
        return bytes;
 }
+
+gboolean
+tracker_sparql_is_serializable (TrackerSparql *sparql)
+{
+       TrackerParserNode *node;
+
+       /* Updates are the other way around */
+       if (sparql->query_type == TRACKER_SPARQL_QUERY_UPDATE)
+               return FALSE;
+
+       node = tracker_node_tree_get_root (sparql->tree);
+
+       for (node = tracker_sparql_parser_tree_find_first (node, FALSE);
+            node;
+            node = tracker_sparql_parser_tree_find_next (node, FALSE)) {
+               const TrackerGrammarRule *rule;
+
+               rule = tracker_parser_node_get_rule (node);
+
+               /* Only DESCRIBE and CONSTRUCT queries apply, since these
+                * are guaranteed to return full RDF data.
+                */
+               if (tracker_grammar_rule_is_a (rule, RULE_TYPE_RULE, NAMED_RULE_DescribeQuery) ||
+                   tracker_grammar_rule_is_a (rule, RULE_TYPE_RULE, NAMED_RULE_ConstructQuery))
+                       return TRUE;
+
+               /* Early out in other query types */
+               if (tracker_grammar_rule_is_a (rule, RULE_TYPE_RULE, NAMED_RULE_SelectQuery) ||
+                   tracker_grammar_rule_is_a (rule, RULE_TYPE_RULE, NAMED_RULE_AskQuery))
+                       break;
+       }
+
+       return FALSE;
+}
diff --git a/src/libtracker-data/tracker-sparql.h b/src/libtracker-data/tracker-sparql.h
index 74297bd0a..29c881cc9 100644
--- a/src/libtracker-data/tracker-sparql.h
+++ b/src/libtracker-data/tracker-sparql.h
@@ -35,6 +35,8 @@ G_DECLARE_FINAL_TYPE (TrackerSparql, tracker_sparql,
 TrackerSparql *       tracker_sparql_new (TrackerDataManager *manager,
                                           const gchar        *sparql);
 
+gboolean              tracker_sparql_is_serializable (TrackerSparql *sparql);
+
 TrackerSparqlCursor * tracker_sparql_execute_cursor (TrackerSparql  *sparql,
                                                      GHashTable     *parameters,
                                                      GError        **error);


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