tracker-0.16.2/utils/tracker-sql/tracker-sql.c

No issues found

  1 /*
  2  * Copyright (C) 2010, Nokia
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 
 21 #include "config.h"
 22 
 23 #include <string.h>
 24 #include <locale.h>
 25 
 26 #include <glib.h>
 27 #include <glib/gi18n.h>
 28 
 29 #include <libtracker-common/tracker-ontologies.h>
 30 #include <libtracker-data/tracker-data-manager.h>
 31 #include <libtracker-data/tracker-data-query.h>
 32 #include <libtracker-data/tracker-data-update.h>
 33 #include <libtracker-data/tracker-sparql-query.h>
 34 
 35 static gchar *file;
 36 static gchar *query;
 37 
 38 static GOptionEntry   entries[] = {
 39 	{ "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
 40 	  "Path to use to run a query from file",
 41 	  "FILE",
 42 	},
 43 	{ "query", 'q', 0, G_OPTION_ARG_STRING, &query,
 44 	  "SQL query",
 45 	  "SQL",
 46 	},
 47 	{ NULL }
 48 };
 49 
 50 int
 51 main (int argc, char **argv)
 52 {
 53 	GOptionContext *context;
 54 	GError *error = NULL;
 55 	const gchar *error_message;
 56 	TrackerDBInterface *iface;
 57 	TrackerDBStatement *stmt;
 58 	TrackerDBCursor *cursor = NULL;
 59 
 60 	setlocale (LC_ALL, "");
 61 
 62 	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 63 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 64 	textdomain (GETTEXT_PACKAGE);
 65 
 66 	context = g_option_context_new (_("- Query or update using SQL"));
 67 
 68 	g_option_context_add_main_entries (context, entries, NULL);
 69 	g_option_context_parse (context, &argc, &argv, NULL);
 70 
 71 	if (!file && !query) {
 72 		error_message = _("An argument must be supplied");
 73 	} else if (file && query) {
 74 		error_message = _("File and query can not be used together");
 75 	} else {
 76 		error_message = NULL;
 77 	}
 78 
 79 	if (error_message) {
 80 		gchar *help;
 81 
 82 		g_printerr ("%s\n\n", error_message);
 83 
 84 		help = g_option_context_get_help (context, TRUE, NULL);
 85 		g_option_context_free (context);
 86 		g_printerr ("%s", help);
 87 		g_free (help);
 88 
 89 		return EXIT_FAILURE;
 90 	}
 91 
 92 	g_option_context_free (context);
 93 
 94 	if (file) {
 95 		gchar *path_in_utf8;
 96 		gsize size;
 97 
 98 		path_in_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, &error);
 99 		if (error) {
100 			g_printerr ("%s:'%s', %s\n",
101 			            _("Could not get UTF-8 path from path"),
102 			            file,
103 			            error->message);
104 			g_error_free (error);
105 
106 			return EXIT_FAILURE;
107 		}
108 
109 		g_file_get_contents (path_in_utf8, &query, &size, &error);
110 		if (error) {
111 			g_printerr ("%s:'%s', %s\n",
112 			            _("Could not read file"),
113 			            path_in_utf8,
114 			            error->message);
115 			g_error_free (error);
116 			g_free (path_in_utf8);
117 
118 			return EXIT_FAILURE;
119 		}
120 
121 		g_free (path_in_utf8);
122 	}
123 
124 	if (query) {
125 		gboolean first_time = FALSE;
126 		gint n_rows = 0;
127 
128 		if (!tracker_data_manager_init (0,
129 		                                NULL,
130 		                                &first_time,
131 		                                FALSE,
132 		                                FALSE,
133 		                                100,
134 		                                100,
135 		                                NULL,
136 		                                NULL,
137 		                                NULL,
138 		                                &error)) {
139 			g_printerr ("%s: %s\n",
140 			            _("Failed to initialize data manager"),
141 			            error->message);
142 			g_error_free (error);
143 			return EXIT_FAILURE;
144 		}
145 
146 		g_print ("--------------------------------------------------\n");
147 		g_print ("\n\n");
148 
149 		iface = tracker_db_manager_get_db_interface ();
150 
151 		stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_NONE, &error, "%s", query);
152 
153 		if (stmt) {
154 			cursor = tracker_db_statement_start_cursor (stmt, &error);
155 		}
156 
157 		if (error) {
158 			g_printerr ("%s: %s\n",
159 			            _("Could not run query"),
160 			            error->message);
161 			g_error_free (error);
162 
163 			return EXIT_FAILURE;
164 		}
165 
166 		g_print ("%s:\n", _("Results"));
167 
168 		while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
169 			guint i;
170 
171 			for (i =  0; i < tracker_db_cursor_get_n_columns (cursor); i++) {
172 				const gchar *str;
173 
174 				if (i)
175 					g_print (" | ");
176 
177 				str = tracker_db_cursor_get_string (cursor, i, NULL);
178 				if (str) {
179 					g_print ("%s", str);
180 				} else {
181 					g_print ("(null)");
182 				}
183 			}
184 
185 			g_print ("\n");
186 
187 			n_rows++;
188 		}
189 
190 		if (error) {
191 			g_printerr ("%s: %s\n",
192 			            _("Could not run query"),
193 			            error->message);
194 			g_error_free (error);
195 
196 			return EXIT_FAILURE;
197 		}
198 
199 		if (n_rows == 0) {
200 			g_print ("%s\n", _("Empty result set"));
201 		}
202 	}
203 
204 	g_print ("\n");
205 
206 	return EXIT_SUCCESS;
207 }