5#include "../SqlColumnTypeDefinitions.hpp"
12#if !defined(SQL_SS_TIME2)
18#define SQL_SS_TIME2 (-154)
20struct SQL_SS_TIME2_STRUCT
29 sizeof(SQL_SS_TIME2_STRUCT) == 12,
30 "SQL_SS_TIME2_STRUCT size must be padded 12 bytes, as per ODBC extension spec."
44 using native_type = std::chrono::hh_mm_ss<std::chrono::microseconds>;
46#if defined(SQL_SS_TIME2)
47 using sql_type = SQL_SS_TIME2_STRUCT;
49 using sql_type = SQL_TIME_STRUCT;
54 constexpr SqlTime()
noexcept =
default;
59 constexpr ~SqlTime()
noexcept =
default;
61 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr native_type value()
const noexcept
63 return ConvertToNative(sqlValue);
66 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(
SqlTime const& other)
const noexcept
68 return value().to_duration().count() == other.value().to_duration().count();
71 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(
SqlTime const& other)
const noexcept
73 return !(*
this == other);
76 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlTime(native_type value)
noexcept:
77 sqlValue { SqlTime::ConvertToSqlValue(value) }
81 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlTime(std::chrono::hours hour,
82 std::chrono::minutes minute,
83 std::chrono::seconds second,
84 std::chrono::microseconds micros = {})
noexcept:
85 SqlTime(native_type { hour + minute + second + micros })
89 static LIGHTWEIGHT_FORCE_INLINE
constexpr sql_type ConvertToSqlValue(native_type value)
noexcept
92 .hour = (SQLUSMALLINT) value.hours().count(),
93 .minute = (SQLUSMALLINT) value.minutes().count(),
94 .second = (SQLUSMALLINT) value.seconds().count(),
95#if defined(SQL_SS_TIME2)
96 .fraction = (SQLUINTEGER) value.subseconds().count(),
101 static LIGHTWEIGHT_FORCE_INLINE
constexpr native_type ConvertToNative(sql_type
const& value)
noexcept
104 return native_type { std::chrono::hours { (int) value.hour }
105 + std::chrono::minutes { (unsigned) value.minute }
106 + std::chrono::seconds { (unsigned) value.second }
107#if defined(SQL_SS_TIME2)
108 + std::chrono::microseconds { value.fraction }
119 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Time {};
121 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
123 SqlTime
const& value,
124 SqlDataBinderCallback& )
noexcept
126 return SQLBindParameter(
127 stmt, column, SQL_PARAM_INPUT, SQL_C_TYPE_TIME, SQL_TYPE_TIME, 0, 0, (SQLPOINTER) &value.sqlValue, 0,
nullptr);
130 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
131 SQLHSTMT stmt, SQLUSMALLINT column, SqlTime* result, SQLLEN* indicator, SqlDataBinderCallback& )
noexcept
133 return SQLBindCol(stmt, column, SQL_C_TYPE_TIME, &result->sqlValue,
sizeof(result->sqlValue), indicator);
136 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(
137 SQLHSTMT stmt, SQLUSMALLINT column, SqlTime* result, SQLLEN* indicator, SqlDataBinderCallback
const& )
noexcept
139 return SQLGetData(stmt, column, SQL_C_TYPE_TIME, &result->sqlValue,
sizeof(result->sqlValue), indicator);
142 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlTime
const& value)
noexcept
144 return std::format(
"{:02}:{:02}:{:02}.{:06}",
146 value.sqlValue.minute,
147 value.sqlValue.second,
148 value.sqlValue.fraction);
155struct std::formatter<Lightweight::SqlTime>: std::formatter<std::string>
157 auto format(
Lightweight::SqlTime const& value, std::format_context& ctx)
const -> std::format_context::iterator
159 return std::formatter<std::string>::format(std::format(
"{:02}:{:02}:{:02}:{:06}",
161 value.sqlValue.minute,
162 value.sqlValue.second,
163 value.sqlValue.fraction),