| 10 | namespace internal { |
| 11 | |
| 12 | int DatabaseUtilities::countQuery(InternalDatabaseInterface const* database, QString const& tableName, QVariantMap const& whereQueryPart) { |
| 13 | QSqlQuery query(database->getQueryObject()); |
| 14 | QString whereString; |
| 15 | whereString.reserve(512); |
| 16 | auto it = whereQueryPart.constBegin(); |
| 17 | auto const end = whereQueryPart.constEnd(); |
| 18 | |
| 19 | int keyIndex = 1; |
| 20 | for (; it != end; ++it) { |
| 21 | if (whereString.isEmpty()) { |
| 22 | whereString = QStringLiteral(" WHERE "); |
| 23 | } else { |
| 24 | whereString.append(QStringLiteral(" AND ")); |
| 25 | } |
| 26 | whereString.append(QStringLiteral("`%1` = :value%2").arg(it.key()).arg(keyIndex, 4, 10, QChar('0'))); |
| 27 | ++keyIndex; |
| 28 | } |
| 29 | |
| 30 | query.prepare(QStringLiteral("SELECT Count(*) AS `count` FROM `%1`%2").arg(tableName).arg(whereString)); |
| 31 | |
| 32 | it = whereQueryPart.constBegin(); |
| 33 | keyIndex = 1; |
| 34 | for (; it != end; ++it) { |
| 35 | query.bindValue(QStringLiteral(":value%1").arg(keyIndex, 4, 10, QChar('0')), it.value()); |
| 36 | ++keyIndex; |
| 37 | } |
| 38 | |
| 39 | if (query.exec() && query.isSelect() && query.next()) { |
| 40 | return query.value(QStringLiteral("count")).toInt(); |
| 41 | } else { |
| 42 | throw openmittsu::exceptions::InternalErrorException() << "Could not execute count query for table '" << tableName.toStdString() << "'. Query error: " << query.lastError().text().toStdString(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void DatabaseUtilities::prepareSetFieldsUpdateQuery(QSqlQuery& query, QString const& queryString, QVariantMap const& fieldsAndValues) { |
| 47 | if (fieldsAndValues.size() > 0) { |
nothing calls this directly
no test coverage detected