tracker-0.16.2/src/libtracker-data/tracker-data-query.c

No issues found

  1 /*
  2  * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
  3  * Copyright (C) 2007, Jason Kivlighn <jkivlighn@gmail.com>
  4  * Copyright (C) 2007, Creative Commons <http://creativecommons.org>
  5  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  6  *
  7  * This library is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU Lesser General Public
  9  * License as published by the Free Software Foundation; either
 10  * version 2.1 of the License, or (at your option) any later version.
 11  *
 12  * This library is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  * Lesser General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Lesser General Public
 18  * License along with this library; if not, write to the
 19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 20  * Boston, MA  02110-1301, USA.
 21  */
 22 
 23 #include "config.h"
 24 
 25 #include <string.h>
 26 #include <stdlib.h>
 27 
 28 #include "tracker-class.h"
 29 #include "tracker-data-manager.h"
 30 #include "tracker-data-query.h"
 31 #include "tracker-db-interface-sqlite.h"
 32 #include "tracker-db-manager.h"
 33 #include "tracker-ontologies.h"
 34 #include "tracker-sparql-query.h"
 35 
 36 GPtrArray*
 37 tracker_data_query_rdf_type (gint id)
 38 {
 39 	TrackerDBCursor *cursor = NULL;
 40 	TrackerDBInterface *iface;
 41 	TrackerDBStatement *stmt;
 42 	GPtrArray *ret = NULL;
 43 	GError *error = NULL;
 44 
 45 	iface = tracker_db_manager_get_db_interface ();
 46 
 47 	stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, &error,
 48 	                                              "SELECT (SELECT Uri FROM Resource WHERE ID = \"rdf:type\") "
 49 	                                              "FROM \"rdfs:Resource_rdf:type\" "
 50 	                                              "WHERE ID = ?");
 51 
 52 	if (stmt) {
 53 		tracker_db_statement_bind_int (stmt, 0, id);
 54 		cursor = tracker_db_statement_start_cursor (stmt, &error);
 55 		g_object_unref (stmt);
 56 	}
 57 
 58 	if (cursor) {
 59 
 60 		/* Query is usually a rather small result, but let's try to
 61 		 * avoid reallocs in gptrarray.c as much as possible (this
 62 		 * function is called fairly often) */
 63 
 64 		ret = g_ptr_array_sized_new (20);
 65 		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
 66 			const gchar *class_uri;
 67 			TrackerClass *cl;
 68 
 69 			class_uri = tracker_db_cursor_get_string (cursor, 0, NULL);
 70 			cl = tracker_ontologies_get_class_by_uri (class_uri);
 71 			if (!cl) {
 72 				g_critical ("Unknown class %s", class_uri);
 73 				continue;
 74 			}
 75 			g_ptr_array_add (ret, cl);
 76 		}
 77 		g_object_unref (cursor);
 78 	}
 79 
 80 	if (G_UNLIKELY (error)) {
 81 		g_critical ("Could not query RDF type: %s\n", error->message);
 82 		g_error_free (error);
 83 
 84 		if (ret) {
 85 			g_ptr_array_free (ret, FALSE);
 86 			ret = NULL;
 87 		}
 88 	}
 89 
 90 	return ret;
 91 }
 92 
 93 gint
 94 tracker_data_query_resource_id (const gchar *uri)
 95 {
 96 	TrackerDBCursor *cursor = NULL;
 97 	TrackerDBInterface *iface;
 98 	TrackerDBStatement *stmt;
 99 	GError *error = NULL;
100 	gint id = 0;
101 
102 	g_return_val_if_fail (uri != NULL, 0);
103 
104 	iface = tracker_db_manager_get_db_interface ();
105 
106 	stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_SELECT, &error,
107 	                                              "SELECT ID FROM Resource WHERE Uri = ?");
108 
109 	if (stmt) {
110 		tracker_db_statement_bind_text (stmt, 0, uri);
111 		cursor = tracker_db_statement_start_cursor (stmt, &error);
112 		g_object_unref (stmt);
113 	}
114 
115 	if (cursor) {
116 		if (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
117 			id = tracker_db_cursor_get_int (cursor, 0);
118 		}
119 
120 		g_object_unref (cursor);
121 	}
122 
123 	if (G_UNLIKELY (error)) {
124 		g_critical ("Could not query resource ID: %s\n", error->message);
125 		g_error_free (error);
126 	}
127 
128 	return id;
129 }
130 
131 
132 TrackerDBCursor *
133 tracker_data_query_sparql_cursor (const gchar  *query,
134                                   GError      **error)
135 {
136 	TrackerSparqlQuery *sparql_query;
137 	TrackerDBCursor *cursor;
138 
139 	g_return_val_if_fail (query != NULL, NULL);
140 
141 	sparql_query = tracker_sparql_query_new (query);
142 
143 	cursor = tracker_sparql_query_execute_cursor (sparql_query, FALSE, error);
144 
145 	g_object_unref (sparql_query);
146 
147 	return cursor;
148 }