ORM C++
Loading...
Searching...
No Matches
value.cppm
Go to the documentation of this file.
1module;
2
3#include <cmath>
4#include <compare>
5#include <concepts>
6#include <cstddef>
7#include <limits>
8#include <optional>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <utility>
13#include <variant>
14
15export module orm:query_value;
16
17import :foundation;
18import :model_views;
19import :value_codec;
20
21// query/QueryValue.hpp
22namespace orm::query
23{
24export
25{
26
27 /**
28 * @brief A value that can be bound as a query parameter.
29 *
30 * QueryValue stores the subset of C++ values supported by the SELECT query DSL.
31 * Values are rendered as SOCI bind parameters, not interpolated into SQL.
32 */
33 class QueryValue
34 {
35 public:
36 using Value = std::variant<int, long long, unsigned long long, double, std::string, Date, TimeOfDay,
39 QueryValue(bool inputValue) : logicalType{model::ColumnType::Bool}, value{static_cast<int>(inputValue)} {}
40 QueryValue(char inputValue) : logicalType{model::ColumnType::Char}, value{static_cast<int>(inputValue)} {}
41 QueryValue(signed char inputValue) : logicalType{model::ColumnType::Char}, value{static_cast<int>(inputValue)}
42 {
43 }
44 QueryValue(unsigned char inputValue)
45 : logicalType{model::ColumnType::UnsignedChar}, value{static_cast<int>(inputValue)}
46 {
47 }
48 QueryValue(short inputValue) : logicalType{model::ColumnType::Short}, value{static_cast<int>(inputValue)} {}
49 QueryValue(unsigned short inputValue)
50 : logicalType{model::ColumnType::UnsignedShort}, value{static_cast<int>(inputValue)}
51 {
52 }
53 QueryValue(int inputValue) : logicalType{model::ColumnType::Int}, value{inputValue} {}
54 QueryValue(unsigned int inputValue)
55 : logicalType{model::ColumnType::UnsignedInt}, value{static_cast<unsigned long long>(inputValue)}
56 {
57 }
58 QueryValue(long inputValue)
59 : logicalType{sizeof(long) > sizeof(int) ? model::ColumnType::LongLong : model::ColumnType::Int},
60 value{storeLong(inputValue)}
61 {
62 }
63 QueryValue(unsigned long inputValue)
64 : logicalType{sizeof(unsigned long) > sizeof(unsigned int) ? model::ColumnType::UnsignedLongLong :
65 model::ColumnType::UnsignedInt},
66 value{static_cast<unsigned long long>(inputValue)}
67 {
68 }
69 QueryValue(long long inputValue) : logicalType{model::ColumnType::LongLong}, value{inputValue} {}
70 QueryValue(unsigned long long inputValue) : logicalType{model::ColumnType::UnsignedLongLong}, value{inputValue}
71 {
72 }
73 QueryValue(float inputValue) : logicalType{model::ColumnType::Float}, value{static_cast<double>(inputValue)} {}
74 QueryValue(double inputValue) : logicalType{model::ColumnType::Double}, value{inputValue} {}
75 QueryValue(const char* inputValue) : logicalType{model::ColumnType::String}, value{std::string{inputValue}} {}
76 QueryValue(std::string inputValue) : logicalType{model::ColumnType::String}, value{std::move(inputValue)} {}
77 QueryValue(std::string_view inputValue) : logicalType{model::ColumnType::String}, value{std::string{inputValue}}
78 {
79 }
81 QueryValue(Date inputValue) : logicalType{model::ColumnType::Date}, value{inputValue} {}
82 QueryValue(TimeOfDay inputValue) : logicalType{model::ColumnType::TimeOfDay}, value{inputValue} {}
83 QueryValue(LocalDateTime inputValue) : logicalType{model::ColumnType::LocalDateTime}, value{inputValue} {}
84 QueryValue(UtcTimestamp inputValue) : logicalType{model::ColumnType::UtcTimestamp}, value{inputValue} {}
85 QueryValue(Uuid inputValue) : logicalType{model::ColumnType::Uuid}, value{inputValue} {}
86
87 template <UuidAdaptable T>
88 QueryValue(const T& inputValue)
89 : logicalType{model::ColumnType::Uuid}, value{UuidAdapter<T>::toUuid(inputValue)}
90 {
91 }
92
93 template <ValueConvertible T>
94// requires(!UuidAdaptable<T>)
95 QueryValue(const T& inputValue) : QueryValue{ValueConverter<T>::toValue(inputValue)}
96 {
97 }
98
99 template <std::size_t Size>
100 QueryValue(const char (&inputValue)[Size])
101 : logicalType{model::ColumnType::String}, value{std::string{inputValue}}
102 {
103 }
104
105 template <typename T>
106 QueryValue(const std::optional<T>& optionalValue) : QueryValue(optionalValue.value())
107 {
108 }
109
110 /**
111 * @brief Restores a canonical transport value with its model-level type.
112 *
113 * Hydration and backend adapters use this factory when several logical
114 * column types share the same SOCI exchange representation.
115 */
116 [[nodiscard]] static auto fromStorage(model::ColumnType logicalType, Value value) -> QueryValue
117 {
118 if (not isCompatibleStorage(logicalType, value))
119 {
120 throw std::invalid_argument{"Query value storage does not match its logical column type"};
121 }
122
123 return QueryValue{logicalType, std::move(value), StorageValueTag{}};
124 }
125
126 /**
127 * @brief Checks both the canonical variant alternative and its logical range.
128 */
129 [[nodiscard]] static auto isCompatibleStorage(model::ColumnType logicalType,
130 const Value& storedValue) noexcept -> bool
131 {
132 const auto* intValue = std::get_if<int>(&storedValue);
133 const auto* unsignedValue = std::get_if<unsigned long long>(&storedValue);
134 const auto* doubleValue = std::get_if<double>(&storedValue);
135
136 switch (logicalType)
137 {
139 return intValue != nullptr and (*intValue == 0 or *intValue == 1);
141 return intValue != nullptr and *intValue >= std::numeric_limits<signed char>::lowest() and
142 *intValue <= std::numeric_limits<unsigned char>::max();
144 return intValue != nullptr and *intValue >= 0 and
145 *intValue <= std::numeric_limits<unsigned char>::max();
147 return intValue != nullptr and *intValue >= std::numeric_limits<short>::lowest() and
148 *intValue <= std::numeric_limits<short>::max();
150 return intValue != nullptr and *intValue >= 0 and
151 *intValue <= std::numeric_limits<unsigned short>::max();
153 return intValue != nullptr;
155 return unsignedValue != nullptr and *unsignedValue <= std::numeric_limits<unsigned int>::max();
157 return std::holds_alternative<long long>(storedValue);
159 return unsignedValue != nullptr;
161 return doubleValue != nullptr and std::isfinite(*doubleValue) and
162 *doubleValue >= static_cast<double>(std::numeric_limits<float>::lowest()) and
163 *doubleValue <= static_cast<double>(std::numeric_limits<float>::max()) and
164 static_cast<double>(static_cast<float>(*doubleValue)) == *doubleValue;
166 return doubleValue != nullptr and std::isfinite(*doubleValue);
168 return std::holds_alternative<std::string>(storedValue);
170 return compatibleValue<Date>(storedValue);
172 return compatibleValue<TimeOfDay>(storedValue);
174 return compatibleValue<LocalDateTime>(storedValue);
176 return compatibleValue<UtcTimestamp>(storedValue);
178 return compatibleValue<Uuid>(storedValue);
179 }
180
181 return false;
182 }
184 [[nodiscard]] auto getLogicalType() const noexcept -> model::ColumnType
185 {
186 return logicalType;
187 }
189 [[nodiscard]] auto get() const -> const Value&
190 {
191 return value;
192 }
194 auto operator==(const QueryValue& other) const -> bool;
195 auto operator<=>(const QueryValue& other) const -> std::partial_ordering;
196
197 private:
198 template <typename T>
199 static auto compatibleValue(const Value& stored) noexcept -> bool
200 {
201 const auto* typed = std::get_if<T>(&stored);
202 return typed != nullptr && orm::detail::values::valid(*typed);
203 }
204
205 struct StorageValueTag
206 {
207 };
208
209 [[nodiscard]] static auto storeLong(long inputValue) -> Value
210 {
211 if constexpr (sizeof(long) > sizeof(int))
212 {
213 return Value{static_cast<long long>(inputValue)};
214 }
215 else
216 {
217 return Value{static_cast<int>(inputValue)};
218 }
219 }
220
221 QueryValue(model::ColumnType logicalTypeInit, Value valueInit, StorageValueTag)
222 : logicalType{logicalTypeInit}, value{std::move(valueInit)}
223 {
224 }
225
226 model::ColumnType logicalType;
227 Value value;
228 };
229
230 /**
231 * @brief A named parameter for raw query fragments.
232 */
233 struct QueryParameter
235 std::string name;
237 std::optional<model::UuidStorage> uuidStorage{};
238 std::optional<model::ColumnType> nullType{};
239 };
240
241 /**
242 * @brief Creates a named parameter for a raw SQL fragment.
243 *
244 * The name must not include the leading ':'.
245 */
246 template <typename T>
247 auto param(std::string name, T value) -> QueryParameter
248 {
249 return QueryParameter{.name = std::move(name), .value = QueryValue{std::move(value)}};
250 }
251
252 /** Explicit physical representation for a UUID parameter in opaque SQL. */
253 template <typename T>
254 auto param(std::string name, T value, model::UuidStorage uuidStorage) -> QueryParameter
255 {
256 return QueryParameter{
257 .name = std::move(name), .value = QueryValue{std::move(value)}, .uuidStorage = uuidStorage};
258 }
259
260 /** A raw SQL NULL UUID requires both its logical type and physical representation. */
261 inline auto param(std::string name, std::nullopt_t, model::ColumnType logicalType,
263 {
265 throw std::invalid_argument{"A NULL UUID parameter requires the UUID logical type"};
266 return QueryParameter{
267 .name = std::move(name), .value = QueryValue{Uuid{}}, .uuidStorage = uuidStorage, .nullType = logicalType};
268 }
269
270 template <UuidDomainValue T>
271 auto param(std::string name, const std::optional<T>& value, model::UuidStorage uuidStorage) -> QueryParameter
272 {
273 if (!value)
274 return param(std::move(name), std::nullopt, model::ColumnType::Uuid, uuidStorage);
275 return param(std::move(name), *value, uuidStorage);
276 }
277}
278} // namespace orm::query
A value that can be bound as a query parameter.
Definition value.cppm:33
QueryValue(std::string_view inputValue)
Definition value.cppm:76
QueryValue(double inputValue)
Definition value.cppm:73
QueryValue(signed char inputValue)
Definition value.cppm:40
QueryValue(unsigned long long inputValue)
Definition value.cppm:69
auto operator==(const QueryValue &other) const -> bool
auto operator<=>(const QueryValue &other) const -> std::partial_ordering
QueryValue(LocalDateTime inputValue)
Definition value.cppm:82
static auto isCompatibleStorage(model::ColumnType logicalType, const Value &storedValue) noexcept -> bool
Checks both the canonical variant alternative and its logical range.
Definition value.cppm:128
QueryValue(std::string inputValue)
Definition value.cppm:75
QueryValue(Uuid inputValue)
Definition value.cppm:84
QueryValue(char inputValue)
Definition value.cppm:39
auto get() const -> const Value &
Definition value.cppm:188
QueryValue(UtcTimestamp inputValue)
Definition value.cppm:83
QueryValue(unsigned short inputValue)
Definition value.cppm:48
QueryValue(TimeOfDay inputValue)
Definition value.cppm:81
auto getLogicalType() const noexcept -> model::ColumnType
Definition value.cppm:183
QueryValue(const char *inputValue)
Definition value.cppm:74
std::variant< int, long long, unsigned long long, double, std::string, Date, TimeOfDay, LocalDateTime, UtcTimestamp, Uuid > Value
Definition value.cppm:35
QueryValue(bool inputValue)
Definition value.cppm:38
static auto fromStorage(model::ColumnType logicalType, Value value) -> QueryValue
Restores a canonical transport value with its model-level type.
Definition value.cppm:115
QueryValue(unsigned int inputValue)
Definition value.cppm:53
constexpr auto param() -> Parameter< T, Index >
consteval auto logicalType() -> model::ColumnType
Definition mapping.cppm:602
std::chrono::year_month_day Date
Definition values.cppm:20
std::chrono::local_time< std::chrono::microseconds > LocalDateTime
Definition values.cppm:23
consteval auto uuidStorage() -> model::UuidStorageDefinition< Member, Backend, Storage >
Definition columns.cppm:710
std::chrono::sys_time< std::chrono::microseconds > UtcTimestamp
Definition values.cppm:26
A named parameter for raw query fragments.
Definition value.cppm:233
std::optional< model::UuidStorage > uuidStorage
Definition value.cppm:236
std::optional< model::ColumnType > nullType
Definition value.cppm:237