kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9fa4ddba0db6d934a4f006f01cb201ce3a528f6d5c1a2184168fb7718f0a7ad9 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-09-16 00:29 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-09-15 15:29 -07:00 |
| Parents | cfd5e99fc305 |
4 files changed
modified changelog/README.md +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence |
| 9 | 9 | - [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting. |
| 10 | 10 | - [improvement] JAVA-1275: Use Netty's default thread factory |
| 11 | +- [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names. | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +1 −2
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | |
| 22 | 22 | import java.nio.ByteBuffer; |
| 23 | 23 | import java.util.ArrayList; |
| 24 | -import java.util.Arrays; | |
| 25 | 24 | import java.util.List; |
| 26 | 25 | import java.util.Map; |
| 27 | 26 | import java.util.regex.Pattern; |
| @@ -228,7 +227,7 @@ | ||
| 228 | 227 | return; |
| 229 | 228 | |
| 230 | 229 | for (int i = 0; i < partitionKey.size(); i++) { |
| 231 | - if (name.equals(partitionKey.get(i).getName())) { | |
| 230 | + if (Utils.handleId(name).equals(partitionKey.get(i).getName())) { | |
| 232 | 231 | routingKeyValues.set(i, value); |
| 233 | 232 | return; |
| 234 | 233 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +32 −0
| @@ -32,8 +32,40 @@ | ||
| 32 | 32 | // Static utilities private to the query builder |
| 33 | 33 | abstract class Utils { |
| 34 | 34 | |
| 35 | + private static final Pattern alphanumeric = Pattern.compile("\\w+"); // this includes _ | |
| 35 | 36 | private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?"); |
| 36 | 37 | |
| 38 | + /** | |
| 39 | + * Deal with case sensitivity for a given element id (keyspace, table, column, etc.) | |
| 40 | + * | |
| 41 | + * This method is used to convert identifiers provided by the client (through methods such as getKeyspace(String)), | |
| 42 | + * to the format used internally by the driver. | |
| 43 | + * | |
| 44 | + * We expect client-facing APIs to behave like cqlsh, that is: | |
| 45 | + * - identifiers that are mixed-case or contain special characters should be quoted. | |
| 46 | + * - unquoted identifiers will be lowercased: getKeyspace("Foo") will look for a keyspace named "foo" | |
| 47 | + * | |
| 48 | + * Copied from {@link Metadata#handleId(String)} to avoid making it public. | |
| 49 | + */ | |
| 50 | + static String handleId(String id) { | |
| 51 | + // Shouldn't really happen for this method, but no reason to fail here | |
| 52 | + if (id == null) | |
| 53 | + return null; | |
| 54 | + | |
| 55 | + if (alphanumeric.matcher(id).matches()) | |
| 56 | + return id.toLowerCase(); | |
| 57 | + | |
| 58 | + // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes | |
| 59 | + if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 60 | + return id.substring(1, id.length() - 1).replaceAll("\"\"", "\""); | |
| 61 | + | |
| 62 | + // Otherwise, just return the id. | |
| 63 | + // Note that this is a bit at odds with the rules explained above, because the client can pass an | |
| 64 | + // identifier that contains special characters, without the need to quote it. | |
| 65 | + // Still it's better to be lenient here rather than throwing an exception. | |
| 66 | + return id; | |
| 67 | + } | |
| 68 | + | |
| 37 | 69 | static StringBuilder joinAndAppend(StringBuilder sb, CodecRegistry codecRegistry, String separator, List<? extends Appendeable> values, List<Object> variables) { |
| 38 | 70 | for (int i = 0; i < values.size(); i++) { |
| 39 | 71 | if (i > 0) |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +64 −1
| @@ -23,17 +23,22 @@ | ||
| 23 | 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 24 | 24 | import static org.testng.Assert.assertEquals; |
| 25 | 25 | import static org.testng.Assert.assertNotNull; |
| 26 | +import static org.testng.Assert.assertNull; | |
| 26 | 27 | |
| 27 | 28 | @CCMConfig(clusterProvider = "createClusterBuilderNoDebouncing") |
| 28 | 29 | public class QueryBuilderRoutingKeyTest extends CCMTestsSupport { |
| 29 | 30 | |
| 30 | 31 | private static final String TABLE_TEXT = "test_text"; |
| 31 | 32 | private static final String TABLE_INT = "test_int"; |
| 33 | + private static final String TABLE_CASE = "test_case"; | |
| 34 | + private static final String TABLE_CASE_QUOTED = "test_case_quoted"; | |
| 32 | 35 | |
| 33 | 36 | @Override |
| 34 | 37 | public void onTestContextInitialized() { |
| 35 | 38 | execute(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT), |
| 36 | - String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT)); | |
| 39 | + String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT), | |
| 40 | + String.format("CREATE TABLE %s (theKey int PRIMARY KEY, a int, b int)", TABLE_CASE), | |
| 41 | + String.format("CREATE TABLE %s (\"theKey\" int PRIMARY KEY, a int, b int, \"tHEkEY\" int)", TABLE_CASE_QUOTED)); | |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | @Test(groups = "short") |
| @@ -58,6 +63,64 @@ | ||
| 58 | 63 | assertEquals(row.getInt("b"), 2); |
| 59 | 64 | } |
| 60 | 65 | |
| 66 | + @Test(groups = "short") | |
| 67 | + public void routingKeyColumnCaseSensitivityTest() throws Exception { | |
| 68 | + | |
| 69 | + BuiltStatement query; | |
| 70 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE); | |
| 71 | + assertNotNull(table); | |
| 72 | + ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 73 | + CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
| 74 | + | |
| 75 | + query = insertInto(table).values(new String[]{"theKey", "a", "b"}, new Object[]{42, 1, 2}); | |
| 76 | + ByteBuffer bb = ByteBuffer.allocate(4); | |
| 77 | + bb.putInt(0, 42); | |
| 78 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 79 | + session().execute(query); | |
| 80 | + | |
| 81 | + query = select().from(table).where(eq("theKey", 42)); | |
| 82 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 83 | + Row row = session().execute(query).one(); | |
| 84 | + assertEquals(row.getInt("theKey"), 42); | |
| 85 | + assertEquals(row.getInt("a"), 1); | |
| 86 | + assertEquals(row.getInt("b"), 2); | |
| 87 | + | |
| 88 | + query = insertInto(table).values(new String[]{"ThEkEy", "a", "b"}, new Object[]{42, 1, 2}); | |
| 89 | + bb = ByteBuffer.allocate(4); | |
| 90 | + bb.putInt(0, 42); | |
| 91 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 92 | + session().execute(query); | |
| 93 | + | |
| 94 | + query = select().from(table).where(eq("ThEkEy", 42)); | |
| 95 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 96 | + row = session().execute(query).one(); | |
| 97 | + assertEquals(row.getInt("theKey"), 42); | |
| 98 | + assertEquals(row.getInt("a"), 1); | |
| 99 | + assertEquals(row.getInt("b"), 2); | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Test(groups = "short") | |
| 103 | + public void routingKeyColumnCaseSensitivityForQuotedIdentifiersTest() throws Exception { | |
| 104 | + | |
| 105 | + BuiltStatement query; | |
| 106 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE_QUOTED); | |
| 107 | + assertNotNull(table); | |
| 108 | + ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 109 | + CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
| 110 | + | |
| 111 | + query = insertInto(table).values(new String[]{"\"theKey\"", "a", "b", "\"tHEkEY\""}, new Object[]{42, 1, 2, 3}); | |
| 112 | + ByteBuffer bb = ByteBuffer.allocate(4); | |
| 113 | + bb.putInt(0, 42); | |
| 114 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 115 | + | |
| 116 | + query = insertInto(table).values(new String[]{"theKey", "a", "b", "\"tHEkEY\""}, new Object[]{42, 1, 2, 3}); | |
| 117 | + assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); | |
| 118 | + | |
| 119 | + query = insertInto(table).values(new String[]{"theKey", "a", "b", "theKey"}, new Object[]{42, 1, 2, 3}); | |
| 120 | + assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); | |
| 121 | + | |
| 122 | + } | |
| 123 | + | |
| 61 | 124 | @Test(groups = "short") |
| 62 | 125 | public void intRoutingKeyTest() throws Exception { |
| 63 | 126 |