Lightweight 0.20260617.0
Loading...
Searching...
No Matches
SqlDate.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlColumnTypeDefinitions.hpp"
6#include "Core.hpp"
7
8#include <chrono>
9#include <format>
10
11namespace Lightweight
12{
13
14/// Represents a date to efficiently write to or read from a database.
15///
16/// @ingroup DataTypes
17struct SqlDate
18{
19 /// Holds the underlying SQL date structure.
20 SQL_DATE_STRUCT sqlValue {};
21
22 /// Default constructor.
23 constexpr SqlDate() noexcept = default;
24 /// Default move constructor.
25 constexpr SqlDate(SqlDate&&) noexcept = default;
26 /// Default move assignment operator.
27 constexpr SqlDate& operator=(SqlDate&&) noexcept = default;
28 /// Default copy constructor.
29 constexpr SqlDate(SqlDate const&) noexcept = default;
30 /// Default copy assignment operator.
31 constexpr SqlDate& operator=(SqlDate const&) noexcept = default;
32 constexpr ~SqlDate() noexcept = default;
33
34 /// Returns the current date.
35 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::chrono::year_month_day value() const noexcept
36 {
38 }
39
40 /// Equality comparison operator.
41 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator==(SqlDate const& other) const noexcept
42 {
43 return sqlValue.year == other.sqlValue.year && sqlValue.month == other.sqlValue.month
44 && sqlValue.day == other.sqlValue.day;
45 }
46
47 /// Inequality comparison operator.
48 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!=(SqlDate const& other) const noexcept
49 {
50 return !(*this == other);
51 }
52
53 /// Constructs a date from individual std::chrono::year_month_day.
54 constexpr SqlDate(std::chrono::year_month_day value) noexcept:
56 {
57 }
58
59 /// Constructs a date from individual components.
60 constexpr SqlDate(std::chrono::year year, std::chrono::month month, std::chrono::day day) noexcept:
61 SqlDate(std::chrono::year_month_day { year, month, day })
62 {
63 }
64
65 /// Returns the current date.
66 [[nodiscard]] static LIGHTWEIGHT_FORCE_INLINE SqlDate Today() noexcept
67 {
68 return SqlDate { std::chrono::year_month_day {
69 std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()),
70 } };
71 }
72
73 /// Converts a std::chrono::year_month_day to the underlying SQL date structure.
74 static LIGHTWEIGHT_FORCE_INLINE constexpr SQL_DATE_STRUCT ConvertToSqlValue(std::chrono::year_month_day value) noexcept
75 {
76 return SQL_DATE_STRUCT {
77 .year = (SQLSMALLINT) (int) value.year(),
78 .month = (SQLUSMALLINT) (unsigned) value.month(),
79 .day = (SQLUSMALLINT) (unsigned) value.day(),
80 };
81 }
82
83 /// Converts a SQL date structure to std::chrono::year_month_day.
84 static LIGHTWEIGHT_FORCE_INLINE constexpr std::chrono::year_month_day ConvertToNative(
85 SQL_DATE_STRUCT const& value) noexcept
86 {
87 return std::chrono::year_month_day { std::chrono::year { value.year },
88 std::chrono::month { static_cast<unsigned>(value.month) },
89 std::chrono::day { static_cast<unsigned>(value.day) } };
90 }
91};
92
93} // namespace Lightweight
94
95template <>
96struct std::formatter<Lightweight::SqlDate>: std::formatter<std::string>
97{
98 auto format(Lightweight::SqlDate const& value, std::format_context& ctx) const -> std::format_context::iterator
99 {
100 return std::formatter<std::string>::format(
101 std::format("{:04}-{:02}-{:02}", value.sqlValue.year, value.sqlValue.month, value.sqlValue.day), ctx);
102 }
103};
104
105namespace Lightweight
106{
107
108template <>
109struct SqlDataBinder<SqlDate>
110{
111 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Date {};
112
113 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
114 SQLUSMALLINT column,
115 SqlDate const& value,
116 SqlDataBinderCallback& /*cb*/) noexcept
117 {
118 return SQLBindParameter(
119 stmt, column, SQL_PARAM_INPUT, SQL_C_TYPE_DATE, SQL_TYPE_DATE, 0, 0, (SQLPOINTER) &value.sqlValue, 0, nullptr);
120 }
121
122 /// Binds an array of dates as an input parameter for native (row-wise or column-wise) batch
123 /// execution. @p values points at the first element; the driver strides by the statement's
124 /// SQL_ATTR_PARAM_BIND_TYPE. @p indicators optionally supplies per-row NULL flags.
125 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN BatchInputParameter(SQLHSTMT stmt,
126 SQLUSMALLINT column,
127 SqlDate const* values,
128 size_t /*rowCount*/,
129 SqlDataBinderCallback& /*cb*/,
130 SQLLEN* indicators = nullptr) noexcept
131 {
132 return SQLBindParameter(stmt,
133 column,
134 SQL_PARAM_INPUT,
135 SQL_C_TYPE_DATE,
136 SQL_TYPE_DATE,
137 0,
138 0,
139 (SQLPOINTER) &values->sqlValue,
140 sizeof(values->sqlValue),
141 indicators);
142 }
143
144 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
145 SQLHSTMT stmt, SQLUSMALLINT column, SqlDate* result, SQLLEN* indicator, SqlDataBinderCallback& /*cb*/) noexcept
146 {
147 return SQLBindCol(stmt, column, SQL_C_TYPE_DATE, &result->sqlValue, sizeof(result->sqlValue), indicator);
148 }
149
150 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(
151 SQLHSTMT stmt, SQLUSMALLINT column, SqlDate* result, SQLLEN* indicator, SqlDataBinderCallback const& /*cb*/) noexcept
152 {
153 return SQLGetData(stmt, column, SQL_C_TYPE_DATE, &result->sqlValue, sizeof(result->sqlValue), indicator);
154 }
155
156 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlDate const& value) noexcept
157 {
158 return std::format("{}", value);
159 }
160};
161
162template <>
163inline constexpr bool SqlIsNativeRowBindableValue<SqlDate> = true;
164
165} // namespace Lightweight
constexpr SqlDate(std::chrono::year_month_day value) noexcept
Constructs a date from individual std::chrono::year_month_day.
Definition SqlDate.hpp:54
LIGHTWEIGHT_FORCE_INLINE constexpr std::chrono::year_month_day value() const noexcept
Returns the current date.
Definition SqlDate.hpp:35
static LIGHTWEIGHT_FORCE_INLINE SqlDate Today() noexcept
Returns the current date.
Definition SqlDate.hpp:66
constexpr SqlDate(std::chrono::year year, std::chrono::month month, std::chrono::day day) noexcept
Constructs a date from individual components.
Definition SqlDate.hpp:60
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator==(SqlDate const &other) const noexcept
Equality comparison operator.
Definition SqlDate.hpp:41
constexpr SqlDate() noexcept=default
Default constructor.
SQL_DATE_STRUCT sqlValue
Holds the underlying SQL date structure.
Definition SqlDate.hpp:20
static LIGHTWEIGHT_FORCE_INLINE constexpr SQL_DATE_STRUCT ConvertToSqlValue(std::chrono::year_month_day value) noexcept
Converts a std::chrono::year_month_day to the underlying SQL date structure.
Definition SqlDate.hpp:74
static LIGHTWEIGHT_FORCE_INLINE constexpr std::chrono::year_month_day ConvertToNative(SQL_DATE_STRUCT const &value) noexcept
Converts a SQL date structure to std::chrono::year_month_day.
Definition SqlDate.hpp:84
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!=(SqlDate const &other) const noexcept
Inequality comparison operator.
Definition SqlDate.hpp:48