[tracker/wip/carlosg/statement-from-resource: 1/2] libtracker-sparql: Add method to create statement from a resource file




commit 3e9e17ad6c2b6a7f3c28276106f23ac6ac27557c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jul 25 11:56:05 2021 +0200

    libtracker-sparql: Add method to create statement from a resource file
    
    This TrackerSparqlConnection method creates a TrackerSparqlStatement from
    a query located in a resource path. This could allow a better code/model
    separation where all Tracker resources are separately editable in the
    source tree (e.g. distinct per-query .rq files, with syntax highlighting,
    etc), and still directly available at runtime without further disk reads.
    
    There's other nice side effects of this, like having queries nicely
    indented in logging output (instead of the usual string concatenation in
    code that makes queries alright to see in the source code, but a mess when
    logged), or avoiding hard to follow code changes (e.g. mass reindenting)
    when only query changes are required. Also, queries would be easily
    readable with the "gresource list/extract" command line tool without
    looking across the source code.

 src/libtracker-sparql/tracker-connection.c | 44 ++++++++++++++++++++++++++++++
 src/libtracker-sparql/tracker-connection.h |  6 ++++
 2 files changed, 50 insertions(+)
---
diff --git a/src/libtracker-sparql/tracker-connection.c b/src/libtracker-sparql/tracker-connection.c
index fc583a3f2..ea197d923 100644
--- a/src/libtracker-sparql/tracker-connection.c
+++ b/src/libtracker-sparql/tracker-connection.c
@@ -809,3 +809,47 @@ tracker_sparql_connection_create_batch (TrackerSparqlConnection *connection)
 
        return TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->create_batch (connection);
 }
+
+/**
+ * tracker_sparql_connection_query_statement_from_gresource:
+ * @connection: a #TrackerSparqlConnection
+ * @resource_path: the resource path of the file to parse.
+ * @cancellable: a #GCancellable, or %NULL
+ * @error: return location for an error, or %NULL
+ *
+ * Prepares a #TrackerSparqlStatement for the SPARQL query contained as a resource
+ * file at @resource_path. SPARQL Query files typically have the .rq extension.
+ *
+ * Returns: (transfer full) (nullable): a prepared statement
+ *
+ * Since: 3.3
+ **/
+TrackerSparqlStatement *
+tracker_sparql_connection_load_statement_from_gresource (TrackerSparqlConnection  *connection,
+                                                         const gchar              *resource_path,
+                                                         GCancellable             *cancellable,
+                                                         GError                  **error)
+{
+       TrackerSparqlStatement *stmt;
+       GBytes *query;
+
+       g_return_val_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection), NULL);
+       g_return_val_if_fail (resource_path && *resource_path, NULL);
+       g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL);
+       g_return_val_if_fail (!error || !*error, NULL);
+
+       query = g_resources_lookup_data (resource_path,
+                                        G_RESOURCE_LOOKUP_FLAGS_NONE,
+                                        error);
+       if (!query)
+               return NULL;
+
+       stmt = TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->query_statement (connection,
+                                                                                 g_bytes_get_data (query,
+                                                                                                   NULL),
+                                                                                 cancellable,
+                                                                                 error);
+       g_bytes_unref (query);
+
+       return stmt;
+}
diff --git a/src/libtracker-sparql/tracker-connection.h b/src/libtracker-sparql/tracker-connection.h
index e8969860f..00840cd16 100644
--- a/src/libtracker-sparql/tracker-connection.h
+++ b/src/libtracker-sparql/tracker-connection.h
@@ -212,6 +212,12 @@ gboolean tracker_sparql_connection_close_finish (TrackerSparqlConnection  *conne
                                                  GAsyncResult             *res,
                                                  GError                  **error);
 
+TRACKER_AVAILABLE_IN_3_3
+TrackerSparqlStatement * tracker_sparql_connection_load_statement_from_gresource (TrackerSparqlConnection  
*connection,
+                                                                                  const gchar              
*resource_path,
+                                                                                  GCancellable             
*cancellable,
+                                                                                  GError                  
**error);
+
 G_END_DECLS
 
 #endif /* __TRACKER_SPARQL_CONNECTION_H__ */


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