-
Notifications
You must be signed in to change notification settings - Fork 16
/
SqlInsert.h
219 lines (192 loc) · 5.94 KB
/
SqlInsert.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
/*
Copyright (C) 2011-2013 Klarälvdalens Datakonsult AB,
a KDAB Group company, [email protected],
author Volker Krause <[email protected]>
author Andras Mantia <[email protected]>
author Jan Dalheimer <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef SQL_INSERT_H
#define SQL_INSERT_H
#include "SqlInsertQueryBuilder.h"
#include <boost/mpl/and.hpp>
#include <boost/utility/enable_if.hpp>
/**
* @file SqlInsert.h
* Insert query expression templates.
*
* @todo Subquery (INSERT INTO a SELECT * FROM b)
*/
namespace Sql {
struct ColumnValue
{
template <typename ColumnT>
ColumnValue(const ColumnT&, const typename ColumnT::type& value) :
columnName(ColumnT::sqlName()), value(QVariant::fromValue( value )), isDefault(false)
{
Sql::warning<boost::is_same<typename ColumnT::type, QDateTime>, UsageOfClientSideTime>::print();
}
template <typename ColumnT, class = typename boost::enable_if<typename ColumnT::is_column>::type>
ColumnValue(const ColumnT&) :
columnName(ColumnT::sqlName()), isDefault(true)
{
}
ColumnValue(const QString& name) :
columnName(name), isDefault(true)
{
}
QString columnName;
QVariant value;
bool isDefault;
/// @internal needed for InsertExpr's vector
ColumnValue() : isDefault(true) {}
};
// TODO find out if it would be possible to have an operator= instead
template <typename ColumnT>
ColumnValue operator<<(const ColumnT& col, const typename ColumnT::type& value)
{
return ColumnValue(col, value);
}
/**
* Represents a INSERT statement
* @internal
* @tparam TableT The table type inserted into.
*/
template <typename TableT>
struct InsertExpr
{
/**
* Empty ctor.
*/
InsertExpr() : useDefaultValues(false) {}
/**
* "Copy" ctor.
*/
template <typename OtherTableT>
explicit
InsertExpr( const InsertExpr<OtherTableT> &other )
{
values = other.values;
}
/**
* Creates the INTO part of a INSERT statement.
* @tparam T The table type to insert into
*/
template <typename T>
InsertExpr<T> into( const T& )
{
BOOST_MPL_ASSERT(( boost::is_same<TableT, detail::missing> )); // only one into allowed
return InsertExpr<T>();
}
/**
* Sets columns + values
*/
InsertExpr<TableT> columns( const QList<ColumnValue>& cols )
{
values += cols.toVector();
return *this;
}
InsertExpr<TableT> columns( const ColumnValue& col )
{
return columns(QList<ColumnValue>() << col);
}
InsertExpr<TableT> defaultValues()
{
useDefaultValues = true;
return *this;
}
/**
* @internal
* for unit testing access only
*/
SqlInsertQueryBuilder queryBuilder() const
{
SqlInsertQueryBuilder qb;
qb.setTable<TableT>();
if (values.isEmpty() || useDefaultValues) {
foreach (const ColumnValue& column, values) {
qb.addColumn(column.columnName);
}
qb.setToDefaultValues();
} else {
foreach (const ColumnValue& col, values) {
if (col.isDefault) {
qb.addColumn(col.columnName);
} else {
qb.addColumnValue(col.columnName, col.value);
}
}
}
return qb;
}
/**
* Returns a prepared QSqlQuery ready for execution.
*/
operator SqlQuery() const
{
return queryBuilder().query();
}
/**
* Returns the pre-filled dynamic query builder.
* This is useful if intermediate queries have to be stored for extension etc.
* Since we don't have support for the auto keyword everywhere yet, that's currently our best option.
*/
operator SqlInsertQueryBuilder() const
{
return queryBuilder();
}
QVector<ColumnValue> values;
bool useDefaultValues;
};
/**
* Creates a INSERT statement.
*/
InsertExpr<detail::missing> insert()
{
return InsertExpr<detail::missing>();
}
QList<ColumnValue> operator&(const ColumnValue& c1, const ColumnValue& c2)
{
return QList<ColumnValue>() << c1 << c2;
}
QList<ColumnValue> operator&(const QList<ColumnValue>& c1, const ColumnValue& c2)
{
return QList<ColumnValue>() << c1 << c2;
}
template <typename ColumnT1, typename ColumnT2>
typename boost::enable_if<boost::mpl::and_<typename ColumnT1::is_column, typename ColumnT2::is_column>, QList<ColumnValue> >::type
operator&(const ColumnT1& c1, const ColumnT2& c2)
{
return operator&(ColumnValue(c1), ColumnValue(c2));
}
template <typename ColumnT>
typename boost::enable_if<typename ColumnT::is_column, QList<ColumnValue> >::type
operator&(const ColumnT& c1, const ColumnValue& c2)
{
return operator&(ColumnValue(c1), c2);
}
template <typename ColumnT>
typename boost::enable_if<typename ColumnT::is_column, QList<ColumnValue> >::type
operator&(const ColumnValue& c1, const ColumnT& c2)
{
return operator&(c1, ColumnValue(c2));
}
template <typename ColumnT>
typename boost::enable_if<typename ColumnT::is_column, QList<ColumnValue> >::type
operator&(const QList<ColumnValue>& c1, const ColumnT& c2)
{
return operator&(c1, ColumnValue(c2));
}
}
#endif