[tracker/tracker-sparql-qt: 3/3] Add example for libtracker-sparql-qt



commit e9b3b1e0f6be6c04cb4fcbaf8736038b19021d51
Author: Adrien Bustany <abustany gnome org>
Date:   Sun Jan 16 13:31:53 2011 +0200

    Add example for libtracker-sparql-qt

 configure.ac                                       |    1 +
 examples/Makefile.am                               |    4 +
 examples/libtracker-sparql-qt/Makefile.am          |   16 +++
 .../libtracker-sparql-qt/tracker-sparql-qt.cpp     |  133 ++++++++++++++++++++
 4 files changed, 154 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index b271e1c..b713ef7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2195,6 +2195,7 @@ AC_CONFIG_FILES([
 	examples/libtracker-miner/Makefile
 	examples/rss-reader/Makefile
 	examples/class-signal/Makefile
+	examples/libtracker-sparql-qt/Makefile
 ])
 
 AC_OUTPUT
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 0098884..bedc576 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -5,3 +5,7 @@ SUBDIRS =                                              \
 	libtracker-miner                               \
 	rss-reader                                     \
 	class-signal
+
+if HAVE_LIBTRACKER_SPARQL_QT
+SUBDIRS += libtracker-sparql-qt
+endif
diff --git a/examples/libtracker-sparql-qt/Makefile.am b/examples/libtracker-sparql-qt/Makefile.am
new file mode 100644
index 0000000..80c2ba0
--- /dev/null
+++ b/examples/libtracker-sparql-qt/Makefile.am
@@ -0,0 +1,16 @@
+noinst_PROGRAMS = tracker-sparql-qt
+
+AM_CPPFLAGS =                                          \
+	$(BUILD_CFLAGS)                                \
+	-I$(top_srcdir)/src                            \
+	-I$(top_srcdir)/src/libtracker-sparql-qt       \
+	-I$(top_builddir)/src                          \
+	-I$(top_srcdir)/tests/common                   \
+	$(LIBTRACKER_SPARQL_QT_CFLAGS)
+
+LDADD =                                                \
+	$(top_builddir)/src/libtracker-sparql-qt/libtracker-sparql-qt- TRACKER_API_VERSION@.la \
+	$(BUILD_LIBS)                                  \
+	$(LIBTRACKER_SPARQL_QT_LIBS)
+
+tracker_sparql_qt_SOURCES = tracker-sparql-qt.cpp
diff --git a/examples/libtracker-sparql-qt/tracker-sparql-qt.cpp b/examples/libtracker-sparql-qt/tracker-sparql-qt.cpp
new file mode 100644
index 0000000..5c67cf3
--- /dev/null
+++ b/examples/libtracker-sparql-qt/tracker-sparql-qt.cpp
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2011, Adrien Bustany <abustany gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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 <tracker-sparql-qt.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+
+using namespace TrackerSparql;
+
+static void
+usage(const char *argv0)
+{
+	printf("Usage: %s [OPTIONS...]\n\n"
+	       "Options:\n"
+	       "  -h, --help    Show this message\n"
+	       "  -u, --update  Run an update query (use for INSERT and DELETE)\n"
+	       "  -f, --file    Read the query from a file, not from stdin\n",
+	       argv0);
+}
+
+int
+main(int argc, char **argv)
+{
+	int updateFlag = 0;
+	char *filePath = 0;
+
+	{ // Option parsing
+		static struct option options[] = {
+			{"update", no_argument, 0, 'u'},
+			{"help", no_argument, 0, 'h'},
+			{"file", required_argument, 0, 'f'}
+		};
+
+		int c;
+
+		while ((c = getopt_long(argc, argv, "uhf:", options, 0)) != -1) {
+			switch (c) {
+				case 0:
+					break;
+				case 'f':
+					filePath = strdup(optarg);
+					break;
+				case 'h':
+					usage(argv[0]);
+					return 1;
+				case 'u':
+					updateFlag = 1;
+					break;
+				default:
+					return 1;
+			}
+		}
+	} // Option parsing
+
+	QString sparql;
+
+	if (filePath) {
+		QFile file(QString::fromUtf8(filePath));
+		free (filePath);
+
+		if (not file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+			fprintf(stderr, "File %s does not exist or cannot be read\n",
+			                qPrintable(file.fileName()));
+			return 1;
+		}
+
+		sparql = file.readAll();
+
+		file.close();
+	} else {
+		if (optind >= argc) {
+			usage(argv[0]);
+			return 1;
+		}
+
+		sparql = QString::fromUtf8(argv[optind]);
+	}
+
+	Connection connection = Connection::get();
+
+	if (not connection.valid()) {
+		fprintf(stderr, "Couldn't connect to Tracker: %s (Error code %d)\n",
+		                qPrintable(connection.error().message()),
+		                connection.error().code());
+		return 1;
+	}
+
+	if (updateFlag) {
+		connection.update(sparql);
+
+		if (connection.error().valid()) {
+			fprintf(stderr, "Error while running query: %s (Error code %d)\n",
+			                qPrintable(connection.error().message()),
+			                connection.error().code());
+			return 1;
+		}
+	} else {
+		Cursor cursor = connection.query(sparql);
+
+		if (cursor.error().valid()) {
+			fprintf(stderr, "Error while running query: %s (Error code %d)\n",
+			                qPrintable(cursor.error().message()),
+			                cursor.error().code());
+			return 1;
+		}
+
+		while (cursor.next()) {
+			for (int i = 0; i < cursor.nColumns(); ++i) {
+				printf("%s%s", qPrintable(cursor.getString(i)),
+				               (i == cursor.nColumns() - 1) ? "\n" : ",");
+			}
+		}
+	}
+
+	return 0;
+}



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