query enhancment

This commit is contained in:
blackdal 2017-05-31 20:40:35 +04:30
parent 36a298d5c0
commit c120ba8545
3 changed files with 49 additions and 20 deletions

View File

@ -43,12 +43,13 @@ class NUT_EXPORT Query : public QueryBase
QueryPrivate *d_ptr;
Q_DECLARE_PRIVATE(Query)
bool m_autoDelete;
public:
Query(Database *database, TableSetBase *tableSet);
Query(Database *database, TableSetBase *tableSet, bool autoDelete);
~Query();
int remove();
T *first();
int count();
@ -74,6 +75,7 @@ public:
Query<T> *orderBy(WherePhrase phrase);
int update(WherePhrase phrase);
int remove();
};
template <typename T>
@ -82,8 +84,8 @@ inline Query<T> *createQuery(TableSet<T> *tableSet){
}
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet) : QueryBase(database),
d_ptr(new QueryPrivate(this))
Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet, bool autoDelete) : QueryBase(database),
d_ptr(new QueryPrivate(this)), m_autoDelete(autoDelete)
{
Q_D(Query);
@ -180,7 +182,8 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
break;
}
deleteLater();
if(m_autoDelete)
deleteLater();
return result;
}
@ -199,12 +202,14 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
d->joinClassName);
QSqlQuery q = d->database->exec(sql);
qDebug() << sql;
while (q.next()) {
QVariant v = q.value(f.data()->text);
ret.append(v.value<F>());
}
deleteLater();
if(m_autoDelete)
deleteLater();
return ret;
}
@ -267,18 +272,6 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
return 0;
}
template<class T>
Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
{
Q_D(Query);
QString sql = d->database->sqlGenertor()->deleteCommand(d->wheres, d->tableName);
// d->_database->sqlGenertor()->deleteRecords(_tableName, queryText());
// sql = compileCommand(sql);
QSqlQuery q = d->database->exec(sql);
return q.numRowsAffected();
}
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &tableName)
{
@ -322,6 +315,22 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::update(WherePhrase phrase)
d->tableName);
qDebug() << sql;
QSqlQuery q = d->database->exec(sql);
if (m_autoDelete)
deleteLater();
return q.numRowsAffected();
}
template<class T>
Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
{
Q_D(Query);
QString sql = d->database->sqlGenertor()->deleteCommand(
d->wheres,
d->tableName);
qDebug() << sql;
QSqlQuery q = d->database->exec(sql);
return q.numRowsAffected();
}

View File

@ -20,6 +20,7 @@
#include <QDate>
#include <QDateTime>
#include <QPointF>
#include <QTime>
#include <QVariant>
@ -477,7 +478,17 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v)const
case QVariant::StringList:
case QVariant::List:
return "['" + v.toStringList().join("', '") + "']";
return "('" + v.toStringList().join("', '") + "')";
case QVariant::Point: {
QPoint pt = v.toPoint();
return QString("POINT(%1 %2)").arg(pt.x()).arg(pt.y());
}
case QVariant::PointF: {
QPointF pt = v.toPointF();
return QString("POINT(%1 %2)").arg(pt.x()).arg(pt.y());
}
case QVariant::Invalid:
qFatal("Invalud field value");

View File

@ -53,6 +53,7 @@ public:
T *at(int i) const;
const T &operator[](int i) const;
Query<T> *query();
Query<T> *query(bool autoDelete);
};
template<class T>
@ -70,7 +71,15 @@ Q_OUTOFLINE_TEMPLATE TableSet<T>::TableSet(Table *parent) : TableSetBase(parent)
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *TableSet<T>::query()
{
Query<T> *q = new Query<T>(_database, this);
Query<T> *q = new Query<T>(_database, this, true);
return q;
}
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *TableSet<T>::query(bool autoDelete)
{
Query<T> *q = new Query<T>(_database, this, autoDelete);
return q;
}