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" #include "dbgeography.h"
NUT_BEGIN_NAMESPACE 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 #ifndef DBGEOGRAPHY_H
#define DBGEOGRAPHY_H #define DBGEOGRAPHY_H

View File

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

View File

@ -40,6 +40,7 @@ public:
QueryPrivate(QueryBase *parent); QueryPrivate(QueryBase *parent);
~QueryPrivate(); ~QueryPrivate();
QString sql;
QString tableName; QString tableName;
QString select; QString select;
Database *database; 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 #ifndef QUERYBASE_H
#define QUERYBASE_H #define QUERYBASE_H

View File

@ -1,21 +1,21 @@
///************************************************************************** /**************************************************************************
//** **
//** This file is part of Nut project. ** This file is part of Nut project.
//** https://github.com/HamedMasafi/Nut ** https://github.com/HamedMasafi/Nut
//** **
//** Nut is free software: you can redistribute it and/or modify ** 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 ** 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 ** the Free Software Foundation, either version 3 of the License, or
//** (at your option) any later version. ** (at your option) any later version.
//** **
//** Nut is distributed in the hope that it will be useful, ** Nut is distributed in the hope that it will be useful,
//** but WITHOUT ANY WARRANTY; without even the implied warranty of ** but WITHOUT ANY WARRANTY; without even the implied warranty of
//** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//** GNU Lesser General Public License for more details. ** GNU Lesser General Public License for more details.
//** **
//** You should have received a copy of the GNU Lesser General Public License ** You should have received a copy of the GNU Lesser General Public License
//** along with Nut. If not, see <http://www.gnu.org/licenses/>. ** along with Nut. If not, see <http://www.gnu.org/licenses/>.
//** **
//**************************************************************************/ **************************************************************************/
#include "tableset.h" #include "tableset.h"

View File

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