Updating the Store

In order to perform updates in the store, a new TrackerSparqlConnection object must be acquired with tracker_sparql_connection_get. Note that you MUST NOT use a specific direct-access connection obtained with tracker_sparql_connection_get_direct, as the direct-access method only supports read-only queries.

Once a proper connection object has been acquired, the update can be launched either synchronously (tracker_sparql_connection_update) or asynchronously (tracker_sparql_connection_update_async). If launched asynchronously, the result of the operation can be obtained with tracker_sparql_connection_update_finish.

Once you no longer need the connection, remember to call g_object_unref for the TrackerSparqlConnection.

The following program shows how a synchronous update can be done to the store:

#include <tracker-sparql.h>

int main (int argc, const char **argv)
{
  GError *error = NULL;
  TrackerSparqlConnection *connection;
  const gchar *query =
    "INSERT { "
    "  _:tag a nao:Tag ; "
    "        nao:prefLabel 'mylabel' . "
    "} WHERE { "
    "  OPTIONAL { "
    "    ?tag a nao:Tag ; "
    "    nao:prefLabel 'mylabel' "
    "  } . "
    "FILTER (!bound(?tag)) "
    "}";

  /* Do NOT get a direct connection if you're going to do some write
   * operation in the Store. The NULL represents a possible
   * GCancellable.
   */
  connection = tracker_sparql_connection_get (NULL, &error);
  if (!connection) {
    g_printerr ("Couldn't obtain a connection to the Tracker store: %s",
                error ? error->message : "unknown error");
    g_clear_error (&error);

    return 1;
  }

  /* Run a synchronous update query */
  tracker_sparql_connection_update (connection,
                                            query,
                                            G_PRIORITY_DEFAULT,
                                            NULL,
                                            &error);
  if (error) {
    /* Some error happened performing the query, not good */
    g_printerr ("Couldn't update the Tracker store: %s",
                error ? error->message : "unknown error");

    g_clear_error (&error);
    g_object_unref (connection);

    return 1;
  }

  g_object_unref (connection);

  return 0;
}