Lightweight 0.20261105.1
Loading...
Searching...
No Matches
ValueStorageLayout.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5// Pins Lightweight's value storage structs against the ODBC structs they stand in for.
6//
7// The date, time, timestamp and numeric binders hand `&value.sqlValue` to the driver as the ODBC struct, so the
8// layouts must match byte for byte: size, alignment and every member offset. Each of those binders includes this
9// header, so a mismatch fails the library build rather than corrupting values at run time.
10
11// <sql.h> below needs a few symbols resolved first — see SqlOdbcPrelude.hpp's header comment.
12#include "../../DataBinder/SqlValueStorage.hpp"
13#include "SqlOdbcPrelude.hpp"
14#include "SqlServerTime2.hpp"
15
16#include <cstddef>
17#include <type_traits>
18
19#include <sql.h>
20#include <sqlext.h>
21
22namespace Lightweight::Driver::Odbc::detail
23{
24
25/// Whether @p Ours can be handed to the driver in place of @p Odbc: same size and alignment, and plain data.
26template <typename Ours, typename Odbc>
27inline constexpr bool SameStorageLayout = sizeof(Ours) == sizeof(Odbc) && alignof(Ours) == alignof(Odbc)
28 && std::is_standard_layout_v<Ours> && std::is_trivially_copyable_v<Ours>;
29
30static_assert(SameStorageLayout<SqlDateStorage, SQL_DATE_STRUCT>);
31static_assert(SameStorageLayout<SqlTimestampStorage, SQL_TIMESTAMP_STRUCT>);
32static_assert(SameStorageLayout<SqlTime2Storage, SQL_SS_TIME2_STRUCT>);
33static_assert(SameStorageLayout<SqlNumericStorage, SQL_NUMERIC_STRUCT>);
34static_assert(sizeof(SqlTime2Storage) == 12);
35
36static_assert(offsetof(SqlDateStorage, year) == offsetof(SQL_DATE_STRUCT, year));
37static_assert(offsetof(SqlDateStorage, month) == offsetof(SQL_DATE_STRUCT, month));
38static_assert(offsetof(SqlDateStorage, day) == offsetof(SQL_DATE_STRUCT, day));
39static_assert(offsetof(SqlTimestampStorage, year) == offsetof(SQL_TIMESTAMP_STRUCT, year));
40static_assert(offsetof(SqlTimestampStorage, month) == offsetof(SQL_TIMESTAMP_STRUCT, month));
41static_assert(offsetof(SqlTimestampStorage, day) == offsetof(SQL_TIMESTAMP_STRUCT, day));
42static_assert(offsetof(SqlTimestampStorage, hour) == offsetof(SQL_TIMESTAMP_STRUCT, hour));
43static_assert(offsetof(SqlTimestampStorage, minute) == offsetof(SQL_TIMESTAMP_STRUCT, minute));
44static_assert(offsetof(SqlTimestampStorage, second) == offsetof(SQL_TIMESTAMP_STRUCT, second));
45static_assert(offsetof(SqlTimestampStorage, fraction) == offsetof(SQL_TIMESTAMP_STRUCT, fraction));
46static_assert(offsetof(SqlTime2Storage, hour) == offsetof(SQL_SS_TIME2_STRUCT, hour));
47static_assert(offsetof(SqlTime2Storage, minute) == offsetof(SQL_SS_TIME2_STRUCT, minute));
48static_assert(offsetof(SqlTime2Storage, second) == offsetof(SQL_SS_TIME2_STRUCT, second));
49static_assert(offsetof(SqlTime2Storage, fraction) == offsetof(SQL_SS_TIME2_STRUCT, fraction));
50static_assert(offsetof(SqlNumericStorage, precision) == offsetof(SQL_NUMERIC_STRUCT, precision));
51static_assert(offsetof(SqlNumericStorage, scale) == offsetof(SQL_NUMERIC_STRUCT, scale));
52static_assert(offsetof(SqlNumericStorage, sign) == offsetof(SQL_NUMERIC_STRUCT, sign));
53static_assert(offsetof(SqlNumericStorage, val) == offsetof(SQL_NUMERIC_STRUCT, val));
54
55} // namespace Lightweight::Driver::Odbc::detail
The SQL Server ODBC extension's buffer for a TIME(7) value (SQL_SS_TIME2 in msodbcsql....