openPilotLog
db.h
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 #ifndef DB_H
19 #define DB_H
20 
21 #include <QCoreApplication>
22 #include <QSqlDatabase>
23 #include <QSqlDriver>
24 #include <QSqlError>
25 #include <QSqlQuery>
26 #include <QSqlRecord>
27 #include <QSqlField>
28 #include <QDir>
29 #include <QDebug>
30 
39 class Db
40 {
41  public:
42  static Db& get()
43  {
44  static Db instance;
45 
46  return instance;
47  }
52  enum editRole {createNew, editExisting};
58  enum matchType {exactMatch, partialMatch};
64  static void connect(){get().iconnect();}
68  static void disconnect(){get().idisconnect();}
69 
74  static QSqlDatabase Database(){return get().iDatabase();}
75 
83  static bool exists(QString column, QString table, QString checkColumn,
84  QString value, Db::matchType match)
85  {
86  return get().iexists(column, table, checkColumn, value, match);
87  }
99  static bool singleUpdate(QString table, QString column, QString value,
100  QString checkColumn, QString checkvalue, Db::matchType match){
101  return get().isingleUpdate(table,column,value,checkColumn,checkvalue,match);
102  }
111  static QString singleSelect(QString column, QString table, QString checkColumn,
112  QString value, Db::matchType match){
113  return get().isingleSelect(column,table,checkColumn,value,match);
114  }
124  static QVector<QString> multiSelect(QVector<QString> columns, QString table,
125  QString checkColumn, QString value, Db::matchType match){
126  return get().imultiSelect(columns,table,checkColumn,value,match);
127  }
134  static QVector<QString> multiSelect(QVector<QString> columns, QString table){
135  return get().imultiSelect(columns, table);
136  }
143  static QVector<QString> customQuery(QString query, int returnValues){
144  return get().icustomQuery(query, returnValues);
145  }
146 
147  private:
148  Db() {}
149  void iconnect();
150  void idisconnect();
151  QSqlDatabase iDatabase();
152  bool iexists(QString column, QString table, QString checkColumn,
153  QString value, Db::matchType match);
154  bool isingleUpdate(QString table, QString column, QString value,
155  QString checkColumn, QString checkvalue, Db::matchType match);
156  QString isingleSelect(QString column, QString table, QString checkColumn,
157  QString value, Db::matchType match);
158  QVector<QString> imultiSelect(QVector<QString> columns, QString table,
159  QString checkColumn, QString value, Db::matchType match);
160  QVector<QString> imultiSelect(QVector<QString> columns, QString table);
161  QVector<QString> icustomQuery(QString query, int returnValues);
162 
163  public:
164  // [George]: Why delete these in particular?
165  Db(Db const&) = delete;
166  void operator=(Db const&) = delete;
167 };
168 
169 #endif // DB_H
Db
The Db class provides a basic API for accessing the database programatically. It is used to set up th...
Definition: db.h:40
Db::singleSelect
static QString singleSelect(QString column, QString table, QString checkColumn, QString value, Db::matchType match)
singleSelect Returns a single value from the database with a sqlite WHERE statement
Definition: db.h:111
Db::multiSelect
static QVector< QString > multiSelect(QVector< QString > columns, QString table)
Db::multiSelect Returns a complete column(s) for a given table.
Definition: db.h:134
Db::disconnect
static void disconnect()
disconnect Closes and removes the default database connection.
Definition: db.h:68
Db::multiSelect
static QVector< QString > multiSelect(QVector< QString > columns, QString table, QString checkColumn, QString value, Db::matchType match)
Db::multiSelect Returns multiple values from the database with a sqlite WHERE statement.
Definition: db.h:124
Db::matchType
matchType
The matchType enum {exactMatch, partialMatch} is used to determine the matching when using a WHERE sq...
Definition: db.h:58
Db::singleUpdate
static bool singleUpdate(QString table, QString column, QString value, QString checkColumn, QString checkvalue, Db::matchType match)
Db::singleUpdate Updates a single value in the database. Query format: UPDATE table SET column = valu...
Definition: db.h:99
Db::editRole
editRole
The editRole enum {createNew, editExisting} is used to differentiate between creating a new entry in ...
Definition: db.h:52
Db::Database
static QSqlDatabase Database()
Can be used to access the database connection.
Definition: db.h:74
Db::connect
static void connect()
connect establishes the database connection. Only needs to be called once within the application....
Definition: db.h:64
Db::exists
static bool exists(QString column, QString table, QString checkColumn, QString value, Db::matchType match)
Db::exists checks if a certain value exists in the database with a sqlite WHERE statement.
Definition: db.h:83
Db::customQuery
static QVector< QString > customQuery(QString query, int returnValues)
Db::customQuery Can be used to send a complex query to the database.
Definition: db.h:143