Lightweight 0.20260303.0
Loading...
Searching...
No Matches
SqlNullValue.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6
7namespace Lightweight
8{
9
10/// Helper binder type to indicate NULL values in SQL queries.
11///
12/// @ingroup DataTypes
14{
15 /// The underlying SQL NULL indicator value.
16 SQLLEN sqlValue = SQL_NULL_DATA;
17};
18
19/// Used to indicate a NULL value in a SQL query.
20///
21/// @ingroup DataTypes
22constexpr inline auto SqlNullValue = SqlNullType {};
23
24template <>
25struct SqlDataBinder<SqlNullType>
26{
27 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
28 SQLUSMALLINT column,
29 SqlNullType const& value,
30 SqlDataBinderCallback& cb) noexcept
31 {
32 // This is generally ignored for NULL values, but MS SQL Server requires a non-zero value
33 // when the underlying type is e.g. an INT.
34
35 // For MS SQL Server, we need to determine the actual SQL type of the column to bind the parameter correctly,
36 // because requires a correctly convertible SQL type for NULL values.
37 // e.g. MS SQL Server cannot convert a VARCHAR NULL to a binary column type (e.g. BLOB).
38 SQLSMALLINT const sqlType = [stmt, column, serverType = cb.ServerType()]() -> SQLSMALLINT {
39 if (serverType == SqlServerType::MICROSOFT_SQL)
40 {
41 SQLSMALLINT columnType {};
42 auto const sqlReturn = SQLDescribeParam(stmt, column, &columnType, nullptr, nullptr, nullptr);
43 if (SQL_SUCCEEDED(sqlReturn))
44 return columnType;
45 }
46 return SQL_C_CHAR;
47 }();
48
49 return SQLBindParameter(stmt,
50 column,
51 SQL_PARAM_INPUT,
52 SQL_C_CHAR,
53 sqlType,
54 10,
55 0,
56 nullptr,
57 0,
58 &const_cast<SqlNullType&>(value).sqlValue);
59 }
60
61 static LIGHTWEIGHT_FORCE_INLINE std::string_view Inspect(SqlNullType const& /*value*/) noexcept
62 {
63 return "NULL";
64 }
65};
66
67} // namespace Lightweight
constexpr auto SqlNullValue
SQLLEN sqlValue
The underlying SQL NULL indicator value.