-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_func.h
286 lines (237 loc) · 9.58 KB
/
sql_func.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*****************************************************************************
The MIT License
Copyright © 2019 Pavel Karelin (hkarel), <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#pragma once
#include "qmetatypes.h"
#include "shared/prog_abort.h"
#include "shared/qt/quuidex.h"
#include "shared/logger/logger.h"
#include <QtSql>
#include <type_traits>
namespace sql {
namespace detail {
template<typename T>
struct not_enum_type : std::enable_if<!std::is_enum<T>::value, int> {};
template<typename T>
struct is_enum_type : std::enable_if<std::is_enum<T>::value, int> {};
//template<typename T>
//auto bindVariant(const T& val, int, int) -> decltype(QVariant(val))
//{
// if (qMetaTypeId<T>() == qMetaTypeId<QUuidEx>()
// || qMetaTypeId<T>() == qMetaTypeId<QUuid>())
// {
// return QVariant::fromValue(val);
// }
//
// return QVariant(val);
//}
//template<typename T>
//auto bindVariant(const T& val, long, long) -> decltype(QVariant())
//{
// int typeId = qMetaTypeId<T>();
// if (QMetaType::Type(typeId) >= QMetaType::User)
// return QVariant::fromValue(val);
//
// return QVariant();
//}
} // namespace detail
QVariant bindVariant(bool);
QVariant bindVariant(qint16);
QVariant bindVariant(quint16);
QVariant bindVariant(qint32);
QVariant bindVariant(quint32);
QVariant bindVariant(qint64);
QVariant bindVariant(quint64);
QVariant bindVariant(const char*);
inline QVariant bindVariant(const QVariant& val) {return val;}
template<typename T>
QVariant bindVariant(const T& val, typename detail::not_enum_type<T>::type = 0)
{
//return detail::bindVariant(val, 0, 0);
return QVariant::fromValue(val);
}
template<typename T>
QVariant bindVariant(const T& val, typename detail::is_enum_type<T>::type = 0)
{
static_assert(std::is_same<typename std::underlying_type<T>::type, quint32>::value,
"Base type of enum must be 'unsigned int'");
return bindVariant(static_cast<quint32>(val));
}
template<typename T>
QVariant bindVariant(const QVector<T>& val, typename detail::is_enum_type<T>::type = 0)
{
static_assert(std::is_same<typename std::underlying_type<T>::type, quint32>::value,
"Base type of enum must be 'unsigned int'");
QVector<qint32> arr;
arr.resize(val.count());
for (int i = 0; i < val.count(); ++i)
arr[i] = static_cast<qint32>(val[i]);
return bindVariant(arr);
}
template<typename T>
void bindValue(QSqlQuery& q, const QString& name, const T& value)
{
q.bindValue(name.trimmed(), bindVariant(value));
}
inline void addBindValue(QSqlQuery&) {}
template<typename T, typename... Args>
void addBindValue(QSqlQuery& q, const T& t, const Args&... args)
{
q.addBindValue(bindVariant(t));
addBindValue(q, args...);
}
void assignValue(bool&, const QSqlRecord&, const QString& fieldName);
void assignValue(qint8&, const QSqlRecord&, const QString& fieldName);
void assignValue(quint8&, const QSqlRecord&, const QString& fieldName);
void assignValue(qint16&, const QSqlRecord&, const QString& fieldName);
void assignValue(quint16&, const QSqlRecord&, const QString& fieldName);
void assignValue(qint32&, const QSqlRecord&, const QString& fieldName);
void assignValue(quint32&, const QSqlRecord&, const QString& fieldName);
void assignValue(qint64&, const QSqlRecord&, const QString& fieldName);
void assignValue(quint64&, const QSqlRecord&, const QString& fieldName);
void assignValue(float&, const QSqlRecord&, const QString& fieldName);
template<typename T>
void assignValue(T& val, const QSqlRecord& rec, const QString& fieldName,
typename detail::not_enum_type<T>::type = 0)
{
const QSqlField& f = rec.field(fieldName.trimmed());
if (f.isNull())
return;
if (!f.isValid())
return;
int typeId = f.value().userType();
int typeId2 = qMetaTypeId<T>();
if (typeId != typeId2)
{
log_error << "Unable convert QVariant type " << typeId
<< " to value type " << typeId2
<< " for field " << f.name().toUtf8().constData();
prog_abort();
}
val = f.value().value<T>();
}
template<typename T>
void assignValue(T& val, const QSqlRecord& rec, const QString& fieldName,
typename detail::is_enum_type<T>::type = 0)
{
static_assert(std::is_same<typename std::underlying_type<T>::type, quint32>::value,
"Base type of enum must be 'unsigned int'");
const QSqlField& field = rec.field(fieldName.trimmed());
if (field.isNull() || !field.isValid())
return;
if (field.value().canConvert<qint32>())
{
qint32 v = field.value().value<qint32>();
quint32 v2 = *((quint32*) &v);
val = static_cast<T>(v2);
}
}
template<int N>
void assignValue(QUuidT<N>& val, const QSqlRecord& rec, const QString& fieldName)
{
assignValue(static_cast<QUuid&>(val), rec, fieldName);
}
template<int N>
void assignValue(QVector<QUuidT<N>>& val, const QSqlRecord& rec, const QString& fieldName)
{
assignValue(reinterpret_cast<QVector<QUuid>&>(val), rec, fieldName);
}
template<typename T>
void assignValue(QVector<T>& val, const QSqlRecord& rec, const QString& fieldName,
typename detail::is_enum_type<T>::type = 0)
{
static_assert(std::is_same<typename std::underlying_type<T>::type, quint32>::value,
"Base type of enum must be 'unsigned int'");
const QSqlField& field = rec.field(fieldName.trimmed());
if (field.isNull() || !field.isValid())
return;
if (field.value().canConvert<QVector<qint32>>())
{
QVector<qint32> arr = field.value().value<QVector<qint32>>();
val.resize(arr.count());
for (int i = 0; i < arr.count(); ++i)
{
quint32 v = *((quint32*) &arr[i]);
val[i] = static_cast<T>(v);
}
}
}
template<typename... Args>
bool exec(QSqlQuery& q, const QString& sql, const Args&... args)
{
if (!q.prepare(sql))
return false;
addBindValue(q, args...);
return q.exec();
}
// Преобразует список полей в список заменителей.
// Входящий список: "FIELD1, FIELD2, FIELD3"
// Результат: ":FIELD1, :FIELD2, :FIELD3"
QString fieldsToPlaceholders(QString fields);
// Генерирует sql-запрос вида: "INSERT INTO %1 (%2) VALUES (%3)"
QString insertIntoStatement(const QString& tableName, const QString& fields);
// Генерирует sql-запрос вида:
// "UPDATE OR INSERT INTO %1 (%2) VALUES (%3) MATCHING (%4)"
QString updateOrInsertStatement(const QString& tableName, const QString& fields,
const QString& matching);
// Генерирует sql-запрос вида:
// "INSERT INTO %1 (%2) VALUES (%3) ON CONFLICT (%4) DO UPDATE SET..."
QString insertOrUpdateStatementPG(const QString& tableName, const QString& fields,
const QString& matching);
// Генерирует sql-запрос вида:
// MERGE types_table_target AS Target
// USING types_table_source AS Source
// ON (Target.f_bigint = Source.f_bigint)
// WHEN MATCHED THEN UPDATE SET
// [f_bigint] = Source.[f_bigint]
// ,[f_binary] = Source.[f_binary]
// WHEN NOT MATCHED THEN INSERT VALUES
// (
// Source.[f_bigint]
// Source.[f_binary]
// );
QString mergeRowStatementMS(const QString& tableName, QStringList fields,
QStringList matching);
QString mergeRowStatementMS(const QString& tableName, const QString& fields,
const QString& matching);
// Генерирует sql-запрос вида:
// MERGE types_table_target AS Target
// USING types_table_source AS Source
// ON (Target.f_bigint = Source.f_bigint)
// WHEN MATCHED THEN UPDATE SET
// [f_bigint] = Source.[f_bigint]
// ,[f_binary] = Source.[f_binary]
// WHEN NOT MATCHED THEN INSERT VALUES
// (
// Source.[f_bigint]
// Source.[f_binary]
// );
QString mergeTableStatementMS(const QString& targetTableName,
const QString& sourceTableName,
QStringList fields, QStringList matching);
QString mergeTableStatementMS(const QString& targetTableName,
const QString& sourceTableName,
const QString& fields, const QString& matching);
} // namespace sql
#define INSERT_INTO insertIntoStatement
#define UPDATE_OR_INSERT_INTO updateOrInsertStatement
#define INSERT_OR_UPDATE_PG insertOrUpdateStatementPG
#define MERGE_ROW_MS mergeRowStatementMS
#define MERGE_TABLE_MS mergeTableStatementMS