123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /*
- *openPilot Log - A FOSS Pilot Logbook Application
- *Copyright (C) 2020 Felix Turowsky
- *
- *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 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 <https://www.gnu.org/licenses/>.
- */
- #include "db.h"
- #include "dbinfo.h"
- #include "src/functions/adebug.h"
- /*!
- * \brief Db::iconnect - see Db::connect
- */
- void Db::iconnect()
- {
- const QString driver("QSQLITE");
- if (QSqlDatabase::isDriverAvailable(driver)) {
- QDir directory("data");
- QString databaseLocation = directory.filePath("logbook.db");
- QSqlDatabase db = QSqlDatabase::addDatabase(driver);
- db.setDatabaseName(databaseLocation);
- if (!db.open()) {
- DEB("DatabaseConnect - ERROR: " << db.lastError().text());
- } else {
- DEB("Database connection established.");
- //Enable foreign key restrictions
- QSqlQuery query("PRAGMA foreign_keys = ON;");
- }
- } else {
- DEB("DatabaseConnect - ERROR: no driver " << driver << " available");
- }
- }
- void Db::idisconnect()
- {
- auto db = Db::Database();
- db.close();
- db.removeDatabase(db.connectionName());
- DEB("Database connection closed.");
- }
- QSqlDatabase Db::iDatabase()
- {
- auto db = QSqlDatabase::database("qt_sql_default_connection");
- return db;
- }
- /*!
- * \brief Db::iexists - see Db::exists
- */
- bool Db::iexists(QString column, QString table, QString checkColumn, QString value,
- Db::matchType match)
- {
- bool output = false;
- QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
- switch (match) {
- case Db::exactMatch:
- statement += " = '" + value + QLatin1Char('\'');
- break;
- case Db::partialMatch:
- value.append(QLatin1Char('%'));
- value.prepend(QLatin1Char('%'));
- statement.append(" LIKE '" + value + QLatin1Char('\''));
- break;
- }
- QSqlQuery q(statement);
- q.exec();
- if (!q.first()) {
- DEB("No result found. Check Query and Error.");
- DEB("Error: " << q.lastError().text());
- } else {
- output = true;
- if (q.next()) {
- DEB("More than one result in Database for your query");
- }
- }
- // Debug:
- q.first();
- q.previous();
- while (q.next()) {
- DEB("Query result: " << q.value(0).toString());
- }
- // end of Debug
- return output;
- }
- /*!
- * \brief Db::isingleSelect - see Db::singleSelect
- */
- QString Db::isingleSelect(QString column, QString table, QString checkColumn, QString value,
- Db::matchType match)
- {
- QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
- QString result;
- switch (match) {
- case Db::exactMatch:
- statement += " = '" + value + QLatin1Char('\'');
- break;
- case Db::partialMatch:
- value.append(QLatin1Char('%'));
- value.prepend(QLatin1Char('%'));
- statement.append(" LIKE '" + value + QLatin1Char('\''));
- break;
- }
- QSqlQuery q(statement);
- q.exec();
- if (!q.first()) {
- DEB("No result found. Check Query and Error.");
- DEB("Error: " << q.lastError().text());
- return QString();
- } else {
- result.append(q.value(0).toString());
- if (q.next()) {
- DEB("More than one result in Database for your query");
- }
- return result;
- }
- }
- /*!
- * \brief Db::imultiSelect - see Db::multiSelect
- */
- QVector<QString> Db::imultiSelect(QVector<QString> columns, QString table, QString checkColumn,
- QString value, Db::matchType match)
- {
- QString statement = "SELECT ";
- for (const auto &column : columns) {
- statement.append(column);
- if (column != columns.last()) {
- statement.append(QLatin1String(", "));
- }
- }
- statement.append(" FROM " + table + " WHERE " + checkColumn);
- switch (match) {
- case Db::exactMatch:
- statement += " = '" + value + QLatin1Char('\'');
- break;
- case Db::partialMatch:
- value.append(QLatin1Char('%'));
- value.prepend(QLatin1Char('%'));
- statement.append(" LIKE '" + value + QLatin1Char('\''));
- break;
- }
- QSqlQuery q(statement);
- q.exec();
- if (!q.first()) {
- DEB("No result found. Check Query and Error.");
- DEB("Error: " << q.lastError().text());
- return QVector<QString>();
- } else {
- q.first();
- q.previous();
- QVector<QString> result;
- while (q.next()) {
- for (int i = 0; i < columns.size() ; i++) {
- result.append(q.value(i).toString());
- }
- }
- return result;
- }
- }
- /*!
- * \brief Db::imultiSelect - see Db::multiSelect
- */
- QVector<QString> Db::imultiSelect(QVector<QString> columns, QString table)
- {
- QString statement = "SELECT ";
- for (const auto &column : columns) {
- statement.append(column);
- if (column != columns.last()) {
- statement.append(QLatin1String(", "));
- }
- }
- statement.append(" FROM " + table);
- QSqlQuery q(statement);
- q.exec();
- if (!q.first()) {
- DEB("No result found. Check Query and Error.");
- DEB("Error: " << q.lastError().text());
- return QVector<QString>();
- } else {
- q.first();
- q.previous();
- QVector<QString> result;
- while (q.next()) {
- for (int i = 0; i < columns.size() ; i++) {
- result.append(q.value(i).toString());
- }
- }
- return result;
- }
- }
- /*!
- * \brief Db::isingleUpdate - see Db::singleUpdate
- */
- bool Db::isingleUpdate(QString table, QString column, QString value, QString checkColumn,
- QString checkvalue, Db::matchType match)
- {
- QString statement = "UPDATE " + table;
- statement.append(QLatin1String(" SET ") + column + QLatin1String(" = '") + value);
- statement.append(QLatin1String("' WHERE "));
- switch (match) {
- case Db::exactMatch:
- statement.append(checkColumn + " = '" + checkvalue + QLatin1Char('\''));
- break;
- case Db::partialMatch:
- value.append(QLatin1Char('%'));
- value.prepend(QLatin1Char('%'));
- statement.append(checkColumn + " LIKE '" + checkvalue + QLatin1Char('\''));
- break;
- }
- QSqlQuery q(statement);
- q.exec();
- QString error = q.lastError().text();
- if (error.length() > 1) {
- DEB("Errors have occured: " << error);
- return false;
- } else {
- DEB("Success!");
- return true;
- }
- }
- /*!
- * \brief Db::icustomQuery - see Db::customQuery
- */
- QVector<QString> Db::icustomQuery(QString query, int returnValues)
- {
- QSqlQuery q(query);
- q.exec();
- if (!q.first()) {
- DEB("No result found. Check Query and Error.");
- DEB("Error: " << q.lastError().text());
- return QVector<QString>();
- } else {
- q.first();
- q.previous();
- QVector<QString> result;
- while (q.next()) {
- for (int i = 0; i < returnValues ; i++) {
- result.append(q.value(i).toString());
- }
- }
- return result;
- }
- }
|