added date_type_t, time_type_t and timestamp, ensure allocated postgres resources are released, split repository into basic_repository and repository.

This commit is contained in:
2026-01-06 15:43:20 +01:00
parent f07a360da2
commit 0c6b5a0a93
60 changed files with 1132 additions and 1225 deletions
+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 write_value(size_t pos, const bool &x) = 0;
virtual void write_value(size_t pos, const float &x) = 0;
virtual void write_value(size_t pos, const double &x) = 0;
virtual void write_value(size_t pos, const time &x) = 0;
virtual void write_value(size_t pos, const date &x) = 0;
virtual void write_value(size_t pos, const date_type_t &x) = 0;
virtual void write_value(size_t pos, const time_type_t &x) = 0;
virtual void write_value(size_t pos, const timestamp &x) = 0;
virtual void write_value(size_t pos, const char *x) = 0;
virtual void write_value(size_t pos, const char *x, size_t size) = 0;
virtual void write_value(size_t pos, const std::string &x) = 0;
@@ -6,11 +6,6 @@
#include <cstdint>
#include <string>
// namespace matador {
//class date;
//class time;
//}
namespace matador::utils {
template < typename Type >
@@ -36,8 +31,9 @@ public:
void operator()(double &x) { this->convert(x); }
void operator()(const char *x) { this->convert(x); }
void operator()(std::string &x) { this->convert(x); }
// void operator()(date &x) { this->convert(result, x); }
// void operator()(time &x) { this->convert(result, x); }
void operator()(date_type_t &x) { this->convert(x); }
void operator()(time_type_t &x) { this->convert(x); }
void operator()(timestamp &x) { this->convert(x); }
void operator()(blob &x) { this->convert(x); }
private:
+1
View File
@@ -23,6 +23,7 @@ enum class basic_type : uint8_t {
Varchar, /*!< Data type varchar */
Text, /*!< Data type text */
Date, /*!< Data type date */
DateTime, /*!< Data type datetime */
Time, /*!< Data type time */
Blob, /*!< Data type blob */
Null, /*!< Data type null */
+120 -26
View File
@@ -3,39 +3,36 @@
#include "matador/utils/types.hpp"
#include "matador/utils/result.hpp"
//#include "matador/utils/date.hpp"
//#include "matador/utils/time.hpp"
//#include "matador/utils/placeholder.hpp"
#include <array>
#include <charconv>
#include <cstring>
#include <iomanip>
#include <string>
#include <type_traits>
/*
* Conversion matrix
* from> | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | float | double | string | date | time | blob | placeholder | null
* to | | | | | | | | | | | | | | | | |
* ------------+------+-------+-------+-------+-------+--------+--------+--------+------+-------+--------+--------+------+------+------+-------------+-----
* int8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* int16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* int32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* int64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* uint8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* uint16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* uint32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* uint64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
* bool | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
* float | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
* double | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
* string | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A
* date | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
* time | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
* blob | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A
* placeholder | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok | N/A
* null | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok
* from> | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | float | double | string | date | time | timestamp | blob | null
* to | | | | | | | | | | | | | | | | |
* ------------+------+-------+-------+-------+-------+--------+--------+--------+------+-------+--------+--------+------+------+-----------+------+-----
* int8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* int16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* int32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* int64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* uint8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* uint16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* uint32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* uint64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
* bool | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
* float | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
* double | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
* string | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A
* date | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
* time | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
* timestamp | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
* blob | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A
* null | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok | ok
*
* from integral to date/time works, value is interpreted as std::chrono::timepoint
* from integral to blob works, bytes of integral data will converted to blob
@@ -97,7 +94,6 @@ result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t
return ok(std::string(buffer.data(), ptr));
}
// return failure({ec, "couldn't convert value to std::string"});
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
}
@@ -110,7 +106,6 @@ result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t
return ok(std::string(buffer.data(), ptr));
}
// return failure({ec, "couldn't convert value to std::string"});
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
}
@@ -257,6 +252,105 @@ result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_
return failure(conversion_error::NotConvertable);
}
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);
}
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);
}
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);
}
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);
}
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<time_type_t, SourceType>>* = 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<DestType> && !std::is_same_v<bool, DestType> && std::is_same_v<timestamp, SourceType>>* = 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_same_v<bool, DestType> && std::is_same_v<date_type_t, SourceType>>* = 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_same_v<bool, DestType> && std::is_same_v<time_type_t, SourceType>>* = 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_same_v<bool, DestType> && std::is_same_v<timestamp, SourceType>>* = 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<DestType> && std::is_same_v<date_type_t, SourceType>>* = 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<DestType> && std::is_same_v<time_type_t, SourceType>>* = 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<DestType> && std::is_same_v<timestamp, SourceType>>* = 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_same_v<blob, DestType> && std::is_same_v<date_type_t, SourceType>>* = 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_same_v<blob, DestType> && std::is_same_v<time_type_t, SourceType>>* = 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_same_v<blob, DestType> && std::is_same_v<timestamp, SourceType>>* = 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_same_v<std::string, DestType> && std::is_same_v<date_type_t, SourceType>>* = 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_same_v<std::string, DestType> && std::is_same_v<time_type_t, SourceType>>* = 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_same_v<std::string, DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
return failure(conversion_error::NotConvertable);
}
}
#endif //MATADOR_CONVERT_HPP
+62 -8
View File
@@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include <cstdint>
#include <chrono>
namespace matador::utils {
@@ -12,21 +13,74 @@ enum class basic_type : uint8_t;
using byte = unsigned char;
using blob = std::vector<byte>;
using timestamp = std::chrono::system_clock::time_point;
struct date_type_t;
struct time_type_t;
using database_type = std::variant<
uint8_t, uint16_t, uint32_t, uint64_t,
int8_t, int16_t, int32_t, int64_t,
float, double,
bool,
const char*,
std::string,
blob,
nullptr_t>;
uint8_t, uint16_t, uint32_t, uint64_t,
int8_t, int16_t, int32_t, int64_t,
float, double,
bool,
const char*,
std::string,
blob,
timestamp,
date_type_t,
time_type_t,
nullptr_t>;
struct null_type_t {};
struct date_type_t {
int32_t year{};
uint8_t month{}; // 1-12
uint8_t day{};
};
struct time_type_t {
uint8_t hour{};
uint8_t minute{};
uint8_t second{};
uint32_t microsecond{};
};
void initialize_by_basic_type(basic_type type, database_type &val);
// Equality
constexpr bool operator==(const date_type_t& a, const date_type_t& b) noexcept {
return a.year == b.year &&
a.month == b.month &&
a.day == b.day;
}
constexpr bool operator!=(const date_type_t& a, const date_type_t& b) noexcept;
// Ordering
constexpr bool operator<(const date_type_t& a, const date_type_t& b) noexcept;
constexpr bool operator>(const date_type_t& a, const date_type_t& b) noexcept;
constexpr bool operator<=(const date_type_t& a, const date_type_t& b) noexcept;
constexpr bool operator>=(const date_type_t& a, const date_type_t& b) noexcept;
// Equality
constexpr bool operator==(const time_type_t &a, const time_type_t &b) noexcept {
return a.hour == b.hour &&
a.minute == b.minute &&
a.second == b.second &&
a.microsecond == b.microsecond;
}
constexpr bool operator!=(const time_type_t& a, const time_type_t& b) noexcept;
// Ordering
constexpr bool operator<(const time_type_t& a, const time_type_t& b) noexcept;
constexpr bool operator>(const time_type_t& a, const time_type_t& b) noexcept;
constexpr bool operator<=(const time_type_t& a, const time_type_t& b) noexcept;
constexpr bool operator>=(const time_type_t& a, const time_type_t& b) noexcept;
}
#endif //MATADOR_TYPES_HPP