[tracker/tracker-cmd: 8/10] tracker: Merged utils/tracker-sql/tracker-sql into 'tracker sql ...'



commit cbf0bb7f66e08c7fb9122e90777d964d4c1bf5a4
Author: Martyn Russell <martyn lanedo com>
Date:   Sat Oct 4 16:34:22 2014 +0100

    tracker: Merged utils/tracker-sql/tracker-sql into 'tracker sql ...'

 configure.ac                    |    1 -
 docs/manpages/Makefile.am       |    1 +
 docs/manpages/tracker-sparql.1  |    1 +
 docs/manpages/tracker-sql.1     |   48 ++++++++
 src/tracker/Makefile.am         |    2 +
 src/tracker/tracker-main.c      |    2 +
 src/tracker/tracker-sql.c       |  252 +++++++++++++++++++++++++++++++++++++++
 src/tracker/tracker-sql.h       |   25 ++++
 utils/Makefile.am               |    1 -
 utils/tracker-sql/.gitignore    |    1 -
 utils/tracker-sql/Makefile.am   |   16 ---
 utils/tracker-sql/tracker-sql.c |  204 -------------------------------
 12 files changed, 331 insertions(+), 223 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index ac9cfb7..8f477e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2702,7 +2702,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 e8006a6..1651d77 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 2e5df8c..f696e86 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 0000000..9fda4d5
--- /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 99e1c34..5652f01 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 c3740b5..0f8f8c7 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 0000000..f4e3703
--- /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 0000000..3e961ea
--- /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 b3a821e..b3fd59b 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]