0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-12-26 07:53:21 +08:00

Add tests for binding time_of_day parameters and results with SQLite3

This commit is contained in:
vaijns 2024-12-07 12:43:23 +01:00 committed by Roland Bock
parent d0cdaae972
commit e3f676079b
2 changed files with 36 additions and 5 deletions

View File

@ -42,6 +42,8 @@ namespace
const auto now = ::sqlpp::chrono::floor<::std::chrono::milliseconds>(std::chrono::system_clock::now()); const auto now = ::sqlpp::chrono::floor<::std::chrono::milliseconds>(std::chrono::system_clock::now());
const auto today = ::sqlpp::chrono::floor<::sqlpp::chrono::days>(now); const auto today = ::sqlpp::chrono::floor<::sqlpp::chrono::days>(now);
const auto yesterday = today - ::sqlpp::chrono::days{1}; const auto yesterday = today - ::sqlpp::chrono::days{1};
const ::std::chrono::microseconds time_of_day{now - today};
const ::std::chrono::microseconds start_of_day{0};
template <typename L, typename R> template <typename L, typename R>
auto require_equal(int line, const L& l, const R& r) -> void auto require_equal(int line, const L& l, const R& r) -> void
@ -70,7 +72,8 @@ int DateTime(int, char*[])
sql::connection db(config); sql::connection db(config);
db.execute(R"(CREATE TABLE tab_date_time ( db.execute(R"(CREATE TABLE tab_date_time (
col_day_point DATE, col_day_point DATE,
col_time_point DATETIME col_time_point DATETIME,
col_time_of_day TIME
))"); ))");
const auto tab = TabDateTime{}; const auto tab = TabDateTime{};
@ -82,30 +85,35 @@ int DateTime(int, char*[])
require_equal(__LINE__, row.colDayPoint.value(), ::sqlpp::chrono::day_point{}); require_equal(__LINE__, row.colDayPoint.value(), ::sqlpp::chrono::day_point{});
require_equal(__LINE__, row.colTimePoint.is_null(), true); require_equal(__LINE__, row.colTimePoint.is_null(), true);
require_equal(__LINE__, row.colTimePoint.value(), ::sqlpp::chrono::microsecond_point{}); require_equal(__LINE__, row.colTimePoint.value(), ::sqlpp::chrono::microsecond_point{});
require_equal(__LINE__, row.colTimeOfDay.is_null(), true);
require_equal(__LINE__, row.colTimeOfDay.value(), ::std::chrono::microseconds{});
} }
db(update(tab).set(tab.colDayPoint = today, tab.colTimePoint = now).unconditionally()); db(update(tab).set(tab.colDayPoint = today, tab.colTimePoint = now, tab.colTimeOfDay = time_of_day).unconditionally());
for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally())) for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally()))
{ {
require_equal(__LINE__, row.colDayPoint.value(), today); require_equal(__LINE__, row.colDayPoint.value(), today);
require_equal(__LINE__, row.colTimePoint.value(), now); require_equal(__LINE__, row.colTimePoint.value(), now);
require_equal(__LINE__, row.colTimeOfDay.value(), time_of_day);
} }
db(update(tab).set(tab.colDayPoint = yesterday, tab.colTimePoint = today).unconditionally()); db(update(tab).set(tab.colDayPoint = yesterday, tab.colTimePoint = today, tab.colTimeOfDay = start_of_day).unconditionally());
for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally())) for (const auto& row : db(select(all_of(tab)).from(tab).unconditionally()))
{ {
require_equal(__LINE__, row.colDayPoint.value(), yesterday); require_equal(__LINE__, row.colDayPoint.value(), yesterday);
require_equal(__LINE__, row.colTimePoint.value(), today); require_equal(__LINE__, row.colTimePoint.value(), today);
require_equal(__LINE__, row.colTimeOfDay.value(), start_of_day);
} }
auto prepared_update = db.prepare( auto prepared_update = db.prepare(
update(tab) update(tab)
.set(tab.colDayPoint = parameter(tab.colDayPoint), tab.colTimePoint = parameter(tab.colTimePoint)) .set(tab.colDayPoint = parameter(tab.colDayPoint), tab.colTimePoint = parameter(tab.colTimePoint), tab.colTimeOfDay = parameter(tab.colTimeOfDay))
.unconditionally()); .unconditionally());
prepared_update.params.colDayPoint = today; prepared_update.params.colDayPoint = today;
prepared_update.params.colTimePoint = now; prepared_update.params.colTimePoint = now;
prepared_update.params.colTimeOfDay = time_of_day;
std::cout << "---- running prepared update ----" << std::endl; std::cout << "---- running prepared update ----" << std::endl;
db(prepared_update); db(prepared_update);
std::cout << "---- finished prepared update ----" << std::endl; std::cout << "---- finished prepared update ----" << std::endl;
@ -113,6 +121,7 @@ int DateTime(int, char*[])
{ {
require_equal(__LINE__, row.colDayPoint.value(), today); require_equal(__LINE__, row.colDayPoint.value(), today);
require_equal(__LINE__, row.colTimePoint.value(), now); require_equal(__LINE__, row.colTimePoint.value(), now);
require_equal(__LINE__, row.colTimeOfDay.value(), time_of_day);
} }
} }
catch (const std::exception& e) catch (const std::exception& e)

View File

@ -222,9 +222,31 @@ namespace TabDateTime_
}; };
using _traits = sqlpp::make_traits<sqlpp::time_point, sqlpp::tag::can_be_null>; using _traits = sqlpp::make_traits<sqlpp::time_point, sqlpp::tag::can_be_null>;
}; };
struct ColTimeOfDay
{
struct _alias_t
{
static constexpr const char _literal[] = "col_time_of_day";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T colTimeOfDay;
T& operator()()
{
return colTimeOfDay;
}
const T& operator()() const
{
return colTimeOfDay;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::time_of_day, sqlpp::tag::can_be_null>;
};
} }
struct TabDateTime : sqlpp::table_t<TabDateTime, TabDateTime_::ColDayPoint, TabDateTime_::ColTimePoint> struct TabDateTime : sqlpp::table_t<TabDateTime, TabDateTime_::ColDayPoint, TabDateTime_::ColTimePoint, TabDateTime_::ColTimeOfDay>
{ {
struct _alias_t struct _alias_t
{ {