mirror of
https://github.com/rbock/sqlpp11.git
synced 2026-05-04 09:59:10 -05:00
Add support for TIME columns in postgresql
This commit is contained in:
committed by
Roland Bock
parent
b2166c636b
commit
f7f2060c44
@@ -31,7 +31,7 @@
|
||||
#include <sqlpp11/postgresql/postgresql.h>
|
||||
#include <sqlpp11/sqlpp11.h>
|
||||
|
||||
#include "TabFoo.h"
|
||||
#include "TabDateTime.h"
|
||||
#include "make_test_connection.h"
|
||||
|
||||
namespace
|
||||
@@ -39,6 +39,7 @@ namespace
|
||||
const auto now = ::date::floor<::std::chrono::microseconds>(std::chrono::system_clock::now());
|
||||
const auto today = ::date::floor<::sqlpp::chrono::days>(now);
|
||||
const auto yesterday = today - ::sqlpp::chrono::days{1};
|
||||
const auto current = now - today;
|
||||
|
||||
template <typename L, typename R>
|
||||
auto require_equal(int line, const L& l, const R& r) -> void
|
||||
@@ -60,18 +61,15 @@ int DateTime(int, char*[])
|
||||
|
||||
sql::connection db = sql::make_test_connection();
|
||||
|
||||
db.execute(R"(DROP TABLE IF EXISTS tabfoo;)");
|
||||
db.execute(R"(CREATE TABLE tabfoo
|
||||
db.execute(R"(DROP TABLE IF EXISTS tabdatetime;)");
|
||||
db.execute(R"(CREATE TABLE tabdatetime
|
||||
(
|
||||
alpha bigserial NOT NULL,
|
||||
beta smallint,
|
||||
gamma text,
|
||||
c_bool boolean,
|
||||
c_timepoint timestamp with time zone,
|
||||
c_time time with time zone,
|
||||
c_day date
|
||||
))");
|
||||
|
||||
model::TabFoo tab = {};
|
||||
model::TabDateTime tab = {};
|
||||
try
|
||||
{
|
||||
db(insert_into(tab).default_values());
|
||||
@@ -79,15 +77,18 @@ int DateTime(int, char*[])
|
||||
{
|
||||
require_equal(__LINE__, row.c_day.is_null(), true);
|
||||
require_equal(__LINE__, row.c_day.value(), ::sqlpp::chrono::day_point{});
|
||||
require_equal(__LINE__, row.c_time.is_null(), true);
|
||||
require_equal(__LINE__, row.c_time.value(), ::std::chrono::microseconds{});
|
||||
require_equal(__LINE__, row.c_timepoint.is_null(), true);
|
||||
require_equal(__LINE__, row.c_timepoint.value(), ::sqlpp::chrono::microsecond_point{});
|
||||
}
|
||||
|
||||
db(update(tab).set(tab.c_day = today, tab.c_timepoint = now).unconditionally());
|
||||
db(update(tab).set(tab.c_day = today, tab.c_time = current, tab.c_timepoint = now).unconditionally());
|
||||
|
||||
for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally()))
|
||||
{
|
||||
require_equal(__LINE__, row.c_day.value(), today);
|
||||
require_equal(__LINE__, row.c_time.value(), current);
|
||||
require_equal(__LINE__, row.c_timepoint.value(), now);
|
||||
}
|
||||
|
||||
@@ -96,14 +97,17 @@ int DateTime(int, char*[])
|
||||
for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally()))
|
||||
{
|
||||
require_equal(__LINE__, row.c_day.value(), yesterday);
|
||||
require_equal(__LINE__, row.c_time.value(), current);
|
||||
require_equal(__LINE__, row.c_timepoint.value(), today);
|
||||
}
|
||||
|
||||
auto prepared_update =
|
||||
db.prepare(update(tab)
|
||||
.set(tab.c_day = parameter(tab.c_day), tab.c_timepoint = parameter(tab.c_timepoint))
|
||||
.unconditionally());
|
||||
auto prepared_update = db.prepare(update(tab)
|
||||
.set(tab.c_day = parameter(tab.c_day),
|
||||
tab.c_time = parameter(tab.c_time),
|
||||
tab.c_timepoint = parameter(tab.c_timepoint))
|
||||
.unconditionally());
|
||||
prepared_update.params.c_day = today;
|
||||
prepared_update.params.c_time = current;
|
||||
prepared_update.params.c_timepoint = now;
|
||||
std::cout << "---- running prepared update ----" << std::endl;
|
||||
db(prepared_update);
|
||||
@@ -111,6 +115,7 @@ int DateTime(int, char*[])
|
||||
for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally()))
|
||||
{
|
||||
require_equal(__LINE__, row.c_day.value(), today);
|
||||
require_equal(__LINE__, row.c_time.value(), current);
|
||||
require_equal(__LINE__, row.c_timepoint.value(), now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef MODEL_TABDATETIME_H
|
||||
#define MODEL_TABDATETIME_H
|
||||
|
||||
#include <sqlpp11/table.h>
|
||||
#include <sqlpp11/char_sequence.h>
|
||||
#include <sqlpp11/column_types.h>
|
||||
|
||||
namespace model
|
||||
{
|
||||
namespace TabDateTime_
|
||||
{
|
||||
struct C_timepoint
|
||||
{
|
||||
struct _alias_t
|
||||
{
|
||||
static constexpr const char _literal[] = "c_timepoint";
|
||||
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
|
||||
template <typename T>
|
||||
struct _member_t
|
||||
{
|
||||
T c_timepoint;
|
||||
T& operator()()
|
||||
{
|
||||
return c_timepoint;
|
||||
}
|
||||
const T& operator()() const
|
||||
{
|
||||
return c_timepoint;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
using _traits = ::sqlpp::make_traits<::sqlpp::time_point, sqlpp::tag::can_be_null>;
|
||||
};
|
||||
|
||||
struct C_day
|
||||
{
|
||||
struct _alias_t
|
||||
{
|
||||
static constexpr const char _literal[] = "c_day";
|
||||
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
|
||||
template <typename T>
|
||||
struct _member_t
|
||||
{
|
||||
T c_day;
|
||||
T& operator()()
|
||||
{
|
||||
return c_day;
|
||||
}
|
||||
const T& operator()() const
|
||||
{
|
||||
return c_day;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
using _traits = ::sqlpp::make_traits<::sqlpp::day_point, sqlpp::tag::can_be_null>;
|
||||
};
|
||||
|
||||
struct C_time
|
||||
{
|
||||
struct _alias_t
|
||||
{
|
||||
static constexpr const char _literal[] = "c_time";
|
||||
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
|
||||
template <typename T>
|
||||
struct _member_t
|
||||
{
|
||||
T c_time;
|
||||
T& operator()()
|
||||
{
|
||||
return c_time;
|
||||
}
|
||||
const T& operator()() const
|
||||
{
|
||||
return c_time;
|
||||
}
|
||||
};
|
||||
};
|
||||
using _traits = sqlpp::make_traits<sqlpp::time_of_day, sqlpp::tag::can_be_null>;
|
||||
};
|
||||
|
||||
} // namespace TabDatetime_
|
||||
|
||||
struct TabDateTime : sqlpp::table_t<TabDateTime,
|
||||
TabDateTime_::C_time,
|
||||
TabDateTime_::C_day,
|
||||
TabDateTime_::C_timepoint>
|
||||
{
|
||||
using _value_type = sqlpp::no_value_t;
|
||||
struct _alias_t
|
||||
{
|
||||
static constexpr const char _literal[] = "TabDatetime";
|
||||
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
|
||||
template <typename T>
|
||||
struct _member_t
|
||||
{
|
||||
T TabDateTime;
|
||||
T& operator()()
|
||||
{
|
||||
return TabDateTime;
|
||||
}
|
||||
const T& operator()() const
|
||||
{
|
||||
return TabDateTime;
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user