2
* Copyright (c) 2011, 2012 Red Hat, Inc.
4
* Gnome Documents is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by the
6
* Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* Gnome Documents is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with Gnome Documents; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Author: Cosimo Cecchi <cosimoc@redhat.com>
24
#include "gd-miner-tracker.h"
27
_tracker_utils_format_into_graph (const gchar *graph)
29
return (graph != NULL) ? g_strdup_printf ("INTO <%s> ", graph) : g_strdup ("");
33
gd_miner_tracker_sparql_connection_ensure_resource (TrackerSparqlConnection *connection,
34
GCancellable *cancellable,
37
const gchar *identifier,
41
GString *select, *insert, *inner;
44
TrackerSparqlCursor *cursor;
50
gchar *key = NULL, *val = NULL;
52
/* build the inner query with all the classes */
53
va_start (args, class);
54
inner = g_string_new (NULL);
56
for (arg = class; arg != NULL; arg = va_arg (args, const gchar *))
57
g_string_append_printf (inner, " a %s; ", arg);
59
g_string_append_printf (inner, "nao:identifier \"%s\"", identifier);
63
/* query if such a resource is already in the DB */
64
select = g_string_new (NULL);
65
g_string_append_printf (select,
66
"SELECT ?urn WHERE { ?urn %s }", inner->str);
68
cursor = tracker_sparql_connection_query (connection,
72
g_string_free (select, TRUE);
77
res = tracker_sparql_cursor_next (cursor, cancellable, error);
84
/* return the found resource */
85
retval = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
86
g_debug ("Found resource in the store: %s", retval);
90
/* not found, create the resource */
91
insert = g_string_new (NULL);
92
graph_str = _tracker_utils_format_into_graph (graph);
94
g_string_append_printf (insert, "INSERT %s { _:res %s }",
95
graph_str, inner->str);
97
g_string_free (inner, TRUE);
100
tracker_sparql_connection_update_blank (connection, insert->str,
101
G_PRIORITY_DEFAULT, NULL, error);
103
g_string_free (insert, TRUE);
108
/* the result is an "aaa{ss}" variant */
109
g_variant_get (insert_res, "aaa{ss}", &iter);
110
g_variant_iter_next (iter, "aa{ss}", &iter);
111
g_variant_iter_next (iter, "a{ss}", &iter);
112
g_variant_iter_next (iter, "{ss}", &key, &val);
114
g_variant_iter_free (iter);
115
g_variant_unref (insert_res);
117
if (g_strcmp0 (key, "res") == 0)
127
g_debug ("Created a new resource: %s", retval);
130
g_clear_object (&cursor);
135
gd_miner_tracker_sparql_connection_insert_or_replace_triple (TrackerSparqlConnection *connection,
136
GCancellable *cancellable,
139
const gchar *resource,
140
const gchar *property_name,
141
const gchar *property_value)
145
gboolean retval = TRUE;
147
graph_str = _tracker_utils_format_into_graph (graph);
149
insert = g_string_new (NULL);
150
g_string_append_printf
152
"INSERT OR REPLACE %s { <%s> a nie:InformationElement ; %s \"%s\" }",
153
graph_str, resource, property_name, property_value);
155
g_debug ("Insert or replace triple: query %s", insert->str);
157
tracker_sparql_connection_update (connection, insert->str,
158
G_PRIORITY_DEFAULT, cancellable,
161
g_string_free (insert, TRUE);
172
gd_miner_tracker_sparql_connection_set_triple (TrackerSparqlConnection *connection,
173
GCancellable *cancellable,
176
const gchar *resource,
177
const gchar *property_name,
178
const gchar *property_value)
181
gboolean retval = TRUE;
183
delete = g_string_new (NULL);
184
g_string_append_printf
186
"DELETE { <%s> %s ?val } WHERE { <%s> %s ?val }", resource,
187
property_name, resource, property_name);
189
tracker_sparql_connection_update (connection, delete->str,
190
G_PRIORITY_DEFAULT, cancellable,
193
g_string_free (delete, TRUE);
201
gd_miner_tracker_sparql_connection_insert_or_replace_triple (connection,
204
property_name, property_value);