diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-04-10 13:59:19 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2024-04-10 13:59:19 +0200 |
commit | 404bb5b3c21b445ae21fbbfcfd7f51d255e07c39 (patch) | |
tree | 59ba0cf7ec61bdf51334c8fbf7def18451ee6dbc /src/server/database/Database/MySQLPreparedStatement.cpp | |
parent | 16853af7c84b1204357333db399100604ab6cb29 (diff) |
Core/DBLayer: Support retrieving DATE/DATETIME/TIMESTAMP column values directly without casting in sql
Diffstat (limited to 'src/server/database/Database/MySQLPreparedStatement.cpp')
-rw-r--r-- | src/server/database/Database/MySQLPreparedStatement.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/server/database/Database/MySQLPreparedStatement.cpp b/src/server/database/Database/MySQLPreparedStatement.cpp index dc25264e85c..0fc4de403f8 100644 --- a/src/server/database/Database/MySQLPreparedStatement.cpp +++ b/src/server/database/Database/MySQLPreparedStatement.cpp @@ -20,6 +20,7 @@ #include "Log.h" #include "MySQLHacks.h" #include "PreparedStatement.h" +#include <chrono> #include <cstring> template<typename T> @@ -145,6 +146,33 @@ void MySQLPreparedStatement::SetParameter(uint8 index, T value) memcpy(param->buffer, &value, len); } +void MySQLPreparedStatement::SetParameter(uint8 index, SystemTimePoint value) +{ + AssertValidIndex(index); + m_paramsSet[index] = true; + MYSQL_BIND* param = &m_bind[index]; + uint32 len = sizeof(MYSQL_TIME); + param->buffer_type = MYSQL_TYPE_DATETIME; + delete[] static_cast<char*>(param->buffer); + param->buffer = new char[len]; + param->buffer_length = len; + param->is_null_value = 0; + delete param->length; + param->length = new unsigned long(len); + + std::chrono::year_month_day ymd(time_point_cast<std::chrono::days>(value)); + std::chrono::hh_mm_ss hms(duration_cast<std::chrono::microseconds>(value - std::chrono::sys_days(ymd))); + + MYSQL_TIME* time = reinterpret_cast<MYSQL_TIME*>(static_cast<char*>(param->buffer)); + time->year = static_cast<int32>(ymd.year()); + time->month = static_cast<uint32>(ymd.month()); + time->day = static_cast<uint32>(ymd.day()); + time->hour = hms.hours().count(); + time->minute = hms.minutes().count(); + time->second = hms.seconds().count(); + time->second_part = hms.subseconds().count(); +} + void MySQLPreparedStatement::SetParameter(uint8 index, std::string const& value) { AssertValidIndex(index); |