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/>.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
#ifndef SQLGENERATORBASE_H
|
|
|
|
|
#define SQLGENERATORBASE_H
|
|
|
|
|
|
|
|
|
|
#include <QtCore/qglobal.h>
|
2016-05-21 16:09:03 +08:00
|
|
|
#include <QtCore/QObject>
|
|
|
|
|
#include <QtCore/QStringList>
|
2016-06-05 20:22:26 +08:00
|
|
|
#include "wherephrase.h"
|
2016-05-12 14:08:58 +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;
|
2016-06-05 20:22:26 +08:00
|
|
|
//struct PhraseData;
|
|
|
|
|
//class WherePhrase;
|
2016-05-21 16:09:03 +08:00
|
|
|
class SqlGeneratorBase : public QObject
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
2016-05-24 14:47:37 +08:00
|
|
|
// Q_OBJECT
|
|
|
|
|
|
|
|
|
|
Database *_database;
|
2016-05-12 14:08:58 +08:00
|
|
|
public:
|
2016-05-24 14:47:37 +08:00
|
|
|
enum CommandType{
|
|
|
|
|
Select,
|
|
|
|
|
Insert,
|
|
|
|
|
Update,
|
|
|
|
|
Delete
|
|
|
|
|
};
|
2016-06-05 20:22:26 +08:00
|
|
|
enum AgregateType{
|
2017-05-30 16:08:12 +08:00
|
|
|
SelectAll,
|
2016-06-05 20:22:26 +08:00
|
|
|
Count,
|
|
|
|
|
Min,
|
|
|
|
|
Max,
|
2017-05-30 16:08:12 +08:00
|
|
|
Average,
|
|
|
|
|
SignleField
|
2016-06-05 20:22:26 +08:00
|
|
|
};
|
2016-05-24 14:47:37 +08:00
|
|
|
|
|
|
|
|
SqlGeneratorBase(Database *parent);
|
2016-05-12 14:08:58 +08:00
|
|
|
virtual ~SqlGeneratorBase();
|
|
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
virtual QString masterDatabaseName(QString databaseName);
|
|
|
|
|
|
|
|
|
|
virtual QString fieldType(FieldModel *field) = 0;
|
|
|
|
|
virtual QString fieldDeclare(FieldModel *field);
|
|
|
|
|
|
|
|
|
|
virtual QStringList diff(DatabaseModel lastModel, DatabaseModel newModel);
|
|
|
|
|
virtual QString diff(FieldModel *oldField, FieldModel *newField);
|
|
|
|
|
virtual QString diff(TableModel *oldTable, TableModel *newTable);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
virtual QString saveRecord(Table *t, QString tableName);
|
|
|
|
|
virtual QString insertRecord(Table *t, QString tableName);
|
|
|
|
|
virtual QString updateRecord(Table *t, QString tableName);
|
|
|
|
|
virtual QString deleteRecord(Table *t, QString tableName);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2016-06-05 20:22:26 +08:00
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
virtual QString deleteRecords(QString tableName, QString where);
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2017-08-10 23:09:41 +08:00
|
|
|
virtual QString selectCommand(AgregateType t,
|
|
|
|
|
QString agregateArg,
|
|
|
|
|
QList<WherePhrase> &wheres,
|
|
|
|
|
QList<WherePhrase> &orders,
|
|
|
|
|
QString tableName,
|
|
|
|
|
QString joinClassName);
|
2016-05-24 14:47:37 +08:00
|
|
|
|
|
|
|
|
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
|
|
|
|
|
2017-05-31 00:19:37 +08:00
|
|
|
virtual QString updateCommand(WherePhrase &phrase, QList<WherePhrase> &wheres, QString tableName);
|
|
|
|
|
|
2016-06-05 20:22:26 +08:00
|
|
|
virtual QString escapeValue(const QVariant &v) const;
|
2017-06-12 00:50:43 +08:00
|
|
|
virtual QVariant readValue(const QVariant::Type &type, const QVariant &dbValue);
|
2016-06-05 20:22:26 +08:00
|
|
|
virtual QString phrase(const PhraseData *d) const;
|
2017-06-01 23:38:53 +08:00
|
|
|
virtual QString phraseUpdate(const PhraseData *d) const;
|
2016-06-05 20:22:26 +08:00
|
|
|
virtual QString operatorString(const PhraseData::Condition &cond) const;
|
|
|
|
|
|
2016-05-24 14:47:37 +08:00
|
|
|
private:
|
2016-06-05 20:22:26 +08:00
|
|
|
QString agregateText(const AgregateType &t, const QString &arg = QString::null) const;
|
|
|
|
|
QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const;
|
2016-05-24 14:47:37 +08:00
|
|
|
QString createWhere(QList<WherePhrase> &wheres);
|
2016-06-05 20:22:26 +08:00
|
|
|
QString phraseOrder(const PhraseData *d) const;
|
2017-08-09 21:19:35 +08:00
|
|
|
void replaceTableNames(QString &command);
|
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
|
|
|
|
|
|
|
|
#endif // SQLGENERATORBASE_H
|