DATACASS-642 - Consider quoted identifiers in EntityQueryUtils and CqlIdentifier.

EntityQueryUtils now considers quoted identifiers when extracting table names.
This commit is contained in:
Mark Paluch
2019-06-18 14:14:56 +02:00
parent f4b243fcfe
commit 8064a6d1dd
3 changed files with 28 additions and 5 deletions

View File

@@ -230,7 +230,7 @@ class EntityQueryUtils {
String table = (String) accessor.getPropertyValue("table");
if (table != null) {
return CqlIdentifier.of(table);
return CqlIdentifier.isQuotedIdentifier(table) ? CqlIdentifier.quoted(unquote(table)) : CqlIdentifier.of(table);
}
}
@@ -240,8 +240,8 @@ class EntityQueryUtils {
if (matcher.find()) {
String cqlTableName = matcher.group(1);
if (cqlTableName.startsWith("\"")) {
return CqlIdentifier.quoted(cqlTableName.substring(1, cqlTableName.length() - 1));
if (CqlIdentifier.isQuotedIdentifier(cqlTableName)) {
return CqlIdentifier.quoted(unquote(cqlTableName));
}
int separator = cqlTableName.indexOf('.');
@@ -335,4 +335,8 @@ class EntityQueryUtils {
return delete;
}
private static String unquote(String identifier) {
return identifier.substring(1, identifier.length() - 1);
}
}

View File

@@ -85,7 +85,7 @@ public final class CqlIdentifier implements Comparable<CqlIdentifier>, Serializa
Assert.hasText(string, "Identifier must not be empty");
if (forceQuote || isQuotedIdentifier(string)) {
if (forceQuote || requiresQuoting(string)) {
this.unquoted = string;
this.identifier = "\"" + string + "\"";
this.quoted = true;
@@ -165,9 +165,18 @@ public final class CqlIdentifier implements Comparable<CqlIdentifier>, Serializa
}
/**
* Returns {@code true} if the given {@link CharSequence} is a legal unquoted identifier.
* Returns {@code true} if the given {@link CharSequence} is an identifier with quotes.
*/
public static boolean isQuotedIdentifier(CharSequence chars) {
return chars != null && chars.length() > 1 && chars.charAt(0) == '"' && chars.charAt(chars.length() - 1) == '"';
}
/**
* Returns {@code true} if the given {@link CharSequence} requires quoting.
*
* @since 2.2
*/
public static boolean requiresQuoting(CharSequence chars) {
return QUOTED.matcher(chars).matches() || ReservedKeyword.isReserved(chars);
}

View File

@@ -47,6 +47,16 @@ public class EntityQueryUtilsUnitTests {
assertThat(tableName).isEqualTo(CqlIdentifier.of("table"));
}
@Test // DATACASS-642
public void shouldRetrieveQuotedTableNameFromSelect() {
Select select = QueryBuilder.select().from("keyspace", "\"table\"");
CqlIdentifier tableName = EntityQueryUtils.getTableName(select);
assertThat(tableName).isEqualTo(CqlIdentifier.quoted("table"));
}
@Test // DATACASS-106
public void shouldRetrieveTableNameFromSimpleStatement() {