Revision 358e1e04
Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt
src/query.cpp | ||
---|---|---|
*/
|
||
|
||
#include "query.h"
|
||
#include "entity.h"
|
||
#include "querybuilder.h"
|
||
using namespace CuteEntityManager;
|
||
Query::Query() {
|
||
}
|
||
... | ... | |
having = value;
|
||
}
|
||
|
||
Expression Query::whereCondition(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant value) const {
|
||
return qb->where(column, value);
|
||
}
|
||
|
||
Join Query::joinClasses(const QSharedPointer<QueryBuilder> &qb,
|
||
const QSharedPointer<Entity> &mainEntity, const QSharedPointer<Entity> &foreignEntity,
|
||
const QString &joinType) const {
|
||
return qb->joinClasses(mainEntity, foreignEntity, joinType);
|
||
}
|
||
|
||
QList<Join> Query::joinBaseClasses(const QSharedPointer<QueryBuilder> &qb,
|
||
const QSharedPointer<Entity> &entity) {
|
||
return qb->joinBaseClasses(entity);
|
||
}
|
||
|
||
Expression Query::whereCondition(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions, QString conjunction) const {
|
||
return qb->where(conditions, conjunction);
|
||
}
|
||
|
||
Expression Query::whereCondition(const QSharedPointer<QueryBuilder> &qb,
|
||
QString condition, QHash<QString, QVariant> values) const {
|
||
return qb->where(condition, values);
|
||
}
|
||
|
||
Expression Query::equal(const QSharedPointer<QueryBuilder> &qb, QString &key,
|
||
QVariant &value) const {
|
||
return qb->equal(key, value);
|
||
}
|
||
|
||
Expression Query::notEqual(const QSharedPointer<QueryBuilder> &qb, QString &key,
|
||
QVariant &value) const {
|
||
return qb->notEqual(key, value);
|
||
}
|
||
|
||
Expression Query::between(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant firstValue, QVariant secondValue) const {
|
||
return qb->between(column, firstValue, secondValue);
|
||
}
|
||
|
||
Expression Query::notBetween(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant firstValue, QVariant secondValue) const {
|
||
return qb->notBetween(column, firstValue, secondValue);
|
||
}
|
||
|
||
Expression Query::in(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QList<QVariant> values) const {
|
||
return qb->in(column, values);
|
||
}
|
||
|
||
Expression Query::notIn(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QList<QVariant> values) const {
|
||
return qb->notIn(column, values);
|
||
}
|
||
|
||
Expression Query::notOperator(const QSharedPointer<QueryBuilder> &qb, QString op,
|
||
QString column, QVariant value) const {
|
||
return qb->notOperator(op, column, value);
|
||
}
|
||
|
||
Expression Query::orOperator(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions, bool like) const {
|
||
return qb->orOperator(conditions, like);
|
||
}
|
||
|
||
Expression Query::orOperator(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->orOperator();
|
||
}
|
||
|
||
Expression Query::norOperator(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->norOperator();
|
||
}
|
||
|
||
Expression Query::andOperator(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions) const {
|
||
return qb->andOperator(conditions);
|
||
}
|
||
|
||
Expression Query::andOperator(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->andOperator();
|
||
}
|
||
|
||
Expression Query::nandOperator(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->nandOperator();
|
||
}
|
||
|
||
Expression Query::arbitraryOperator(const QSharedPointer<QueryBuilder> &qb, QString op,
|
||
QString column, QVariant value) const {
|
||
return qb->arbitraryOperator(op, column, value);
|
||
}
|
||
|
||
Expression Query::isNull(const QSharedPointer<QueryBuilder> &qb, QString column) const {
|
||
return qb->isNull(column);
|
||
}
|
||
|
||
Expression Query::isNotNull(const QSharedPointer<QueryBuilder> &qb,
|
||
QString column) const {
|
||
return qb->isNotNull(column);
|
||
}
|
||
|
||
Expression Query::plainOr(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->plainOr();
|
||
}
|
||
|
||
Expression Query::plainNor(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->plainNor();
|
||
}
|
||
|
||
Expression Query::plainAnd(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->plainAnd();
|
||
}
|
||
|
||
Expression Query::plainNand(const QSharedPointer<QueryBuilder> &qb) const {
|
||
return qb->plainNand();
|
||
}
|
||
|
||
Expression Query::like(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant value, JokerPosition jp, QChar wildcard) {
|
||
return qb->like(column, value, jp, wildcard);
|
||
}
|
||
|
||
Expression Query::like(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions, QString conjunction, JokerPosition jp,
|
||
QChar wildcard) {
|
||
return qb->like(conditions, conjunction, jp, wildcard);
|
||
}
|
||
|
||
QString Query::getSelectOption() const {
|
||
return selectOption;
|
||
}
|
src/query.h | ||
---|---|---|
namespace CuteEntityManager {
|
||
class Condition;
|
||
class OrderBy;
|
||
class QueryBuilder;
|
||
class Entity;
|
||
/**
|
||
* @brief The JokerPosition enum
|
||
* FRONT -> e.g. "%foo"
|
||
* BEHIND -> e.g. "foo%"
|
||
* BOTH -> e.g. "%foo%"
|
||
*/
|
||
enum class JokerPosition {
|
||
FRONT,
|
||
BEHIND,
|
||
BOTH,
|
||
NONE
|
||
};
|
||
enum class Direction;
|
||
class Query {
|
||
public:
|
||
... | ... | |
QList<Expression> getHaving() const;
|
||
void setHaving(const QList<Expression> &value);
|
||
|
||
|
||
Expression whereCondition(const QSharedPointer<QueryBuilder> &qb, QString column, QVariant value) const;
|
||
Join joinClasses(const QSharedPointer<QueryBuilder> &qb,
|
||
const QSharedPointer<Entity> &mainEntity,
|
||
const QSharedPointer<Entity> &foreignEntity,
|
||
const QString &joinType = QStringLiteral("LEFT JOIN"))const;
|
||
QList<Join> joinBaseClasses(const QSharedPointer<QueryBuilder> &qb,
|
||
const QSharedPointer<Entity> &entity);
|
||
Expression whereCondition(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions,
|
||
QString conjunction = QStringLiteral("AND")) const;
|
||
Expression whereCondition(const QSharedPointer<QueryBuilder> &qb, QString condition,
|
||
QHash<QString, QVariant> values = QHash<QString, QVariant>()) const;
|
||
Expression equal(const QSharedPointer<QueryBuilder> &qb, QString &key, QVariant &value) const;
|
||
Expression notEqual(const QSharedPointer<QueryBuilder> &qb, QString &key,
|
||
QVariant &value) const;
|
||
Expression between(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant firstValue,
|
||
QVariant secondValue) const;
|
||
Expression notBetween(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QVariant firstValue,
|
||
QVariant secondValue) const;
|
||
Expression in(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QList<QVariant> values) const;
|
||
Expression notIn(const QSharedPointer<QueryBuilder> &qb, QString column,
|
||
QList<QVariant> values) const;
|
||
Expression notOperator(const QSharedPointer<QueryBuilder> &qb, QString op, QString column,
|
||
QVariant value) const;
|
||
Expression orOperator(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions,
|
||
bool like = false) const;
|
||
Expression orOperator(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression norOperator(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression andOperator(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions) const;
|
||
Expression andOperator(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression nandOperator(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression arbitraryOperator(const QSharedPointer<QueryBuilder> &qb, QString op,
|
||
QString column,
|
||
QVariant value) const;
|
||
Expression isNull(const QSharedPointer<QueryBuilder> &qb, QString column) const;
|
||
Expression isNotNull(const QSharedPointer<QueryBuilder> &qb, QString column) const;
|
||
|
||
Expression plainOr(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression plainNor(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression plainAnd(const QSharedPointer<QueryBuilder> &qb)
|
||
const;
|
||
Expression plainNand(const QSharedPointer<QueryBuilder> &qb) const;
|
||
Expression like(const QSharedPointer<QueryBuilder> &qb, QString column, QVariant value,
|
||
JokerPosition jp = JokerPosition::BOTH, QChar wildcard = '%');
|
||
Expression like(const QSharedPointer<QueryBuilder> &qb,
|
||
QHash<QString, QVariant> conditions,
|
||
QString conjunction = QStringLiteral("AND"),
|
||
JokerPosition jp = JokerPosition::BOTH, QChar wildcard = '%');
|
||
|
||
private:
|
||
QList<Expression> select;
|
||
QString selectOption = QStringLiteral("");
|
||
... | ... | |
quint64 limit = 0;
|
||
quint64 offset = 0;
|
||
};
|
||
|
||
/**
|
||
* @brief The JokerPosition enum
|
||
* FRONT -> e.g. "%foo"
|
||
* BEHIND -> e.g. "foo%"
|
||
* BOTH -> e.g. "%foo%"
|
||
*/
|
||
enum class JokerPosition {
|
||
FRONT,
|
||
BEHIND,
|
||
BOTH,
|
||
NONE
|
||
};
|
||
|
||
|
||
}
|
||
|
||
#endif // QUERY_H
|
src/querybuilder.cpp | ||
---|---|---|
return exp;
|
||
}
|
||
|
||
Expression QueryBuilder::where(QString column, QVariant value) {
|
||
QString placeholder = column + "_where";
|
||
Expression exp = Expression(this->schema->quoteColumnName(column) + "=" +
|
||
this->placeHolder(placeholder));
|
||
exp.appendParam(placeholder, value);
|
||
return exp;
|
||
}
|
||
|
||
Join QueryBuilder::joinClasses(const QSharedPointer<Entity> &mainEntity,
|
||
const QSharedPointer<Entity> &foreignEntity, const QString &joinType) const {
|
||
Join j = Join(foreignEntity->getTablename(),
|
||
... | ... | |
return joined;
|
||
}
|
||
|
||
//QHash<QString, QVariant> values
|
||
|
||
Expression QueryBuilder::where(QHash<QString, QVariant> conditions,
|
||
QString conjunction) const {
|
||
Expression exp = Expression(this->where(conditions, conjunction, false, "id",
|
||
... | ... | |
return exp;
|
||
}
|
||
|
||
Expression QueryBuilder::where(QString condition,
|
||
QHash<QString, QVariant> values) const {
|
||
Expression exp = Expression(condition);
|
||
for (auto i = values.constBegin(); i != values.constEnd(); ++i) {
|
||
exp.appendParam(i.key(), i.value());
|
||
Expression QueryBuilder::where(QString c, QVariant value) {
|
||
Expression exp;
|
||
if(value.type() == QVariant::Hash) {
|
||
auto values = value.toHash();
|
||
exp = Expression(c);
|
||
for (auto i = values.constBegin(); i != values.constEnd(); ++i) {
|
||
exp.appendParam(i.key(), i.value());
|
||
}
|
||
} else {
|
||
QString placeholder = c + "_where";
|
||
exp = Expression(this->schema->quoteColumnName(c) + "=" +
|
||
this->placeHolder(placeholder));
|
||
exp.appendParam(placeholder, value);
|
||
}
|
||
return exp;
|
||
}
|
src/querybuilder.h | ||
---|---|---|
void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q,
|
||
bool ignoreID = false, const QString &primaryKey = QStringLiteral("id")) const;
|
||
void bindValue(const QString &key, const QVariant &value, QSqlQuery &q) const;
|
||
Expression where(QString column, QVariant value);
|
||
/**
|
||
* @brief where
|
||
* @param c if value is a single value than c is the column name if value is a QHash<QString, QVariant> than its a condition
|
||
* @param value
|
||
* @return
|
||
*/
|
||
Expression where(QString c, QVariant value);
|
||
Join joinClasses(const QSharedPointer<Entity> &mainEntity,
|
||
const QSharedPointer<Entity> &foreignEntity,
|
||
const QString &joinType = QStringLiteral("LEFT JOIN"))const;
|
||
... | ... | |
*/
|
||
Expression where(QHash<QString, QVariant> conditions,
|
||
QString conjunction = QStringLiteral("AND")) const;
|
||
Expression where(QString condition,
|
||
QHash<QString, QVariant> values = QHash<QString, QVariant>()) const;
|
||
Expression equal(QString &key, QVariant &value);
|
||
Expression notEqual(QString &key, QVariant &value);
|
||
//void where(Query &query,QHash<QString, QList<QVariant>> conditions, QString concat="AND");
|
||
... | ... | |
bool ignoreID = false) const;
|
||
QString where(const QHash<QString, QVariant> &m,
|
||
const QString &conjunction,
|
||
bool ignoreID = false, const QString &primaryKey = "id",
|
||
bool ignoreID, const QString &primaryKey = "id",
|
||
bool withKeyword = true) const;
|
||
QString where(const QString &key, const QVariant &var, bool withKeyword, bool select=true, bool notEqual=false) const;
|
||
QString where(const QString &key, const QVariant &var, bool withKeyword,
|
||
bool select = true, bool notEqual = false) const;
|
||
QString attributes(const QHash<QString, QVariant> &m, bool select = true,
|
||
const QString &conjunction = ",",
|
||
bool ignoreID = false, const QString &primaryKey = "id") const;
|
Auch abrufbar als: Unified diff
more comfort for query class