Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (8,44 KB) Statistiken
| Zweig: | Revision:
38838b5b Christian Ehringfeld
/*
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
*
* This program 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.
*
* This program 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 General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

2ee5022f Christian Ehringfeld
#include "queryinterpreter.h"
3160499c Christian Ehringfeld
#include "join.h"
#include "query.h"
dbd41a3a Christian Ehringfeld
#include "attributeresolver.h"
3160499c Christian Ehringfeld
#include "orderby.h"
#include "expression.h"
#include "schema.h"
2ee5022f Christian Ehringfeld
using namespace CuteEntityManager;


dbd41a3a Christian Ehringfeld
QueryInterpreter::QueryInterpreter(QSharedPointer<AttributeResolver> ar) {
this->ar = ar;
2ee5022f Christian Ehringfeld
}

dbd41a3a Christian Ehringfeld
QSqlQuery QueryInterpreter::build(Query &q, const QMetaObject *obj) {
3160499c Christian Ehringfeld
QList<QString> clauses = QList<QString>();
1b167b6c Christian Ehringfeld
clauses.append(this->buildSelect(q, q.getSelect(), q.getDistinct(),
3160499c Christian Ehringfeld
q.getSelectOption()));
clauses.append(this->buildFrom(q.getFrom()));
clauses.append(this->buildJoin(q.getJoins()));
1b167b6c Christian Ehringfeld
clauses.append(this->buildWhere(q, q.getWhere()));
3160499c Christian Ehringfeld
clauses.append(this->buildGroupBy(q.getGroupBy()));
1b167b6c Christian Ehringfeld
clauses.append(this->buildHaving(q, q.getHaving()));
3160499c Christian Ehringfeld
QString sql = "";
bool first = true;
for (int i = 0; i < clauses.size(); ++i) {
QString clause = clauses.at(i);
if (!clause.isEmpty()) {
if (first) {
first = false;
} else {
dbd41a3a Christian Ehringfeld
sql += this->ar->getQb()->getSeparator();
3160499c Christian Ehringfeld
}
sql += clause;
}
}
sql = this->buildOrderByAndLimit(sql, q.getOrderBy(), q.getLimit(),
q.getOffset());
dbd41a3a Christian Ehringfeld
QSqlQuery sqlQuery = this->ar->getQb()->getQuery();
3160499c Christian Ehringfeld
sqlQuery.prepare(sql);
dbd41a3a Christian Ehringfeld
this->ar->getQb()->bindValues(q.getParams(), sqlQuery, false);
3160499c Christian Ehringfeld
return sqlQuery;
}

1b167b6c Christian Ehringfeld
QString QueryInterpreter::buildSelect(Query &q,
const QList<Expression> &columns,
3160499c Christian Ehringfeld
const bool &distinct, const QString &selectOption) const {
dbd41a3a Christian Ehringfeld
QString sqlSelect = distinct ? ("SELECT " + this->ar->getQb()->distinct() + " ") :
87739ae0 Christian Ehringfeld
"SELECT ";
3160499c Christian Ehringfeld
if (!selectOption.isEmpty()) {
38335e94 Christian Ehringfeld
sqlSelect += selectOption + " ";
3160499c Christian Ehringfeld
}
if (columns.isEmpty()) {
87739ae0 Christian Ehringfeld
return sqlSelect + "*";
3160499c Christian Ehringfeld
}
bool first = true;
for (int i = 0; i < columns.size(); ++i) {
if (first) {
first = false;
} else {
sqlSelect += ", ";
}
Expression e = columns.at(i);
1b167b6c Christian Ehringfeld
q.appendParams(e.getParams());
3160499c Christian Ehringfeld
QString nExp = e.getExpression();
if (e.getOnlyColumn()) {
dbd41a3a Christian Ehringfeld
sqlSelect += this->ar->getQb()->getSchema()->quoteColumnName(e.getExpression());
3160499c Christian Ehringfeld
} else if (!nExp.contains("(")) {
QRegularExpression re =
QRegularExpression(
QRegularExpression::escape("/^(.*?)(?i:\\s+as\\s+|\\s+)([\\w\\-_\\.]+)$/"));
ae872f6f Christian Ehringfeld
re.optimize();
3160499c Christian Ehringfeld
QRegularExpressionMatchIterator iterator = re.globalMatch(nExp, 0,
QRegularExpression::PartialPreferFirstMatch);
if (iterator.hasNext()) {
for (int var = 0; var < 2; ++var) {
QRegularExpressionMatch match = iterator.next();
if (var == 1) {
dbd41a3a Christian Ehringfeld
sqlSelect += this->ar->getQb()->getSchema()->quoteColumnName(
match.captured()) + " AS " + this->ar->getQb()->getSchema()->quoteColumnName("a" +
3160499c Christian Ehringfeld
QString::number(i));
}
}
} else {
dbd41a3a Christian Ehringfeld
nExp = this->ar->getQb()->getSchema()->quoteColumnName(nExp);
3160499c Christian Ehringfeld
}
} else {
dbd41a3a Christian Ehringfeld
sqlSelect += nExp + " AS " + this->ar->getQb()->getSchema()->quoteColumnName("a" +
3160499c Christian Ehringfeld
QString::number(i));
}
}
return sqlSelect;
}

QString QueryInterpreter::buildFrom(const QStringList &from) const {
if (from.isEmpty()) {
return "";
}
dbd41a3a Christian Ehringfeld
auto tables = this->ar->getQb()->quoteTableNames(from);
3160499c Christian Ehringfeld
QString clause = "FROM ";
bool first = true;
for (int var = 0; var < tables.size(); ++var) {
if (first) {
first = false;
} else {
clause += ", ";
}
f89ad1ad Christian Ehringfeld
clause += tables.at(var);
3160499c Christian Ehringfeld
}
return clause;
}

QString QueryInterpreter::buildJoin(const QList<Join> &joins) const {
if (joins.isEmpty()) {
return "";
}
QString sqlJoin = "";
for (int i = 0; i < joins.size(); ++i) {
Join j = joins.at(i);
dbd41a3a Christian Ehringfeld
sqlJoin += j.getType() + this->ar->getQb()->getSeparator() +
this->ar->getQb()->getSchema()->quoteTableName(j.getForeignTable());
506067a2 Christian Ehringfeld
if (!j.getExpression().getExpression().isEmpty()) {
6cfbc25e Christian Ehringfeld
QString expression = j.getExpression().getExpression();
int count = expression.count("=");
if (count < 1) {
dbd41a3a Christian Ehringfeld
expression = this->ar->getQb()->getSchema()->quoteTableName(expression);
6cfbc25e Christian Ehringfeld
} else if (count == 1) {
QStringList list = expression.split("=");
dbd41a3a Christian Ehringfeld
expression = this->ar->getQb()->getSchema()->quoteColumnName(list.at(
6cfbc25e Christian Ehringfeld
0).trimmed()) + " = ";
dbd41a3a Christian Ehringfeld
expression += this->ar->getQb()->getSchema()->quoteColumnName(list.at(1).trimmed());
6cfbc25e Christian Ehringfeld
}
sqlJoin += " ON " + expression;
3160499c Christian Ehringfeld
}
}
return sqlJoin;
}

1b167b6c Christian Ehringfeld
QString QueryInterpreter::buildWhere(Query &q,
const QList<Expression> &conditions)
3160499c Christian Ehringfeld
const {
1b167b6c Christian Ehringfeld
QString where = this->buildCondition(q, conditions);
3160499c Christian Ehringfeld
return where.isEmpty() ? "" : ("WHERE " + where);
}
2ee5022f Christian Ehringfeld
3160499c Christian Ehringfeld
QString QueryInterpreter::buildGroupBy(const QStringList &groupBy) const {
dbd41a3a Christian Ehringfeld
return groupBy.isEmpty() ? "" : "GROUP BY " + this->ar->getQb()->buildColumns(
3160499c Christian Ehringfeld
groupBy);
2ee5022f Christian Ehringfeld
}
3160499c Christian Ehringfeld
1b167b6c Christian Ehringfeld
QString QueryInterpreter::buildHaving(Query &q,
19cf5548 Christian Ehringfeld
const QList<Expression> &conditions) const {
1b167b6c Christian Ehringfeld
QString having = this->buildCondition(q, conditions);
3160499c Christian Ehringfeld
return having.isEmpty() ? "" : ("HAVING " + having);
}

QString QueryInterpreter::buildOrderByAndLimit(QString sql,
const QList<OrderBy> &orderBy, const quint64 &limit,
const quint64 &offset) const {
QString sqlOrderBy = this->buildOrderBy(orderBy);
if (!sqlOrderBy.isEmpty()) {
dbd41a3a Christian Ehringfeld
sql += this->ar->getQb()->getSeparator() + sqlOrderBy;
3160499c Christian Ehringfeld
}
dbd41a3a Christian Ehringfeld
QString sqlLimit = this->ar->getQb()->limit(limit, offset, false);
3160499c Christian Ehringfeld
if (!sqlLimit.isEmpty()) {
dbd41a3a Christian Ehringfeld
sql += this->ar->getQb()->getSeparator() + sqlLimit;
3160499c Christian Ehringfeld
}
return sql;
}

QString QueryInterpreter::buildOrderBy(const QList<OrderBy> &columns) const {
if (columns.isEmpty()) {
return "";
}
bool first = true;
QString sqlOrder = "ORDER BY ";
for (int i = 0; i < columns.size(); ++i) {
if (first) {
first = false;
} else {
sqlOrder += ", ";
}
OrderBy order = columns.at(i);
if (order.getColumn().isEmpty()) {
sqlOrder += order.getExpressedDirection().getExpression();
} else {
dbd41a3a Christian Ehringfeld
sqlOrder += this->ar->getQb()->getSchema()->quoteColumnName(order.getColumn());
3160499c Christian Ehringfeld
switch (order.getDirection()) {
case Direction::SORT_ASC:
sqlOrder += " ASC";
break;
case Direction::SORT_DESC:
sqlOrder += " DESC";
break;
default:
break;
}
}
}
return sqlOrder;
}

1b167b6c Christian Ehringfeld
QString QueryInterpreter::buildCondition(Query &q,
19cf5548 Christian Ehringfeld
const QList<Expression> &conditions) const {
3160499c Christian Ehringfeld
if (conditions.isEmpty()) {
return "";
}
506067a2 Christian Ehringfeld
QString sqlCondition = "";
bool first = true;
for (int i = 0; i < conditions.size(); ++i) {
Expression exp = conditions.at(i);
QString expression = exp.getExpression();
if (!expression.isEmpty()) {
if (first) {
first = false;
} else if (expression.at(0) != ' ') {
dbd41a3a Christian Ehringfeld
sqlCondition += this->ar->getQb()->getSeparator();
506067a2 Christian Ehringfeld
}
}
sqlCondition += expression;
dc618bda Christian Ehringfeld
q.appendParams(exp.getParams());
506067a2 Christian Ehringfeld
}
return sqlCondition;
3160499c Christian Ehringfeld
}