-
Notifications
You must be signed in to change notification settings - Fork 292
CPP-747 Fix case sensitive keyspaces #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0ffbfd1
60623c0
00f0651
9bdd521
02e66e7
d68d48c
96ae0d8
38534a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,38 +111,22 @@ String& trim(String& str) { | |
| return str; | ||
| } | ||
|
|
||
| static bool is_word_char(int c) { | ||
| return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; | ||
| } | ||
| static bool is_lowercase(const String& str) { | ||
| if (str.empty()) return true; | ||
|
|
||
| static bool is_lower_word_char(int c) { | ||
| return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'; | ||
| } | ||
| char c = str[0]; | ||
| if (!(c >= 'a' && c <= 'z')) return false; | ||
|
|
||
| bool is_valid_cql_id(const String& str) { | ||
| for (String::const_iterator i = str.begin(), end = str.end(); i != end; ++i) { | ||
| if (!is_word_char(*i)) { | ||
| for (String::const_iterator it = str.begin() + 1, end = str.end(); it != end; ++it) { | ||
| char c = *it; | ||
| if (!((c >= '0' && c <= '9') || (c == '_') || (c >= 'a' && c <= 'z'))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool is_valid_lower_cql_id(const String& str) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was implemented incorrectly. |
||
| if (str.empty() || !is_lower_word_char(str[0])) { | ||
| return false; | ||
| } | ||
| if (str.size() > 1) { | ||
| for (String::const_iterator i = str.begin() + 1, end = str.end(); i != end; ++i) { | ||
| if (!is_lower_word_char(*i)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| String& quote_id(String& str) { | ||
| static String& quote_id(String& str) { | ||
| String temp(str); | ||
| str.clear(); | ||
| str.push_back('"'); | ||
|
|
@@ -159,18 +143,7 @@ String& quote_id(String& str) { | |
| return str; | ||
| } | ||
|
|
||
| String& escape_id(String& str) { return is_valid_lower_cql_id(str) ? str : quote_id(str); } | ||
|
|
||
| String& to_cql_id(String& str) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (is_valid_cql_id(str)) { | ||
| std::transform(str.begin(), str.end(), str.begin(), tolower); | ||
| return str; | ||
| } | ||
| if (str.length() > 2 && str[0] == '"' && str[str.length() - 1] == '"') { | ||
| return str.erase(str.length() - 1, 1).erase(0, 1); | ||
| } | ||
| return str; | ||
| } | ||
| String& escape_id(String& str) { return is_lowercase(str) ? str : quote_id(str); } | ||
|
|
||
| int32_t get_pid() { | ||
| #if (defined(WIN32) || defined(_WIN32)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| Copyright (c) DataStax, Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "integration.hpp" | ||
|
|
||
| #include <locale> | ||
|
|
||
| /** | ||
| * "USE <keyspace>" case-sensitive tests | ||
| */ | ||
| class UseKeyspaceCaseSensitiveTests : public Integration { | ||
| public: | ||
| UseKeyspaceCaseSensitiveTests() {} | ||
|
|
||
| // Make a case-sensitive keyspace capitalizing the first char and wrapping in double quotes | ||
| virtual std::string default_keyspace() { | ||
| std::string temp(Integration::default_keyspace()); | ||
| temp[0] = std::toupper(temp[0]); | ||
| return "\"" + temp + "\""; | ||
| } | ||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smart! |
||
|
|
||
| virtual void SetUp() { | ||
| Integration::SetUp(); | ||
| session_.execute( | ||
| format_string(CASSANDRA_KEY_VALUE_TABLE_FORMAT, table_name_.c_str(), "int", "int")); | ||
| session_.execute( | ||
| format_string(CASSANDRA_KEY_VALUE_INSERT_FORMAT, table_name_.c_str(), "1", "2")); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Verify that case-sensitive keyspaces work when connecting a session with a keyspace. | ||
| */ | ||
| CASSANDRA_INTEGRATION_TEST_F(UseKeyspaceCaseSensitiveTests, ConnectWithKeyspace) { | ||
| CHECK_FAILURE; | ||
| Session session = default_cluster().connect(keyspace_name_); | ||
|
|
||
| Result result = | ||
| session.execute(format_string(CASSANDRA_SELECT_VALUE_FORMAT, table_name_.c_str(), "1")); | ||
|
|
||
| Row row = result.first_row(); | ||
| EXPECT_EQ(row.column_by_name<Integer>("value"), Integer(2)); | ||
| } | ||
|
|
||
| /** | ||
| * Verify that case-sensitive keyspaces work with "USE <keyspace>". | ||
| */ | ||
| CASSANDRA_INTEGRATION_TEST_F(UseKeyspaceCaseSensitiveTests, UseKeyspace) { | ||
| CHECK_FAILURE; | ||
| Session session = default_cluster().connect(); | ||
|
|
||
| { // Expect failure there's no keyspace set | ||
| Result result = | ||
| session.execute(format_string(CASSANDRA_SELECT_VALUE_FORMAT, table_name_.c_str(), "1"), | ||
| CASS_CONSISTENCY_ONE, false, false); | ||
|
|
||
| EXPECT_EQ(result.error_code(), CASS_ERROR_SERVER_INVALID_QUERY); | ||
| } | ||
|
|
||
| session.execute("USE " + keyspace_name_); | ||
|
|
||
| { // Success | ||
| Result result = | ||
| session.execute(format_string(CASSANDRA_SELECT_VALUE_FORMAT, table_name_.c_str(), "1")); | ||
|
|
||
| Row row = result.first_row(); | ||
| EXPECT_EQ(row.column_by_name<Integer>("value"), Integer(2)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable. Unrelated.