diff --git a/src/server/database/Database/PreparedStatement.cpp b/src/server/database/Database/PreparedStatement.cpp index 8ea8f0f5e..ed6dc99ac 100644 --- a/src/server/database/Database/PreparedStatement.cpp +++ b/src/server/database/Database/PreparedStatement.cpp @@ -29,6 +29,52 @@ m_index(index), statement_data(capacity) { } PreparedStatementBase::~PreparedStatementBase() { } //- Bind to buffer +template +Acore::Types::is_non_string_view_v PreparedStatementBase::SetValidData(const uint8 index, T const& value) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace(value); +} + +template<> +void PreparedStatementBase::SetValidData(const uint8 index, std::string const& value) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace(value); +} + +template<> +void PreparedStatementBase::SetValidData(const uint8 index, std::vector const& value) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace>(value); +} + +// Non template functions +void PreparedStatementBase::SetValidData(const uint8 index) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace(nullptr); +} + +void PreparedStatementBase::SetValidData(const uint8 index, std::string_view value) +{ + ASSERT(index < statement_data.size()); + statement_data[index].data.emplace(value); +} + +template void PreparedStatementBase::SetValidData(const uint8 index, uint8 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, int8 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, uint16 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, int16 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, uint32 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, int32 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, uint64 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, int64 const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, bool const& value); +template void PreparedStatementBase::SetValidData(const uint8 index, float const& value); + +// Old api void PreparedStatementBase::setBool(const uint8 index, const bool value) { ASSERT(index < statement_data.size()); diff --git a/src/server/database/Database/PreparedStatement.h b/src/server/database/Database/PreparedStatement.h index a213ddf7e..9053d00d0 100644 --- a/src/server/database/Database/PreparedStatement.h +++ b/src/server/database/Database/PreparedStatement.h @@ -19,11 +19,25 @@ #define _PREPAREDSTATEMENT_H #include "Define.h" +#include "Duration.h" #include "SQLOperation.h" #include +#include #include #include +namespace Acore::Types +{ + template + using is_default = std::enable_if_t || std::is_same_v, T>>; + + template + using is_enum_v = std::enable_if_t>; + + template + using is_non_string_view_v = std::enable_if_t>; +} + struct PreparedStatementData { std::variant< @@ -85,10 +99,77 @@ public: setBinary(index, vec); } + // Set numerlic and default binary + template + inline Acore::Types::is_default SetData(const uint8 index, T value) + { + SetValidData(index, value); + } + + // Set enums + template + inline Acore::Types::is_enum_v SetData(const uint8 index, T value) + { + SetValidData(index, std::underlying_type_t(value)); + } + + // Set string_view + inline void SetData(const uint8 index, std::string_view value) + { + SetValidData(index, value); + } + + // Set nullptr + inline void SetData(const uint8 index, std::nullptr_t = nullptr) + { + SetValidData(index); + } + + // Set non default binary + template + inline void SetData(const uint8 index, std::array const& value) + { + std::vector vec(value.begin(), value.end()); + SetValidData(index, vec); + } + + // Set duration + template + inline void SetData(const uint8 index, std::chrono::duration<_Rep, _Period> const& value, bool convertToUin32 = true) + { + SetValidData(index, convertToUin32 ? static_cast(value.count()) : value.count()); + } + + // Set all + template + inline void SetArguments(Args&&... args) + { + SetDataTuple(std::make_tuple(std::forward(args)...)); + } + [[nodiscard]] uint32 GetIndex() const { return m_index; } [[nodiscard]] std::vector const& GetParameters() const { return statement_data; } protected: + template + Acore::Types::is_non_string_view_v SetValidData(const uint8 index, T const& value); + + void SetValidData(const uint8 index); + void SetValidData(const uint8 index, std::string_view value); + + template + inline void SetDataTuple(std::tuple const& argsList) + { + std::apply + ( + [this](Ts const&... arguments) + { + uint8 index{ 0 }; + ((SetData(index, arguments), index++), ...); + }, argsList + ); + } + uint32 m_index; //- Buffer of parameters, not tied to MySQL in any way yet