# openPilotLog: src/database/db.h Source File

db.h
/*
*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/>.
*/
#ifndef DB_H
#define DB_H
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include <QDir>
#include <QDebug>
class Db
{
public:
static Db& get()
{
static Db instance;
return instance;
}
enum editRole {createNew, editExisting};
enum matchType {exactMatch, partialMatch};
static void connect(){get().iconnect();}
static void disconnect(){get().idisconnect();}
static QSqlDatabase Database(){return get().iDatabase();}
static bool exists(QString column, QString table, QString checkColumn,
QString value, Db::matchType match)
{
return get().iexists(column, table, checkColumn, value, match);
}
static bool singleUpdate(QString table, QString column, QString value,
QString checkColumn, QString checkvalue, Db::matchType match){
return get().isingleUpdate(table,column,value,checkColumn,checkvalue,match);
}
static QString singleSelect(QString column, QString table, QString checkColumn,
QString value, Db::matchType match){
return get().isingleSelect(column,table,checkColumn,value,match);
}
static QVector<QString> multiSelect(QVector<QString> columns, QString table,
QString checkColumn, QString value, Db::matchType match){
return get().imultiSelect(columns,table,checkColumn,value,match);
}
static QVector<QString> multiSelect(QVector<QString> columns, QString table){
return get().imultiSelect(columns, table);
}
static QVector<QString> customQuery(QString query, int returnValues){
return get().icustomQuery(query, returnValues);
}
private:
Db() {}
void iconnect();
void idisconnect();
QSqlDatabase iDatabase();
bool iexists(QString column, QString table, QString checkColumn,
QString value, Db::matchType match);
bool isingleUpdate(QString table, QString column, QString value,
QString checkColumn, QString checkvalue, Db::matchType match);
QString isingleSelect(QString column, QString table, QString checkColumn,
QString value, Db::matchType match);
QVector<QString> imultiSelect(QVector<QString> columns, QString table,
QString checkColumn, QString value, Db::matchType match);
QVector<QString> imultiSelect(QVector<QString> columns, QString table);
QVector<QString> icustomQuery(QString query, int returnValues);
public:
// [George]: Why delete these in particular?
Db(Db const&) = delete;
void operator=(Db const&) = delete;
};
#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