Lightweight 0.20260213.0
Loading...
Searching...
No Matches
SqlDateTime.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlColumnTypeDefinitions.hpp"
6#include "Core.hpp"
7#include "SqlDate.hpp"
8#include "SqlTime.hpp"
9
10#include <chrono>
11#include <format>
12
13#include <sql.h>
14#include <sqlext.h>
15#include <sqltypes.h>
16
17namespace Lightweight
18{
19
20/// Represents a date and time to efficiently write to or read from a database.
21///
22/// @see SqlDate, SqlTime
23/// @ingroup DataTypes
25{
26 /// The native C++ type representing a date-time value.
27 using native_type = std::chrono::system_clock::time_point;
28 /// The duration type used for arithmetic operations.
29 using duration_type = std::chrono::system_clock::duration;
30
31 /// Returns the current date and time.
32 [[nodiscard]] static LIGHTWEIGHT_FORCE_INLINE SqlDateTime Now() noexcept
33 {
34 return SqlDateTime { std::chrono::system_clock::now() };
35 }
36
37#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
38 /// Return the current date and time in UTC.
39 [[nodiscard]] static LIGHTWEIGHT_FORCE_INLINE SqlDateTime NowUTC() noexcept
40 {
41 return SqlDateTime { std::chrono::system_clock::time_point { std::chrono::utc_clock::now().time_since_epoch() } };
42 }
43#endif
44
45 /// Default constructor.
46 constexpr SqlDateTime() noexcept = default;
47 /// Default move constructor.
48 constexpr SqlDateTime(SqlDateTime&&) noexcept = default;
49 /// Default move assignment operator.
50 constexpr SqlDateTime& operator=(SqlDateTime&&) noexcept = default;
51 /// Default copy constructor.
52 constexpr SqlDateTime(SqlDateTime const&) noexcept = default;
53 /// Default copy assignment operator.
54 constexpr SqlDateTime& operator=(SqlDateTime const& other) noexcept = default;
55 constexpr ~SqlDateTime() noexcept = default;
56
57 /// Three-way comparison operator.
58 constexpr std::weak_ordering operator<=>(SqlDateTime const& other) const noexcept
59 {
60 return value() <=> other.value();
61 }
62 /// Equality comparison operator.
63 constexpr bool operator==(SqlDateTime const& other) const noexcept
64 {
65 return (*this <=> other) == std::weak_ordering::equivalent;
66 }
67
68 /// Inequality comparison operator.
69 constexpr bool operator!=(SqlDateTime const& other) const noexcept
70 {
71 return !(*this == other);
72 }
73
74 /// Constructs a date and time from individual components.
75 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::year_month_day ymd,
76 std::chrono::hh_mm_ss<duration_type> time) noexcept:
77 sqlValue {
78 .year = (SQLSMALLINT) (int) ymd.year(),
79 .month = (SQLUSMALLINT) (unsigned) ymd.month(),
80 .day = (SQLUSMALLINT) (unsigned) ymd.day(),
81 .hour = (SQLUSMALLINT) time.hours().count(),
82 .minute = (SQLUSMALLINT) time.minutes().count(),
83 .second = (SQLUSMALLINT) time.seconds().count(),
84 .fraction =
85 (SQLUINTEGER) (std::chrono::duration_cast<std::chrono::nanoseconds>(time.to_duration()).count() / 100) * 100,
86 }
87 {
88 }
89
90 /// Constructs a date and time from individual components.
91 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(
92 std::chrono::year year,
93 std::chrono::month month,
94 std::chrono::day day,
95 std::chrono::hours hour,
96 std::chrono::minutes minute,
97 std::chrono::seconds second,
98 std::chrono::nanoseconds nanosecond = std::chrono::nanoseconds(0)) noexcept:
99 sqlValue {
100 .year = (SQLSMALLINT) (int) year,
101 .month = (SQLUSMALLINT) (unsigned) month,
102 .day = (SQLUSMALLINT) (unsigned) day,
103 .hour = (SQLUSMALLINT) hour.count(),
104 .minute = (SQLUSMALLINT) minute.count(),
105 .second = (SQLUSMALLINT) second.count(),
106 .fraction = (SQLUINTEGER) (nanosecond.count() / 100) * 100,
107 }
108 {
109 }
110
111 /// Constructs a date and time from a time point.
112 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::system_clock::time_point value) noexcept:
114 {
115 }
116
117 // NOLINTBEGIN(readability-identifier-naming)
118
119 /// Returns the year of this date-time object.
120 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year year() const noexcept
121 {
122 return std::chrono::year(static_cast<int>(sqlValue.year));
123 }
124
125 /// Returns the month of this date-time object.
126 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month month() const noexcept
127 {
128 return std::chrono::month(static_cast<unsigned>(sqlValue.month));
129 }
130
131 /// Returns the day of this date-time object.
132 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day day() const noexcept
133 {
134 return std::chrono::day(static_cast<unsigned>(sqlValue.day));
135 }
136
137 /// Returns the hour of this date-time object.
138 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours hour() const noexcept
139 {
140 return std::chrono::hours(static_cast<unsigned>(sqlValue.hour));
141 }
142
143 /// Returns the minute of this date-time object.
144 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes minute() const noexcept
145 {
146 return std::chrono::minutes(static_cast<unsigned>(sqlValue.minute));
147 }
148
149 /// Returns the second of this date-time object.
150 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds second() const noexcept
151 {
152 return std::chrono::seconds(static_cast<unsigned>(sqlValue.second));
153 }
154
155 /// Returns the nanosecond of this date-time object.
156 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds nanosecond() const noexcept
157 {
158 return std::chrono::nanoseconds(static_cast<unsigned>(sqlValue.fraction));
159 }
160
161 // NOLINTEND(readability-identifier-naming)
162
163 /// Converts this SqlDateTime to its native time_point representation.
164 LIGHTWEIGHT_FORCE_INLINE constexpr operator native_type() const noexcept
165 {
166 return value();
167 }
168
169 /// @brief Constructs only a date from a SQL date-time structure.
170 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE SqlDate Date() const noexcept
171 {
172 return SqlDate { year(), month(), day() };
173 }
174
175 /// @brief Constructs only a time from a SQL date-time structure.
176 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE SqlTime Time() const noexcept
177 {
178 return SqlTime { std::chrono::hours { hour() },
179 std::chrono::minutes { minute() },
180 std::chrono::seconds { second() },
181 std::chrono::microseconds { std::chrono::duration_cast<std::chrono::microseconds>(nanosecond()) } };
182 }
183
184 /// Converts a native time_point to the underlying SQL timestamp structure.
185 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(native_type value) noexcept
186 {
187 using namespace std::chrono;
188 auto const totalDays = floor<days>(value);
189 auto const ymd = year_month_day { totalDays };
190 auto const hms =
191 hh_mm_ss<duration_type> { std::chrono::duration_cast<duration_type>(floor<nanoseconds>(value - totalDays)) };
192 return ConvertToSqlValue(ymd, hms);
193 }
194
195 /// Converts year_month_day and hh_mm_ss components to the underlying SQL timestamp structure.
196 static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(
197 std::chrono::year_month_day ymd, std::chrono::hh_mm_ss<duration_type> hms) noexcept
198 {
199 // clang-format off
200 // NB: The fraction field is in 100ns units.
201 return SQL_TIMESTAMP_STRUCT {
202 .year = (SQLSMALLINT) (int) ymd.year(),
203 .month = (SQLUSMALLINT) (unsigned) ymd.month(),
204 .day = (SQLUSMALLINT) (unsigned) ymd.day(),
205 .hour = (SQLUSMALLINT) hms.hours().count(),
206 .minute = (SQLUSMALLINT) hms.minutes().count(),
207 .second = (SQLUSMALLINT) hms.seconds().count(),
208 .fraction = (SQLUINTEGER) (((static_cast<unsigned long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(hms.to_duration()).count()) % 1'000'000'000LLU) / 100) * 100)
209 };
210 // clang-format on
211 }
212
213 /// Converts a SQL timestamp structure to the native time_point representation.
214 static LIGHTWEIGHT_FORCE_INLINE native_type constexpr ConvertToNative(SQL_TIMESTAMP_STRUCT const& time) noexcept
215 {
216 // clang-format off
217 using namespace std::chrono;
218 auto const ymd = year_month_day { std::chrono::year { time.year } / std::chrono::month { time.month } / std::chrono::day { time.day } };
219 auto const hms = hh_mm_ss<duration_type> {
220 duration_cast<duration_type>(
221 hours { time.hour }
222 + minutes { time.minute }
223 + seconds { time.second }
224 + nanoseconds { time.fraction }
225 )
226 };
227 return sys_days { ymd } + hms.to_duration();
228 // clang-format on
229 }
230
231 /// Returns the current date and time.
232 [[nodiscard]] constexpr LIGHTWEIGHT_FORCE_INLINE native_type value() const noexcept
233 {
235 }
236
237 /// Adds a duration to this date-time.
238 LIGHTWEIGHT_FORCE_INLINE SqlDateTime& operator+=(duration_type duration) noexcept
239 {
240 *this = SqlDateTime { value() + duration };
241 return *this;
242 }
243
244 /// Subtracts a duration from this date-time.
245 LIGHTWEIGHT_FORCE_INLINE SqlDateTime& operator-=(duration_type duration) noexcept
246 {
247 *this = SqlDateTime { value() - duration };
248 return *this;
249 }
250
251 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator+(SqlDateTime dateTime, duration_type duration) noexcept
252 {
253 return SqlDateTime { dateTime.value() + duration };
254 }
255
256 friend LIGHTWEIGHT_FORCE_INLINE SqlDateTime operator-(SqlDateTime dateTime, duration_type duration) noexcept
257 {
258 return SqlDateTime { dateTime.value() - duration };
259 }
260
261 /// Holds the underlying SQL timestamp structure.
262 SQL_TIMESTAMP_STRUCT sqlValue {};
263};
264
265} // namespace Lightweight
266
267template <>
268struct std::formatter<Lightweight::SqlDateTime>: std::formatter<std::string>
269{
270 LIGHTWEIGHT_FORCE_INLINE auto format(Lightweight::SqlDateTime const& value, std::format_context& ctx) const
271 -> std::format_context::iterator
272 {
273 // This can be used manually to format the date and time inside sql query builder,
274 // that is why we need to use iso standard 8601, also millisecond precision is used for the formatting
275 //
276 // internally fraction is stored in nanoseconds, here we need to get it in milliseconds
277 auto const milliseconds = value.sqlValue.fraction / 1'000'000;
278 return std::formatter<std::string>::format(std::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}",
279 value.sqlValue.year,
280 value.sqlValue.month,
281 value.sqlValue.day,
282 value.sqlValue.hour,
283 value.sqlValue.minute,
284 value.sqlValue.second,
285 milliseconds),
286 ctx);
287 }
288};
289
290template <>
291struct std::formatter<std::optional<Lightweight::SqlDateTime>>: std::formatter<std::string>
292{
293 LIGHTWEIGHT_FORCE_INLINE auto format(std::optional<Lightweight::SqlDateTime> const& value,
294 std::format_context& ctx) const -> std::format_context::iterator
295 {
296 if (!value.has_value())
297 return std::formatter<std::string>::format("nullopt", ctx);
298 return std::formatter<std::string>::format(std::format("{}", value.value()), ctx);
299 }
300};
301
302namespace Lightweight
303{
304
305template <>
306struct SqlDataBinder<SqlDateTime::native_type>
307{
308 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
309 SQLUSMALLINT column,
311 SQLLEN* indicator,
312 SqlDataBinderCallback const& /*cb*/) noexcept
313 {
314 SQL_TIMESTAMP_STRUCT sqlValue {};
315 auto const rc = SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &sqlValue, sizeof(sqlValue), indicator);
316 if (SQL_SUCCEEDED(rc))
317 *result = SqlDateTime::ConvertToNative(sqlValue);
318 return rc;
319 }
320};
321
322template <>
323struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime>
324{
325 static constexpr auto ColumnType = SqlColumnTypeDefinitions::DateTime {};
326
327 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
328 SQLUSMALLINT column,
329 SqlDateTime const& value,
330 [[maybe_unused]] SqlDataBinderCallback const& cb) noexcept
331 {
332#if defined(_WIN32) || defined(_WIN64)
333 // Microsoft Windows also chips with SQLSRV32.DLL, which is legacy, but seems to be used sometimes.
334 // See: https://learn.microsoft.com/en-us/sql/connect/connect-history
335 using namespace std::string_view_literals;
336 if (cb.ServerType() == SqlServerType::MICROSOFT_SQL && cb.DriverName() == "SQLSRV32.DLL"sv)
337 {
338 struct
339 {
340 SQLSMALLINT sqlType { SQL_TYPE_TIMESTAMP };
341 SQLULEN paramSize { 23 };
342 SQLSMALLINT decimalDigits { 3 };
343 SQLSMALLINT nullable {};
344 } hints;
345 auto const sqlDescribeParamResult =
346 SQLDescribeParam(stmt, column, &hints.sqlType, &hints.paramSize, &hints.decimalDigits, &hints.nullable);
347 if (SQL_SUCCEEDED(sqlDescribeParamResult))
348 {
349 return SQLBindParameter(stmt,
350 column,
351 SQL_PARAM_INPUT,
352 SQL_C_TIMESTAMP,
353 hints.sqlType,
354 hints.paramSize,
355 hints.decimalDigits,
356 (SQLPOINTER) &value.sqlValue,
357 sizeof(value),
358 nullptr);
359 }
360 }
361#endif
362
363 return SQLBindParameter(stmt,
364 column,
365 SQL_PARAM_INPUT,
366 SQL_C_TIMESTAMP,
367 SQL_TYPE_TIMESTAMP,
368 27,
369 7,
370 (SQLPOINTER) &value.sqlValue,
371 sizeof(value),
372 nullptr);
373 }
374
375 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
376 SQLHSTMT stmt, SQLUSMALLINT column, SqlDateTime* result, SQLLEN* indicator, SqlDataBinderCallback& /*cb*/) noexcept
377 {
378 // TODO: handle indicator to check for NULL values
379 *indicator = sizeof(result->sqlValue);
380 return SQLBindCol(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue, 0, indicator);
381 }
382
383 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
384 SQLUSMALLINT column,
385 SqlDateTime* result,
386 SQLLEN* indicator,
387 SqlDataBinderCallback const& /*cb*/) noexcept
388 {
389 return SQLGetData(stmt, column, SQL_C_TYPE_TIMESTAMP, &result->sqlValue, sizeof(result->sqlValue), indicator);
390 }
391
392 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlDateTime const& value) noexcept
393 {
394 return std::format("{}", value);
395 }
396};
397
398} // namespace Lightweight
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::year year() const noexcept
Returns the year of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::day day() const noexcept
Returns the day of this date-time object.
static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(std::chrono::year_month_day ymd, std::chrono::hh_mm_ss< duration_type > hms) noexcept
Converts year_month_day and hh_mm_ss components to the underlying SQL timestamp structure.
static LIGHTWEIGHT_FORCE_INLINE SqlDateTime Now() noexcept
Returns the current date and time.
constexpr bool operator==(SqlDateTime const &other) const noexcept
Equality comparison operator.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::system_clock::time_point value) noexcept
Constructs a date and time from a time point.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlDate Date() const noexcept
Constructs only a date from a SQL date-time structure.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::year year, std::chrono::month month, std::chrono::day day, std::chrono::hours hour, std::chrono::minutes minute, std::chrono::seconds second, std::chrono::nanoseconds nanosecond=std::chrono::nanoseconds(0)) noexcept
Constructs a date and time from individual components.
std::chrono::system_clock::time_point native_type
The native C++ type representing a date-time value.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::month month() const noexcept
Returns the month of this date-time object.
SQL_TIMESTAMP_STRUCT sqlValue
Holds the underlying SQL timestamp structure.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::seconds second() const noexcept
Returns the second of this date-time object.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::hours hour() const noexcept
Returns the hour of this date-time object.
std::chrono::system_clock::duration duration_type
The duration type used for arithmetic operations.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::minutes minute() const noexcept
Returns the minute of this date-time object.
LIGHTWEIGHT_FORCE_INLINE SqlDateTime & operator-=(duration_type duration) noexcept
Subtracts a duration from this date-time.
constexpr LIGHTWEIGHT_FORCE_INLINE std::chrono::nanoseconds nanosecond() const noexcept
Returns the nanosecond of this date-time object.
constexpr SqlDateTime() noexcept=default
Default constructor.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDateTime(std::chrono::year_month_day ymd, std::chrono::hh_mm_ss< duration_type > time) noexcept
Constructs a date and time from individual components.
static LIGHTWEIGHT_FORCE_INLINE SqlDateTime NowUTC() noexcept
Return the current date and time in UTC.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlTime Time() const noexcept
Constructs only a time from a SQL date-time structure.
static LIGHTWEIGHT_FORCE_INLINE SQL_TIMESTAMP_STRUCT constexpr ConvertToSqlValue(native_type value) noexcept
Converts a native time_point to the underlying SQL timestamp structure.
LIGHTWEIGHT_FORCE_INLINE SqlDateTime & operator+=(duration_type duration) noexcept
Adds a duration to this date-time.
constexpr LIGHTWEIGHT_FORCE_INLINE native_type value() const noexcept
Returns the current date and time.
constexpr bool operator!=(SqlDateTime const &other) const noexcept
Inequality comparison operator.
static LIGHTWEIGHT_FORCE_INLINE native_type constexpr ConvertToNative(SQL_TIMESTAMP_STRUCT const &time) noexcept
Converts a SQL timestamp structure to the native time_point representation.