Lightweight 0.1.0
Loading...
Searching...
No Matches
StdOptional.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6#include "SqlNullValue.hpp"
7
8#include <format>
9#include <optional>
10
11template <typename T>
12struct SqlDataBinder<std::optional<T>>
13{
14 using OptionalValue = std::optional<T>;
15
16 static constexpr auto ColumnType = SqlDataBinder<T>::ColumnType;
17
18 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
19 SQLUSMALLINT column,
20 OptionalValue const& value,
21 SqlDataBinderCallback& cb) noexcept
22 {
23 if (value.has_value())
24 return SqlDataBinder<T>::InputParameter(stmt, column, *value, cb);
25 else
26 return SqlDataBinder<SqlNullType>::InputParameter(stmt, column, SqlNullValue, cb);
27 }
28
29 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(SQLHSTMT stmt,
30 SQLUSMALLINT column,
31 OptionalValue* result,
32 SQLLEN* indicator,
33 SqlDataBinderCallback& cb) noexcept
34 {
35 if (!result)
36 return SQL_ERROR;
37
38 auto const sqlReturn = SqlDataBinder<T>::OutputColumn(stmt, column, &result->emplace(), indicator, cb);
39 cb.PlanPostProcessOutputColumn([result, indicator]() {
40 if (indicator && *indicator == SQL_NULL_DATA)
41 *result = std::nullopt;
42 });
43 return sqlReturn;
44 }
45
46 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
47 SQLUSMALLINT column,
48 OptionalValue* result,
49 SQLLEN* indicator,
50 SqlDataBinderCallback const& cb) noexcept
51 {
52 auto const sqlReturn = SqlDataBinder<T>::GetColumn(stmt, column, &result->emplace(), indicator, cb);
53 if (indicator && *indicator == SQL_NULL_DATA)
54 *result = std::nullopt;
55 return sqlReturn;
56 }
57
58 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(OptionalValue const& value) noexcept
59 {
60 if (!value)
61 return "NULL";
62 else
63 return std::string(SqlDataBinder<T>::Inspect(*value));
64 }
65};
constexpr auto SqlNullValue