2023-06-16 12:57:19 +08:00
|
|
|
/**
|
|
|
|
* Copyright © 2023 Vesselin Atanasov
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <sqlpp11/postgresql/postgresql.h>
|
|
|
|
#include <sqlpp11/sqlpp11.h>
|
|
|
|
|
|
|
|
#include "make_test_connection.h"
|
2024-06-17 00:45:26 +08:00
|
|
|
#include "Tables.h"
|
2023-06-16 12:57:19 +08:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2024-06-17 00:45:26 +08:00
|
|
|
void save_regular (sqlpp::postgresql::connection& db, sqlpp::chrono::microsecond_point tp, std::chrono::microseconds tod, sqlpp::chrono::day_point dp)
|
2023-06-16 12:57:19 +08:00
|
|
|
{
|
2024-06-17 00:45:26 +08:00
|
|
|
test::TabDateTime tab {};
|
|
|
|
db(
|
2023-06-16 12:57:19 +08:00
|
|
|
update(tab)
|
|
|
|
.set(
|
2024-06-17 00:45:26 +08:00
|
|
|
tab.timePointNTz = tp,
|
|
|
|
tab.timeOfDayNTz = tod,
|
|
|
|
tab.dayPointN = dp
|
2023-06-16 12:57:19 +08:00
|
|
|
)
|
|
|
|
.unconditionally()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
void save_prepared (sqlpp::postgresql::connection& db, sqlpp::chrono::microsecond_point tp, std::chrono::microseconds tod, sqlpp::chrono::day_point dp)
|
2023-06-16 12:57:19 +08:00
|
|
|
{
|
2024-06-17 00:45:26 +08:00
|
|
|
test::TabDateTime tab {};
|
|
|
|
auto prepared_update = db.prepare(
|
2023-06-16 12:57:19 +08:00
|
|
|
update(tab)
|
|
|
|
.set(
|
2024-06-17 00:45:26 +08:00
|
|
|
tab.timePointNTz = parameter(tab.timePointNTz),
|
|
|
|
tab.timeOfDayNTz = parameter(tab.timeOfDayNTz),
|
|
|
|
tab.dayPointN = parameter(tab.dayPointN)
|
2023-06-16 12:57:19 +08:00
|
|
|
)
|
|
|
|
.unconditionally()
|
|
|
|
);
|
2024-06-17 00:45:26 +08:00
|
|
|
prepared_update.params.timePointNTz = tp;
|
|
|
|
prepared_update.params.timeOfDayNTz = tod;
|
|
|
|
prepared_update.params.dayPointN = dp;
|
|
|
|
db(prepared_update);
|
2023-06-16 12:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename L, typename R>
|
|
|
|
void require_equal(int line, const L& l, const R& r)
|
|
|
|
{
|
|
|
|
if (l != r)
|
|
|
|
{
|
|
|
|
std::cerr << line << ": ";
|
|
|
|
serialize(::sqlpp::wrap_operand_t<L>{l}, std::cerr);
|
|
|
|
std::cerr << " != ";
|
|
|
|
serialize(::sqlpp::wrap_operand_t<R>{r}, std::cerr);
|
|
|
|
throw std::runtime_error("Unexpected result");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
void check_saved_values(sqlpp::postgresql::connection& db, sqlpp::chrono::microsecond_point tp, std::chrono::microseconds tod, sqlpp::chrono::day_point dp)
|
2023-06-16 12:57:19 +08:00
|
|
|
{
|
2024-06-17 00:45:26 +08:00
|
|
|
test::TabDateTime tab {};
|
2023-06-16 12:57:19 +08:00
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
const auto &rows_1 = db(
|
2023-06-16 12:57:19 +08:00
|
|
|
select(
|
2024-06-17 00:45:26 +08:00
|
|
|
// timePointNTz as microseconds from the start of the UNIX epoch (1970-01-01 00:00:00 UTC)
|
|
|
|
sqlpp::verbatim<sqlpp::integer>("floor(extract(epoch from time_point_n_tz)*1000000)::int8").as(sqlpp::alias::a),
|
|
|
|
// timeOfDayNTz as microseconds from the start of the day (00:00:00 UTC)
|
|
|
|
sqlpp::verbatim<sqlpp::integer>("floor(extract(epoch from time_of_day_n_tz)*1000000)::int8").as(sqlpp::alias::b),
|
|
|
|
// dayPointN as days from 1970-01-01 (timezone is not applicable to date fields)
|
|
|
|
sqlpp::verbatim<sqlpp::integer>("floor(extract(epoch from day_point_n)/86400)::int8").as(sqlpp::alias::c)
|
2023-06-16 12:57:19 +08:00
|
|
|
)
|
|
|
|
.from(tab)
|
|
|
|
.unconditionally()
|
|
|
|
);
|
|
|
|
// Check if the internal values of our C++ time variables match the internal values of the PostgreSQL date/time fields.
|
|
|
|
// This tests the conversion of date/time types from C++ to PostgreSQL while skipping the conversion from C++ to PostgreSQL.
|
|
|
|
const auto &row_1 = rows_1.front();
|
|
|
|
require_equal(__LINE__, row_1.a.value(), tp.time_since_epoch().count());
|
|
|
|
require_equal(__LINE__, row_1.b.value(), tod.count());
|
|
|
|
require_equal(__LINE__, row_1.c.value(), dp.time_since_epoch().count());
|
|
|
|
|
|
|
|
// Check if saving date/time variables from C++ to PostgreSQL and then reading them back yields the same values.
|
|
|
|
// This tests the conversion of date/time types from C++ to PostgreSQL and then back from PostgreSQL to C++.
|
2024-06-17 00:45:26 +08:00
|
|
|
const auto rows_2 = db(select(all_of(tab)).from(tab).unconditionally());
|
2023-06-16 12:57:19 +08:00
|
|
|
const auto &row_2 = rows_2.front();
|
2024-06-17 00:45:26 +08:00
|
|
|
require_equal(__LINE__, row_2.timePointNTz.value(), tp);
|
|
|
|
require_equal(__LINE__, row_2.timeOfDayNTz.value(), tod);
|
|
|
|
require_equal(__LINE__, row_2.dayPointN.value(), dp);
|
2023-06-16 12:57:19 +08:00
|
|
|
}
|
|
|
|
|
2024-06-17 00:45:26 +08:00
|
|
|
void test_time_point(sqlpp::postgresql::connection& db, sqlpp::chrono::microsecond_point tp)
|
2023-06-16 12:57:19 +08:00
|
|
|
{
|
|
|
|
auto dp = date::floor<sqlpp::chrono::days> (tp);
|
|
|
|
auto tod = tp - dp; // Time of day
|
|
|
|
|
|
|
|
// Test time values passed in a regular (non-prepared) statement
|
2024-06-17 00:45:26 +08:00
|
|
|
save_regular(db, tp, tod, dp);
|
|
|
|
check_saved_values(db, tp, tod, dp);
|
2023-06-16 12:57:19 +08:00
|
|
|
|
|
|
|
// Test time values passed in a prepared statement
|
2024-06-17 00:45:26 +08:00
|
|
|
save_prepared(db, tp, tod, dp);
|
|
|
|
check_saved_values(db, tp, tod, dp);
|
2023-06-16 12:57:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int TimeZone(int, char*[])
|
|
|
|
{
|
|
|
|
namespace sql = sqlpp::postgresql;
|
|
|
|
|
2023-07-24 12:40:18 +08:00
|
|
|
// We use a time zone with non-zero offset from UTC in order to catch serialization/parsing bugs
|
2024-06-17 00:45:26 +08:00
|
|
|
auto db = sql::make_test_connection("+1");
|
|
|
|
|
|
|
|
test::createTabDateTime(db);
|
|
|
|
|
|
|
|
test::TabDateTime tab {};
|
2023-06-16 12:57:19 +08:00
|
|
|
try {
|
2024-06-17 00:45:26 +08:00
|
|
|
db(insert_into(tab).default_values());
|
2023-06-16 12:57:19 +08:00
|
|
|
|
|
|
|
std::vector<sqlpp::chrono::microsecond_point> tps {
|
|
|
|
static_cast<date::sys_days>(date::January/1/1970) + std::chrono::hours{1} + std::chrono::minutes{20} + std::chrono::seconds{14} + std::chrono::microseconds{1},
|
|
|
|
static_cast<date::sys_days>(date::June/13/1986) + std::chrono::hours{12} + std::chrono::minutes{0} + std::chrono::seconds{1} + std::chrono::microseconds{123},
|
|
|
|
static_cast<date::sys_days>(date::December/31/2022) + std::chrono::hours{0} + std::chrono::minutes{59} + std::chrono::seconds{59} + std::chrono::microseconds{987654}
|
|
|
|
};
|
|
|
|
for (const auto &tp : tps) {
|
2024-06-17 00:45:26 +08:00
|
|
|
test_time_point(db, tp);
|
2023-06-16 12:57:19 +08:00
|
|
|
}
|
|
|
|
} catch (const sql::failure& e) {
|
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|