2019-09-06 23:43:07 -04:00
|
|
|
#ifndef DB_H
|
|
|
|
#define DB_H
|
|
|
|
|
|
|
|
// This file is part of MRCI.
|
|
|
|
|
|
|
|
// MRCI 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.
|
|
|
|
|
|
|
|
// MRCI 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 MRCI under the LICENSE.md file. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-11-10 14:47:00 -05:00
|
|
|
#include "common.h"
|
2019-09-06 23:43:07 -04:00
|
|
|
|
2020-08-09 12:21:33 -04:00
|
|
|
QString genPw();
|
|
|
|
QList<int> genSequence(int min, int max, int len);
|
|
|
|
QChar genLetter();
|
|
|
|
QChar genNum();
|
|
|
|
QChar genSpecialChar();
|
|
|
|
int inRange(int pos, int min, int max);
|
|
|
|
QString columnType(const QString &column);
|
|
|
|
QByteArray getSalt(const QByteArray &uId, const QString &table);
|
|
|
|
QByteArray genUniqueHash();
|
2020-11-10 14:47:00 -05:00
|
|
|
bool createUser(const QString &userName, const QString &email, const QString &dispName, const QString &password, int rank, bool requireNewPass = false);
|
2020-08-09 12:21:33 -04:00
|
|
|
bool createTempPw(const QByteArray &uId, const QString &password);
|
|
|
|
bool updatePassword(const QByteArray &uId, const QString &password, const QString &table, bool requireNewPass = false);
|
|
|
|
bool auth(const QByteArray &uId, const QString &password, const QString &table);
|
2020-11-10 14:47:00 -05:00
|
|
|
bool testDbWritable();
|
2020-08-09 12:21:33 -04:00
|
|
|
void cleanupDbConnection();
|
|
|
|
void saveDbSettings(const QJsonObject &obj);
|
|
|
|
void moveCharLeft(int pos, QString &str);
|
|
|
|
void moveCharRight(int pos, QString &str);
|
2019-09-06 23:43:07 -04:00
|
|
|
|
|
|
|
class Query : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum QueryType
|
|
|
|
{
|
|
|
|
UPDATE,
|
|
|
|
PUSH,
|
|
|
|
PULL,
|
|
|
|
DEL,
|
2019-11-08 22:06:09 -05:00
|
|
|
INNER_JOIN_PULL,
|
2019-09-06 23:43:07 -04:00
|
|
|
CREATE_TABLE,
|
2020-11-10 14:47:00 -05:00
|
|
|
ALTER_TABLE,
|
|
|
|
DEL_TABLE
|
2019-09-06 23:43:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Condition
|
|
|
|
{
|
|
|
|
EQUAL,
|
|
|
|
NOT_EQUAL,
|
|
|
|
LIKE,
|
|
|
|
LIKE_STARTS_WITH,
|
|
|
|
LIKE_ENDS_WITH
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FKAction
|
|
|
|
{
|
|
|
|
NO_ACTION,
|
|
|
|
RESTRICT,
|
|
|
|
SET_NULL,
|
|
|
|
SET_DEFAULT,
|
|
|
|
CASCADE
|
|
|
|
};
|
|
|
|
|
|
|
|
static QString getConnectionName();
|
|
|
|
static QSqlDatabase getDatabase();
|
|
|
|
|
|
|
|
explicit Query(QObject *parent = nullptr);
|
|
|
|
|
2019-11-08 22:06:09 -05:00
|
|
|
void addRandBlob(const QString &column, int len);
|
|
|
|
void addCondition(const QString &column, const QVariant &data, Condition cond = EQUAL, const QString &tbl = QString());
|
|
|
|
void addJoinCondition(const QString &column, const QString &joinTable, Condition cond = EQUAL);
|
|
|
|
void addUnique(const QString &column);
|
|
|
|
void setPrimary(const QString &column);
|
|
|
|
void setPrimaryAsc(const QString &column);
|
|
|
|
void setPrimaryDesc(const QString &column);
|
|
|
|
void setQueryLimit(uint value, uint offset = 0);
|
|
|
|
void increment(const QString &column, double value);
|
|
|
|
void decrement(const QString &column, double value);
|
|
|
|
void addForeign(const QString &column, const QString &refTable, const QString &refColum, FKAction onDel = RESTRICT, FKAction onUpdate = RESTRICT);
|
|
|
|
void addColumn(const QString &column);
|
|
|
|
void addColumn(const QString &column, const QVariant &dataIn);
|
|
|
|
void addTableColumn(const QString &table, const QString &column);
|
|
|
|
void setType(QueryType qType, const QString &tbl);
|
|
|
|
void enableForeignKeys(bool state);
|
|
|
|
void setTextEncoding(const QString &encoding);
|
|
|
|
void clearConditions();
|
|
|
|
int rows();
|
|
|
|
int columns();
|
|
|
|
bool exec();
|
|
|
|
bool createExecuted();
|
|
|
|
bool inErrorstate();
|
|
|
|
QStringList tables();
|
|
|
|
QStringList columnsInTable(const QString &tbl);
|
|
|
|
QVariant getData(const QString &column, int row = 0);
|
|
|
|
QString errDetail();
|
2020-11-10 14:47:00 -05:00
|
|
|
QString errString();
|
2019-11-08 22:06:09 -05:00
|
|
|
QList<QList<QVariant> > &allData();
|
2019-09-06 23:43:07 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool createRan;
|
|
|
|
bool restraintAdded;
|
|
|
|
bool queryOk;
|
|
|
|
int rowsAffected;
|
|
|
|
QString table;
|
|
|
|
QString lastErr;
|
|
|
|
QString limit;
|
|
|
|
QString qStr;
|
|
|
|
QString wStr;
|
2019-11-08 22:06:09 -05:00
|
|
|
QString jStr;
|
2019-09-06 23:43:07 -04:00
|
|
|
QueryType type;
|
|
|
|
QList<int> directBind;
|
|
|
|
QStringList columnsAsPassed;
|
|
|
|
QList<QString> columnList;
|
|
|
|
QList<QVariant> bindValues;
|
|
|
|
QList<QVariant> whereBinds;
|
|
|
|
QList<QList<QVariant> > data;
|
|
|
|
|
|
|
|
bool createRedirect();
|
|
|
|
void postUpdate();
|
|
|
|
void preExec();
|
|
|
|
void changeValue(const QString &column, double value, const QString &sign);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DB_H
|