Lightweight 0.20261031.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 // The type comes from the statement rather than a SQLDescribeParam() here: the driver
39 // refuses to describe once any parameter is bound, which a NULL after the first one always is.
40 SQLSMALLINT const sqlType = cb.ServerType() == SqlServerType::MICROSOFT_SQL
41 ? cb.DescribeInputParameterType(column).value_or(SQL_C_CHAR)
42 : SQLSMALLINT { SQL_C_CHAR };
43
44 return SQLBindParameter(stmt,
45 column,
46 SQL_PARAM_INPUT,
47 SQL_C_CHAR,
48 sqlType,
49 10,
50 0,
51 nullptr,
52 0,
53 &const_cast<SqlNullType&>(value).sqlValue);
54 }
55
56 static LIGHTWEIGHT_FORCE_INLINE std::string_view Inspect(SqlNullType const& /*value*/) noexcept
57 {
58 return "NULL";
59 }
60};
61
62} // namespace Lightweight
constexpr auto SqlNullValue
SQLLEN sqlValue
The underlying SQL NULL indicator value.