Lightweight 0.20251202.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 BatchInputParameter(
25 SQLHSTMT stmt, SQLUSMALLINT column, T const* values, size_t /*rowCount*/, SqlDataBinderCallback& /*cb*/) noexcept
26 {
27 return SQLBindParameter(
28 stmt, column, SQL_PARAM_INPUT, TheCType, TheSqlType, 0, 0, (SQLPOINTER) values, sizeof(T), nullptr);
29 }
30
31 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
32 SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback& /*unused*/) noexcept
33 {
34 return SQLBindCol(stmt, column, TheCType, result, 0, indicator);
35 }
36
37 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN
38 GetColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback const& /*cb*/) noexcept
39 {
40 return SQLGetData(stmt, column, TheCType, result, 0, indicator);
41 }
42
43 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(T value)
44 {
45 return std::to_string(value);
46 }
47};
48
49template <typename Int64Type, SQLSMALLINT TheCType>
50struct Int64DataBinderHelper
51{
52 static constexpr SqlColumnTypeDefinition ColumnType = SqlColumnTypeDefinitions::Bigint {};
53
54 static LIGHTWEIGHT_API SQLRETURN InputParameter(SQLHSTMT stmt,
55 SQLUSMALLINT column,
56 Int64Type const& value,
57 SqlDataBinderCallback& cb) noexcept;
58
59 static LIGHTWEIGHT_API SQLRETURN BatchInputParameter(
60 SQLHSTMT stmt, SQLUSMALLINT column, Int64Type const* values, size_t rowCount, SqlDataBinderCallback& cb) noexcept;
61
62 static LIGHTWEIGHT_API SQLRETURN OutputColumn(
63 SQLHSTMT stmt, SQLUSMALLINT column, Int64Type* result, SQLLEN* indicator, SqlDataBinderCallback& cb) noexcept;
64
65 static LIGHTWEIGHT_API SQLRETURN GetColumn(
66 SQLHSTMT stmt, SQLUSMALLINT column, Int64Type* result, SQLLEN* indicator, SqlDataBinderCallback const& cb) noexcept;
67
68 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(Int64Type value)
69 {
70 return std::to_string(value);
71 }
72};
73
74// clang-format off
75template <> struct SqlDataBinder<bool>: SqlSimpleDataBinder<bool, SQL_BIT, SQL_BIT, SqlColumnTypeDefinitions::Bool {}> {};
76template <> struct SqlDataBinder<char>: SqlSimpleDataBinder<char, SQL_C_CHAR, SQL_CHAR, SqlColumnTypeDefinitions::Char {}> {};
77template <> struct SqlDataBinder<int8_t>: SqlSimpleDataBinder<int8_t, SQL_C_STINYINT, SQL_TINYINT, SqlColumnTypeDefinitions::Tinyint {}> {};
78template <> struct SqlDataBinder<uint8_t>: SqlSimpleDataBinder<uint8_t, SQL_C_UTINYINT, SQL_TINYINT, SqlColumnTypeDefinitions::Tinyint {}> {};
79template <> struct SqlDataBinder<int16_t>: SqlSimpleDataBinder<int16_t, SQL_C_SSHORT, SQL_SMALLINT, SqlColumnTypeDefinitions::Smallint {}> {};
80template <> struct SqlDataBinder<uint16_t>: SqlSimpleDataBinder<uint16_t, SQL_C_USHORT, SQL_SMALLINT, SqlColumnTypeDefinitions::Smallint {}> {};
81template <> struct SqlDataBinder<int32_t>: SqlSimpleDataBinder<int32_t, SQL_C_SLONG, SQL_INTEGER, SqlColumnTypeDefinitions::Integer {}> {};
82template <> struct SqlDataBinder<uint32_t>: SqlSimpleDataBinder<uint32_t, SQL_C_ULONG, SQL_INTEGER, SqlColumnTypeDefinitions::Integer {}> {};
83template <> struct SqlDataBinder<int64_t>: Int64DataBinderHelper<int64_t, SQL_C_SBIGINT> {};
84template <> struct SqlDataBinder<uint64_t>: Int64DataBinderHelper<uint64_t, SQL_C_UBIGINT> {};
85//template <> struct SqlDataBinder<uint64_t>: Int64DataBinderHelper<uint64_t, SQL_C_UBIGINT> {};
86template <> struct SqlDataBinder<float>: SqlSimpleDataBinder<float, SQL_C_FLOAT, SQL_REAL, SqlColumnTypeDefinitions::Real {}> {};
87template <> struct SqlDataBinder<double>: SqlSimpleDataBinder<double, SQL_C_DOUBLE, SQL_DOUBLE, SqlColumnTypeDefinitions::Real {}> {};
88#if !defined(_WIN32) && !defined(__APPLE__)
89template <> struct SqlDataBinder<long long>: Int64DataBinderHelper<long long, SQL_C_SBIGINT> {};
90template <> struct SqlDataBinder<unsigned long long>: Int64DataBinderHelper<unsigned long long, SQL_C_UBIGINT> {};
91#endif
92#if defined(__APPLE__) // size_t is a different type on macOS
93template <> struct SqlDataBinder<std::size_t>: SqlSimpleDataBinder<std::size_t, SQL_C_SBIGINT, SqlColumnTypeDefinitions::Bigint {}> {};
94#endif
95// clang-format on
96
97} // namespace Lightweight