return sql command from query

This commit is contained in:
blackdal 2017-07-25 18:40:43 +04:30
parent a9224fcb40
commit eae5d8c185
7 changed files with 136 additions and 55 deletions

View File

@ -1,3 +1,23 @@
/**************************************************************************
**
** 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/>.
**
**************************************************************************/
#include "dbgeography.h"
NUT_BEGIN_NAMESPACE

View File

@ -1,3 +1,23 @@
/**************************************************************************
**
** 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/>.
**
**************************************************************************/
#ifndef DBGEOGRAPHY_H
#define DBGEOGRAPHY_H

View File

@ -51,18 +51,6 @@ public:
~Query();
T *first();
int count();
QList<T *> toList(int count = -1);
template<typename F>
QList<F> select(const FieldPhrase<F> f);
QVariant max(FieldPhrase<int> &f);
QVariant min(FieldPhrase<int> &f);
QVariant average(FieldPhrase<int> &f);
Query<T> *join(const QString &tableName);
Query<T> *setWhere(WherePhrase where);
@ -71,12 +59,22 @@ public:
return this;
}
// Query<T> *setWhere(const QString &where);
Query<T> *orderBy(QString fieldName, QString type);
Query<T> *orderBy(WherePhrase phrase);
int count();
QVariant max(FieldPhrase<int> &f);
QVariant min(FieldPhrase<int> &f);
QVariant average(FieldPhrase<int> &f);
T *first();
QList<T *> toList(int count = -1);
template<typename F>
QList<F> select(const FieldPhrase<F> f);
int update(WherePhrase phrase);
int remove();
QString sqlCommand() const;
};
template <typename T>
@ -93,7 +91,7 @@ Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet,
d->database = database;
d->tableSet = tableSet;
d->tableName = //TableModel::findByClassName(T::staticMetaObject.className())->name();
d->database->model().modelByClass(T::staticMetaObject.className())->name();
d->database->model().modelByClass(T::staticMetaObject.className())->name();
}
template<class T>
@ -112,15 +110,16 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
d->select = "*";
// QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand(d->wheres, d->orders, d->tableName, d->joinClassName));
QString sql = d->database->sqlGenertor()->selectCommand(
SqlGeneratorBase::SelectAll,
"",
d->wheres,
d->orderPhrases,
d->tableName,
d->joinClassName);
d->sql = d->database->sqlGenertor()->selectCommand(
SqlGeneratorBase::SelectAll,
"",
d->wheres,
d->orderPhrases,
d->tableName,
d->joinClassName);
QSqlQuery q = d->database->exec(sql);
// qDebug() << sql;
QSqlQuery q = d->database->exec(d->sql);
// QString pk = TableModel::findByName(d->tableName)->primaryKey();
QString pk = d->database->model().model(d->tableName)->primaryKey();
@ -202,14 +201,14 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
{
Q_D(Query);
QList<F> ret;
QString sql = d->database->sqlGenertor()->selectCommand(
d->sql = d->database->sqlGenertor()->selectCommand(
SqlGeneratorBase::SignleField,
f.data()->text,
d->wheres,
d->orderPhrases,
d->tableName,
d->joinClassName);
QSqlQuery q = d->database->exec(sql);
QSqlQuery q = d->database->exec(d->sql);
while (q.next()) {
QVariant v = q.value(f.data()->text);
@ -238,7 +237,8 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::count()
Q_D(Query);
d->select = "COUNT(*)";
QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand("COUNT(*)", d->wheres, d->orders, d->tableName, d->joinClassName));
d->sql = d->database->sqlGenertor()->selectCommand("COUNT(*)", d->wheres, d->orders, d->tableName, d->joinClassName);
QSqlQuery q = d->database->exec(d->sql);
if(q.next())
return q.value(0).toInt();
@ -249,7 +249,8 @@ template<class T>
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f){
Q_D(Query);
QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand("MAX(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName));
d->sql = d->database->sqlGenertor()->selectCommand("MAX(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
QSqlQuery q = d->database->exec(d->sql);
if(q.next())
return q.value(0).toInt();
@ -260,7 +261,8 @@ template<class T>
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f){
Q_D(Query);
QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand("MIN(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName));
d->sql = d->database->sqlGenertor()->selectCommand("MIN(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
QSqlQuery q = d->database->exec(d->sql);
if(q.next())
return q.value(0).toInt();
@ -272,7 +274,8 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
{
Q_D(Query);
QSqlQuery q = d->database->exec(d->database->sqlGenertor()->selectCommand("AVG(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName));
d->sql = d->database->sqlGenertor()->selectCommand("AVG(" + f.data()->text + ")", d->wheres, d->orders, d->tableName, d->joinClassName);
QSqlQuery q = d->database->exec(d->sql);
if(q.next())
return q.value(0).toInt();
@ -316,12 +319,12 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::update(WherePhrase phrase)
{
Q_D(Query);
QString sql = d->database->sqlGenertor()->updateCommand(
d->sql = d->database->sqlGenertor()->updateCommand(
phrase,
d->wheres,
d->tableName);
QSqlQuery q = d->database->exec(sql);
qDebug()<<sql;
QSqlQuery q = d->database->exec(d->sql);
if (m_autoDelete)
deleteLater();
return q.numRowsAffected();
@ -332,13 +335,23 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
{
Q_D(Query);
QString sql = d->database->sqlGenertor()->deleteCommand(
d->sql = d->database->sqlGenertor()->deleteCommand(
d->wheres,
d->tableName);
QSqlQuery q = d->database->exec(sql);
QSqlQuery q = d->database->exec(d->sql);
if (m_autoDelete)
deleteLater();
return q.numRowsAffected();
}
template<class T>
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
{
Q_D(const Query);
return d->sql;
}
NUT_END_NAMESPACE
#endif // QUERY_H

View File

@ -40,6 +40,7 @@ public:
QueryPrivate(QueryBase *parent);
~QueryPrivate();
QString sql;
QString tableName;
QString select;
Database *database;

View File

@ -1,3 +1,23 @@
/**************************************************************************
**
** 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/>.
**
**************************************************************************/
#ifndef QUERYBASE_H
#define QUERYBASE_H

View File

@ -1,21 +1,21 @@
///**************************************************************************
//**
//** 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/>.
//**
//**************************************************************************/
/**************************************************************************
**
** 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/>.
**
**************************************************************************/
#include "tableset.h"

View File

@ -392,7 +392,7 @@ public:
}
};
#define SPECIALIZATION_NUMERIC(type) \
#define SPECIALIZATION_NUMERIC(type) \
template<> \
class FieldPhrase<type>: public NumericFieldPhrase{ \
public: \
@ -402,18 +402,25 @@ public:
{ \
return WherePhrase(this, PhraseData::Set, (WherePhrase *)&other); \
} \
WherePhrase in(QList<type> list) \
WherePhrase in(QList<type> list) \
{ \
QVariantList vlist; \
foreach (type t, list) \
foreach (type t, list) \
vlist.append(QVariant::fromValue(t)); \
return WherePhrase(this, PhraseData::In, vlist); \
} \
};
SPECIALIZATION_NUMERIC(qint8)
SPECIALIZATION_NUMERIC(qint16)
SPECIALIZATION_NUMERIC(qint32)
SPECIALIZATION_NUMERIC(qint64)
SPECIALIZATION_NUMERIC(quint8)
SPECIALIZATION_NUMERIC(quint16)
SPECIALIZATION_NUMERIC(quint32)
SPECIALIZATION_NUMERIC(quint64)
SPECIALIZATION_NUMERIC(qreal)