Cutelyst  1.11.0
sql.cpp
1 /*
2  * Copyright (C) 2015-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "sql.h"
19 
20 #include <QtCore/QLoggingCategory>
21 #include <QThread>
22 
23 #include <QtSql/QSqlQuery>
24 #include <QtSql/QSqlError>
25 #include <QtSql/QSqlRecord>
26 
27 Q_LOGGING_CATEGORY(C_SQL, "cutelyst.utils.sql")
28 
29 using namespace Cutelyst;
30 
31 QVariantHash Sql::queryToHashObject(QSqlQuery &query)
32 {
33  QVariantHash ret;
34  if (query.next()) {
35  int columns = query.record().count();
36  for (int i = 0; i < columns; ++i) {
37  ret.insert(query.record().fieldName(i), query.value(i));
38  }
39  }
40  return ret;
41 }
42 
43 QVariantList Sql::queryToHashList(QSqlQuery &query)
44 {
45  QVariantList ret;
46 
47  int columns = query.record().count();
48  QStringList cols;
49  for (int i = 0; i < columns; ++i) {
50  cols.append(query.record().fieldName(i));
51  }
52 
53  while (query.next()) {
54  QVariantHash line;
55  for (int i = 0; i < columns; ++i) {
56  line.insert(cols.at(i), query.value(i));
57  }
58  ret.append(line);
59  }
60  return ret;
61 }
62 
63 QVariantMap Sql::queryToMapObject(QSqlQuery &query)
64 {
65  QVariantMap ret;
66  if (query.next()) {
67  int columns = query.record().count();
68  for (int i = 0; i < columns; ++i) {
69  ret.insert(query.record().fieldName(i), query.value(i));
70  }
71  }
72  return ret;
73 }
74 
75 QVariantList Sql::queryToMapList(QSqlQuery &query)
76 {
77  QVariantList ret;
78 
79  int columns = query.record().count();
80  QStringList cols;
81  for (int i = 0; i < columns; ++i) {
82  cols.append(query.record().fieldName(i));
83  }
84 
85  while (query.next()) {
86  QVariantMap line;
87  for (int i = 0; i < columns; ++i) {
88  line.insert(cols.at(i), query.value(i));
89  }
90  ret.append(line);
91  }
92  return ret;
93 }
94 
95 QVariantHash Sql::queryToIndexedHash(QSqlQuery &query, const QString &key)
96 {
97  QVariantHash ret;
98  QSqlRecord record = query.record();
99 
100  if (!record.contains(key)) {
101  qCCritical(C_SQL) << "Field Name " << key <<
102  " not found in result set";
103  return ret;
104  }
105 
106  int columns = record.count();
107  QStringList cols;
108 
109  for (int i = 0; i < columns; ++i) {
110  cols.append(record.fieldName(i));
111  }
112 
113  while (query.next()) {
114  QVariantHash line;
115  for (int i = 0; i < columns; ++i) {
116  line.insertMulti(cols.at(i),query.value(i));
117  }
118 
119  ret.insertMulti(query.value(key).toString(), line);
120  }
121 
122  return ret;
123 }
124 
125 void Sql::bindParamsToQuery(QSqlQuery &query, const Cutelyst::ParamsMultiMap &params, bool htmlEscaped)
126 {
127  auto it = params.constBegin();
128  if (htmlEscaped) {
129  while (it != params.constEnd()) {
130  if (it.value().isNull()) {
131  query.bindValue(QLatin1Char(':') + it.key(), QVariant());
132  } else {
133  query.bindValue(QLatin1Char(':') + it.key(), it.value().toHtmlEscaped());
134  }
135  ++it;
136  }
137  } else {
138  while (it != params.constEnd()) {
139  if (it.value().isNull()) {
140  query.bindValue(QLatin1Char(':') + it.key(), QVariant());
141  } else {
142  query.bindValue(QLatin1Char(':') + it.key(), it.value());
143  }
144  ++it;
145  }
146  }
147 }
148 
149 QSqlQuery Sql::preparedQuery(const QString &query, QSqlDatabase db, bool forwardOnly)
150 {
151  QSqlQuery sqlQuery(db);
152  sqlQuery.setForwardOnly(forwardOnly);
153  if (!sqlQuery.prepare(query)) {
154  qCCritical(C_SQL) << "Failed to prepare query:" << query << sqlQuery.lastError().databaseText();
155  }
156  return sqlQuery;
157 }
158 
159 QSqlQuery Sql::preparedQueryThread(const QString &query, const QString &dbName, bool forwardOnly)
160 {
161  QSqlQuery sqlQuery(QSqlDatabase::database(databaseNameThread(dbName)));
162  sqlQuery.setForwardOnly(forwardOnly);
163  if (!sqlQuery.prepare(query)) {
164  qCCritical(C_SQL) << "Failed to prepare query:" << query << sqlQuery.lastError().databaseText();
165  }
166  return sqlQuery;
167 }
168 
169 QString Sql::databaseNameThread(const QString &dbName)
170 {
171  return dbName + QLatin1Char('-') + QThread::currentThread()->objectName();
172 }
QMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7