-
Notifications
You must be signed in to change notification settings - Fork 74
v0.2.48..v0.2.49 changeset WordCountWriter.cpp
Garret Voltz edited this page Oct 2, 2019
·
1 revision
diff --git a/hoot-core/src/main/cpp/hoot/core/io/WordCountWriter.cpp b/hoot-core/src/main/cpp/hoot/core/io/WordCountWriter.cpp
new file mode 100644
index 0000000..8cd475d
--- /dev/null
+++ b/hoot-core/src/main/cpp/hoot/core/io/WordCountWriter.cpp
@@ -0,0 +1,89 @@
+/*
+ * This file is part of Hootenanny.
+ *
+ * Hootenanny 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * --------------------------------------------------------------------
+ *
+ * The following copyright notices are generated automatically. If you
+ * have a new notice to add, please use the format:
+ * " * @copyright Copyright ..."
+ * This will properly maintain the copyright information. DigitalGlobe
+ * copyrights will be updated automatically.
+ *
+ * @copyright Copyright (C) 2015, 2017, 2018, 2019 DigitalGlobe (http://www.digitalglobe.com/)
+ */
+
+#include "WordCountWriter.h"
+
+// hoot
+#include <hoot/core/io/WordCount.h>
+#include <hoot/core/util/DbUtils.h>
+#include <hoot/core/util/HootException.h>
+#include <hoot/core/util/Log.h>
+
+// Qt
+#include <QtSql/QSqlDatabase>
+#include <QSqlError>
+#include <QSqlQuery>
+
+namespace hoot
+{
+
+bool ascendingWords(const WordCount& w1, const WordCount& w2)
+{
+ return w1.word < w2.word;
+}
+
+bool descendingCount(const WordCount& w1, const WordCount& w2)
+{
+ return w1.count > w2.count;
+}
+
+void WordCountWriter::write(QString basePath, QVector<WordCount> words)
+{
+ LOG_INFO("Start writing");
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
+ db.setDatabaseName(basePath);
+ if (!db.open())
+ {
+ throw HootException("Error opening database: " + basePath);
+ }
+
+ DbUtils::execNoPrepare(db, "CREATE TABLE words (word TEXT PRIMARY KEY, count INT)");
+ DbUtils::execNoPrepare(db, "BEGIN");
+
+ QSqlQuery q(db);
+ if (q.prepare("INSERT INTO words (word, count) VALUES(:word, :count)") == false)
+ {
+ throw HootException(QString("Error preparing query: %1").arg(q.lastError().text()));
+ }
+ for (int i = 0; i < words.size(); i++)
+ {
+ q.bindValue(":word", words[i].word);
+ q.bindValue(":count", words[i].count);
+
+ if (q.exec() == false)
+ {
+ QString err = QString("Error executing query: %1 (%2)").arg(q.executedQuery()).
+ arg(q.lastError().text());
+ throw HootException(err);
+ }
+ }
+
+ DbUtils::execNoPrepare(db, "COMMIT");
+ LOG_INFO("Committed");
+}
+
+}