From 404bb5b3c21b445ae21fbbfcfd7f51d255e07c39 Mon Sep 17 00:00:00 2001 From: Shauren Date: Wed, 10 Apr 2024 13:59:19 +0200 Subject: Core/DBLayer: Support retrieving DATE/DATETIME/TIMESTAMP column values directly without casting in sql --- .../database/Database/MySQLPreparedStatement.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/server/database/Database/MySQLPreparedStatement.cpp') 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 #include template @@ -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(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(value)); + std::chrono::hh_mm_ss hms(duration_cast(value - std::chrono::sys_days(ymd))); + + MYSQL_TIME* time = reinterpret_cast(static_cast(param->buffer)); + time->year = static_cast(ymd.year()); + time->month = static_cast(ymd.month()); + time->day = static_cast(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); -- cgit v1.2.3