-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn.h++
269 lines (229 loc) · 7.64 KB
/
column.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
#pragma once
#include <string>
#include <foam/strlib/format.h>
#include <foam/meta/typelist.h++>
#include <foam/optional.h>
#include <tagsql/common/exceptions.h++>
#include <tagsql/core/tag_category.h++>
#include <tagsql/core/productive_tag.h++>
#include <tagsql/core/deferred_assignment.h++>
#include <pqxx/pqxx>
namespace tagsql
{
template<typename ... Tags>
struct named_tuple;
namespace bare
{
template<typename T>
using type = typename std::remove_cv<typename std::remove_reference<T>::type>::type; //or use std::decay, as it removes both cv and const!
template<typename T, typename U>
struct is_same : std::is_same<type<T>, type<U>> {};
}
struct order_by_argument_t
{
std::string value;
};
template<typename Tag>
class column : public Tag::sql_data_type::template operators<Tag>, column_productive_tag_t<Tag>
{
public:
using self_type = column<Tag>;
using table_type = typename Tag::table_type;
using value_type = typename Tag::column_type;
using tag_type = Tag;
using tag_category = core::context_independent_value_tag;
using _is_column = std::true_type;
using column_productive_tag_t<Tag>::repr;
static constexpr bool is_nullable = tag_type::is_nullable;
static constexpr bool server_default = tag_type::server_default;
column() : _data() {}
explicit column(value_type value) : _data(std::move(value))
{
}
template<typename U, typename Unused=typename std::enable_if<!bare::is_same<U,pqxx::field>::value>::type >
explicit column(U && value)
{
set(std::forward<U>(value));
}
explicit column(pqxx::field const & field)
{
try
{
if ( !field.is_null() )
set(field.as<value_type>());
}
catch(std::exception const & e)
{
throw parse_exception(foam::strlib::format("'{0}' cannot be converted into column '{1}' which is of type {2}. Original exception is : {3}.",
field.c_str(), column_name(true), Tag::type_name(), e.what() ));
}
}
#if 0
//it is not copy-assignment!
column& operator = (value_type const & value)
{
return set(value);
}
//it is not copy-assignment either!
template<typename U>
column& operator = (U && value)
{
static_assert(std::is_constructible<value_type, U>::value, "column::value_type cannot be constructed from the argument");
return set(std::forward<U>(value));
}
//it is not copy-assignment!
column& operator = (std::nullptr_t)
{
return set(::foam::none);
}
#else
//it is not copy-assignment!
auto operator = (value_type const & value) -> core::deferred_assignment<self_type, value_type>
{
return core::deferred_assignment<self_type, value_type>(this, value);
}
//it is not copy-assignment either!
template<typename U>
auto operator = (U && value) -> core::deferred_assignment<self_type, U>
{
static_assert(std::is_constructible<value_type, U>::value, "column::value_type cannot be constructed from the argument");
return core::deferred_assignment<self_type, U>(this, std::forward<U>(value));
}
//it is not copy-assignment!
auto operator = (std::nullptr_t) -> core::deferred_assignment<self_type, ::foam::none_t>
{
return core::deferred_assignment<self_type, ::foam::none_t>(this, ::foam::none);
}
#endif
//value access using method => T v = item.value;
value_type const & value() const { return *_data; }
value_type & value() { return *_data; }
//value access using function-style call => T v = item();
value_type const & operator()() const { return value(); }
value_type & operator()() { return value(); }
//value access by implicit conversoon => T v = item;
//operator value_type const & () const { return value(); }
//operator value_type& () { return value(); }
bool is_null() const
{
return _data == ::foam::none;
}
bool null_has_been_set_explicitly() const
{
return _null_has_been_set;
}
static auto column_name(bool qualified) -> std::string
{
return qualified ? qualify(tag_type()) : tag_type().column_name;
}
auto operator-() const -> order_by_argument_t
{
return {qualify(tag_type()) + " DESC"};
}
auto operator+() const -> order_by_argument_t
{
return {qualify(tag_type()) + " ASC"};
}
private:
template<typename ...Tags>
friend class named_tuple;
template<typename Column, typename ValueType>
friend class core::deferred_assignment;
template<typename U>
column& set(U && value)
{
using type = typename std::remove_cv<typename std::remove_reference<U>::type>::type;
set_internal(std::forward<U>(value), static_cast<type*>(0));
return *this;
}
template<typename U>
void set_internal(U && value, pqxx::field const *)
{
try
{
if ( !value.is_null() )
set(value.template as<value_type>());
}
catch(std::exception const & e)
{
throw parse_exception(foam::strlib::format("'{0}' cannot be converted into column '{1}' which is of type {2}. Original exception is : {3}.",
value.c_str(), column_name(true), Tag::type_name(), e.what() ));
}
}
template<typename U>
void set_internal(U && other, column<Tag> const *)
{
if ( not other.is_null() )
set(other.value());
else if ( other.null_has_been_set_explicitly() )
set(::foam::none);
}
template<typename U>
void set_internal(U && value, ... )
{
static_assert(std::is_constructible<::foam::optional<value_type>, bare::type<U>>::value, "column::value_type cannot be constructed from the argument");
_data = std::forward<U>(value);
_null_has_been_set = (_data == ::foam::none);
}
private:
::foam::optional<value_type> _data;
bool _null_has_been_set = false;
};
namespace ostream_detail
{
namespace overload
{
namespace detail
{
struct fallback { template<typename ... T> fallback(T && ...) {} };
}
//preferences to enforce total order of unordered overloads
using _1st_preference = int&&;
using _2nd_preference = int const&&;
using _3rd_preference = int const volatile&&;
using _4th_preference = int const &;
using _5th_preference = long &&;
//when all preferences fail, choose fallback as fallback.
using fallback = detail::fallback;
//pass resolver() as argument to overloaded function.
using resolver = int;
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::_1st_preference) -> decltype(out << c(), out)
{
return out << c();
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::_2nd_preference) -> decltype(c.print(out), out)
{
return (c.print(out), out);
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::_3rd_preference) -> decltype(print(out,c), out)
{
return (print(out,c),out);
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::_4th_preference) -> decltype(out << c.to_string(), out)
{
return out << c.to_string();
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::_5th_preference) -> decltype(out << to_string(c), out)
{
return out << to_string(c);
}
template<typename Tag>
auto print(std::ostream & out, column<Tag> const &c, overload::fallback ) -> decltype(out)
{
return out << Tag::type_name() << "<" << static_cast<void const*>(&c) << ">";
}
}
template<typename Tag>
std::ostream & operator <<(std::ostream & out, column<Tag> const & c)
{
if ( c.is_null() )
return out << "NULL";
return ostream_detail::print(out, c, ostream_detail::overload::resolver());
}
} //tagsql