131 if constexpr (std::is_same_v<T, Date>)
132 return orm::detail::values::parseDate(text);
133 else if constexpr (std::is_same_v<T, TimeOfDay>)
134 return orm::detail::values::parseTimeOfDay(text);
135 else if constexpr (std::is_same_v<T, LocalDateTime>)
136 return orm::detail::values::parseLocalDateTime(text);
137 else if constexpr (std::is_same_v<T, UtcTimestamp>)
138 return orm::detail::values::parseUtcTimestamp(text);
144 catch (
const std::invalid_argument&)
146 throw ConversionError{
"A database value cannot be represented by its logical column type"};
176 using result_t = std::remove_cv_t<Result>;
177 using source_t = std::remove_cv_t<Source>;
179 if constexpr (std::is_signed_v<result_t> == std::is_signed_v<source_t>)
181 if constexpr (std::numeric_limits<result_t>::digits >= std::numeric_limits<source_t>::digits)
187 return value >=
static_cast<source_t
>(std::numeric_limits<result_t>::lowest()) and
188 value <=
static_cast<source_t
>(std::numeric_limits<result_t>::max());
191 else if constexpr (std::is_signed_v<source_t>)
198 if constexpr (std::numeric_limits<result_t>::digits >= std::numeric_limits<source_t>::digits)
204 using unsigned_source_t = std::make_unsigned_t<source_t>;
205 return static_cast<unsigned_source_t
>(value) <=
206 static_cast<unsigned_source_t
>(std::numeric_limits<result_t>::max());
211 if constexpr (std::numeric_limits<result_t>::digits >= std::numeric_limits<source_t>::digits)
217 return value <= static_cast<source_t>(std::numeric_limits<result_t>::max());
231 if constexpr (std::is_floating_point_v<Source>)
233 if (not std::isfinite(value))
239 if constexpr (std::is_integral_v<Result> and std::is_integral_v<Source>)
241 if constexpr (std::is_same_v<std::remove_cv_t<Result>,
bool>)
243 if (value != 0 and value != 1)
248 else if constexpr (not std::is_same_v<std::remove_cv_t<Source>,
bool>)
256 return static_cast<Result
>(value);
258 else if constexpr (std::is_integral_v<Result> and std::is_floating_point_v<Source>)
260 const auto numericValue =
static_cast<long double>(value);
261 const auto upperExclusive = std::ldexp(1.0L, std::numeric_limits<Result>::digits);
262 const auto lowerInclusive = std::is_signed_v<Result> ? -upperExclusive : 0.0L;
264 if (std::trunc(numericValue) != numericValue or numericValue < lowerInclusive or
265 numericValue >= upperExclusive or
266 (std::is_same_v<std::remove_cv_t<Result>,
bool> and numericValue != 0.0L and numericValue != 1.0L))
271 return static_cast<Result
>(value);
273 else if constexpr (std::is_floating_point_v<Result> and std::is_integral_v<Source>)
275 const auto numericValue =
static_cast<long double>(value);
277 if (numericValue <
static_cast<long double>(std::numeric_limits<Result>::lowest()) or
278 numericValue >
static_cast<long double>(std::numeric_limits<Result>::max()))
283 if constexpr (not std::is_same_v<std::remove_cv_t<Source>,
bool>)
285 using unsigned_source_t = std::make_unsigned_t<Source>;
286 const auto unsignedValue =
static_cast<unsigned_source_t
>(value);
287 auto magnitude = unsignedValue;
289 if constexpr (std::is_signed_v<Source>)
293 magnitude = unsigned_source_t{} - unsignedValue;
299 const auto significantBits = std::bit_width(magnitude) - std::countr_zero(magnitude);
301 if (significantBits > std::numeric_limits<Result>::digits)
308 return static_cast<Result
>(value);
310 else if constexpr (std::is_floating_point_v<Result> and std::is_floating_point_v<Source> and
311 std::numeric_limits<Result>::digits < std::numeric_limits<Source>::digits)
313 if (value <
static_cast<Source
>(std::numeric_limits<Result>::lowest()) or
314 value >
static_cast<Source
>(std::numeric_limits<Result>::max()))
319 const auto converted =
static_cast<Result
>(value);
321 if (
static_cast<Source
>(converted) != value)
330 return static_cast<Result
>(value);
340 const auto storedValue = std::string{value};
344 if constexpr (std::is_floating_point_v<Result>)
347 const auto*
const end = storedValue.data() + storedValue.size();
348 const auto [parsedEnd, error] =
349 std::from_chars(storedValue.data(), end, parsed, std::chars_format::general);
351 if (error != std::errc{} or parsedEnd != end)
353 throw ConversionError{
"Cannot hydrate numeric field: " + std::string{fieldName}};
358 else if constexpr (std::is_unsigned_v<Result>)
360 std::size_t parsedCharacters{};
361 const auto firstNonWhitespace = storedValue.find_first_not_of(
" \f\n\r\t\v");
363 if (firstNonWhitespace != std::string::npos and storedValue[firstNonWhitespace] ==
'-')
365 throw ConversionError{
"Cannot hydrate numeric field: " + std::string{fieldName}};
368 const auto parsed = std::stoull(storedValue, &parsedCharacters);
370 if (parsedCharacters != storedValue.size())
372 throw ConversionError{
"Cannot hydrate numeric field: " + std::string{fieldName}};
379 std::size_t parsedCharacters{};
380 const auto parsed = std::stoll(storedValue, &parsedCharacters);
382 if (parsedCharacters != storedValue.size())
384 throw ConversionError{
"Cannot hydrate numeric field: " + std::string{fieldName}};
394 catch (
const std::exception&)
396 throw ConversionError{
"Cannot hydrate numeric field: " + std::string{fieldName}};
423 auto convertStoredValue = [&values, &fieldName]<
typename Stored>() -> Result
428 switch (values.get_properties(fieldName).get_data_type())
430 case soci::dt_integer:
431 return convertStoredValue.template operator()<
int>();
432 case soci::dt_long_long:
433 return convertStoredValue.template operator()<
long long>();
434 case soci::dt_unsigned_long_long:
435 return convertStoredValue.template operator()<
unsigned long long>();
436 case soci::dt_double:
437 return convertStoredValue.template operator()<
double>();
438 case soci::dt_string:
448 catch (
const std::exception&)
453 auto result = Result{};
471 catch (
const std::exception&)