Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion driver/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cstdio>

static const char exception_code_header_name[] = "X-ClickHouse-Exception-Code";
static const char timezone_header_name[] = "X-ClickHouse-Timezone";

Statement::Statement(Connection & connection)
: ChildType(connection)
Expand All @@ -31,6 +32,16 @@ const TypeInfo & Statement::getTypeInfo(const std::string & type_name, const std
return getParent().getTypeInfo(type_name, type_name_without_parameters);
}

std::string Statement::getResponseTimezone(
const Poco::Net::HTTPResponse & response,
std::string (*get_default_timezone)())
{
if (response.has(timezone_header_name))
return response.get(timezone_header_name);

return get_default_timezone();
}

void Statement::prepareQuery(const std::string & q) {
closeCursor();

Expand Down Expand Up @@ -201,9 +212,11 @@ void Statement::requestNextPackOfResultSets(std::unique_ptr<ResultMutator> && mu
throw std::runtime_error(error_message.str());
}

const auto timezone = getResponseTimezone(*response, Poco::Timezone::name);

result_reader = make_result_reader(
response->get("X-ClickHouse-Format", connection.default_format),
response->get("X-ClickHouse-Timezone", Poco::Timezone::name()),
timezone,
*in,
*connection.session,
std::move(mutator)
Expand Down
4 changes: 4 additions & 0 deletions driver/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class Statement

public:
// public only for the unit tests
static std::string getResponseTimezone(
const Poco::Net::HTTPResponse & response,
std::string (*get_default_timezone)());

struct HttpRequestData {
std::string query;
std::map<std::string, std::string> params;
Expand Down
1 change: 1 addition & 0 deletions driver/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function (declare_odbc_test_targets libname UNICODE)
connection_string_ut.cpp
performance_ut.cpp
statement_parameter_binding_ut.cpp
statement_ut.cpp
type_info_ut.cpp
)

Expand Down
36 changes: 36 additions & 0 deletions driver/test/statement_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <gtest/gtest.h>

#include "driver/statement.h"

namespace {

std::string unexpectedTimezoneFallback() {
ADD_FAILURE() << "The local timezone fallback must not be evaluated when the response has a timezone header";
return {};
}

std::string expectedTimezoneFallback() {
return "local-timezone";
}

TEST(Statement, ResponseTimezoneUsesHeaderWithoutFallback) {
Poco::Net::HTTPResponse response;
response.set("X-ClickHouse-Timezone", "UTC");

EXPECT_EQ(Statement::getResponseTimezone(response, unexpectedTimezoneFallback), "UTC");
}

TEST(Statement, ResponseTimezoneUsesEmptyHeaderWithoutFallback) {
Poco::Net::HTTPResponse response;
response.set("X-ClickHouse-Timezone", "");

EXPECT_EQ(Statement::getResponseTimezone(response, unexpectedTimezoneFallback), "");
}

TEST(Statement, ResponseTimezoneUsesFallbackWithoutHeader) {
Poco::Net::HTTPResponse response;

EXPECT_EQ(Statement::getResponseTimezone(response, expectedTimezoneFallback), "local-timezone");
}

} // namespace