Lightweight 0.20261105.1
Loading...
Searching...
No Matches
NativeColumnType.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5// Included at the end of SqlColumnTypeDefinitions.hpp, which therefore is complete here even when this
6// header is the one included first.
7#include "../../SqlColumnTypeDefinitions.hpp"
8#include "SqlServerTime2.hpp"
9
10#include <cstddef>
11#include <optional>
12
13#include <sql.h>
14#include <sqlext.h>
15
16namespace Lightweight
17{
18
19/// Maps ODBC data type (with given @p size and @p precision) to SqlColumnTypeDefinition
20///
21/// @return SqlColumnTypeDefinition if the data type is supported, otherwise std::nullopt
22constexpr std::optional<SqlColumnTypeDefinition> MakeColumnTypeFromNative(int value, std::size_t size, std::size_t precision)
23{
24 // Maps ODBC data types to SqlColumnTypeDefinition
25 // See: https://learn.microsoft.com/en-us/sql/odbc/reference/appendixes/sql-data-types?view=sql-server-ver16
26 using namespace SqlColumnTypeDefinitions;
27 // clang-format off
28 switch (value)
29 {
30 case SQL_BIGINT: return Bigint {};
31 case SQL_BINARY: return Binary { size };
32 case SQL_BIT: return Bool {};
33 case SQL_CHAR: return Char { size };
34 case SQL_DATE: return Date {};
35 case SQL_DECIMAL: return Decimal { .precision = size, .scale = precision };
36 case SQL_DOUBLE: return Real { .precision = 53 };
37 case SQL_FLOAT: return Real { . precision = precision };
38 case SQL_GUID: return Guid {};
39 case SQL_INTEGER: return Integer {};
40 case SQL_LONGVARBINARY: return VarBinary { .size = size };
41 case SQL_LONGVARCHAR: return Varchar { .size = size };
42 case SQL_NUMERIC: return Decimal { .precision = size, .scale = precision };
43 case SQL_REAL: return Real { .precision = 24 };
44 case SQL_SMALLINT: return Smallint {};
45 case SQL_TIME: return Time {};
46 case SQL_TIMESTAMP: return DateTime {};
47 case SQL_TINYINT: return Tinyint {};
48 case SQL_TYPE_DATE: return Date {};
49 case SQL_TYPE_TIME: return Time {};
50 case SQL_SS_TIME2: return Time {}; // Microsoft SQL Server extension
51 case SQL_TYPE_TIMESTAMP: return DateTime {};
52 case SQL_VARBINARY: return VarBinary { .size = size };
53 case SQL_VARCHAR: return Varchar { .size = size };
54 case SQL_WCHAR: return NChar { size };
55 case SQL_WLONGVARCHAR: return NVarchar { .size = size };
56 case SQL_WVARCHAR: return NVarchar { .size = size };
57 case SQL_UNKNOWN_TYPE: return std::nullopt;
58 default: return std::nullopt;
59 }
60 // clang-format on
61}
62
63} // namespace Lightweight
64
65namespace Lightweight::Driver::Odbc
66{
67
68// SqlSchema's floating-point fixups name the definitions the 4- and 8-byte ODBC float types map onto.
69static_assert(MakeColumnTypeFromNative(SQL_REAL, 0, 0)
70 == SqlColumnTypeDefinition { SqlColumnTypeDefinitions::Real { .precision = 24 } });
71static_assert(MakeColumnTypeFromNative(SQL_DOUBLE, 0, 0)
72 == SqlColumnTypeDefinition { SqlColumnTypeDefinitions::Real { .precision = 53 } });
73
74} // namespace Lightweight::Driver::Odbc
@ Integer
A signed integer of up to 8 bytes.