DATACASS-642 - Consider quoted identifiers in QueryUtils and CqlIdentifier.
QueryUtils now considers quoted identifiers when extracting table names.
This commit is contained in:
@@ -230,7 +230,7 @@ class QueryUtils {
|
||||
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 QueryUtils {
|
||||
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('.');
|
||||
@@ -326,4 +326,8 @@ class QueryUtils {
|
||||
|
||||
return delete;
|
||||
}
|
||||
|
||||
private static String unquote(String identifier) {
|
||||
return identifier.substring(1, identifier.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.1.10
|
||||
*/
|
||||
public static boolean requiresQuoting(CharSequence chars) {
|
||||
return QUOTED.matcher(chars).matches() || ReservedKeyword.isReserved(chars);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,16 @@ public class QueryUtilsUnitTests {
|
||||
assertThat(tableName).isEqualTo(CqlIdentifier.of("table"));
|
||||
}
|
||||
|
||||
@Test // DATACASS-642
|
||||
public void shouldRetrieveQuotedTableNameFromSelect() {
|
||||
|
||||
Select select = QueryBuilder.select().from("keyspace", "\"table\"");
|
||||
|
||||
CqlIdentifier tableName = QueryUtils.getTableName(select);
|
||||
|
||||
assertThat(tableName).isEqualTo(CqlIdentifier.quoted("table"));
|
||||
}
|
||||
|
||||
@Test // DATACASS-106
|
||||
public void shouldRetrieveTableNameFromSimpleStatement() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user