Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision dbf92b46

Von Christian Ehringfeld vor etwa 9 Jahren hinzugefügt

  • ID dbf92b46962b690efc729933026c728feb5f6492
  • Vorgänger 612083d3
  • Nachfolger 8925defa

some comfort methods for query builder

Unterschiede anzeigen:

src/entitymanager.h
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
bool joinBaseClasses = false, const bool resolveRelations = true) {
QSharedPointer<Entity> e = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance<T *>());
(EntityInstanceFactory::createInstance<T*>());
if (e) {
Query query = Query(QStringList(e->getTablename()));
if (joinBaseClasses) {
src/query.cpp
*/
#include "query.h"
#include "entity.h"
using namespace CuteEntityManager;
Query::Query() {
}
......
this->groupBy.append(groupBy);
this->distinct = distinct;
this->having = having;
}
void Query::appendWhere(const QString &condition) {
src/query.h
namespace CuteEntityManager {
class Condition;
class OrderBy;
class Entity;
enum class Direction;
class Query {
public:
......
quint64 offset = 0;
};
/**
* @brief The JokerPosition enum
* FRONT -> e.g. "%foo"
* BEHIND -> e.g. "foo%"
* BOTH -> e.g. "%foo%"
*/
enum class JokerPosition {
FRONT, // e.g. "%foo"
BEHIND, // e.g. "foo%"
BOTH, // e.g. "%foo%"
FRONT,
BEHIND,
BOTH,
NONE
};
src/querybuilder.cpp
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
entity->getTablename())) + this->joinSuperClasses(
entity) + " WHERE " + this->schema->quoteColumnName(entity->getTablename() + "."
+ pk) + "= " + this->placeHolder(pk) + this->limit(1, offset));
+ pk) + this->equalOperator() + " " + this->placeHolder(pk) + this->limit(1, offset));
this->bindValue(pk, id, q);
return q;
}
......
sql += " " + this->whereKeyword() + " ";
sql += this->schema->quoteColumnName(
attribute);
sql += " = " + this->placeHolder(pk) + ";";
sql += " " + this->equalOperator()+" " + this->placeHolder(pk) + ";";
q.prepare(sql);
this->bindValue(pk, id, q);
return q;
......
QString pkCol = "id";
QString sql = "DELETE FROM " + this->schema->quoteTableName(
tableName) + " WHERE " + this->schema->quoteColumnName(
attribute) + "=" + this->placeHolder(pkCol);
attribute) + this->equalOperator() + this->placeHolder(pkCol);
q.prepare(sql);
this->bindValue(pkCol, id, q);
return q;
......
return "COUNT";
}
QString QueryBuilder::notEqualOperator() const {
return "!=";
}
QString QueryBuilder::equalOperator() const {
return "=";
}
QString QueryBuilder::fromKeyword() const {
return "FROM";
}
QString QueryBuilder::isNullKeywords() const {
return "IS NULL";
}
QString QueryBuilder::isNotNullKeywords() const {
return "IS NOT NULL";
}
Expression QueryBuilder::inFunction(QString column,
QList<QVariant> values, bool notOp) const {
QString condition = "";
......
ignoreID, primaryKey);
}
QString QueryBuilder::where(const QString &key, const QVariant &var, bool withKeyword, bool select, bool notEqual) const {
QString r = (withKeyword ? " WHERE " : "");
r += this->schema->quoteColumnName(key) + (var.isNull() && select
? (" " +(notEqual ? this->isNotNullKeywords(): this->isNullKeywords()))
: (notEqual ? this->notEqualOperator() : this->equalOperator() ) + this->placeHolder(key));
return r;
}
QString QueryBuilder::attributes(const QHash<QString, QVariant> &m, bool select,
const QString &conjunction,
bool ignoreID, const QString &primaryKey) const {
......
if (!rc.isEmpty()) {
rc += " " + conjunction + " ";
}
rc += this->schema->quoteColumnName(i.key()) + (i.value().isNull()
&& select ? " is null"
: "=" + this->placeHolder(i.key()));
rc += this->where(i.key(), i.value(), false, select);
}
}
return rc;
......
}
Expression QueryBuilder::isNull(QString column) const {
return Expression(this->schema->quoteColumnName(column) + " IS NULL");
return Expression(this->schema->quoteColumnName(column) + this->isNullKeywords());
}
Expression QueryBuilder::isNotNull(QString column) const {
return Expression(this->schema->quoteColumnName(column) + " IS " +
this->notKeyword() + " NULL");
return Expression(this->schema->quoteColumnName(column) + this->isNotNullKeywords());
}
Expression QueryBuilder::plainOr() const {
......
return exp;
}
Expression QueryBuilder::equal(QString &key, QVariant &value) {
Expression exp = Expression(this->where(key, value, false, true, false));
exp.appendParam(key,value);
return exp;
}
Expression QueryBuilder::notEqual(QString &key, QVariant &value) {
Expression exp = Expression(this->where(key, value, false, true, true));
exp.appendParam(key,value);
return exp;
}
Expression QueryBuilder::between(QString column, QVariant firstValue,
QVariant secondValue) const {
QString firstPh = column + "_bet1";
......
condition += " " + this->orKeyword() + " ";
}
condition += this->schema->quoteColumnName(i.key()) + (like ? " " +
this->likeKeyword() + " " : "=") +
this->likeKeyword() + " " : this->equalOperator()) +
this->placeHolder(i.key());
exp.appendParam(i.key(), i.value());
}
src/querybuilder.h
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");
Expression between(QString column, QVariant firstValue,
QVariant secondValue) const;
......
const QString &conjunction,
bool ignoreID = false, 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 attributes(const QHash<QString, QVariant> &m, bool select = true,
const QString &conjunction = ",",
bool ignoreID = false, const QString &primaryKey = "id") const;
......
virtual QString inKeyword() const;
virtual QString whereKeyword() const;
virtual QString countKeyword() const;
virtual QString notEqualOperator() const;
virtual QString equalOperator() const;
virtual QString fromKeyword() const;
virtual QString isNullKeywords() const;
virtual QString isNotNullKeywords() const;
virtual Expression inFunction(QString column, QList<QVariant> values,
bool notOp = false) const;
virtual QString between(QString colName, QString valName1, QString valName2,

Auch abrufbar als: Unified diff