Skip to content

Commit 46396a6

Browse files
authored
Merge pull request #1 from Enmk/bugfix-insertSpaceColumn
Validating names of the columns received from clickhouse
2 parents d008751 + f812600 commit 46396a6

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

clickhouse/client.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,20 @@ void Client::Impl::ExecuteQuery(Query query) {
203203

204204
std::string NameToQueryString(const std::string &input)
205205
{
206-
std::string output = "\"";
207-
const char *c = input.c_str();
208-
while (*c) {
209-
if(*c == '"'){
210-
//escape " with ""
211-
output.append("\"\"");
212-
}else{
213-
output.push_back(*c);
214-
}
215-
++c;
216-
}
217-
output += "\"";
206+
std::string output;
207+
output.reserve(input.size() + 2);
208+
output += '`';
209+
210+
for (const auto & c : input) {
211+
if (c == '`') {
212+
//escape ` with ``
213+
output.append("``");
214+
} else {
215+
output.push_back(c);
216+
}
217+
}
218+
219+
output += '`';
218220
return output;
219221
}
220222

ut/client_ut.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,9 @@ TEST_P(ClientCase, Decimal) {
718718

719719
// Test special chars in names
720720
TEST_P(ClientCase, ColEscapeNameTest) {
721-
client_->Execute("DROP TABLE IF EXISTS test_clickhouse_cpp.\"col_escape_\"\"name_test\";");
721+
client_->Execute(R"sql(DROP TABLE IF EXISTS test_clickhouse_cpp."col_escape_""name_test";)sql");
722722

723-
client_->Execute("CREATE TABLE IF NOT EXISTS test_clickhouse_cpp.\"col_escape_\"\"name_test\" (\"test space\" UInt64, \"test \"\" quote\" UInt64, \"test \"\"`'[]&_\\ all\" UInt64) ENGINE = Memory");
723+
client_->Execute(R"sql(CREATE TABLE IF NOT EXISTS test_clickhouse_cpp."col_escape_""name_test" ("test space" UInt64, "test "" quote" UInt64, "test ""`'[]&_\ all" UInt64) ENGINE = Memory)sql");
724724

725725
auto col1 = std::make_shared<ColumnUInt64>();
726726
col1->Append(1);
@@ -732,18 +732,28 @@ TEST_P(ClientCase, ColEscapeNameTest) {
732732
col3->Append(16);
733733
col3->Append(32);
734734

735+
static const std::string column_names[] = {
736+
"test space",
737+
R"sql(test " quote)sql",
738+
R"sql(test "`'[]&_\ all)sql"
739+
};
740+
static const auto columns_count = sizeof(column_names)/sizeof(column_names[0]);
741+
735742
Block block;
736-
block.AppendColumn("test space", col1);
737-
block.AppendColumn("test \" quote", col2);
738-
block.AppendColumn("test \"`'[]&_\\ all", col3);
743+
block.AppendColumn(column_names[0], col1);
744+
block.AppendColumn(column_names[1], col2);
745+
block.AppendColumn(column_names[2], col3);
739746

740-
client_->Insert("test_clickhouse_cpp.\"col_escape_\"\"name_test\"", block);
741-
client_->Select("SELECT * FROM test_clickhouse_cpp.\"col_escape_\"\"name_test\"", [] (const Block& sblock)
747+
client_->Insert(R"sql(test_clickhouse_cpp."col_escape_""name_test")sql", block);
748+
client_->Select(R"sql(SELECT * FROM test_clickhouse_cpp."col_escape_""name_test")sql", [] (const Block& sblock)
742749
{
743750
int row = sblock.GetRowCount();
744751
if (row <= 0) {return;}
745-
int col = sblock.GetColumnCount();
746-
EXPECT_EQ(col, 3);
752+
ASSERT_EQ(columns_count, sblock.GetColumnCount());
753+
for (size_t i = 0; i < columns_count; ++i) {
754+
EXPECT_EQ(column_names[i], sblock.GetColumnName(i));
755+
}
756+
747757
EXPECT_EQ(row, 2);
748758
EXPECT_EQ(sblock[0]->As<ColumnUInt64>()->At(0), 1u);
749759
EXPECT_EQ(sblock[0]->As<ColumnUInt64>()->At(1), 2u);

0 commit comments

Comments
 (0)