Revision 31916fa0
Von Christian Ehringfeld vor etwa 10 Jahren hinzugefügt
| src/querybuilder.cpp | ||
|---|---|---|
|
const QString &tableName,
|
||
|
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
|
||
|
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
|
tableName)) + this->where(m, "AND", ignoreID) + this->limit(limit, offset));
|
||
|
tableName)) + this->where(m, this->andKeyword(), ignoreID) + this->limit(limit,
|
||
|
offset));
|
||
|
this->bindValues(m, q, ignoreID);
|
||
|
return q;
|
||
|
}
|
||
| ... | ... | |
|
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
|
entity->getTablename()),
|
||
|
QStringList(entity->getPrimaryKey())) + this->where(values,
|
||
|
"AND", true) + " LIMIT 1");
|
||
|
this->andKeyword(), true) + " LIMIT 1");
|
||
|
this->bindValues(values, q);
|
||
|
return q;
|
||
|
}
|
||
| ... | ... | |
|
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
|
entity->getTablename()),
|
||
|
QStringList(this->countFunction())) + this->where(
|
||
|
values, "AND", ignoreID));
|
||
|
values, this->andKeyword(), ignoreID));
|
||
|
this->bindValues(values, q, ignoreID);
|
||
|
return q;
|
||
|
}
|
||
| ... | ... | |
|
QSqlQuery QueryBuilder::update(const QString &tableName,
|
||
|
QHash<QString, QVariant> &attributes, const QString &primaryKey) const {
|
||
|
QSqlQuery q = this->database->getQuery("UPDATE " + this->schema->quoteTableName(
|
||
|
tableName) + " SET " + this->attributes(attributes) + " WHERE " +
|
||
|
tableName) + " SET " + this->attributes(attributes) + " " + this->whereKeyword()
|
||
|
+ " " +
|
||
|
this->schema->quoteColumnName(primaryKey) + " = " + this->placeHolder(
|
||
|
primaryKey) + ";");
|
||
|
this->bindValues(attributes, q);
|
||
| ... | ... | |
|
QSqlQuery q = this->database->getQuery();
|
||
|
QString sql = this->selectBase(QStringList(tableName), QStringList("*"));
|
||
|
QString pk = "id";
|
||
|
sql += " WHERE ";
|
||
|
sql += " " + this->whereKeyword() + " ";
|
||
|
sql += this->schema->quoteColumnName(
|
||
|
attribute);
|
||
|
sql += " = " + this->placeHolder(pk) + ";";
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
QString QueryBuilder::countFunction(const QString &distinctColumn) const {
|
||
|
return QString("COUNT(" + distinctColumn.isEmpty() ? "*" : (this->distinct() +
|
||
|
return QString(this->countKeyword() + "(" + (distinctColumn.isEmpty() ? "*" :
|
||
|
(this->distinct()) +
|
||
|
this->schema->quoteColumnName(distinctColumn)) + ")");
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
return "DISTINCT";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::notKeyword() const {
|
||
|
return "NOT";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::between() const {
|
||
|
return "BETWEEN";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::andKeyword() const {
|
||
|
return "AND";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::orKeyword() const {
|
||
|
return "OR";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::inKeyword() const {
|
||
|
return "IN";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::whereKeyword() const {
|
||
|
return "WHERE";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::countKeyword() const {
|
||
|
return "COUNT";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::inFunction(Query &q, QString column,
|
||
|
QList<QVariant> values) {
|
||
|
QString condition = "";
|
||
|
if (!values.isEmpty()) {
|
||
|
bool first = true;
|
||
|
condition = this->schema->quoteColumnName(column) + " " + this->inKeyword() +
|
||
|
" (";
|
||
|
for (int var = 0; var < values.size(); ++var) {
|
||
|
if (first) {
|
||
|
first = false;
|
||
|
} else {
|
||
|
condition += ", ";
|
||
|
}
|
||
|
QString paramName = column + "_in" + var;
|
||
|
condition += this->placeHolder(paramName);
|
||
|
q.appendParam(paramName, values.at(var));
|
||
|
}
|
||
|
condition += ")";
|
||
|
}
|
||
|
return condition;
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::between(QString colName, QString valName1,
|
||
|
QString valName2) {
|
||
|
return "(" + this->schema->quoteColumnName(colName) + " " + this->between() +
|
||
|
" " + this->placeHolder(valName1) + " " + this->andKeyword() + " " +
|
||
|
this->placeHolder(valName2) + ")";
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::entityClassname() const {
|
||
|
return QString("CuteEntityManager::Entity");
|
||
|
}
|
||
| ... | ... | |
|
if (first) {
|
||
|
first = false;
|
||
|
} else {
|
||
|
condition += " AND ";
|
||
|
condition += " " + this->andKeyword() + " ";
|
||
|
}
|
||
|
condition += this->schema->quoteColumnName(var.key()) + " = " +
|
||
|
this->placeHolder(var.key());
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
void QueryBuilder::plainOr(Query &query) {
|
||
|
query.appendCondition("OR");
|
||
|
query.appendCondition(this->orKeyword());
|
||
|
}
|
||
|
|
||
|
void QueryBuilder::plainAnd(Query &query) {
|
||
|
query.appendCondition("AND");
|
||
|
query.appendCondition(this->andKeyword());
|
||
|
}
|
||
|
|
||
|
|
||
| ... | ... | |
|
|
||
|
void QueryBuilder::between(Query &query, QString column, QVariant firstValue,
|
||
|
QVariant secondValue) {
|
||
|
|
||
|
QString firstPh = column + "_bet1";
|
||
|
QString secondPh = column + "_bet2";
|
||
|
query.appendParam(firstPh, firstValue);
|
||
|
query.appendParam(secondPh, secondValue);
|
||
|
query.appendCondition(this->between(column, firstPh, secondPh));
|
||
|
}
|
||
|
|
||
|
void QueryBuilder::in(Query &query, QString column, QList<QVariant> values) {
|
||
|
|
||
|
query.appendCondition(this->inFunction(query, column, values));
|
||
|
}
|
||
|
|
||
|
void QueryBuilder::notIn(Query &query, QString column, QList<QVariant> values) {
|
||
|
|
||
|
query.appendCondition(this->notKeyword() + " " + this->inFunction(query, column,
|
||
|
values));
|
||
|
}
|
||
|
|
||
|
void QueryBuilder::notOperator(Query &query, QString column, QVariant value) {
|
||
Auch abrufbar als: Unified diff
continuing query stuff