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)) "
    "}";

  /* Initialize GLib type system */
  g_type_init ();

  /* Do NOT get a direct connection if you're going to
   * do some write operation in the Store */
  connection = tracker_sparql_connection_get (&error);
  if (!connection) {
    /* Some error happened getting the connection, not good */
    g_error ("Couldn't obtain a connection to the "
             "Tracker Store: '%s'",
             error ? error->message : "unknown error");
  }

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

  g_object_unref (connection);

  return 0;
}