added test for date and time, sql read and write functionality

This commit is contained in:
2026-01-06 22:23:50 +01:00
parent 0c6b5a0a93
commit c48bd4f9d6
17 changed files with 516 additions and 360 deletions
+3 -2
View File
@@ -69,7 +69,7 @@ public:
[[nodiscard]] std::string prepare_literal(const std::string &str) const;
/**
* Wrap identifier quotes around a sql identifier keyword
* Wrap identifier quotes around a SQL identifier keyword
*
* @param str Identifier to put quotes around
*/
@@ -259,7 +259,8 @@ private:
{utils::basic_type::Varchar, "VARCHAR"},
{utils::basic_type::Text, "TEXT"},
{utils::basic_type::Date, "DATE"},
{utils::basic_type::Time, "DATETIME"},
{utils::basic_type::Time, "TIME"},
{utils::basic_type::DateTime, "DATETIME"},
{utils::basic_type::Blob, "BLOB"},
{utils::basic_type::Null, "NULL"}
};
+3 -6
View File
@@ -5,10 +5,6 @@
#include <string>
namespace matador {
class date;
class time;
}
namespace matador::utils {
class value;
@@ -29,8 +25,9 @@ public:
virtual void read_value(const char *id, size_t index, bool &value) = 0;
virtual void read_value(const char *id, size_t index, float &value) = 0;
virtual void read_value(const char *id, size_t index, double &value) = 0;
virtual void read_value(const char *id, size_t index, time &value) = 0;
virtual void read_value(const char *id, size_t index, date &value) = 0;
virtual void read_value(const char *id, size_t index, time_type_t &value) = 0;
virtual void read_value(const char *id, size_t index, date_type_t &value) = 0;
virtual void read_value(const char *id, size_t index, timestamp &value) = 0;
virtual void read_value(const char *id, size_t index, char *value, size_t size) = 0;
virtual void read_value(const char *id, size_t index, std::string &value) = 0;
virtual void read_value(const char *id, size_t index, std::string &value, size_t size) = 0;
+134 -5
View File
@@ -256,26 +256,57 @@ template < typename DestType >
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, date_type_t>>* = nullptr) {
std::tm result{};
std::istringstream iss(source);
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
return ok(result);
iss >> std::get_time(&result, "%Y-%m-%d");
if (iss.fail()) {
return failure(conversion_error::NotConvertable);
}
return ok(date_type_t{result.tm_year + 1900, static_cast<uint8_t>(result.tm_mon + 1), static_cast<uint8_t>(result.tm_mday)});
}
template < typename DestType >
result<DestType, conversion_error> to(const char *source, std::enable_if_t<std::is_same_v<DestType, date_type_t>>* = nullptr) {
return to<DestType>(std::string{source});
}
template < typename DestType >
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, time_type_t>>* = nullptr) {
std::tm result{};
std::istringstream iss(source);
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
return ok(result);
iss >> std::get_time(&result, "%H:%M:%S");
if (iss.fail()) {
return failure(conversion_error::NotConvertable);
}
return ok(time_type_t{static_cast<uint8_t>(result.tm_hour), static_cast<uint8_t>(result.tm_min), static_cast<uint8_t>(result.tm_sec)});
}
template < typename DestType >
result<DestType, conversion_error> to(const char *source, std::enable_if_t<std::is_same_v<DestType, time_type_t>>* = nullptr) {
return to<DestType>(std::string{source});
}
template < typename DestType >
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, timestamp>>* = nullptr) {
std::tm result{};
std::istringstream iss(source);
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
return ok(result);
if (iss.fail()) {
return failure(conversion_error::NotConvertable);
}
const std::time_t tt = std::mktime(&result);
if (tt == -1) {
return failure(conversion_error::NotConvertable);
}
return ok(std::chrono::system_clock::from_time_t(tt));
}
template < typename DestType >
result<DestType, conversion_error> to(const char *source, std::enable_if_t<std::is_same_v<DestType, timestamp>>* = nullptr) {
return to<DestType>(std::string{source});
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<DestType> && !std::is_same_v<bool, DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
@@ -351,6 +382,104 @@ result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<SourceType> && !std::is_same_v<bool, SourceType> && std::is_same_v<date_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<SourceType> && !std::is_same_v<bool, SourceType> && std::is_same_v<time_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<SourceType> && !std::is_same_v<bool, SourceType> && std::is_same_v<timestamp, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<SourceType> && std::is_same_v<date_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<SourceType> && std::is_same_v<time_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType, typename SourceType >
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<SourceType> && std::is_same_v<timestamp, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const blob &/*source*/, std::enable_if_t<std::is_same_v<date_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const blob &/*source*/, std::enable_if_t<std::is_same_v<time_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const blob &/*source*/, std::enable_if_t<std::is_same_v<timestamp, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const bool &/*source*/, std::enable_if_t<std::is_same_v<date_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const bool &/*source*/, std::enable_if_t<std::is_same_v<time_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const bool &/*source*/, std::enable_if_t<std::is_same_v<timestamp, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const time_type_t &/*source*/, std::enable_if_t<std::is_same_v<date_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const time_type_t &source, std::enable_if_t<std::is_same_v<time_type_t, DestType>>* = nullptr) {
return ok(source);
}
template < typename DestType>
result<DestType, conversion_error> to(const date_type_t &/*source*/, std::enable_if_t<std::is_same_v<time_type_t, DestType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
template < typename DestType>
result<DestType, conversion_error> to(const date_type_t &source, std::enable_if_t<std::is_same_v<date_type_t, DestType>>* = nullptr) {
return ok(source);
}
template < typename DestType>
result<DestType, conversion_error> to(const timestamp &source, std::enable_if_t<std::is_same_v<time_type_t, DestType>>* = nullptr) {
const std::time_t tt = std::chrono::system_clock::to_time_t(source);
const std::tm* result = std::localtime(&tt);
return ok(time_type_t{static_cast<uint8_t>(result->tm_hour), static_cast<uint8_t>(result->tm_min), static_cast<uint8_t>(result->tm_sec)});
}
template < typename DestType>
result<DestType, conversion_error> to(const timestamp &source, std::enable_if_t<std::is_same_v<date_type_t, DestType>>* = nullptr) {
const std::time_t tt = std::chrono::system_clock::to_time_t(source);
const std::tm* result = std::localtime(&tt);
return ok(date_type_t{result->tm_year + 1900, static_cast<uint8_t>(result->tm_mon + 1), static_cast<uint8_t>(result->tm_mday)});
}
}
#endif //MATADOR_CONVERT_HPP
+99 -96
View File
@@ -8,159 +8,162 @@
#include <string>
namespace matador::utils {
/// @cond MATADOR_DEV
template <> struct data_type_traits<nullptr_t, void>
{
template<>
struct data_type_traits<nullptr_t, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::Null; }
static void read_value(attribute_reader &reader, const char *id, size_t index, nullptr_t &/*value*/, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, nullptr_t &/*value*/, size_t /*size*/ = 0);
static void read_value(attribute_reader &reader, const char *id, size_t index, nullptr_t &/*value*/,
size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, nullptr_t &/*value*/, size_t /*size*/ = 0);
};
template <> struct data_type_traits<int8_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int8; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int8_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int8_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<int8_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int8; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int8_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int8_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<int16_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int16; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int16_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int16_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<int16_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int16; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int16_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int16_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<int32_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int32; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int32_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int32_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<int32_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int32; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int32_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int32_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<int64_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int64_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int64_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<int64_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, int64_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const int64_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<uint8_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt8; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint8_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint8_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<uint8_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt8; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint8_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint8_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<uint16_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt16; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint16_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint16_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<uint16_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt16; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint16_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint16_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<uint32_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt32; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint32_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint32_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<uint32_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt32; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint32_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint32_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<uint64_t, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint64_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint64_t &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<uint64_t, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::UInt64; }
static void read_value(attribute_reader &reader, const char *id, size_t index, uint64_t &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const uint64_t &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<bool, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Boolean; }
static void read_value(attribute_reader &reader, const char *id, size_t index, bool &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const bool &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<bool, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Boolean; }
static void read_value(attribute_reader &reader, const char *id, size_t index, bool &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const bool &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<float, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Float; }
static void read_value(attribute_reader &reader, const char *id, size_t index, float &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const float &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<float, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Float; }
static void read_value(attribute_reader &reader, const char *id, size_t index, float &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const float &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<double, void>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Double; }
static void read_value(attribute_reader &reader, const char *id, size_t index, double &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const double &value, size_t /*size*/ = 0);
template<>
struct data_type_traits<double, void> {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Double; }
static void read_value(attribute_reader &reader, const char *id, size_t index, double &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, const double &value, size_t /*size*/ = 0);
};
template <> struct data_type_traits<const char*, void>
{
template<>
struct data_type_traits<const char *, void> {
static basic_type type(const std::size_t size) { return size == 0 ? basic_type::Text : basic_type::Varchar; }
static void read_value(attribute_reader &reader, const char *id, size_t index, const char* value, size_t size);
static void read_value(attribute_reader &reader, const char *id, size_t index, const char *value, size_t size);
static void bind_value(attribute_writer &binder, size_t index, const char *value, size_t size = 0);
};
template <> struct data_type_traits<char*, void>
{
template<>
struct data_type_traits<char *, void> {
static basic_type type(const std::size_t size) { return size == 0 ? basic_type::Text : basic_type::Varchar; }
static void read_value(attribute_reader &reader, const char *id, size_t index, char *value, size_t size);
static void bind_value(attribute_writer &binder, size_t index, const char *value, size_t size = 0);
};
template <> struct data_type_traits<char[], void>
{
template<>
struct data_type_traits<char[], void> {
static basic_type type(const std::size_t size) { return size == 0 ? basic_type::Text : basic_type::Varchar; }
template < int N >
template<int N>
static void read_value(attribute_reader &reader, const char *id, const size_t index, char (&value)[N], const size_t size) {
data_type_traits<const char*>::read_value(reader, id, index, value, size);
data_type_traits<const char *>::read_value(reader, id, index, value, size);
}
template < int N >
template<int N>
static void bind_value(attribute_writer &binder, const size_t index, char *value, const size_t size = 0) {
data_type_traits<const char*>::bind_value(binder, index, value, size);
data_type_traits<const char *>::bind_value(binder, index, value, size);
}
};
template <> struct data_type_traits<std::string, void>
{
template<>
struct data_type_traits<std::string, void> {
static basic_type type(const std::size_t size) { return size == 0 ? basic_type::Text : basic_type::Varchar; }
static void read_value(attribute_reader &reader, const char *id, size_t index, std::string &value, size_t size);
static void bind_value(attribute_writer &binder, size_t index, std::string &value, size_t size = 0);
};
template <> struct data_type_traits<utils::blob, void>
{
template<>
struct data_type_traits<utils::blob, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::Blob; }
static void read_value(attribute_reader &reader, const char *id, size_t index, utils::blob &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, utils::blob &value, size_t /*size*/ = 0);
static void read_value(attribute_reader &reader, const char *id, size_t index, utils::blob &value, size_t /*size*/ = 0);
static void bind_value(attribute_writer &binder, size_t index, utils::blob &value, size_t /*size*/ = 0);
};
//template <> struct data_type_traits<matador::date, void>
//{
// static basic_type type(std::size_t /*size*/) { return basic_type::type_date; }
// static void read_value(attribute_reader &reader, const char *id, size_t index, matador::date &value);
// static void bind_value(attribute_writer &binder, size_t index, matador::date &value);
//};
//
//template <> struct data_type_traits<matador::time, void>
//{
// static basic_type type(std::size_t /*size*/) { return basic_type::type_time; }
// static void read_value(attribute_reader &reader, const char *id, size_t index, matador::time &value);
// static void bind_value(attribute_writer &binder, size_t index, matador::time &value);
//};
template<>
struct data_type_traits<date_type_t, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::Date; }
static void read_value(attribute_reader &reader, const char *id, size_t index, date_type_t &value);
static void bind_value(attribute_writer &binder, size_t index, date_type_t &value);
};
template<>
struct data_type_traits<time_type_t, void> {
static basic_type type(std::size_t /*size*/) { return basic_type::Time; }
static void read_value(attribute_reader &reader, const char *id, size_t index, time_type_t &value);
static void bind_value(attribute_writer &binder, size_t index, time_type_t &value);
};
template<typename EnumType>
struct data_type_traits<EnumType, std::enable_if_t<std::is_enum_v<EnumType> > > {
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int32; }
template < typename EnumType >
struct data_type_traits<EnumType, std::enable_if_t<std::is_enum_v<EnumType>>>
{
static basic_type type(std::size_t /*size*/ = 0) { return basic_type::Int32; }
static void read_value(attribute_reader &reader, const char *id, const size_t index, EnumType &value, const size_t size = 0) {
data_type_traits<int>::read_value(reader, id, index, reinterpret_cast<int&>(value), size);
data_type_traits<int>::read_value(reader, id, index, reinterpret_cast<int &>(value), size);
}
static void bind_value(attribute_writer &binder, const size_t index, EnumType &value, const size_t size = 0) {
data_type_traits<int>::bind_value(binder, index, static_cast<int &>(value), size);
}
};
/// @endcond
/// @endcond
}
#endif //MATADOR_DEFAULT_TYPE_TRAITS_HPP
+2
View File
@@ -19,6 +19,8 @@ namespace matador::utils {
* @return Binary data as string
*/
MATADOR_UTILS_API std::string to_string(const blob &data);
MATADOR_UTILS_API std::string to_string(const date_type_t &data);
MATADOR_UTILS_API std::string to_string(const time_type_t &data);
/**
* Splits a string by a delimiter and