Revision dbd41a3a
Von Christian Ehringfeld vor fast 10 Jahren hinzugefügt
| src/queryinterpreter.cpp | ||
|---|---|---|
| 
     #include "queryinterpreter.h"
 
   | 
||
| 
     #include "join.h"
 
   | 
||
| 
     #include "query.h"
 
   | 
||
| 
     #include "querybuilder.h"
 
   | 
||
| 
     #include "attributeresolver.h"
 
   | 
||
| 
     #include "orderby.h"
 
   | 
||
| 
     #include "expression.h"
 
   | 
||
| 
     #include "schema.h"
 
   | 
||
| 
     using namespace CuteEntityManager;
 
   | 
||
| 
     | 
||
| 
     | 
||
| 
     QueryInterpreter::QueryInterpreter(QueryBuilder *builder) {
 
   | 
||
| 
         this->builder = builder;
 
   | 
||
| 
     QueryInterpreter::QueryInterpreter(QSharedPointer<AttributeResolver> ar) {
 
   | 
||
| 
         this->ar = ar;
 
   | 
||
| 
     }
 
   | 
||
| 
     | 
||
| 
     QSqlQuery QueryInterpreter::build(Query &q) {
 
   | 
||
| 
     QSqlQuery QueryInterpreter::build(Query &q, const QMetaObject *obj) {
 
   | 
||
| 
         QList<QString> clauses = QList<QString>();
 
   | 
||
| 
         clauses.append(this->buildSelect(q, q.getSelect(), q.getDistinct(),
 
   | 
||
| 
                                          q.getSelectOption()));
 
   | 
||
| ... | ... | |
| 
                 if (first) {
 
   | 
||
| 
                     first = false;
 
   | 
||
| 
                 } else {
 
   | 
||
| 
                     sql += this->builder->getSeparator();
 
   | 
||
| 
                     sql += this->ar->getQb()->getSeparator();
 
   | 
||
| 
                 }
 
   | 
||
| 
                 sql += clause;
 
   | 
||
| 
             }
 
   | 
||
| 
         }
 
   | 
||
| 
         sql = this->buildOrderByAndLimit(sql, q.getOrderBy(), q.getLimit(),
 
   | 
||
| 
                                          q.getOffset());
 
   | 
||
| 
         QSqlQuery sqlQuery = this->builder->getQuery();
 
   | 
||
| 
         QSqlQuery sqlQuery = this->ar->getQb()->getQuery();
 
   | 
||
| 
         sqlQuery.prepare(sql);
 
   | 
||
| 
         this->builder->bindValues(q.getParams(), sqlQuery, false);
 
   | 
||
| 
         this->ar->getQb()->bindValues(q.getParams(), sqlQuery, false);
 
   | 
||
| 
         return sqlQuery;
 
   | 
||
| 
     }
 
   | 
||
| 
     | 
||
| 
     QString QueryInterpreter::buildSelect(Query &q,
 
   | 
||
| 
                                           const QList<Expression> &columns,
 
   | 
||
| 
                                           const bool &distinct, const QString &selectOption) const {
 
   | 
||
| 
         QString sqlSelect = distinct ? ("SELECT " + this->builder->distinct() + " ") :
 
   | 
||
| 
         QString sqlSelect = distinct ? ("SELECT " + this->ar->getQb()->distinct() + " ") :
 
   | 
||
| 
                             "SELECT ";
 
   | 
||
| 
         if (!selectOption.isEmpty()) {
 
   | 
||
| 
             sqlSelect += selectOption + " ";
 
   | 
||
| ... | ... | |
| 
             q.appendParams(e.getParams());
 
   | 
||
| 
             QString nExp = e.getExpression();
 
   | 
||
| 
             if (e.getOnlyColumn()) {
 
   | 
||
| 
                 sqlSelect += this->builder->getSchema()->quoteColumnName(e.getExpression());
 
   | 
||
| 
                 sqlSelect += this->ar->getQb()->getSchema()->quoteColumnName(e.getExpression());
 
   | 
||
| 
             } else if (!nExp.contains("(")) {
 
   | 
||
| 
                 QRegularExpression re =
 
   | 
||
| 
                     QRegularExpression(
 
   | 
||
| ... | ... | |
| 
                     for (int var = 0; var < 2; ++var) {
 
   | 
||
| 
                         QRegularExpressionMatch match = iterator.next();
 
   | 
||
| 
                         if (var == 1) {
 
   | 
||
| 
                             sqlSelect += this->builder->getSchema()->quoteColumnName(
 
   | 
||
| 
                                              match.captured()) + " AS " + this->builder->getSchema()->quoteColumnName("a" +
 
   | 
||
| 
                             sqlSelect += this->ar->getQb()->getSchema()->quoteColumnName(
 
   | 
||
| 
                                              match.captured()) + " AS " + this->ar->getQb()->getSchema()->quoteColumnName("a" +
 
   | 
||
| 
                                                      QString::number(i));
 
   | 
||
| 
                         }
 
   | 
||
| 
                     }
 
   | 
||
| 
                 } else {
 
   | 
||
| 
                     nExp = this->builder->getSchema()->quoteColumnName(nExp);
 
   | 
||
| 
                     nExp = this->ar->getQb()->getSchema()->quoteColumnName(nExp);
 
   | 
||
| 
                 }
 
   | 
||
| 
             } else {
 
   | 
||
| 
                 sqlSelect += nExp + " AS " + this->builder->getSchema()->quoteColumnName("a" +
 
   | 
||
| 
                 sqlSelect += nExp + " AS " + this->ar->getQb()->getSchema()->quoteColumnName("a" +
 
   | 
||
| 
                              QString::number(i));
 
   | 
||
| 
             }
 
   | 
||
| 
         }
 
   | 
||
| ... | ... | |
| 
         if (from.isEmpty()) {
 
   | 
||
| 
             return "";
 
   | 
||
| 
         }
 
   | 
||
| 
         auto tables = this->builder->quoteTableNames(from);
 
   | 
||
| 
         auto tables = this->ar->getQb()->quoteTableNames(from);
 
   | 
||
| 
         QString clause = "FROM ";
 
   | 
||
| 
         bool first = true;
 
   | 
||
| 
         for (int var = 0; var < tables.size(); ++var) {
 
   | 
||
| ... | ... | |
| 
         QString sqlJoin = "";
 
   | 
||
| 
         for (int i = 0; i < joins.size(); ++i) {
 
   | 
||
| 
             Join j = joins.at(i);
 
   | 
||
| 
             sqlJoin += j.getType() + this->builder->getSeparator() +
 
   | 
||
| 
                        this->builder->getSchema()->quoteTableName(j.getForeignTable());
 
   | 
||
| 
             sqlJoin += j.getType() + this->ar->getQb()->getSeparator() +
 
   | 
||
| 
                        this->ar->getQb()->getSchema()->quoteTableName(j.getForeignTable());
 
   | 
||
| 
             if (!j.getExpression().getExpression().isEmpty()) {
 
   | 
||
| 
                 QString expression = j.getExpression().getExpression();
 
   | 
||
| 
                 int count = expression.count("=");
 
   | 
||
| 
                 if (count < 1) {
 
   | 
||
| 
                     expression = this->builder->getSchema()->quoteTableName(expression);
 
   | 
||
| 
                     expression = this->ar->getQb()->getSchema()->quoteTableName(expression);
 
   | 
||
| 
                 } else if (count == 1) {
 
   | 
||
| 
                     QStringList list = expression.split("=");
 
   | 
||
| 
                     expression = this->builder->getSchema()->quoteColumnName(list.at(
 
   | 
||
| 
                     expression = this->ar->getQb()->getSchema()->quoteColumnName(list.at(
 
   | 
||
| 
                                      0).trimmed()) + " = ";
 
   | 
||
| 
                     expression += this->builder->getSchema()->quoteColumnName(list.at(1).trimmed());
 
   | 
||
| 
                     expression += this->ar->getQb()->getSchema()->quoteColumnName(list.at(1).trimmed());
 
   | 
||
| 
                 }
 
   | 
||
| 
                 sqlJoin += " ON " + expression;
 
   | 
||
| 
             }
 
   | 
||
| ... | ... | |
| 
     }
 
   | 
||
| 
     | 
||
| 
     QString QueryInterpreter::buildGroupBy(const QStringList &groupBy) const {
 
   | 
||
| 
         return groupBy.isEmpty() ? "" : "GROUP BY " + this->builder->buildColumns(
 
   | 
||
| 
         return groupBy.isEmpty() ? "" : "GROUP BY " + this->ar->getQb()->buildColumns(
 
   | 
||
| 
                    groupBy);
 
   | 
||
| 
     }
 
   | 
||
| 
     | 
||
| ... | ... | |
| 
             const quint64 &offset) const {
 
   | 
||
| 
         QString sqlOrderBy = this->buildOrderBy(orderBy);
 
   | 
||
| 
         if (!sqlOrderBy.isEmpty()) {
 
   | 
||
| 
             sql += this->builder->getSeparator() + sqlOrderBy;
 
   | 
||
| 
             sql += this->ar->getQb()->getSeparator() + sqlOrderBy;
 
   | 
||
| 
         }
 
   | 
||
| 
         QString sqlLimit = this->builder->limit(limit, offset, false);
 
   | 
||
| 
         QString sqlLimit = this->ar->getQb()->limit(limit, offset, false);
 
   | 
||
| 
         if (!sqlLimit.isEmpty()) {
 
   | 
||
| 
             sql += this->builder->getSeparator() + sqlLimit;
 
   | 
||
| 
             sql += this->ar->getQb()->getSeparator() + sqlLimit;
 
   | 
||
| 
         }
 
   | 
||
| 
         return sql;
 
   | 
||
| 
     }
 
   | 
||
| ... | ... | |
| 
             if (order.getColumn().isEmpty()) {
 
   | 
||
| 
                 sqlOrder += order.getExpressedDirection().getExpression();
 
   | 
||
| 
             } else {
 
   | 
||
| 
                 sqlOrder += this->builder->getSchema()->quoteColumnName(order.getColumn());
 
   | 
||
| 
                 sqlOrder += this->ar->getQb()->getSchema()->quoteColumnName(order.getColumn());
 
   | 
||
| 
                 switch (order.getDirection()) {
 
   | 
||
| 
                 case Direction::SORT_ASC:
 
   | 
||
| 
                     sqlOrder += " ASC";
 
   | 
||
| ... | ... | |
| 
                 if (first) {
 
   | 
||
| 
                     first = false;
 
   | 
||
| 
                 } else if (expression.at(0) != ' ') {
 
   | 
||
| 
                     sqlCondition += this->builder->getSeparator();
 
   | 
||
| 
                     sqlCondition += this->ar->getQb()->getSeparator();
 
   | 
||
| 
                 }
 
   | 
||
| 
             }
 
   | 
||
| 
             sqlCondition += expression;
 
   | 
||
Auch abrufbar als: Unified diff
...