summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2021-07-25 11:56:05 +0200
committerCarlos Garnacho <carlosg@gnome.org>2021-11-06 15:07:13 +0100
commit3e9e17ad6c2b6a7f3c28276106f23ac6ac27557c (patch)
tree29d5d5e4f26bbc54ba4e8693eb70fed7e8d22d49 /src
parentea72f2283760fe670c92db95f726666cf5492522 (diff)
downloadtracker-3e9e17ad6c2b6a7f3c28276106f23ac6ac27557c.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/libtracker-sparql/tracker-connection.c44
-rw-r--r--src/libtracker-sparql/tracker-connection.h6
2 files changed, 50 insertions, 0 deletions
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__ */