summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartyn Russell <martyn@lanedo.com>2014-10-04 16:34:22 +0100
committerMartyn Russell <martyn@lanedo.com>2014-12-10 15:51:35 +0000
commit56f1ae7310273284d32f305c442009e074463f00 (patch)
tree21daca48d47a61360652e9cd252a20df2b314b0a
parentb193d8e7d8512845351796b7ee30491f50089704 (diff)
downloadtracker-56f1ae7310273284d32f305c442009e074463f00.tar.gz
tracker: Merged utils/tracker-sql/tracker-sql into 'tracker sql ...'
-rw-r--r--configure.ac1
-rw-r--r--docs/manpages/Makefile.am1
-rw-r--r--docs/manpages/tracker-sparql.11
-rw-r--r--docs/manpages/tracker-sql.148
-rw-r--r--src/tracker/Makefile.am2
-rw-r--r--src/tracker/tracker-main.c2
-rw-r--r--src/tracker/tracker-sql.c252
-rw-r--r--src/tracker/tracker-sql.h25
-rw-r--r--utils/Makefile.am1
-rw-r--r--utils/tracker-sql/.gitignore1
-rw-r--r--utils/tracker-sql/Makefile.am16
-rw-r--r--utils/tracker-sql/tracker-sql.c204
12 files changed, 331 insertions, 223 deletions
diff --git a/configure.ac b/configure.ac
index 539679c6a..6ae79fa73 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2731,7 +2731,6 @@ AC_CONFIG_FILES([
utils/data-generators/cc/Makefile
utils/mtp/Makefile
utils/sandbox/Makefile
- utils/tracker-sql/Makefile
utils/tracker-resdump/Makefile
examples/Makefile
examples/libtracker-miner/Makefile
diff --git a/docs/manpages/Makefile.am b/docs/manpages/Makefile.am
index e8006a63c..1651d7727 100644
--- a/docs/manpages/Makefile.am
+++ b/docs/manpages/Makefile.am
@@ -9,6 +9,7 @@ common = \
tracker-daemon.1 \
tracker-search.1 \
tracker-sparql.1 \
+ tracker-sql.1 \
tracker-status.1 \
tracker-store.1 \
tracker-tag.1 \
diff --git a/docs/manpages/tracker-sparql.1 b/docs/manpages/tracker-sparql.1
index 2e5df8cde..f696e86f9 100644
--- a/docs/manpages/tracker-sparql.1
+++ b/docs/manpages/tracker-sparql.1
@@ -356,6 +356,7 @@ $ tracker sparql -q "SELECT ?image ?date WHERE {
.fi
.SH SEE ALSO
+.BR tracker-sql (1),
.BR tracker-store (1),
.BR tracker-info (1).
.TP
diff --git a/docs/manpages/tracker-sql.1 b/docs/manpages/tracker-sql.1
new file mode 100644
index 000000000..9fda4d522
--- /dev/null
+++ b/docs/manpages/tracker-sql.1
@@ -0,0 +1,48 @@
+.TH tracker-sql 1 "October 2014" GNU "User Commands"
+
+.SH NAME
+tracker-sql \- Use SQL to query the Tracker databases.
+
+.SH SYNOPSIS
+.nf
+\fBtracker sql\fR \-q <\fIsql\fR> | \-f <\fIfile\fR>
+.fi
+
+.SH DESCRIPTION
+This command allows probing of the current database. When using
+commands like \fBtracker sparql\fR, the SPARQL used is translated into
+SQL before being run on the database. This allows direct use of the
+database using SQL avoiding the SPARQL engine entirely.
+
+The caller can run a query two ways, either by providing a \fIfile\fR
+with the query or by providing a string with the \fIsql\fR query.
+
+The \fIfile\fR argument can be either a local path or a URI. It also
+does not have to be an absolute path.
+
+.SH OPTIONS
+.TP
+.B \-f, \-\-file\fR=<\fIfile\fR>
+Use a \fIfile\fR with SPARQL content to query. Don't forget to end all
+queries with a semicolon (;) and also to use quotes around table
+names. The quotes are important because most tables are named after
+ontology classes like "nfo:Document" and queries will fail without the
+quotes.
+.TP
+.B \-q, \-\-query\fR=<\fIsql\fR>
+Use a \fIsql\fR string to query the database with.
+
+.SH EXAMPLES
+.TP
+Show first 10 "nfo:Document" entries where the TOC is not NULL:
+.BR
+.nf
+$ tracker sql -q 'SELECT * FROM "nfo:Document" WHERE "nfo:tableOfContents" NOT NULL LIMIT 10;'
+.fi
+
+.SH SEE ALSO
+.BR tracker-sparql (1),
+.BR tracker-store (1),
+.BR tracker-info (1).
+.TP
+.BR http://en.wikipedia.org/wiki/SQL
diff --git a/src/tracker/Makefile.am b/src/tracker/Makefile.am
index 99e1c349d..5652f017d 100644
--- a/src/tracker/Makefile.am
+++ b/src/tracker/Makefile.am
@@ -39,6 +39,8 @@ tracker_SOURCES = \
tracker-search.h \
tracker-sparql.c \
tracker-sparql.h \
+ tracker-sql.c \
+ tracker-sql.h \
tracker-status.c \
tracker-status.h \
tracker-tag.c \
diff --git a/src/tracker/tracker-main.c b/src/tracker/tracker-main.c
index c3740b52e..0f8f8c712 100644
--- a/src/tracker/tracker-main.c
+++ b/src/tracker/tracker-main.c
@@ -35,6 +35,7 @@
#include "tracker-reset.h"
#include "tracker-search.h"
#include "tracker-sparql.h"
+#include "tracker-sql.h"
#include "tracker-status.h"
#include "tracker-tag.h"
@@ -99,6 +100,7 @@ static struct cmd_struct commands[] = {
{ "reset", tracker_reset, NEED_NOTHING, N_("Reset the index, configuration or replay journal") },
{ "search", tracker_search, NEED_WORK_TREE, N_("Search the index by RDF class") },
{ "sparql", tracker_sparql, NEED_WORK_TREE, N_("Query and update the index using SPARQL or search and list ontology in use") },
+ { "sql", tracker_sql, NEED_WORK_TREE, N_("Query the database using SQL at the lowest level") },
{ "status", tracker_status, NEED_NOTHING, N_("Show the index status for the working tree") },
{ "tag", tracker_tag, NEED_WORK_TREE, N_("Create, list or delete tags and related content") },
{ "version", tracker_version, NEED_NOTHING, N_("Show the license and version in use") },
diff --git a/src/tracker/tracker-sql.c b/src/tracker/tracker-sql.c
new file mode 100644
index 000000000..f4e37039b
--- /dev/null
+++ b/src/tracker/tracker-sql.c
@@ -0,0 +1,252 @@
+/*
+ * Copyright (C) 2010, Nokia
+ * Copyright (C) 2014, Lanedo <martyn@lanedo.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <locale.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include <libtracker-common/tracker-common.h>
+#include <libtracker-data/tracker-data.h>
+
+#include "tracker-sql.h"
+
+#define SQL_OPTIONS_ENABLED() \
+ (file || \
+ query)
+
+static gchar *file;
+static gchar *query;
+
+static GOptionEntry entries[] = {
+ { "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
+ "Path to use to run a query from file",
+ "FILE",
+ },
+ { "query", 'q', 0, G_OPTION_ARG_STRING, &query,
+ "SQL query",
+ "SQL",
+ },
+ { NULL }
+};
+
+static int
+sql_by_file (void)
+{
+ GError *error = NULL;
+ gchar *path_in_utf8;
+ gsize size;
+
+ path_in_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, &error);
+ if (error) {
+ g_printerr ("%s:'%s', %s\n",
+ _("Could not get UTF-8 path from path"),
+ file,
+ error->message);
+ g_error_free (error);
+
+ return EXIT_FAILURE;
+ }
+
+ g_file_get_contents (path_in_utf8, &query, &size, &error);
+ if (error) {
+ g_printerr ("%s:'%s', %s\n",
+ _("Could not read file"),
+ path_in_utf8,
+ error->message);
+ g_error_free (error);
+ g_free (path_in_utf8);
+
+ return EXIT_FAILURE;
+ }
+
+ g_free (path_in_utf8);
+
+ return EXIT_SUCCESS;
+}
+
+static int
+sql_by_query (void)
+{
+ TrackerDBInterface *iface;
+ TrackerDBStatement *stmt;
+ TrackerDBCursor *cursor = NULL;
+ GError *error = NULL;
+ gboolean first_time = FALSE;
+ gint n_rows = 0;
+
+ if (!tracker_data_manager_init (0,
+ NULL,
+ &first_time,
+ FALSE,
+ FALSE,
+ 100,
+ 100,
+ NULL,
+ NULL,
+ NULL,
+ &error)) {
+ g_printerr ("%s: %s\n",
+ _("Failed to initialize data manager"),
+ error->message);
+ g_error_free (error);
+ return EXIT_FAILURE;
+ }
+
+ g_print ("--------------------------------------------------\n");
+ g_print ("\n\n");
+
+ iface = tracker_db_manager_get_db_interface ();
+
+ stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_NONE, &error, "%s", query);
+
+ if (stmt) {
+ cursor = tracker_db_statement_start_cursor (stmt, &error);
+ }
+
+ if (error) {
+ g_printerr ("%s: %s\n",
+ _("Could not run query"),
+ error->message);
+ g_error_free (error);
+
+ return EXIT_FAILURE;
+ }
+
+ g_print ("%s:\n", _("Results"));
+
+ while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
+ guint i;
+
+ for (i = 0; i < tracker_db_cursor_get_n_columns (cursor); i++) {
+ const gchar *str;
+
+ if (i)
+ g_print (" | ");
+
+ str = tracker_db_cursor_get_string (cursor, i, NULL);
+ if (str) {
+ g_print ("%s", str);
+ } else {
+ g_print ("(null)");
+ }
+ }
+
+ g_print ("\n");
+
+ n_rows++;
+ }
+
+ if (error) {
+ g_printerr ("%s: %s\n",
+ _("Could not run query"),
+ error->message);
+ g_error_free (error);
+
+ return EXIT_FAILURE;
+ }
+
+ if (n_rows == 0) {
+ g_print ("%s\n", _("Empty result set"));
+ }
+
+ return EXIT_SUCCESS;
+}
+
+static int
+sql_run (void)
+{
+ if (file) {
+ return sql_by_file ();
+ }
+
+ if (query) {
+ return sql_by_query ();
+ }
+
+ /* All known options have their own exit points */
+ g_warn_if_reached ();
+
+ return EXIT_SUCCESS;
+}
+
+static int
+sql_run_default (void)
+{
+ GOptionContext *context;
+ gchar *help;
+
+ context = g_option_context_new (NULL);
+ g_option_context_add_main_entries (context, entries, NULL);
+ help = g_option_context_get_help (context, TRUE, NULL);
+ g_option_context_free (context);
+ g_printerr ("%s\n", help);
+ g_free (help);
+
+ return EXIT_FAILURE;
+}
+
+static gboolean
+sql_options_enabled (void)
+{
+ return SQL_OPTIONS_ENABLED ();
+}
+
+int
+tracker_sql (int argc, const char **argv)
+{
+ GOptionContext *context;
+ GError *error = NULL;
+ const gchar *failed;
+
+ context = g_option_context_new (NULL);
+ g_option_context_add_main_entries (context, entries, NULL);
+
+ argv[0] = "tracker sql";
+
+ if (!g_option_context_parse (context, &argc, (char***) &argv, &error)) {
+ g_printerr ("%s, %s\n", _("Unrecognized options"), error->message);
+ g_error_free (error);
+ g_option_context_free (context);
+ return EXIT_FAILURE;
+ }
+
+ g_option_context_free (context);
+
+ if (file && query) {
+ failed = _("File and query can not be used together");
+ } else {
+ failed = NULL;
+ }
+
+ if (failed) {
+ g_printerr ("%s\n\n", failed);
+ return EXIT_FAILURE;
+ }
+
+ if (sql_options_enabled ()) {
+ return sql_run ();
+ }
+
+ return sql_run_default ();
+}
diff --git a/src/tracker/tracker-sql.h b/src/tracker/tracker-sql.h
new file mode 100644
index 000000000..3e961ea95
--- /dev/null
+++ b/src/tracker/tracker-sql.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2014, Lanedo <martyn@lanedo.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __TRACKER_SQL_H__
+#define __TRACKER_SQL_H__
+
+int tracker_sql (int argc, const char **argv);
+
+#endif /* __TRACKER_SQL_H__ */
diff --git a/utils/Makefile.am b/utils/Makefile.am
index b3a821e6f..b3fd59bd3 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -3,7 +3,6 @@ SUBDIRS = \
ontology \
data-generators \
mtp \
- tracker-sql \
sandbox
if HAVE_TRACKER_RESDUMP
diff --git a/utils/tracker-sql/.gitignore b/utils/tracker-sql/.gitignore
deleted file mode 100644
index ec536046a..000000000
--- a/utils/tracker-sql/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-tracker-sql
diff --git a/utils/tracker-sql/Makefile.am b/utils/tracker-sql/Makefile.am
deleted file mode 100644
index 7d519302b..000000000
--- a/utils/tracker-sql/Makefile.am
+++ /dev/null
@@ -1,16 +0,0 @@
-noinst_PROGRAMS = tracker-sql
-
-AM_CPPFLAGS = \
- $(BUILD_CFLAGS) \
- -DLOCALEDIR=\""$(localedir)"\" \
- -I$(top_srcdir)/src \
- $(TRACKER_UTILS_CFLAGS)
-
-LDADD = \
- $(BUILD_LIBS) \
- $(top_builddir)/src/libtracker-data/libtracker-data.la \
- $(top_builddir)/src/libtracker-common/libtracker-common.la \
- $(top_builddir)/src/libtracker-sparql-backend/libtracker-sparql-@TRACKER_API_VERSION@.la \
- $(TRACKER_UTILS_LIBS)
-
-tracker_sql_SOURCES = tracker-sql.c
diff --git a/utils/tracker-sql/tracker-sql.c b/utils/tracker-sql/tracker-sql.c
deleted file mode 100644
index 654347b42..000000000
--- a/utils/tracker-sql/tracker-sql.c
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * Copyright (C) 2010, Nokia
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-
-#include "config.h"
-
-#include <string.h>
-#include <locale.h>
-
-#include <glib.h>
-#include <glib/gi18n.h>
-
-#include <libtracker-data/tracker-data.h>
-#include <libtracker-sparql/tracker-sparql.h>
-
-static gchar *file;
-static gchar *query;
-
-static GOptionEntry entries[] = {
- { "file", 'f', 0, G_OPTION_ARG_FILENAME, &file,
- "Path to use to run a query from file",
- "FILE",
- },
- { "query", 'q', 0, G_OPTION_ARG_STRING, &query,
- "SQL query",
- "SQL",
- },
- { NULL }
-};
-
-int
-main (int argc, char **argv)
-{
- GOptionContext *context;
- GError *error = NULL;
- const gchar *error_message;
- TrackerDBInterface *iface;
- TrackerDBStatement *stmt;
- TrackerDBCursor *cursor = NULL;
-
- setlocale (LC_ALL, "");
-
- bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
- bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- textdomain (GETTEXT_PACKAGE);
-
- context = g_option_context_new (_("- Query or update using SQL"));
-
- g_option_context_add_main_entries (context, entries, NULL);
- g_option_context_parse (context, &argc, &argv, NULL);
-
- if (!file && !query) {
- error_message = _("An argument must be supplied");
- } else if (file && query) {
- error_message = _("File and query can not be used together");
- } else {
- error_message = NULL;
- }
-
- if (error_message) {
- gchar *help;
-
- g_printerr ("%s\n\n", error_message);
-
- help = g_option_context_get_help (context, TRUE, NULL);
- g_option_context_free (context);
- g_printerr ("%s", help);
- g_free (help);
-
- return EXIT_FAILURE;
- }
-
- g_option_context_free (context);
-
- if (file) {
- gchar *path_in_utf8;
- gsize size;
-
- path_in_utf8 = g_filename_to_utf8 (file, -1, NULL, NULL, &error);
- if (error) {
- g_printerr ("%s:'%s', %s\n",
- _("Could not get UTF-8 path from path"),
- file,
- error->message);
- g_error_free (error);
-
- return EXIT_FAILURE;
- }
-
- g_file_get_contents (path_in_utf8, &query, &size, &error);
- if (error) {
- g_printerr ("%s:'%s', %s\n",
- _("Could not read file"),
- path_in_utf8,
- error->message);
- g_error_free (error);
- g_free (path_in_utf8);
-
- return EXIT_FAILURE;
- }
-
- g_free (path_in_utf8);
- }
-
- if (query) {
- gboolean first_time = FALSE;
- gint n_rows = 0;
-
- if (!tracker_data_manager_init (0,
- NULL,
- &first_time,
- FALSE,
- FALSE,
- 100,
- 100,
- NULL,
- NULL,
- NULL,
- &error)) {
- g_printerr ("%s: %s\n",
- _("Failed to initialize data manager"),
- error->message);
- g_error_free (error);
- return EXIT_FAILURE;
- }
-
- g_print ("--------------------------------------------------\n");
- g_print ("\n\n");
-
- iface = tracker_db_manager_get_db_interface ();
-
- stmt = tracker_db_interface_create_statement (iface, TRACKER_DB_STATEMENT_CACHE_TYPE_NONE, &error, "%s", query);
-
- if (stmt) {
- cursor = tracker_db_statement_start_cursor (stmt, &error);
- }
-
- if (error) {
- g_printerr ("%s: %s\n",
- _("Could not run query"),
- error->message);
- g_error_free (error);
-
- return EXIT_FAILURE;
- }
-
- g_print ("%s:\n", _("Results"));
-
- while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
- guint i;
-
- for (i = 0; i < tracker_db_cursor_get_n_columns (cursor); i++) {
- const gchar *str;
-
- if (i)
- g_print (" | ");
-
- str = tracker_db_cursor_get_string (cursor, i, NULL);
- if (str) {
- g_print ("%s", str);
- } else {
- g_print ("(null)");
- }
- }
-
- g_print ("\n");
-
- n_rows++;
- }
-
- if (error) {
- g_printerr ("%s: %s\n",
- _("Could not run query"),
- error->message);
- g_error_free (error);
-
- return EXIT_FAILURE;
- }
-
- if (n_rows == 0) {
- g_print ("%s\n", _("Empty result set"));
- }
- }
-
- g_print ("\n");
-
- return EXIT_SUCCESS;
-}