Added documentation for the timestamps

This commit is contained in:
Dr. Patrick Urbanke
2025-05-11 14:18:41 +02:00
parent 0638490399
commit 34c91ad7fd
7 changed files with 153 additions and 0 deletions
+1
View File
@@ -13,6 +13,7 @@
#include "sqlgen/Range.hpp"
#include "sqlgen/Ref.hpp"
#include "sqlgen/Result.hpp"
#include "sqlgen/Timestamp.hpp"
#include "sqlgen/Varchar.hpp"
#include "sqlgen/col.hpp"
#include "sqlgen/delete_from.hpp"
+14
View File
@@ -0,0 +1,14 @@
#ifndef SQLGEN_TIMESTAMP_HPP_
#define SQLGEN_TIMESTAMP_HPP_
#include <rfl.hpp>
namespace sqlgen {
template <rfl::internal::StringLiteral _format>
using Timestamp = rfl::Timestamp<_format>;
}; // namespace sqlgen
#endif
+1
View File
@@ -7,6 +7,7 @@
#include "Parser_primary_key.hpp"
#include "Parser_shared_ptr.hpp"
#include "Parser_string.hpp"
#include "Parser_timestamp.hpp"
#include "Parser_unique_ptr.hpp"
#include "Parser_varchar.hpp"
@@ -0,0 +1,42 @@
#ifndef SQLGEN_PARSING_PARSER_TIMESTAMP_HPP_
#define SQLGEN_PARSING_PARSER_TIMESTAMP_HPP_
#include <string>
#include <type_traits>
#include "../Result.hpp"
#include "../Timestamp.hpp"
#include "../dynamic/Type.hpp"
#include "../dynamic/types.hpp"
#include "Parser_base.hpp"
namespace sqlgen::parsing {
template <rfl::internal::StringLiteral _format>
struct Parser<Timestamp<_format>> {
using TSType = Timestamp<_format>;
static Result<TSType> read(const std::optional<std::string>& _str) noexcept {
return Parser<std::string>::read(_str).and_then(
[](auto&& _s) -> Result<TSType> {
return TSType::from_string(std::move(_s));
});
}
static std::optional<std::string> write(const TSType& _t) noexcept {
return Parser<std::string>::write(_t.str());
}
static dynamic::Type to_type() noexcept {
const std::string format = typename TSType::Format().str();
if (format.find("%z") == std::string::npos) {
return dynamic::types::Timestamp{};
} else {
return dynamic::types::TimestampWithTZ{};
}
}
};
} // namespace sqlgen::parsing
#endif
+23
View File
@@ -0,0 +1,23 @@
#include <gtest/gtest.h>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
namespace test_timestamp_dry {
struct TestTable {
std::string field1;
int32_t field2;
sqlgen::PrimaryKey<uint32_t> id;
sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S"> ts;
};
TEST(postgres, test_timestamp_dry) {
const auto query = sqlgen::CreateTable<TestTable>{};
const auto expected =
R"(CREATE TABLE IF NOT EXISTS "TestTable" ("field1" TEXT NOT NULL, "field2" INTEGER NOT NULL, "id" INTEGER NOT NULL, "ts" TIMESTAMP NOT NULL, PRIMARY KEY ("id"));)";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}
} // namespace test_timestamp_dry
@@ -0,0 +1,23 @@
#include <gtest/gtest.h>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
namespace test_timestamp_with_tz_dry {
struct TestTable {
std::string field1;
int32_t field2;
sqlgen::PrimaryKey<uint32_t> id;
sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S%z"> ts;
};
TEST(postgres, test_timestamp_with_tz_dry) {
const auto query = sqlgen::CreateTable<TestTable>{};
const auto expected =
R"(CREATE TABLE IF NOT EXISTS "TestTable" ("field1" TEXT NOT NULL, "field2" INTEGER NOT NULL, "id" INTEGER NOT NULL, "ts" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));)";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}
} // namespace test_timestamp_with_tz_dry
+49
View File
@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/sqlite.hpp>
#include <vector>
namespace test_timestamp {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S"> birthdate;
};
TEST(sqlite, test_birthdate) {
const auto people1 =
std::vector<Person>({Person{.id = 0,
.first_name = "Homer",
.last_name = "Simpson",
.birthdate = "1989-12-17 12:00:00"},
Person{.id = 1,
.first_name = "Bart",
.last_name = "Simpson",
.birthdate = "1989-12-17 12:00:00"},
Person{.id = 2,
.first_name = "Lisa",
.last_name = "Simpson",
.birthdate = "1989-12-17 12:00:00"},
Person{.id = 3,
.first_name = "Maggie",
.last_name = "Simpson",
.birthdate = "1989-12-17 12:00:00"}});
const auto conn = sqlgen::sqlite::connect();
sqlgen::write(conn, people1);
const auto people2 = sqlgen::read<std::vector<Person>>(conn).value();
const auto json1 = rfl::json::write(people1);
const auto json2 = rfl::json::write(people2);
EXPECT_EQ(json1, json2);
}
} // namespace test_timestamp