Nut/src/nut/generators/abstractsqlgenerator.h

171 lines
6.5 KiB
C
Raw Normal View History

2016-05-21 16:09:03 +08:00
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
2020-08-06 23:19:27 +08:00
#ifndef NUT_ABSTRACTSQLGENERATOR_H
#define NUT_ABSTRACTSQLGENERATOR_H
2016-05-12 14:08:58 +08:00
#include <QtCore/qglobal.h>
2016-05-21 16:09:03 +08:00
#include <QtCore/QObject>
#include <QtCore/QStringList>
2020-08-06 23:19:27 +08:00
#include "phrase.h"
2016-05-12 14:08:58 +08:00
2019-02-10 17:35:35 +08:00
class SqlSerializer;
2019-02-08 16:39:24 +08:00
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-12 14:08:58 +08:00
class Table;
2016-05-21 16:09:03 +08:00
struct FieldModel;
2016-05-12 14:08:58 +08:00
class DatabaseModel;
2016-05-21 16:09:03 +08:00
class TableModel;
2016-05-24 14:47:37 +08:00
class Database;
2018-03-11 21:13:13 +08:00
struct RelationModel;
2020-08-06 23:19:27 +08:00
class NUT_EXPORT AbstractSqlGenerator : public QObject
2016-05-12 14:08:58 +08:00
{
2016-05-24 14:47:37 +08:00
// Q_OBJECT
Database *_database;
2019-02-12 23:23:46 +08:00
protected:
2019-02-10 17:35:35 +08:00
SqlSerializer *_serializer;
2019-02-08 16:39:24 +08:00
2019-02-28 17:28:49 +08:00
bool isNumeric(const QMetaType::Type &type);
2016-05-12 14:08:58 +08:00
public:
2018-02-25 23:17:49 +08:00
//TODO: remove this enum
2016-05-24 14:47:37 +08:00
enum CommandType{
Select,
Insert,
Update,
Delete
};
2016-06-05 20:22:26 +08:00
enum AgregateType{
2018-02-17 23:44:39 +08:00
// SelectAll,
2016-06-05 20:22:26 +08:00
Count,
Min,
Max,
Average,
SingleField,
Sum
2016-06-05 20:22:26 +08:00
};
2016-05-24 14:47:37 +08:00
2020-08-06 23:19:27 +08:00
explicit AbstractSqlGenerator(Database *parent);
virtual ~AbstractSqlGenerator() = default;
2016-05-12 14:08:58 +08:00
2019-02-12 22:33:16 +08:00
virtual bool supportPrimaryKey(const QMetaType::Type &type) {
Q_UNUSED(type)
2019-02-12 22:33:16 +08:00
return true;
}
virtual bool supportAutoIncrement(const QMetaType::Type &type) {
Q_UNUSED(type)
2019-02-12 22:33:16 +08:00
return true;
}
2019-02-28 17:28:49 +08:00
//fields
virtual QString fieldType(FieldModel *field) = 0;
virtual QString fieldDeclare(FieldModel *field);
virtual QStringList constraints(TableModel *table);
virtual QString escapeValue(const QVariant &v) const;
virtual QVariant unescapeValue(const QMetaType::Type &type, const QVariant &dbValue);
2019-02-28 17:28:49 +08:00
2016-05-21 16:09:03 +08:00
virtual QString masterDatabaseName(QString databaseName);
2018-01-10 01:32:28 +08:00
virtual QString createTable(TableModel *table);
2018-02-24 23:43:15 +08:00
virtual QString relationDeclare(const RelationModel *relation);
2016-05-21 16:09:03 +08:00
2019-02-10 22:11:22 +08:00
virtual QStringList diff(const DatabaseModel &lastModel, const DatabaseModel &newModel);
2016-05-21 16:09:03 +08:00
virtual QString diff(FieldModel *oldField, FieldModel *newField);
2019-02-16 21:36:38 +08:00
virtual QStringList diff(TableModel *oldTable, TableModel *newTable);
virtual QStringList diffRelation(TableModel *oldTable, TableModel *newTable);
virtual QStringList diff(RelationModel *oldRel, RelationModel *newRel);
2016-05-12 14:08:58 +08:00
2018-01-13 23:59:55 +08:00
virtual QString join(const QString &mainTable,
2019-02-10 22:11:22 +08:00
const QList<RelationModel*> &list,
2018-01-13 23:59:55 +08:00
QStringList *order = Q_NULLPTR);
2018-01-11 00:18:49 +08:00
virtual QString join(const QStringList &list, QStringList *order = Q_NULLPTR);
2018-01-10 01:32:28 +08:00
2016-05-21 16:09:03 +08:00
virtual QString saveRecord(Table *t, QString tableName);
2018-01-11 15:13:01 +08:00
virtual QString recordsPhrase(TableModel *table);
2018-01-12 00:14:29 +08:00
2019-03-08 00:48:48 +08:00
virtual QString insertBulk(const QString &tableName, const PhraseList &ph, const QList<QVariantList> &vars);
2016-05-21 16:09:03 +08:00
virtual QString insertRecord(Table *t, QString tableName);
virtual QString updateRecord(Table *t, QString tableName);
virtual QString deleteRecord(Table *t, QString tableName);
2019-02-10 22:11:22 +08:00
virtual QString deleteRecords(const QString &tableName, const QString &where);
2016-05-12 14:08:58 +08:00
2018-02-17 23:44:39 +08:00
virtual QString selectCommand(const QString &tableName,
const PhraseList &fields,
const ConditionalPhrase &where,
const PhraseList &order,
2019-02-10 22:11:22 +08:00
const QList<RelationModel *> &joins,
2018-02-17 23:44:39 +08:00
const int skip = -1,
const int take = -1);
virtual QString selectCommand(const QString &tableName,
2018-02-25 23:17:49 +08:00
const AgregateType &t,
const QString &agregateArg,
2018-02-17 23:44:39 +08:00
const ConditionalPhrase &where,
const QList<RelationModel *> &joins,
const int skip = -1,
const int take = -1);
virtual QString deleteCommand(const QString &tableName,
const ConditionalPhrase &where);
virtual QString updateCommand(const QString &tableName,
const AssignmentPhraseList &assigments,
const ConditionalPhrase &where);
2018-10-15 22:34:50 +08:00
virtual QString insertCommand(const QString &tableName,
const AssignmentPhraseList &assigments);
2018-02-17 23:44:39 +08:00
// virtual QString selectCommand(AgregateType t,
// QString agregateArg, QString tableName,
// QList<WherePhrase> &wheres,
// QList<WherePhrase> &orders,
// QList<RelationModel*> joins,
// int skip = -1, int take = -1);
// virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
// virtual QString updateCommand(WherePhrase &phrase, QList<WherePhrase> &wheres, QString tableName);
2017-05-31 00:19:37 +08:00
2016-06-05 20:22:26 +08:00
virtual QString phrase(const PhraseData *d) const;
virtual QString operatorString(const PhraseData::Condition &cond) const;
2018-02-21 23:41:45 +08:00
virtual void appendSkipTake(QString &sql, int skip = -1, int take = -1);
2019-01-28 15:53:34 +08:00
virtual QString primaryKeyConstraint(const TableModel *table) const;
2018-02-24 23:43:15 +08:00
protected:
virtual QString createConditionalPhrase(const PhraseData *d) const;
2018-02-17 23:44:39 +08:00
QString createFieldPhrase(const PhraseList &ph);
QString createOrderPhrase(const PhraseList &ph);
void createInsertPhrase(const AssignmentPhraseList &ph, QString &fields, QString &values);
QString agregateText(const AgregateType &t, const QString &arg = QString()) const;
2016-06-05 20:22:26 +08:00
QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const;
2018-02-17 23:44:39 +08:00
// QString createWhere(QList<WherePhrase> &wheres);
2019-06-19 17:24:58 +08:00
2019-07-20 14:37:08 +08:00
virtual void replaceTableNames(QString &command);
2019-06-19 17:24:58 +08:00
void removeTableNames(QString &command);
2019-07-06 22:08:40 +08:00
QString dateTimePartName(const PhraseData::Condition &op) const;
2016-05-12 14:08:58 +08:00
};
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE
2016-05-12 14:08:58 +08:00
2020-08-06 23:19:27 +08:00
#endif // NUT_ABSTRACTSQLGENERATOR_H