Lightweight 0.20250904.0
Loading...
Searching...
No Matches
Primitives.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlColumnTypeDefinitions.hpp"
6#include "Core.hpp"
7
8namespace Lightweight
9{
10
11template <typename T, SQLSMALLINT TheCType, SQLINTEGER TheSqlType, auto TheColumnType>
12struct SqlSimpleDataBinder
13{
14 static constexpr SqlColumnTypeDefinition ColumnType = TheColumnType;
15
16 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
17 SQLUSMALLINT column,
18 T const& value,
19 SqlDataBinderCallback& /*cb*/) noexcept
20 {
21 return SQLBindParameter(stmt, column, SQL_PARAM_INPUT, TheCType, TheSqlType, 0, 0, (SQLPOINTER) &value, 0, nullptr);
22 }
23
24 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
25 SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback& /*unused*/) noexcept
26 {
27 return SQLBindCol(stmt, column, TheCType, result, 0, indicator);
28 }
29
30 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN
31 GetColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback const& /*cb*/) noexcept
32 {
33 return SQLGetData(stmt, column, TheCType, result, 0, indicator);
34 }
35
36 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(T value)
37 {
38 return std::to_string(value);
39 }
40};
41
42template <typename Int64Type, SQLSMALLINT TheCType>
43struct Int64DataBinderHelper
44{
45 static constexpr SqlColumnTypeDefinition ColumnType = SqlColumnTypeDefinitions::Bigint {};
46
47 static LIGHTWEIGHT_API SQLRETURN InputParameter(SQLHSTMT stmt,
48 SQLUSMALLINT column,
49 Int64Type const& value,
50 SqlDataBinderCallback& cb) noexcept;
51
52 static LIGHTWEIGHT_API SQLRETURN OutputColumn(
53 SQLHSTMT stmt, SQLUSMALLINT column, Int64Type* result, SQLLEN* indicator, SqlDataBinderCallback& cb) noexcept;
54
55 static LIGHTWEIGHT_API SQLRETURN GetColumn(
56 SQLHSTMT stmt, SQLUSMALLINT column, Int64Type* result, SQLLEN* indicator, SqlDataBinderCallback const& cb) noexcept;
57
58 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(Int64Type value)
59 {
60 return std::to_string(value);
61 }
62};
63
64// clang-format off
65template <> struct SqlDataBinder<bool>: SqlSimpleDataBinder<bool, SQL_BIT, SQL_BIT, SqlColumnTypeDefinitions::Bool {}> {};
66template <> struct SqlDataBinder<char>: SqlSimpleDataBinder<char, SQL_C_CHAR, SQL_CHAR, SqlColumnTypeDefinitions::Char {}> {};
67template <> struct SqlDataBinder<int8_t>: SqlSimpleDataBinder<int8_t, SQL_C_STINYINT, SQL_TINYINT, SqlColumnTypeDefinitions::Tinyint {}> {};
68template <> struct SqlDataBinder<uint8_t>: SqlSimpleDataBinder<uint8_t, SQL_C_UTINYINT, SQL_TINYINT, SqlColumnTypeDefinitions::Tinyint {}> {};
69template <> struct SqlDataBinder<int16_t>: SqlSimpleDataBinder<int16_t, SQL_C_SSHORT, SQL_SMALLINT, SqlColumnTypeDefinitions::Smallint {}> {};
70template <> struct SqlDataBinder<uint16_t>: SqlSimpleDataBinder<uint16_t, SQL_C_USHORT, SQL_SMALLINT, SqlColumnTypeDefinitions::Smallint {}> {};
71template <> struct SqlDataBinder<int32_t>: SqlSimpleDataBinder<int32_t, SQL_C_SLONG, SQL_INTEGER, SqlColumnTypeDefinitions::Integer {}> {};
72template <> struct SqlDataBinder<uint32_t>: SqlSimpleDataBinder<uint32_t, SQL_C_ULONG, SQL_INTEGER, SqlColumnTypeDefinitions::Integer {}> {};
73template <> struct SqlDataBinder<int64_t>: Int64DataBinderHelper<int64_t, SQL_C_SBIGINT> {};
74template <> struct SqlDataBinder<uint64_t>: Int64DataBinderHelper<uint64_t, SQL_C_UBIGINT> {};
75//template <> struct SqlDataBinder<uint64_t>: Int64DataBinderHelper<uint64_t, SQL_C_UBIGINT> {};
76template <> struct SqlDataBinder<float>: SqlSimpleDataBinder<float, SQL_C_FLOAT, SQL_REAL, SqlColumnTypeDefinitions::Real {}> {};
77template <> struct SqlDataBinder<double>: SqlSimpleDataBinder<double, SQL_C_DOUBLE, SQL_DOUBLE, SqlColumnTypeDefinitions::Real {}> {};
78#if !defined(_WIN32) && !defined(__APPLE__)
79template <> struct SqlDataBinder<long long>: Int64DataBinderHelper<long long, SQL_C_SBIGINT> {};
80template <> struct SqlDataBinder<unsigned long long>: Int64DataBinderHelper<unsigned long long, SQL_C_UBIGINT> {};
81#endif
82#if defined(__APPLE__) // size_t is a different type on macOS
83template <> struct SqlDataBinder<std::size_t>: SqlSimpleDataBinder<std::size_t, SQL_C_SBIGINT, SqlColumnTypeDefinitions::Bigint {}> {};
84#endif
85// clang-format on
86
87} // namespace Lightweight