Nut/src/wherephrase.h

232 lines
5.8 KiB
C
Raw Normal View History

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/>.
**
**************************************************************************/
#ifndef PHRASE_H
#define PHRASE_H
2016-05-20 21:13:49 +08:00
#include <QtCore/qglobal.h>
2016-05-21 16:09:03 +08:00
#include <QVariant>
#include <QDate>
#include <QDateTime>
#include <QTime>
2016-06-05 20:22:26 +08:00
#include <QSharedPointer>
2017-02-01 18:01:21 +08:00
#include "defines.h"
2017-05-25 23:46:54 +08:00
#include "dbgeography.h"
2016-05-21 16:09:03 +08:00
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-21 16:09:03 +08:00
class SqlGeneratorBase;
2016-06-05 20:22:26 +08:00
class PhraseData{
public:
2016-05-21 16:09:03 +08:00
enum Condition
{
2016-06-05 20:22:26 +08:00
NotAssign = 0,
Equal,
2016-05-21 16:09:03 +08:00
Less,
LessEqual,
Null,
In,
Like,
2016-06-05 20:22:26 +08:00
Not = 10,
NotEqual,
2016-05-21 16:09:03 +08:00
GreaterEqual,
Greater,
NotNull,
NotIn,
NotLike,
And = 20,
Or,
2016-06-05 20:22:26 +08:00
2016-05-21 16:09:03 +08:00
Append,
Set,
Add,
Minus,
Multiple,
2017-05-25 23:46:54 +08:00
Divide,
//special types
Distance
2016-05-21 16:09:03 +08:00
};
enum Type{
Field,
WithVariant,
WithOther,
WithoutOperand
};
Type type;
Condition operatorCond;
QString text;
const PhraseData *left;
const PhraseData *right;
QVariant operand;
PhraseData(const char *className, const char* s);
PhraseData(PhraseData *l, Condition o);
PhraseData(PhraseData *l, Condition o, const PhraseData *r);
PhraseData(PhraseData *l, Condition o, QVariant r);
~PhraseData();
};
2016-05-24 14:47:37 +08:00
class WherePhrase{
protected:
2016-06-05 20:22:26 +08:00
PhraseData *_data;
QSharedPointer<PhraseData> _dataPointer;
2016-05-24 14:47:37 +08:00
2016-05-20 21:13:49 +08:00
public:
2016-05-24 14:47:37 +08:00
WherePhrase(const char *className, const char* s);
2016-05-21 16:09:03 +08:00
2016-06-05 20:22:26 +08:00
WherePhrase(const WherePhrase &l);
WherePhrase(WherePhrase *l);
WherePhrase(WherePhrase *l, PhraseData::Condition o);
WherePhrase(WherePhrase *l, PhraseData::Condition o, WherePhrase *r);
WherePhrase(WherePhrase *l, PhraseData::Condition o, QVariant r);
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
~WherePhrase();
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase operator ==(const WherePhrase &other);
WherePhrase operator !=(const WherePhrase &other);
WherePhrase operator <(const WherePhrase &other);
WherePhrase operator >(const WherePhrase &other);
WherePhrase operator <=(const WherePhrase &other);
WherePhrase operator >=(const WherePhrase &other);
2017-05-31 00:19:37 +08:00
WherePhrase operator !();
2016-05-24 14:47:37 +08:00
WherePhrase operator =(const WherePhrase &other);
WherePhrase operator +(const WherePhrase &other);
WherePhrase operator -(const WherePhrase &other);
WherePhrase operator *(const WherePhrase &other);
WherePhrase operator /(const WherePhrase &other);
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase operator &&(const WherePhrase &other);
WherePhrase operator ||(const WherePhrase &other);
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase operator &(const WherePhrase &other);
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase operator ==(const QVariant &other);
WherePhrase operator !=(const QVariant &other);
WherePhrase operator <(const QVariant &other);
WherePhrase operator >(const QVariant &other);
WherePhrase operator <=(const QVariant &other);
WherePhrase operator >=(const QVariant &other);
2016-05-21 16:09:03 +08:00
2016-06-05 20:22:26 +08:00
PhraseData *data() const;
2016-05-24 14:47:37 +08:00
};
2017-05-25 23:46:54 +08:00
template<typename T>
2016-05-24 14:47:37 +08:00
class FieldPhrase: public WherePhrase{
public:
FieldPhrase(const char *className, const char* s);
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase operator =(const QVariant &other);
WherePhrase operator !();
2016-05-21 16:09:03 +08:00
2016-05-24 14:47:37 +08:00
WherePhrase isNull();
2017-05-31 00:19:37 +08:00
WherePhrase in(QList<T> list);
// WherePhrase in(QStringList list);
2016-05-24 14:47:37 +08:00
WherePhrase like(QString pattern);
2016-05-20 21:13:49 +08:00
};
2016-06-05 20:22:26 +08:00
//TODO: make FieldPhrase template class
//template <typename T>
//class FieldPhrase: public WherePhrase{
//};
2017-05-25 23:46:54 +08:00
template<typename T>
Q_OUTOFLINE_TEMPLATE FieldPhrase<T>::FieldPhrase(const char *className, const char *s) : WherePhrase(className, s)
{
// qDebug() << "(" << this << ")" << "FieldPhrase ctor" << className << s;
}
template<typename T>
Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::operator =(const QVariant &other)
{
return WherePhrase(this, PhraseData::Set, other);
}
template<typename T>
Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::operator !()
{
if(_data->operatorCond < 20)
_data->operatorCond = (PhraseData::Condition)((_data->operatorCond + 10) % 20);
else
qFatal("Operator ! can not aplied to non condition statements");
return this;//WherePhrase(this, PhraseData::Not);
}
template<typename T>
Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::isNull(){
return WherePhrase(this, PhraseData::Null);
}
template<typename T>
2017-05-31 00:19:37 +08:00
Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::in(QList<T> list)
2017-05-25 23:46:54 +08:00
{
2017-05-31 00:19:37 +08:00
QVariantList vlist;
foreach (T t, list)
vlist.append(QVariant::fromValue(t));
2017-05-25 23:46:54 +08:00
2017-05-31 00:19:37 +08:00
return WherePhrase(this, PhraseData::In, vlist);
2017-05-25 23:46:54 +08:00
}
2017-05-31 00:19:37 +08:00
//template<typename T>
//Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::in(QStringList list)
//{
// return WherePhrase(this, PhraseData::In, list);
//}
2017-05-25 23:46:54 +08:00
template<typename T>
Q_OUTOFLINE_TEMPLATE WherePhrase FieldPhrase<T>::like(QString pattern)
{
return WherePhrase(this, PhraseData::Like, pattern);
}
template<>
class FieldPhrase<DbGeography>: public WherePhrase {
public:
FieldPhrase(const char *className, const char* s) : WherePhrase(className, s){
}
WherePhrase distance(const DbGeography &geo) {
return WherePhrase(this, PhraseData::Distance, QVariant::fromValue(geo));
}
};
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE
2016-05-21 16:09:03 +08:00
#endif // PHRASE_H