db.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "db.h"
  19. #include "dbinfo.h"
  20. #include "src/functions/adebug.h"
  21. /*!
  22. * \brief Db::iconnect - see Db::connect
  23. */
  24. void Db::iconnect()
  25. {
  26. const QString driver("QSQLITE");
  27. if (QSqlDatabase::isDriverAvailable(driver)) {
  28. QDir directory("data");
  29. QString databaseLocation = directory.filePath("logbook.db");
  30. QSqlDatabase db = QSqlDatabase::addDatabase(driver);
  31. db.setDatabaseName(databaseLocation);
  32. if (!db.open()) {
  33. DEB("DatabaseConnect - ERROR: " << db.lastError().text());
  34. } else {
  35. DEB("Database connection established.");
  36. //Enable foreign key restrictions
  37. QSqlQuery query("PRAGMA foreign_keys = ON;");
  38. }
  39. } else {
  40. DEB("DatabaseConnect - ERROR: no driver " << driver << " available");
  41. }
  42. }
  43. void Db::idisconnect()
  44. {
  45. auto db = Db::Database();
  46. db.close();
  47. db.removeDatabase(db.connectionName());
  48. DEB("Database connection closed.");
  49. }
  50. QSqlDatabase Db::iDatabase()
  51. {
  52. auto db = QSqlDatabase::database("qt_sql_default_connection");
  53. return db;
  54. }
  55. /*!
  56. * \brief Db::iexists - see Db::exists
  57. */
  58. bool Db::iexists(QString column, QString table, QString checkColumn, QString value,
  59. Db::matchType match)
  60. {
  61. bool output = false;
  62. QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
  63. switch (match) {
  64. case Db::exactMatch:
  65. statement += " = '" + value + QLatin1Char('\'');
  66. break;
  67. case Db::partialMatch:
  68. value.append(QLatin1Char('%'));
  69. value.prepend(QLatin1Char('%'));
  70. statement.append(" LIKE '" + value + QLatin1Char('\''));
  71. break;
  72. }
  73. QSqlQuery q(statement);
  74. q.exec();
  75. if (!q.first()) {
  76. DEB("No result found. Check Query and Error.");
  77. DEB("Error: " << q.lastError().text());
  78. } else {
  79. output = true;
  80. if (q.next()) {
  81. DEB("More than one result in Database for your query");
  82. }
  83. }
  84. // Debug:
  85. q.first();
  86. q.previous();
  87. while (q.next()) {
  88. DEB("Query result: " << q.value(0).toString());
  89. }
  90. // end of Debug
  91. return output;
  92. }
  93. /*!
  94. * \brief Db::isingleSelect - see Db::singleSelect
  95. */
  96. QString Db::isingleSelect(QString column, QString table, QString checkColumn, QString value,
  97. Db::matchType match)
  98. {
  99. QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
  100. QString result;
  101. switch (match) {
  102. case Db::exactMatch:
  103. statement += " = '" + value + QLatin1Char('\'');
  104. break;
  105. case Db::partialMatch:
  106. value.append(QLatin1Char('%'));
  107. value.prepend(QLatin1Char('%'));
  108. statement.append(" LIKE '" + value + QLatin1Char('\''));
  109. break;
  110. }
  111. QSqlQuery q(statement);
  112. q.exec();
  113. if (!q.first()) {
  114. DEB("No result found. Check Query and Error.");
  115. DEB("Error: " << q.lastError().text());
  116. return QString();
  117. } else {
  118. result.append(q.value(0).toString());
  119. if (q.next()) {
  120. DEB("More than one result in Database for your query");
  121. }
  122. return result;
  123. }
  124. }
  125. /*!
  126. * \brief Db::imultiSelect - see Db::multiSelect
  127. */
  128. QVector<QString> Db::imultiSelect(QVector<QString> columns, QString table, QString checkColumn,
  129. QString value, Db::matchType match)
  130. {
  131. QString statement = "SELECT ";
  132. for (const auto &column : columns) {
  133. statement.append(column);
  134. if (column != columns.last()) {
  135. statement.append(QLatin1String(", "));
  136. }
  137. }
  138. statement.append(" FROM " + table + " WHERE " + checkColumn);
  139. switch (match) {
  140. case Db::exactMatch:
  141. statement += " = '" + value + QLatin1Char('\'');
  142. break;
  143. case Db::partialMatch:
  144. value.append(QLatin1Char('%'));
  145. value.prepend(QLatin1Char('%'));
  146. statement.append(" LIKE '" + value + QLatin1Char('\''));
  147. break;
  148. }
  149. QSqlQuery q(statement);
  150. q.exec();
  151. if (!q.first()) {
  152. DEB("No result found. Check Query and Error.");
  153. DEB("Error: " << q.lastError().text());
  154. return QVector<QString>();
  155. } else {
  156. q.first();
  157. q.previous();
  158. QVector<QString> result;
  159. while (q.next()) {
  160. for (int i = 0; i < columns.size() ; i++) {
  161. result.append(q.value(i).toString());
  162. }
  163. }
  164. return result;
  165. }
  166. }
  167. /*!
  168. * \brief Db::imultiSelect - see Db::multiSelect
  169. */
  170. QVector<QString> Db::imultiSelect(QVector<QString> columns, QString table)
  171. {
  172. QString statement = "SELECT ";
  173. for (const auto &column : columns) {
  174. statement.append(column);
  175. if (column != columns.last()) {
  176. statement.append(QLatin1String(", "));
  177. }
  178. }
  179. statement.append(" FROM " + table);
  180. QSqlQuery q(statement);
  181. q.exec();
  182. if (!q.first()) {
  183. DEB("No result found. Check Query and Error.");
  184. DEB("Error: " << q.lastError().text());
  185. return QVector<QString>();
  186. } else {
  187. q.first();
  188. q.previous();
  189. QVector<QString> result;
  190. while (q.next()) {
  191. for (int i = 0; i < columns.size() ; i++) {
  192. result.append(q.value(i).toString());
  193. }
  194. }
  195. return result;
  196. }
  197. }
  198. /*!
  199. * \brief Db::isingleUpdate - see Db::singleUpdate
  200. */
  201. bool Db::isingleUpdate(QString table, QString column, QString value, QString checkColumn,
  202. QString checkvalue, Db::matchType match)
  203. {
  204. QString statement = "UPDATE " + table;
  205. statement.append(QLatin1String(" SET ") + column + QLatin1String(" = '") + value);
  206. statement.append(QLatin1String("' WHERE "));
  207. switch (match) {
  208. case Db::exactMatch:
  209. statement.append(checkColumn + " = '" + checkvalue + QLatin1Char('\''));
  210. break;
  211. case Db::partialMatch:
  212. value.append(QLatin1Char('%'));
  213. value.prepend(QLatin1Char('%'));
  214. statement.append(checkColumn + " LIKE '" + checkvalue + QLatin1Char('\''));
  215. break;
  216. }
  217. QSqlQuery q(statement);
  218. q.exec();
  219. QString error = q.lastError().text();
  220. if (error.length() > 1) {
  221. DEB("Errors have occured: " << error);
  222. return false;
  223. } else {
  224. DEB("Success!");
  225. return true;
  226. }
  227. }
  228. /*!
  229. * \brief Db::icustomQuery - see Db::customQuery
  230. */
  231. QVector<QString> Db::icustomQuery(QString query, int returnValues)
  232. {
  233. QSqlQuery q(query);
  234. q.exec();
  235. if (!q.first()) {
  236. DEB("No result found. Check Query and Error.");
  237. DEB("Error: " << q.lastError().text());
  238. return QVector<QString>();
  239. } else {
  240. q.first();
  241. q.previous();
  242. QVector<QString> result;
  243. while (q.next()) {
  244. for (int i = 0; i < returnValues ; i++) {
  245. result.append(q.value(i).toString());
  246. }
  247. }
  248. return result;
  249. }
  250. }