| Tracker SPARQL Library Reference Manual |
|---|
In order to perform read-only queries to the store, a new
TrackerSparqlConnection
object must be acquired. In this case, as there is no intention of updating any
value in the store, both the general connection (with
tracker_sparql_connection_get)
or a specific direct-access connection (with
tracker_sparql_connection_get_direct)
may be acquired. Note that in the latter case, every non read-only operation will result
in an error being thrown by the TrackerSparqlConnection.
Once a proper connection object has been acquired, the read-only query can be launched either
synchronously (tracker_sparql_connection_query)
or asynchronously (tracker_sparql_connection_query_async).
If launched asynchronously, the results of the query can be obtained with
tracker_sparql_connection_query_finish.
If the query was successful, a TrackerSparqlCursor
will be available. You can now iterate the results of the query both synchronously (with
tracker_sparql_cursor_next) or
asynchronously (with
tracker_sparql_cursor_next_async and
tracker_sparql_cursor_next_finish)
Once you end up with the query, remember to call g_object_unref
for the TrackerSparqlCursor. And the same applies to the
TrackerSparqlConnection when no longer needed.
The following program shows how Read-Only queries can be done to the store in a synchronous way:
#include <tracker-sparql.h>
int main (int argc, const char **argv)
{
GError *error = NULL;
TrackerSparqlConnection *connection;
TrackerSparqlCursor *cursor;
const gchar *query = "SELECT nie:url(?u) WHERE { ?u a nfo:FileDataObject }";
/* Initialize GLib type system */
g_type_init ();
/* As we know only read-only queries will be done, it's enough
* to use a connection with only direct-access setup. */
connection = tracker_sparql_connection_get_direct (&error);
if (!connection) {
/* Some error happened getting the connection, not good */
g_error ("Couldn't obtain a direct-access connection to the "
"Tracker Store: '%s'",
error ? error->message : "unknown error");
}
/* Make a synchronous query to the store */
cursor = tracker_sparql_connection_query (connection,
query,
NULL,
&error);
if (error) {
/* Some error happened performing the query, not good */
g_error ("Couldn't query the Tracker Store: '%s'",
error ? error->message : "unknown error");
}
/* Check results... */
if (!cursor) {
g_print ("No results found :-/\n");
} else {
gint i = 0;
/* Iterate, synchronously, the results... */
while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
g_print ("Result [%d]: %s\n",
i++,
tracker_sparql_cursor_get_string (cursor, 0, NULL));
}
g_print ("A total of '%d' results were found\n", i);
g_object_unref (cursor);
}
g_object_unref (connection);
return 0;
}