db.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // Debug Makro
  21. #define DEB(expr) \
  22. qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
  23. /*!
  24. * \brief Db::iconnect - see Db::connect
  25. */
  26. void Db::iconnect()
  27. {
  28. const QString driver("QSQLITE");
  29. if (QSqlDatabase::isDriverAvailable(driver)) {
  30. QDir directory("data");
  31. QString databaseLocation = directory.filePath("logbook.db");
  32. QSqlDatabase db = QSqlDatabase::addDatabase(driver);
  33. db.setDatabaseName(databaseLocation);
  34. if (!db.open()) {
  35. DEB("DatabaseConnect - ERROR: " << db.lastError().text());
  36. } else {
  37. DEB("Database connection established.");
  38. //Enable foreign key restrictions
  39. QSqlQuery query("PRAGMA foreign_keys = ON;");
  40. }
  41. } else {
  42. DEB("DatabaseConnect - ERROR: no driver " << driver << " available");
  43. }
  44. }
  45. QSqlDatabase Db::iDatabase()
  46. {
  47. auto db = QSqlDatabase::database("qt_sql_default_connection");
  48. return db;
  49. }
  50. /*!
  51. * \brief Db::iexists - see Db::exists
  52. */
  53. bool Db::iexists(QString column, QString table, QString checkColumn, QString value,
  54. Db::matchType match)
  55. {
  56. bool output = false;
  57. QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
  58. switch (match) {
  59. case Db::exactMatch:
  60. statement += " = '" + value + QLatin1Char('\'');
  61. break;
  62. case Db::partialMatch:
  63. value.append(QLatin1Char('%'));
  64. value.prepend(QLatin1Char('%'));
  65. statement.append(" LIKE '" + value + QLatin1Char('\''));
  66. break;
  67. }
  68. DEB(statement);
  69. QSqlQuery q(statement);
  70. q.exec();
  71. if (!q.first()) {
  72. DEB("No result found. Check Query and Error.");
  73. DEB("Error: " << q.lastError().text());
  74. } else {
  75. DEB("Success. Found a result.");
  76. output = true;
  77. if (q.next()) {
  78. DEB("More than one result in Database for your query");
  79. }
  80. }
  81. // Debug:
  82. q.first();
  83. q.previous();
  84. while (q.next()) {
  85. DEB("Query result: " << q.value(0).toString());
  86. }
  87. // end of Debug
  88. return output;
  89. }
  90. /*!
  91. * \brief Db::isingleSelect - see Db::singleSelect
  92. */
  93. QString Db::isingleSelect(QString column, QString table, QString checkColumn, QString value,
  94. Db::matchType match)
  95. {
  96. QString statement = "SELECT " + column + " FROM " + table + " WHERE " + checkColumn;
  97. QString result;
  98. switch (match) {
  99. case Db::exactMatch:
  100. statement += " = '" + value + QLatin1Char('\'');
  101. break;
  102. case Db::partialMatch:
  103. value.append(QLatin1Char('%'));
  104. value.prepend(QLatin1Char('%'));
  105. statement.append(" LIKE '" + value + QLatin1Char('\''));
  106. break;
  107. }
  108. DEB(statement);
  109. QSqlQuery q(statement);
  110. q.exec();
  111. if (!q.first()) {
  112. DEB("No result found. Check Query and Error.");
  113. DEB("Error: " << q.lastError().text());
  114. return QString();
  115. } else {
  116. DEB("Success. Found a result.");
  117. result.append(q.value(0).toString());
  118. if (q.next()) {
  119. DEB("More than one result in Database for your query");
  120. }
  121. return result;
  122. }
  123. }
  124. /*!
  125. * \brief Db::imultiSelect - see Db::multiSelect
  126. */
  127. QVector<QString> Db::imultiSelect(QVector<QString> columns, QString table, QString checkColumn,
  128. QString value, Db::matchType match)
  129. {
  130. QString statement = "SELECT ";
  131. for (const auto &column : columns) {
  132. statement.append(column);
  133. if (column != columns.last()) {
  134. statement.append(QLatin1String(", "));
  135. }
  136. }
  137. statement.append(" FROM " + table + " WHERE " + checkColumn);
  138. switch (match) {
  139. case Db::exactMatch:
  140. statement += " = '" + value + QLatin1Char('\'');
  141. break;
  142. case Db::partialMatch:
  143. value.append(QLatin1Char('%'));
  144. value.prepend(QLatin1Char('%'));
  145. statement.append(" LIKE '" + value + QLatin1Char('\''));
  146. break;
  147. }
  148. DEB(statement);
  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. DEB(statement);
  181. QSqlQuery q(statement);
  182. q.exec();
  183. if (!q.first()) {
  184. DEB("No result found. Check Query and Error.");
  185. DEB("Error: " << q.lastError().text());
  186. return QVector<QString>();
  187. } else {
  188. q.first();
  189. q.previous();
  190. QVector<QString> result;
  191. while (q.next()) {
  192. for (int i = 0; i < columns.size() ; i++) {
  193. result.append(q.value(i).toString());
  194. }
  195. }
  196. return result;
  197. }
  198. }
  199. /*!
  200. * \brief Db::isingleUpdate - see Db::singleUpdate
  201. */
  202. bool Db::isingleUpdate(QString table, QString column, QString value, QString checkColumn,
  203. QString checkvalue, Db::matchType match)
  204. {
  205. QString statement = "UPDATE " + table;
  206. statement.append(QLatin1String(" SET ") + column + QLatin1String(" = '") + value);
  207. statement.append(QLatin1String("' WHERE "));
  208. switch (match) {
  209. case Db::exactMatch:
  210. statement.append(checkColumn + " = '" + checkvalue + QLatin1Char('\''));
  211. break;
  212. case Db::partialMatch:
  213. value.append(QLatin1Char('%'));
  214. value.prepend(QLatin1Char('%'));
  215. statement.append(checkColumn + " LIKE '" + checkvalue + QLatin1Char('\''));
  216. break;
  217. }
  218. DEB(statement);
  219. QSqlQuery q(statement);
  220. q.exec();
  221. QString error = q.lastError().text();
  222. if (error.length() > 1) {
  223. DEB("Errors have occured: " << error);
  224. return false;
  225. } else {
  226. DEB("Success!");
  227. return true;
  228. }
  229. }
  230. /*!
  231. * \brief Db::icustomQuery - see Db::customQuery
  232. */
  233. QVector<QString> Db::icustomQuery(QString query, int returnValues)
  234. {
  235. QSqlQuery q(query);
  236. DEB(query);
  237. q.exec();
  238. if (!q.first()) {
  239. DEB("No result found. Check Query and Error.");
  240. DEB("Error: " << q.lastError().text());
  241. return QVector<QString>();
  242. } else {
  243. q.first();
  244. q.previous();
  245. QVector<QString> result;
  246. while (q.next()) {
  247. for (int i = 0; i < returnValues ; i++) {
  248. result.append(q.value(i).toString());
  249. }
  250. }
  251. return result;
  252. }
  253. }