wip: query and model improvment
This commit is contained in:
parent
0f47ffd28a
commit
87b46a808a
|
|
@ -161,6 +161,7 @@ bool DatabasePrivate::updateDatabase()
|
||||||
bool DatabasePrivate::getCurrectScheema()
|
bool DatabasePrivate::getCurrectScheema()
|
||||||
{
|
{
|
||||||
Q_Q(Database);
|
Q_Q(Database);
|
||||||
|
|
||||||
if (allTableMaps.contains(q->metaObject()->className())) {
|
if (allTableMaps.contains(q->metaObject()->className())) {
|
||||||
currentModel = allTableMaps[q->metaObject()->className()];
|
currentModel = allTableMaps[q->metaObject()->className()];
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -207,7 +208,7 @@ bool DatabasePrivate::getCurrectScheema()
|
||||||
int typeId = QMetaType::type(tableProperty.typeName());
|
int typeId = QMetaType::type(tableProperty.typeName());
|
||||||
|
|
||||||
if (tables.values().contains(tableProperty.name())
|
if (tables.values().contains(tableProperty.name())
|
||||||
&& typeId >= QVariant::UserType) {
|
&& (unsigned)typeId >= QVariant::UserType) {
|
||||||
TableModel *sch = new TableModel(typeId, tableProperty.name());
|
TableModel *sch = new TableModel(typeId, tableProperty.name());
|
||||||
currentModel.append(sch);
|
currentModel.append(sch);
|
||||||
}
|
}
|
||||||
|
|
@ -215,7 +216,7 @@ bool DatabasePrivate::getCurrectScheema()
|
||||||
|
|
||||||
foreach (TableModel *sch, currentModel)
|
foreach (TableModel *sch, currentModel)
|
||||||
foreach (RelationModel *fk, sch->foregionKeys())
|
foreach (RelationModel *fk, sch->foregionKeys())
|
||||||
fk->table = currentModel.modelByClass(fk->className);
|
fk->table = currentModel.tableByClassName(fk->className);
|
||||||
|
|
||||||
allTableMaps.insert(q->metaObject()->className(), currentModel);
|
allTableMaps.insert(q->metaObject()->className(), currentModel);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -224,12 +225,12 @@ bool DatabasePrivate::getCurrectScheema()
|
||||||
DatabaseModel DatabasePrivate::getLastScheema()
|
DatabaseModel DatabasePrivate::getLastScheema()
|
||||||
{
|
{
|
||||||
Q_Q(Database);
|
Q_Q(Database);
|
||||||
// ChangeLogTable *u = q->_change_logs()->createQuery()->orderBy("id",
|
|
||||||
// "desc")->first();
|
|
||||||
ChangeLogTable *u
|
|
||||||
= changeLogs->query()->orderBy("id", "desc")->first();
|
|
||||||
|
|
||||||
DatabaseModel ret;
|
ChangeLogTable *u = changeLogs->query()
|
||||||
|
->orderBy(!ChangeLogTable::idField())
|
||||||
|
->first();
|
||||||
|
|
||||||
|
DatabaseModel ret(q->metaObject()->className());
|
||||||
|
|
||||||
if (u) {
|
if (u) {
|
||||||
QJsonObject json
|
QJsonObject json
|
||||||
|
|
@ -291,7 +292,7 @@ bool DatabasePrivate::storeScheemaInDB()
|
||||||
void DatabasePrivate::createChangeLogs()
|
void DatabasePrivate::createChangeLogs()
|
||||||
{
|
{
|
||||||
// currentModel.model("change_log")
|
// currentModel.model("change_log")
|
||||||
QString diff = sqlGenertor->diff(0, currentModel.model("__change_log"));
|
QString diff = sqlGenertor->diff(0, currentModel.tableByName("__change_log"));
|
||||||
|
|
||||||
db.exec(diff);
|
db.exec(diff);
|
||||||
}
|
}
|
||||||
|
|
@ -392,7 +393,7 @@ DatabaseModel Database::model() const
|
||||||
QString Database::tableName(QString className)
|
QString Database::tableName(QString className)
|
||||||
{
|
{
|
||||||
Q_D(Database);
|
Q_D(Database);
|
||||||
TableModel *m = model().modelByClass(className);
|
TableModel *m = model().tableByClassName(className);
|
||||||
if (m)
|
if (m)
|
||||||
return m->name();
|
return m->name();
|
||||||
else
|
else
|
||||||
|
|
@ -438,7 +439,7 @@ void Database::setConnectionName(QString connectionName)
|
||||||
void Database::setDriver(QString driver)
|
void Database::setDriver(QString driver)
|
||||||
{
|
{
|
||||||
Q_D(Database);
|
Q_D(Database);
|
||||||
d->driver = driver;
|
d->driver = driver.toUpper();
|
||||||
}
|
}
|
||||||
|
|
||||||
SqlGeneratorBase *Database::sqlGenertor() const
|
SqlGeneratorBase *Database::sqlGenertor() const
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ public:
|
||||||
|
|
||||||
QSqlQuery exec(QString sql);
|
QSqlQuery exec(QString sql);
|
||||||
|
|
||||||
|
//TODO: make me private and rename me
|
||||||
void add(TableSetBase *);
|
void add(TableSetBase *);
|
||||||
int saveChanges();
|
int saveChanges();
|
||||||
void cleanUp();
|
void cleanUp();
|
||||||
|
|
@ -71,6 +72,7 @@ public:
|
||||||
SqlGeneratorBase *sqlGenertor() const;
|
SqlGeneratorBase *sqlGenertor() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
//remove minor version
|
||||||
virtual void databaseUpdated(int oldMajor, int oldMinor, int newMajor,
|
virtual void databaseUpdated(int oldMajor, int oldMinor, int newMajor,
|
||||||
int newMinor);
|
int newMinor);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,11 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
DatabaseModel::DatabaseModel() : QList<TableModel*>(), _versionMajor(0), _versionMinor(0)
|
QMap<QString, DatabaseModel*> DatabaseModel::_models;
|
||||||
{
|
|
||||||
|
|
||||||
|
DatabaseModel::DatabaseModel(const QString &name) : QList<TableModel*>(), _databaseClassName(name), _versionMajor(0), _versionMinor(0)
|
||||||
|
{
|
||||||
|
_models.insert(name, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList<TableModel*>(other), _versionMajor(0), _versionMinor(0)
|
DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList<TableModel*>(other), _versionMajor(0), _versionMinor(0)
|
||||||
|
|
@ -35,7 +37,7 @@ DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList<TableModel*>(ot
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TableModel *DatabaseModel::model(QString tableName) const
|
TableModel *DatabaseModel::tableByName(QString tableName) const
|
||||||
{
|
{
|
||||||
for(int i = 0; i < size(); i++){
|
for(int i = 0; i < size(); i++){
|
||||||
TableModel *s = at(i);
|
TableModel *s = at(i);
|
||||||
|
|
@ -49,7 +51,7 @@ TableModel *DatabaseModel::model(QString tableName) const
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TableModel *DatabaseModel::modelByClass(QString className) const
|
TableModel *DatabaseModel::tableByClassName(QString className) const
|
||||||
{
|
{
|
||||||
for(int i = 0; i < size(); i++){
|
for(int i = 0; i < size(); i++){
|
||||||
TableModel *s = at(i);
|
TableModel *s = at(i);
|
||||||
|
|
@ -71,7 +73,7 @@ bool DatabaseModel::operator ==(const DatabaseModel &other) const
|
||||||
|
|
||||||
for(int i = 0; i < size(); i++){
|
for(int i = 0; i < size(); i++){
|
||||||
TableModel *mine = at(i);
|
TableModel *mine = at(i);
|
||||||
TableModel *others = other.model(mine->name());
|
TableModel *others = other.tableByName(mine->name());
|
||||||
|
|
||||||
if(!others)
|
if(!others)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -98,9 +100,9 @@ QJsonObject DatabaseModel::toJson() const
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
RelationModel *DatabaseModel::relationByClassNames(QString masterClassName, QString childClassName)
|
RelationModel *DatabaseModel::relationByClassNames(const QString &masterClassName, const QString &childClassName)
|
||||||
{
|
{
|
||||||
TableModel *childTable = modelByClass(childClassName);
|
TableModel *childTable = tableByClassName(childClassName);
|
||||||
|
|
||||||
if(!childTable)
|
if(!childTable)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -112,9 +114,9 @@ RelationModel *DatabaseModel::relationByClassNames(QString masterClassName, QStr
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
RelationModel *DatabaseModel::relationByTableNames(QString masterTableName, QString childTableName)
|
RelationModel *DatabaseModel::relationByTableNames(const QString &masterTableName, const QString &childTableName)
|
||||||
{
|
{
|
||||||
TableModel *childTable = model(childTableName);
|
TableModel *childTable = tableByName(childTableName);
|
||||||
|
|
||||||
if(!childTable)
|
if(!childTable)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -128,7 +130,7 @@ RelationModel *DatabaseModel::relationByTableNames(QString masterTableName, QStr
|
||||||
|
|
||||||
DatabaseModel DatabaseModel::fromJson(QJsonObject &json)
|
DatabaseModel DatabaseModel::fromJson(QJsonObject &json)
|
||||||
{
|
{
|
||||||
DatabaseModel model;
|
DatabaseModel model(QString::null);
|
||||||
|
|
||||||
// model.setVersionMajor(json.value(QT_STRINGIFY(versionMajor)).toInt());
|
// model.setVersionMajor(json.value(QT_STRINGIFY(versionMajor)).toInt());
|
||||||
// model.setVersionMinor(json.value(QT_STRINGIFY(versionMinor)).toInt());
|
// model.setVersionMinor(json.value(QT_STRINGIFY(versionMinor)).toInt());
|
||||||
|
|
@ -163,7 +165,7 @@ void DatabaseModel::setVersionMinor(int versionMinor)
|
||||||
_versionMinor = versionMinor;
|
_versionMinor = versionMinor;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DatabaseModel::remove(QString tableName)
|
bool DatabaseModel::remove(const QString &tableName)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < size(); i++){
|
for(int i = 0; i < size(); i++){
|
||||||
TableModel *s = at(i);
|
TableModel *s = at(i);
|
||||||
|
|
@ -175,4 +177,12 @@ bool DatabaseModel::remove(QString tableName)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DatabaseModel *DatabaseModel::modelByName(const QString &name)
|
||||||
|
{
|
||||||
|
if (_models.contains(name))
|
||||||
|
return _models[name];
|
||||||
|
|
||||||
|
return Q_NULLPTR;
|
||||||
|
}
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
#define DATABASEMODEL_H
|
#define DATABASEMODEL_H
|
||||||
|
|
||||||
#include <QtCore/QList>
|
#include <QtCore/QList>
|
||||||
#include"defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
class QJsonObject;
|
class QJsonObject;
|
||||||
|
|
||||||
|
|
@ -30,20 +30,25 @@ NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
class TableModel;
|
class TableModel;
|
||||||
struct RelationModel;
|
struct RelationModel;
|
||||||
class DatabaseModel : public QList<TableModel*>
|
class DatabaseModel : public QList<TableModel *>
|
||||||
{
|
{
|
||||||
|
QString _databaseClassName;
|
||||||
int _versionMajor, _versionMinor;
|
int _versionMajor, _versionMinor;
|
||||||
|
static QMap<QString, DatabaseModel *> _models;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DatabaseModel();
|
DatabaseModel(const QString &name = QString::null);
|
||||||
DatabaseModel(const DatabaseModel &other);
|
DatabaseModel(const DatabaseModel &other);
|
||||||
|
|
||||||
TableModel *model(QString tableName) const;
|
TableModel *tableByName(QString tableName) const;
|
||||||
TableModel *modelByClass(QString className) const;
|
TableModel *tableByClassName(QString className) const;
|
||||||
|
|
||||||
RelationModel *relationByClassNames(QString masterClassName, QString childClassName);
|
RelationModel *relationByClassNames(const QString &masterClassName,
|
||||||
RelationModel *relationByTableNames(QString masterTableName, QString childTableName);
|
const QString &childClassName);
|
||||||
|
RelationModel *relationByTableNames(const QString &masterClassName,
|
||||||
|
const QString &childClassName);
|
||||||
|
|
||||||
bool operator ==(const DatabaseModel &other) const;
|
bool operator==(const DatabaseModel &other) const;
|
||||||
|
|
||||||
static DatabaseModel fromJson(QJsonObject &json);
|
static DatabaseModel fromJson(QJsonObject &json);
|
||||||
QJsonObject toJson() const;
|
QJsonObject toJson() const;
|
||||||
|
|
@ -54,7 +59,9 @@ public:
|
||||||
int versionMinor() const;
|
int versionMinor() const;
|
||||||
void setVersionMinor(int versionMinor);
|
void setVersionMinor(int versionMinor);
|
||||||
|
|
||||||
bool remove(QString tableName);
|
bool remove(const QString &tableName);
|
||||||
|
|
||||||
|
static DatabaseModel *modelByName(const QString &name);
|
||||||
};
|
};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -43,18 +43,19 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
|
//TODO: remove minor version
|
||||||
#define NUT_DB_VERSION(major, minor) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_DB_VERSION), QT_STRINGIFY(#major "." #minor))
|
#define NUT_DB_VERSION(major, minor) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_DB_VERSION), QT_STRINGIFY(#major "." #minor))
|
||||||
|
|
||||||
#define NUT_DECLARE_TABLE(type, name) \
|
#define NUT_DECLARE_TABLE(type, name) \
|
||||||
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \
|
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \
|
||||||
Q_PROPERTY(type* name READ name) \
|
Q_PROPERTY(type* name READ name) \
|
||||||
Q_PROPERTY(__NUT_NAMESPACE_PERFIX TableSet<type> name##s READ name##s) \
|
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type>) name##s READ name##s) \
|
||||||
type* m_##name; \
|
type* m_##name; \
|
||||||
__NUT_NAMESPACE_PERFIX TableSet<type> *m_##name##s; \
|
NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name##s; \
|
||||||
public: \
|
public: \
|
||||||
static const type _##name; \
|
static const type _##name; \
|
||||||
type* name() const{ return m_##name; } \
|
type* name() const{ return m_##name; } \
|
||||||
__NUT_NAMESPACE_PERFIX TableSet<type> *name##s() const { return m_##name##s; }
|
NUT_WRAP_NAMESPACE(TableSet<type>) *name##s() const { return m_##name##s; }
|
||||||
|
|
||||||
//Table
|
//Table
|
||||||
#define NUT_DECLARE_FIELD(type, name, read, write) \
|
#define NUT_DECLARE_FIELD(type, name, read, write) \
|
||||||
|
|
@ -62,8 +63,8 @@ public: \
|
||||||
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name " " __nut_FIELD), #name) \
|
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name " " __nut_FIELD), #name) \
|
||||||
type m_##name; \
|
type m_##name; \
|
||||||
public: \
|
public: \
|
||||||
static __NUT_NAMESPACE_PERFIX FieldPhrase<type> name##Field(){ \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) name ## Field(){ \
|
||||||
static __NUT_NAMESPACE_PERFIX FieldPhrase<type> f = __NUT_NAMESPACE_PERFIX FieldPhrase<type>(staticMetaObject.className(), #name); \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) f = NUT_WRAP_NAMESPACE(FieldPhrase<type>)(staticMetaObject.className(), #name); \
|
||||||
return f; \
|
return f; \
|
||||||
} \
|
} \
|
||||||
type read() const{ \
|
type read() const{ \
|
||||||
|
|
|
||||||
201
src/query.h
201
src/query.h
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
class NUT_EXPORT Query : public QueryBase
|
class NUT_EXPORT Query : public QueryBase
|
||||||
{
|
{
|
||||||
QueryPrivate *d_ptr;
|
QueryPrivate *d_ptr;
|
||||||
|
|
@ -54,12 +54,13 @@ public:
|
||||||
Query<T> *join(const QString &tableName);
|
Query<T> *join(const QString &tableName);
|
||||||
Query<T> *setWhere(WherePhrase where);
|
Query<T> *setWhere(WherePhrase where);
|
||||||
|
|
||||||
Query<T> *join(Table *c){
|
Query<T> *join(Table *c)
|
||||||
|
{
|
||||||
join(c->metaObject()->className());
|
join(c->metaObject()->className());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Query<T> *orderBy(QString fieldName, QString type);
|
// Query<T> *orderBy(QString fieldName, QString type);
|
||||||
Query<T> *orderBy(WherePhrase phrase);
|
Query<T> *orderBy(WherePhrase phrase);
|
||||||
|
|
||||||
int count();
|
int count();
|
||||||
|
|
@ -68,7 +69,7 @@ public:
|
||||||
QVariant average(FieldPhrase<int> &f);
|
QVariant average(FieldPhrase<int> &f);
|
||||||
T *first();
|
T *first();
|
||||||
QList<T *> toList(int count = -1);
|
QList<T *> toList(int count = -1);
|
||||||
template<typename F>
|
template <typename F>
|
||||||
QList<F> select(const FieldPhrase<F> f);
|
QList<F> select(const FieldPhrase<F> f);
|
||||||
|
|
||||||
int update(WherePhrase phrase);
|
int update(WherePhrase phrase);
|
||||||
|
|
@ -78,23 +79,28 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline Query<T> *createQuery(TableSet<T> *tableSet){
|
inline Query<T> *createQuery(TableSet<T> *tableSet)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet, bool autoDelete) : QueryBase(database),
|
Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet,
|
||||||
d_ptr(new QueryPrivate(this)), m_autoDelete(autoDelete)
|
bool autoDelete)
|
||||||
|
: QueryBase(database), d_ptr(new QueryPrivate(this)),
|
||||||
|
m_autoDelete(autoDelete)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->database = database;
|
d->database = database;
|
||||||
d->tableSet = tableSet;
|
d->tableSet = tableSet;
|
||||||
d->tableName = //TableModel::findByClassName(T::staticMetaObject.className())->name();
|
d->tableName
|
||||||
d->database->model().modelByClass(T::staticMetaObject.className())->name();
|
= // TableModel::findByClassName(T::staticMetaObject.className())->name();
|
||||||
|
d->database->model()
|
||||||
|
.tableByClassName(T::staticMetaObject.className())
|
||||||
|
->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
@ -102,55 +108,63 @@ Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
QList<T*> result;
|
QList<T *> result;
|
||||||
d->select = "*";
|
d->select = "*";
|
||||||
|
|
||||||
// QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand(d->wheres, d->orders, d->tableName, d->joinClassName));
|
// QSqlQuery q =
|
||||||
|
// d->database->exec(d->database->sqlGenertor()->selectCommand(d->wheres,
|
||||||
|
// d->orders, d->tableName, d->joinClassName));
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
SqlGeneratorBase::SelectAll,
|
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
||||||
"",
|
d->tableName, d->joinClassName);
|
||||||
d->wheres,
|
|
||||||
d->orderPhrases,
|
|
||||||
d->tableName,
|
|
||||||
d->joinClassName);
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
|
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
|
||||||
QString pk = d->database->model().model(d->tableName)->primaryKey();
|
QString pk = d->database->model().tableByName(d->tableName)->primaryKey();
|
||||||
QVariant lastPkValue = QVariant();
|
QVariant lastPkValue = QVariant();
|
||||||
int childTypeId = 0;
|
int childTypeId = 0;
|
||||||
T *lastRow = 0;
|
T *lastRow = 0;
|
||||||
TableSetBase *childTableSet = Q_NULLPTR;
|
TableSetBase *childTableSet = Q_NULLPTR;
|
||||||
|
|
||||||
//FIXME: getting table error
|
// FIXME: getting table error
|
||||||
// QStringList masterFields = TableModel::findByName(d->tableName)->fieldsNames();
|
// QStringList masterFields =
|
||||||
QStringList masterFields = d->database->model().model(d->tableName)->fieldsNames();
|
// TableModel::findByName(d->tableName)->fieldsNames();
|
||||||
|
QStringList masterFields
|
||||||
|
= d->database->model().tableByName(d->tableName)->fieldsNames();
|
||||||
QStringList childFields;
|
QStringList childFields;
|
||||||
if(!d->joinClassName.isNull()) {
|
if (!d->joinClassName.isNull()) {
|
||||||
TableModel *joinTableModel = TableModel::findByClassName(d->joinClassName);
|
TableModel *joinTableModel
|
||||||
if(joinTableModel){
|
= TableModel::findByClassName(d->joinClassName);
|
||||||
// childFields = d->database->model().modelByClass(d->joinClassName)->fieldsNames();
|
if (joinTableModel) {
|
||||||
childFields = TableModel::findByClassName(d->joinClassName)->fieldsNames();
|
// childFields =
|
||||||
|
// d->database->model().modelByClass(d->joinClassName)->fieldsNames();
|
||||||
|
childFields
|
||||||
|
= TableModel::findByClassName(d->joinClassName)->fieldsNames();
|
||||||
QString joinTableName = d->database->tableName(d->joinClassName);
|
QString joinTableName = d->database->tableName(d->joinClassName);
|
||||||
childTypeId = d->database->model().model(joinTableName)->typeId();
|
childTypeId = d->database->model().tableByName(joinTableName)->typeId();
|
||||||
// childTypeId = TableModel::findByName(joinTableName)->typeId();
|
// childTypeId =
|
||||||
|
// TableModel::findByName(joinTableName)->typeId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (q.next()) {
|
while (q.next()) {
|
||||||
if(lastPkValue != q.value(pk)){
|
if (lastPkValue != q.value(pk)) {
|
||||||
T *t = new T();
|
T *t = new T();
|
||||||
foreach (QString field, masterFields)
|
foreach (QString field, masterFields)
|
||||||
t->setProperty(field.toLatin1().data(), q.value(field));
|
t->setProperty(field.toLatin1().data(), q.value(field));
|
||||||
// for (int i = 0; i < t->metaObject()->propertyCount(); i++) {
|
// for (int i = 0; i < t->metaObject()->propertyCount();
|
||||||
// const QMetaProperty p = t->metaObject()->property(i);
|
// i++) {
|
||||||
|
// const QMetaProperty p =
|
||||||
|
// t->metaObject()->property(i);
|
||||||
|
|
||||||
// p.write(t, d->database->sqlGenertor()->readValue(p.type(), q.value(p.name())));
|
// p.write(t,
|
||||||
// }
|
// d->database->sqlGenertor()->readValue(p.type(),
|
||||||
|
// q.value(p.name())));
|
||||||
|
// }
|
||||||
|
|
||||||
t->setTableSet(d->tableSet);
|
t->setTableSet(d->tableSet);
|
||||||
t->setStatus(Table::FeatchedFromDB);
|
t->setStatus(Table::FeatchedFromDB);
|
||||||
|
|
@ -160,21 +174,24 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
result.append(t);
|
result.append(t);
|
||||||
lastRow = t;
|
lastRow = t;
|
||||||
|
|
||||||
if(childTypeId){
|
if (childTypeId) {
|
||||||
QSet<TableSetBase*> tableSets = t->tableSets;
|
QSet<TableSetBase *> tableSets = t->tableSets;
|
||||||
foreach (TableSetBase *ts, tableSets)
|
foreach (TableSetBase *ts, tableSets)
|
||||||
if(ts->childClassName() == d->joinClassName)
|
if (ts->childClassName() == d->joinClassName)
|
||||||
childTableSet = ts;
|
childTableSet = ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(childTypeId){
|
if (childTypeId) {
|
||||||
const QMetaObject *childMetaObject = QMetaType::metaObjectForType(childTypeId);
|
const QMetaObject *childMetaObject
|
||||||
Table *childTable = qobject_cast<Table*>(childMetaObject->newInstance());
|
= QMetaType::metaObjectForType(childTypeId);
|
||||||
|
Table *childTable
|
||||||
|
= qobject_cast<Table *>(childMetaObject->newInstance());
|
||||||
|
|
||||||
foreach (QString field, childFields)
|
foreach (QString field, childFields)
|
||||||
childTable->setProperty(field.toLatin1().data(), q.value(field));
|
childTable->setProperty(field.toLatin1().data(),
|
||||||
//TODO: set database for table
|
q.value(field));
|
||||||
|
// TODO: set database for table
|
||||||
childTable->setParent(this);
|
childTable->setParent(this);
|
||||||
childTable->setParentTable(lastRow);
|
childTable->setParentTable(lastRow);
|
||||||
childTable->setStatus(Table::FeatchedFromDB);
|
childTable->setStatus(Table::FeatchedFromDB);
|
||||||
|
|
@ -184,11 +201,11 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
}
|
}
|
||||||
lastPkValue = q.value(pk);
|
lastPkValue = q.value(pk);
|
||||||
|
|
||||||
if(!--count)
|
if (!--count)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m_autoDelete)
|
if (m_autoDelete)
|
||||||
deleteLater();
|
deleteLater();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -200,12 +217,8 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
QList<F> ret;
|
QList<F> ret;
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
SqlGeneratorBase::SignleField,
|
SqlGeneratorBase::SignleField, f.data()->text, d->wheres,
|
||||||
f.data()->text,
|
d->orderPhrases, d->tableName, d->joinClassName);
|
||||||
d->wheres,
|
|
||||||
d->orderPhrases,
|
|
||||||
d->tableName,
|
|
||||||
d->joinClassName);
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
while (q.next()) {
|
while (q.next()) {
|
||||||
|
|
@ -213,74 +226,83 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
|
||||||
ret.append(v.value<F>());
|
ret.append(v.value<F>());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m_autoDelete)
|
if (m_autoDelete)
|
||||||
deleteLater();
|
deleteLater();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE T *Query<T>::first()
|
Q_OUTOFLINE_TEMPLATE T *Query<T>::first()
|
||||||
{
|
{
|
||||||
QList<T *> list = toList(1);
|
QList<T *> list = toList(1);
|
||||||
|
|
||||||
if(list.count())
|
if (list.count())
|
||||||
return list.first();
|
return list.first();
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE int Query<T>::count()
|
Q_OUTOFLINE_TEMPLATE int Query<T>::count()
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->select = "COUNT(*)";
|
d->select = "COUNT(*)";
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand("COUNT(*)", d->wheres, d->orders, d->tableName, d->joinClassName);
|
d->sql = d->database->sqlGenertor()->selectCommand(SqlGeneratorBase::Count,
|
||||||
|
QStringLiteral("*"), d->wheres, d->orderPhrases, d->tableName, d->joinClassName);
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if(q.next())
|
if (q.next())
|
||||||
return q.value(0).toInt();
|
return q.value(0).toInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f){
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f)
|
||||||
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand("MAX(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
|
SqlGeneratorBase::Max, f.data()->text, d->wheres, d->orderPhrases,
|
||||||
|
d->tableName, d->joinClassName);
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if(q.next())
|
if (q.next())
|
||||||
return q.value(0).toInt();
|
return q.value(0).toInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f){
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f)
|
||||||
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand("MIN(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
|
SqlGeneratorBase::Min, f.data()->text, d->wheres, d->orderPhrases,
|
||||||
|
d->tableName, d->joinClassName);
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if(q.next())
|
if (q.next())
|
||||||
return q.value(0).toInt();
|
return q.value(0).toInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand("AVG(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
|
SqlGeneratorBase::Average, f.data()->text, d->wheres, d->orderPhrases,
|
||||||
|
d->tableName, d->joinClassName);
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if(q.next())
|
if (q.next())
|
||||||
return q.value(0).toInt();
|
return q.value(0).toInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &tableName)
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &tableName)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
@ -288,7 +310,7 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &tableName)
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
@ -296,15 +318,16 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
//template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName, QString type)
|
//Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName,
|
||||||
{
|
// QString type)
|
||||||
Q_D(Query);
|
//{
|
||||||
d->orders.insert(fieldName, type);
|
// Q_D(Query);
|
||||||
return this;
|
// d->orderPhrases.append(fieldName, type);
|
||||||
}
|
// return this;
|
||||||
|
//}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(WherePhrase phrase)
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(WherePhrase phrase)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
@ -312,15 +335,13 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(WherePhrase phrase)
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE int Query<T>::update(WherePhrase phrase)
|
Q_OUTOFLINE_TEMPLATE int Query<T>::update(WherePhrase phrase)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->updateCommand(
|
d->sql = d->database->sqlGenertor()->updateCommand(phrase, d->wheres,
|
||||||
phrase,
|
d->tableName);
|
||||||
d->wheres,
|
|
||||||
d->tableName);
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if (m_autoDelete)
|
if (m_autoDelete)
|
||||||
|
|
@ -328,14 +349,12 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::update(WherePhrase phrase)
|
||||||
return q.numRowsAffected();
|
return q.numRowsAffected();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
|
Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->deleteCommand(
|
d->sql = d->database->sqlGenertor()->deleteCommand(d->wheres, d->tableName);
|
||||||
d->wheres,
|
|
||||||
d->tableName);
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
if (m_autoDelete)
|
if (m_autoDelete)
|
||||||
|
|
@ -343,7 +362,7 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
|
||||||
return q.numRowsAffected();
|
return q.numRowsAffected();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
|
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
|
||||||
{
|
{
|
||||||
Q_D(const Query);
|
Q_D(const Query);
|
||||||
|
|
|
||||||
|
|
@ -33,15 +33,15 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
SqlGeneratorBase::SqlGeneratorBase(Database *parent) : QObject((QObject*)parent)
|
SqlGeneratorBase::SqlGeneratorBase(Database *parent)
|
||||||
|
: QObject((QObject *)parent)
|
||||||
{
|
{
|
||||||
if(parent)
|
if (parent)
|
||||||
_database = parent;
|
_database = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
SqlGeneratorBase::~SqlGeneratorBase()
|
SqlGeneratorBase::~SqlGeneratorBase()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::masterDatabaseName(QString databaseName)
|
QString SqlGeneratorBase::masterDatabaseName(QString databaseName)
|
||||||
|
|
@ -53,7 +53,7 @@ QString SqlGeneratorBase::masterDatabaseName(QString databaseName)
|
||||||
QString SqlGeneratorBase::saveRecord(Table *t, QString tableName)
|
QString SqlGeneratorBase::saveRecord(Table *t, QString tableName)
|
||||||
{
|
{
|
||||||
Q_ASSERT(!tableName.isEmpty() && !tableName.isNull());
|
Q_ASSERT(!tableName.isEmpty() && !tableName.isNull());
|
||||||
switch(t->status()){
|
switch (t->status()) {
|
||||||
case Table::Added:
|
case Table::Added:
|
||||||
return insertRecord(t, tableName);
|
return insertRecord(t, tableName);
|
||||||
|
|
||||||
|
|
@ -77,7 +77,8 @@ QString SqlGeneratorBase::fieldDeclare(FieldModel *field)
|
||||||
return field->name + " " + fieldType(field);
|
return field->name + " " + fieldType(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList SqlGeneratorBase::diff(DatabaseModel lastModel, DatabaseModel newModel)
|
QStringList SqlGeneratorBase::diff(DatabaseModel lastModel,
|
||||||
|
DatabaseModel newModel)
|
||||||
{
|
{
|
||||||
QStringList ret;
|
QStringList ret;
|
||||||
|
|
||||||
|
|
@ -88,10 +89,9 @@ QStringList SqlGeneratorBase::diff(DatabaseModel lastModel, DatabaseModel newMod
|
||||||
foreach (TableModel *table, newModel)
|
foreach (TableModel *table, newModel)
|
||||||
tableNames.insert(table->name());
|
tableNames.insert(table->name());
|
||||||
|
|
||||||
|
|
||||||
foreach (QString tableName, tableNames) {
|
foreach (QString tableName, tableNames) {
|
||||||
TableModel *oldTable = lastModel.model(tableName);
|
TableModel *oldTable = lastModel.tableByName(tableName);
|
||||||
TableModel *newTable = newModel.model(tableName);
|
TableModel *newTable = newModel.tableByName(tableName);
|
||||||
QString sql = diff(oldTable, newTable);
|
QString sql = diff(oldTable, newTable);
|
||||||
ret << sql;
|
ret << sql;
|
||||||
}
|
}
|
||||||
|
|
@ -102,14 +102,14 @@ QStringList SqlGeneratorBase::diff(DatabaseModel lastModel, DatabaseModel newMod
|
||||||
QString SqlGeneratorBase::diff(FieldModel *oldField, FieldModel *newField)
|
QString SqlGeneratorBase::diff(FieldModel *oldField, FieldModel *newField)
|
||||||
{
|
{
|
||||||
QString sql = "";
|
QString sql = "";
|
||||||
if(oldField && newField)
|
if (oldField && newField)
|
||||||
if(*oldField == *newField)
|
if (*oldField == *newField)
|
||||||
return QString::null;
|
return QString::null;
|
||||||
|
|
||||||
if(!newField){
|
if (!newField) {
|
||||||
sql = "DROP COLUMN " + oldField->name;
|
sql = "DROP COLUMN " + oldField->name;
|
||||||
}else{
|
} else {
|
||||||
if(oldField)
|
if (oldField)
|
||||||
sql = "MODIFY COLUMN ";
|
sql = "MODIFY COLUMN ";
|
||||||
else
|
else
|
||||||
sql = "ADD COLUMN ";
|
sql = "ADD COLUMN ";
|
||||||
|
|
@ -120,16 +120,16 @@ QString SqlGeneratorBase::diff(FieldModel *oldField, FieldModel *newField)
|
||||||
|
|
||||||
QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
|
QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
|
||||||
{
|
{
|
||||||
if(oldTable && newTable)
|
if (oldTable && newTable)
|
||||||
if(*oldTable == *newTable)
|
if (*oldTable == *newTable)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
if(!newTable)
|
if (!newTable)
|
||||||
return "DROP TABLE " + oldTable->name();
|
return "DROP TABLE " + oldTable->name();
|
||||||
|
|
||||||
QSet<QString> fieldNames;
|
QSet<QString> fieldNames;
|
||||||
|
|
||||||
if(oldTable)
|
if (oldTable)
|
||||||
foreach (FieldModel *f, oldTable->fields())
|
foreach (FieldModel *f, oldTable->fields())
|
||||||
fieldNames.insert(f->name);
|
fieldNames.insert(f->name);
|
||||||
|
|
||||||
|
|
@ -139,31 +139,28 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
|
||||||
QStringList columnSql;
|
QStringList columnSql;
|
||||||
foreach (QString fieldName, fieldNames) {
|
foreach (QString fieldName, fieldNames) {
|
||||||
FieldModel *newField = newTable->field(fieldName);
|
FieldModel *newField = newTable->field(fieldName);
|
||||||
if(oldTable){
|
if (oldTable) {
|
||||||
FieldModel *oldField = oldTable->field(fieldName);
|
FieldModel *oldField = oldTable->field(fieldName);
|
||||||
|
|
||||||
QString buffer = diff(oldField, newField);
|
QString buffer = diff(oldField, newField);
|
||||||
if(!buffer.isNull())
|
if (!buffer.isNull())
|
||||||
columnSql << buffer;
|
columnSql << buffer;
|
||||||
}else{
|
} else {
|
||||||
columnSql << fieldDeclare(newField);
|
columnSql << fieldDeclare(newField);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QString sql;
|
QString sql;
|
||||||
if(oldTable){
|
if (oldTable) {
|
||||||
sql = QString("ALTER TABLE %1 \n%2")
|
sql = QString("ALTER TABLE %1 \n%2").arg(newTable->name()).arg(
|
||||||
.arg(newTable->name())
|
columnSql.join(",\n"));
|
||||||
.arg(columnSql.join(",\n"));
|
} else {
|
||||||
}else{
|
if (!newTable->primaryKey().isNull())
|
||||||
if(!newTable->primaryKey().isNull())
|
|
||||||
columnSql << QString("CONSTRAINT pk_%1 PRIMARY KEY (%2)")
|
columnSql << QString("CONSTRAINT pk_%1 PRIMARY KEY (%2)")
|
||||||
.arg(newTable->name())
|
.arg(newTable->name())
|
||||||
.arg(newTable->primaryKey());
|
.arg(newTable->primaryKey());
|
||||||
|
|
||||||
sql = QString("CREATE TABLE %1 \n(%2)")
|
|
||||||
.arg(newTable->name())
|
|
||||||
.arg(columnSql.join(",\n"));
|
|
||||||
|
|
||||||
|
sql = QString("CREATE TABLE %1 \n(%2)").arg(newTable->name()).arg(
|
||||||
|
columnSql.join(",\n"));
|
||||||
}
|
}
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
@ -175,20 +172,21 @@ QString SqlGeneratorBase::insertRecord(Table *t, QString tableName)
|
||||||
QStringList values;
|
QStringList values;
|
||||||
|
|
||||||
foreach (QString f, t->changedProperties())
|
foreach (QString f, t->changedProperties())
|
||||||
if(f != key)
|
if (f != key)
|
||||||
values.append("'" + t->property(f.toLatin1().data()).toString() + "'");
|
values.append("'" + t->property(f.toLatin1().data()).toString()
|
||||||
|
+ "'");
|
||||||
|
|
||||||
QString changedPropertiesText = "";
|
QString changedPropertiesText = "";
|
||||||
QSet<QString> props = t->changedProperties();
|
QSet<QString> props = t->changedProperties();
|
||||||
foreach (QString s, props) {
|
foreach (QString s, props) {
|
||||||
if(changedPropertiesText != "")
|
if (changedPropertiesText != "")
|
||||||
changedPropertiesText.append(", ");
|
changedPropertiesText.append(", ");
|
||||||
changedPropertiesText.append(s);
|
changedPropertiesText.append(s);
|
||||||
}
|
}
|
||||||
sql = QString("INSERT INTO %1 (%2) VALUES (%3)")
|
sql = QString("INSERT INTO %1 (%2) VALUES (%3)")
|
||||||
.arg(tableName)
|
.arg(tableName)
|
||||||
.arg(changedPropertiesText)
|
.arg(changedPropertiesText)
|
||||||
.arg(values.join(", "));
|
.arg(values.join(", "));
|
||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
@ -200,13 +198,14 @@ QString SqlGeneratorBase::updateRecord(Table *t, QString tableName)
|
||||||
QStringList values;
|
QStringList values;
|
||||||
|
|
||||||
foreach (QString f, t->changedProperties())
|
foreach (QString f, t->changedProperties())
|
||||||
if(f != key)
|
if (f != key)
|
||||||
values.append(f + "='" + t->property(f.toLatin1().data()).toString() + "'");
|
values.append(f + "='" + t->property(f.toLatin1().data()).toString()
|
||||||
|
+ "'");
|
||||||
sql = QString("UPDATE %1 SET %2 WHERE %3=%4")
|
sql = QString("UPDATE %1 SET %2 WHERE %3=%4")
|
||||||
.arg(tableName)
|
.arg(tableName)
|
||||||
.arg(values.join(", "))
|
.arg(values.join(", "))
|
||||||
.arg(key)
|
.arg(key)
|
||||||
.arg(t->primaryValue().toString());
|
.arg(t->primaryValue().toString());
|
||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
@ -214,12 +213,13 @@ QString SqlGeneratorBase::updateRecord(Table *t, QString tableName)
|
||||||
QString SqlGeneratorBase::deleteRecord(Table *t, QString tableName)
|
QString SqlGeneratorBase::deleteRecord(Table *t, QString tableName)
|
||||||
{
|
{
|
||||||
return QString("DELETE FROM %1 WHERE %2='%3'")
|
return QString("DELETE FROM %1 WHERE %2='%3'")
|
||||||
.arg(tableName)
|
.arg(tableName)
|
||||||
.arg(t->primaryKey())
|
.arg(t->primaryKey())
|
||||||
.arg(t->primaryValue().toString());
|
.arg(t->primaryValue().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::agregateText(const AgregateType &t, const QString &arg) const
|
QString SqlGeneratorBase::agregateText(const AgregateType &t,
|
||||||
|
const QString &arg) const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case SelectAll:
|
case SelectAll:
|
||||||
|
|
@ -251,26 +251,32 @@ QString SqlGeneratorBase::agregateText(const AgregateType &t, const QString &arg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const
|
QString SqlGeneratorBase::fromTableText(const QString &tableName,
|
||||||
|
QString &joinClassName,
|
||||||
|
QString &orderBy) const
|
||||||
{
|
{
|
||||||
QString tableNameText = tableName;
|
QString tableNameText = tableName;
|
||||||
if(!joinClassName.isNull()){
|
if (!joinClassName.isNull()) {
|
||||||
QString joinTableName = _database->tableName(joinClassName);
|
QString joinTableName = _database->tableName(joinClassName);
|
||||||
RelationModel *rel = _database->model().relationByTableNames(tableName, joinTableName);
|
RelationModel *rel
|
||||||
if(rel){
|
= _database->model().relationByTableNames(tableName, joinTableName);
|
||||||
QString pk = _database->model().model(tableName)->primaryKey();
|
if (rel) {
|
||||||
tableNameText = QString("%1 INNER JOIN %2 ON (%1.%3 = %2.%4)")
|
QString pk = _database->model().tableByName(tableName)->primaryKey();
|
||||||
.arg(tableName)
|
tableNameText = QString("%1 INNER JOIN %2 ON (%1.%3 = %2.%4)")
|
||||||
.arg(joinTableName)
|
.arg(tableName)
|
||||||
.arg(pk)
|
.arg(joinTableName)
|
||||||
.arg(rel->localColumn);
|
.arg(pk)
|
||||||
|
.arg(rel->localColumn);
|
||||||
orderBy = tableName + "." + pk;
|
orderBy = tableName + "." + pk;
|
||||||
}else{
|
} else {
|
||||||
qWarning(QString("Relation between table %1 and class %2 (%3) not exists!")
|
qWarning(
|
||||||
.arg(tableName)
|
QString(
|
||||||
.arg(joinClassName)
|
"Relation between table %1 and class %2 (%3) not exists!")
|
||||||
.arg(joinTableName.isNull() ? "NULL" : joinTableName)
|
.arg(tableName)
|
||||||
.toLatin1().data());
|
.arg(joinClassName)
|
||||||
|
.arg(joinTableName.isNull() ? "NULL" : joinTableName)
|
||||||
|
.toLatin1()
|
||||||
|
.data());
|
||||||
joinClassName = QString::null;
|
joinClassName = QString::null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -281,7 +287,7 @@ QString SqlGeneratorBase::fromTableText(const QString &tableName, QString &joinC
|
||||||
QString SqlGeneratorBase::deleteRecords(QString tableName, QString where)
|
QString SqlGeneratorBase::deleteRecords(QString tableName, QString where)
|
||||||
{
|
{
|
||||||
QString sql = "";
|
QString sql = "";
|
||||||
if(where.isEmpty() || where.isNull())
|
if (where.isEmpty() || where.isNull())
|
||||||
sql = "DELETE FROM " + tableName;
|
sql = "DELETE FROM " + tableName;
|
||||||
else
|
else
|
||||||
sql = "DELETE FROM " + tableName + " WHERE " + where;
|
sql = "DELETE FROM " + tableName + " WHERE " + where;
|
||||||
|
|
@ -291,86 +297,51 @@ QString SqlGeneratorBase::deleteRecords(QString tableName, QString where)
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t, QString agregateArg, QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QString tableName, QString joinClassName)
|
QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
||||||
|
QString agregateArg,
|
||||||
|
QList<WherePhrase> &wheres,
|
||||||
|
QList<WherePhrase> &orders,
|
||||||
|
QString tableName,
|
||||||
|
QString joinClassName)
|
||||||
{
|
{
|
||||||
QString select = agregateText(t, agregateArg);
|
QString select = agregateText(t, agregateArg);
|
||||||
QString where = createWhere(wheres);
|
QString where = createWhere(wheres);
|
||||||
QString order = "";
|
QString order = "";
|
||||||
QString from = fromTableText(tableName, joinClassName, order);
|
QString from = fromTableText(tableName, joinClassName, order);
|
||||||
|
|
||||||
foreach(WherePhrase p, orders){
|
foreach (WherePhrase p, orders) {
|
||||||
if(order != "")
|
if (order != "")
|
||||||
order.append(", ");
|
order.append(", ");
|
||||||
order.append(phraseOrder(p.data()));
|
order.append(phraseOrder(p.data()));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString sql = "SELECT " + select + " FROM " + from;
|
QString sql = "SELECT " + select + " FROM " + from;
|
||||||
|
|
||||||
if(where != "")
|
if (where != "")
|
||||||
sql.append(" WHERE " + where);
|
sql.append(" WHERE " + where);
|
||||||
|
|
||||||
if(order != "")
|
if (order != "")
|
||||||
sql.append(" ORDER BY " + order);
|
sql.append(" ORDER BY " + order);
|
||||||
|
|
||||||
for(int i = 0; i < _database->model().count(); i++)
|
for (int i = 0; i < _database->model().count(); i++)
|
||||||
sql = sql.replace(_database->model().at(i)->className() + "." , _database->model().at(i)->name() + ".");
|
sql = sql.replace(_database->model().at(i)->className() + ".",
|
||||||
|
_database->model().at(i)->name() + ".");
|
||||||
|
|
||||||
replaceTableNames(sql);
|
replaceTableNames(sql);
|
||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::selectCommand(QString selectPhrase, QString agregateArg,
|
|
||||||
QList<WherePhrase> &wheres, QList<WherePhrase> &orders,
|
|
||||||
QString tableName, QString joinClassName)
|
|
||||||
{
|
|
||||||
QString where = createWhere(wheres);
|
|
||||||
QString order = "";
|
|
||||||
QString from = fromTableText(tableName, joinClassName, order);
|
|
||||||
|
|
||||||
foreach(WherePhrase p, orders){
|
|
||||||
if(order != "")
|
|
||||||
order.append(", ");
|
|
||||||
order.append(phraseOrder(p.data()));
|
|
||||||
}
|
|
||||||
|
|
||||||
QString sql = "SELECT " + selectPhrase + " FROM " + from;
|
|
||||||
|
|
||||||
if(where != "")
|
|
||||||
sql.append(" WHERE " + where);
|
|
||||||
|
|
||||||
if(order != "")
|
|
||||||
sql.append(" ORDER BY " + order);
|
|
||||||
|
|
||||||
for(int i = 0; i < _database->model().count(); i++)
|
|
||||||
sql = sql.replace(_database->model().at(i)->className() + "." , _database->model().at(i)->name() + ".");
|
|
||||||
|
|
||||||
replaceTableNames(sql);
|
|
||||||
|
|
||||||
return sql;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString SqlGeneratorBase::selectCommand(QList<WherePhrase> &wheres, QHash<QString, QString> &orders, QString tableName, QString joinClassName)
|
|
||||||
{
|
|
||||||
return selectCommand("*", wheres, orders, tableName, joinClassName);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString SqlGeneratorBase::createWhere(QList<WherePhrase> &wheres)
|
QString SqlGeneratorBase::createWhere(QList<WherePhrase> &wheres)
|
||||||
{
|
{
|
||||||
QString whereText = "";
|
QString whereText = "";
|
||||||
// for (int i = 0; i < wheres.count(); i++) {
|
|
||||||
// if(whereText != "")
|
|
||||||
// whereText.append(" AND ");
|
|
||||||
// whereText.append(phrase(wheres[i].data()));
|
|
||||||
// }
|
|
||||||
foreach (WherePhrase w, wheres) {
|
foreach (WherePhrase w, wheres) {
|
||||||
if(whereText != "")
|
if (whereText != "")
|
||||||
whereText.append(" AND ");
|
whereText.append(" AND ");
|
||||||
|
|
||||||
whereText.append(phrase(w.data()));
|
whereText.append(phrase(w.data()));
|
||||||
}
|
}
|
||||||
// if(whereText != "")
|
|
||||||
// whereText.prepend(" WHERE ");
|
|
||||||
|
|
||||||
return whereText;
|
return whereText;
|
||||||
}
|
}
|
||||||
|
|
@ -378,93 +349,49 @@ QString SqlGeneratorBase::createWhere(QList<WherePhrase> &wheres)
|
||||||
void SqlGeneratorBase::replaceTableNames(QString &command)
|
void SqlGeneratorBase::replaceTableNames(QString &command)
|
||||||
{
|
{
|
||||||
foreach (TableModel *m, TableModel::allModels())
|
foreach (TableModel *m, TableModel::allModels())
|
||||||
command = command.replace("[" + m->className() + "]." , m->name() + ".");
|
command = command.replace("[" + m->className() + "].", m->name() + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::selectCommand(QString selectPhrase, QList<WherePhrase> &wheres, QHash<QString, QString> &orders, QString tableName, QString joinClassName)
|
QString SqlGeneratorBase::deleteCommand(QList<WherePhrase> &wheres,
|
||||||
{
|
QString tableName)
|
||||||
QString orderText = "";
|
|
||||||
QStringList orderby;
|
|
||||||
|
|
||||||
QString whereText = createWhere(wheres);
|
|
||||||
|
|
||||||
if(whereText != "")
|
|
||||||
whereText.prepend(" WHERE ");
|
|
||||||
|
|
||||||
QString tableNameText = tableName;
|
|
||||||
if(!joinClassName.isNull()){
|
|
||||||
QString joinTableName = _database->tableName(joinClassName);
|
|
||||||
RelationModel *rel = _database->model().relationByTableNames(tableName, joinTableName);
|
|
||||||
if(rel){
|
|
||||||
QString pk = _database->model().model(tableName)->primaryKey();
|
|
||||||
tableNameText = QString("%1 INNER JOIN %2 ON (%1.%3 = %2.%4)")
|
|
||||||
.arg(tableName)
|
|
||||||
.arg(joinTableName)
|
|
||||||
.arg(pk)
|
|
||||||
.arg(rel->localColumn);
|
|
||||||
orderby.append(tableName + "." + pk);
|
|
||||||
}else{
|
|
||||||
qWarning("Relation between table %s and class %s (%s) not exists!",
|
|
||||||
qPrintable(tableName),
|
|
||||||
qPrintable(joinClassName),
|
|
||||||
qPrintable(joinTableName.isNull() ? "NULL" : joinTableName));
|
|
||||||
joinClassName = QString::null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(orders.count())
|
|
||||||
foreach (QString o, orders.keys())
|
|
||||||
// orderby.append(o + (orders.value(o) ? " ASC" : " DESC"));
|
|
||||||
orderby.append(o + " " + orders.value(o));
|
|
||||||
|
|
||||||
if(orderby.count())
|
|
||||||
orderText = " ORDER BY " + orderby.join(", ");
|
|
||||||
|
|
||||||
QString command = "SELECT "
|
|
||||||
+selectPhrase
|
|
||||||
+ " FROM "
|
|
||||||
+ tableNameText
|
|
||||||
+ whereText
|
|
||||||
+ orderText;
|
|
||||||
|
|
||||||
replaceTableNames(command);
|
|
||||||
|
|
||||||
return command;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString SqlGeneratorBase::deleteCommand(QList<WherePhrase> &wheres, QString tableName)
|
|
||||||
{
|
{
|
||||||
QString command = "DELETE FROM " + tableName;
|
QString command = "DELETE FROM " + tableName;
|
||||||
QString where = createWhere(wheres);
|
QString where = createWhere(wheres);
|
||||||
|
|
||||||
if(where != "")
|
if (where != "")
|
||||||
command.append(" WHERE " + where);
|
command.append(" WHERE " + where);
|
||||||
|
|
||||||
for(int i = 0; i < _database->model().count(); i++)
|
for (int i = 0; i < _database->model().count(); i++)
|
||||||
command = command.replace(_database->model().at(i)->className() + "." , _database->model().at(i)->name() + ".");
|
command = command.replace(_database->model().at(i)->className() + ".",
|
||||||
|
_database->model().at(i)->name() + ".");
|
||||||
|
|
||||||
replaceTableNames(command);
|
replaceTableNames(command);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::updateCommand(WherePhrase &phrase, QList<WherePhrase> &wheres, QString tableName)
|
QString SqlGeneratorBase::updateCommand(WherePhrase &phrase,
|
||||||
|
QList<WherePhrase> &wheres,
|
||||||
|
QString tableName)
|
||||||
{
|
{
|
||||||
QString p = this->phrase(phrase.data());
|
QString p = this->phrase(phrase.data());
|
||||||
QString where = createWhere(wheres);
|
QString where = createWhere(wheres);
|
||||||
|
|
||||||
QString sql = "UPDATE " + tableName + " SET " + p;
|
QString sql = "UPDATE " + tableName + " SET " + p;
|
||||||
|
|
||||||
if(where != "")
|
if (where != "")
|
||||||
sql.append(" WHERE " + where);
|
sql.append(" WHERE " + where);
|
||||||
|
|
||||||
for(int i = 0; i < _database->model().count(); i++)
|
for (int i = 0; i < _database->model().count(); i++)
|
||||||
sql = sql.replace(_database->model().at(i)->className() + "." , _database->model().at(i)->name() + ".");
|
sql = sql.replace(_database->model().at(i)->className() + ".",
|
||||||
|
_database->model().at(i)->name() + ".");
|
||||||
|
|
||||||
|
replaceTableNames(sql);
|
||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::escapeValue(const QVariant &v)const
|
QString SqlGeneratorBase::escapeValue(const QVariant &v) const
|
||||||
{
|
{
|
||||||
switch (v.type()) {
|
switch (v.type()) {
|
||||||
case QVariant::Bool:
|
case QVariant::Bool:
|
||||||
|
|
@ -513,7 +440,8 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v)const
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant SqlGeneratorBase::readValue(const QVariant::Type &type, const QVariant &dbValue)
|
QVariant SqlGeneratorBase::readValue(const QVariant::Type &type,
|
||||||
|
const QVariant &dbValue)
|
||||||
{
|
{
|
||||||
return dbValue;
|
return dbValue;
|
||||||
}
|
}
|
||||||
|
|
@ -523,16 +451,16 @@ QString SqlGeneratorBase::phraseOrder(const PhraseData *d) const
|
||||||
|
|
||||||
QString ret = "";
|
QString ret = "";
|
||||||
|
|
||||||
switch(d->type){
|
switch (d->type) {
|
||||||
case PhraseData::Field:
|
case PhraseData::Field:
|
||||||
if(d->operatorCond == PhraseData::Not)
|
if (d->operatorCond == PhraseData::Not)
|
||||||
ret = d->text + " DESC";
|
ret = d->text + " DESC";
|
||||||
else
|
else
|
||||||
ret = d->text;
|
ret = d->text;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithOther:
|
case PhraseData::WithOther:
|
||||||
if(d->operatorCond != PhraseData::Append)
|
if (d->operatorCond != PhraseData::Append)
|
||||||
qFatal("Order phease can only have & operator");
|
qFatal("Order phease can only have & operator");
|
||||||
|
|
||||||
ret = phraseOrder(d->left) + ", " + phraseOrder(d->right);
|
ret = phraseOrder(d->left) + ", " + phraseOrder(d->right);
|
||||||
|
|
@ -550,17 +478,19 @@ QString SqlGeneratorBase::phrase(const PhraseData *d) const
|
||||||
{
|
{
|
||||||
QString ret = "";
|
QString ret = "";
|
||||||
|
|
||||||
switch(d->type){
|
switch (d->type) {
|
||||||
case PhraseData::Field:
|
case PhraseData::Field:
|
||||||
ret = d->text;
|
ret = d->text;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithVariant:
|
case PhraseData::WithVariant:
|
||||||
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " " + escapeValue(d->operand);
|
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " "
|
||||||
|
+ escapeValue(d->operand);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithOther:
|
case PhraseData::WithOther:
|
||||||
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " " + phrase(d->right);
|
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " "
|
||||||
|
+ phrase(d->right);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithoutOperand:
|
case PhraseData::WithoutOperand:
|
||||||
|
|
@ -571,7 +501,7 @@ QString SqlGeneratorBase::phrase(const PhraseData *d) const
|
||||||
ret = "<FAIL>";
|
ret = "<FAIL>";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(d->operatorCond == PhraseData::And || d->operatorCond == PhraseData::Or)
|
if (d->operatorCond == PhraseData::And || d->operatorCond == PhraseData::Or)
|
||||||
ret = "(" + ret + ")";
|
ret = "(" + ret + ")";
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -581,20 +511,23 @@ QString SqlGeneratorBase::phraseUpdate(const PhraseData *d) const
|
||||||
{
|
{
|
||||||
QString ret = "";
|
QString ret = "";
|
||||||
|
|
||||||
if (d->operatorCond != PhraseData::And && d->operatorCond != PhraseData::Equal)
|
if (d->operatorCond != PhraseData::And
|
||||||
|
&& d->operatorCond != PhraseData::Equal)
|
||||||
qFatal("Update command does not accept any phrase else &, =");
|
qFatal("Update command does not accept any phrase else &, =");
|
||||||
|
|
||||||
switch(d->type){
|
switch (d->type) {
|
||||||
case PhraseData::Field:
|
case PhraseData::Field:
|
||||||
ret = d->text;
|
ret = d->text;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithVariant:
|
case PhraseData::WithVariant:
|
||||||
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " " + escapeValue(d->operand);
|
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " "
|
||||||
|
+ escapeValue(d->operand);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithOther:
|
case PhraseData::WithOther:
|
||||||
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " " + phrase(d->right);
|
ret = phrase(d->left) + " " + operatorString(d->operatorCond) + " "
|
||||||
|
+ phrase(d->right);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PhraseData::WithoutOperand:
|
case PhraseData::WithoutOperand:
|
||||||
|
|
@ -605,15 +538,16 @@ QString SqlGeneratorBase::phraseUpdate(const PhraseData *d) const
|
||||||
ret = "<FAIL>";
|
ret = "<FAIL>";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(d->operatorCond == PhraseData::And || d->operatorCond == PhraseData::Or)
|
if (d->operatorCond == PhraseData::And || d->operatorCond == PhraseData::Or)
|
||||||
ret = "(" + ret + ")";
|
ret = "(" + ret + ")";
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::operatorString(const PhraseData::Condition &cond) const
|
QString
|
||||||
|
SqlGeneratorBase::operatorString(const PhraseData::Condition &cond) const
|
||||||
{
|
{
|
||||||
switch (cond){
|
switch (cond) {
|
||||||
case PhraseData::Equal:
|
case PhraseData::Equal:
|
||||||
return "=";
|
return "=";
|
||||||
case PhraseData::NotEqual:
|
case PhraseData::NotEqual:
|
||||||
|
|
@ -667,5 +601,4 @@ QString SqlGeneratorBase::operatorString(const PhraseData::Condition &cond) cons
|
||||||
return QString("<FAIL>");
|
return QString("<FAIL>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -76,19 +76,12 @@ public:
|
||||||
|
|
||||||
virtual QString deleteRecords(QString tableName, QString where);
|
virtual QString deleteRecords(QString tableName, QString where);
|
||||||
|
|
||||||
virtual QString selectCommand(AgregateType t, QString agregateArg,
|
virtual QString selectCommand(AgregateType t,
|
||||||
QList<WherePhrase> &wheres, QList<WherePhrase> &orders,
|
QString agregateArg,
|
||||||
QString tableName, QString joinClassName);
|
QList<WherePhrase> &wheres,
|
||||||
|
QList<WherePhrase> &orders,
|
||||||
virtual QString selectCommand(QString selectPhrase, QString agregateArg,
|
QString tableName,
|
||||||
QList<WherePhrase> &wheres, QList<WherePhrase> &orders,
|
QString joinClassName);
|
||||||
QString tableName, QString joinClassName);
|
|
||||||
|
|
||||||
virtual QString selectCommand(QList<WherePhrase> &wheres, QHash<QString, QString> &orders,
|
|
||||||
QString tableName, QString joinClassName);
|
|
||||||
virtual QString selectCommand(QString selectPhrase,
|
|
||||||
QList<WherePhrase> &wheres, QHash<QString, QString> &orders,
|
|
||||||
QString tableName, QString joinClassName);
|
|
||||||
|
|
||||||
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
virtual QString deleteCommand(QList<WherePhrase> &wheres, QString tableName);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue