Skip to content

Commit

Permalink
Switch StatementParser to pass through to the facade (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: @dmitry-s

Updates to the latest Cloud Spanner client library and uses its Connection API to check query type.

Also updates cloud libraries BOM, but it has not had time to pull in the newest Cloud Spanner client library, so an override is needed.


Fixes #236.
  • Loading branch information
elefeint authored Apr 23, 2020
1 parent 1b339eb commit 165bfe8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
12 changes: 6 additions & 6 deletions cloud-spanner-spring-data-r2dbc/cloud-spanner-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- test dependencies -->
<!-- Connection API from the Cloud Spanner client library -->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi-test</artifactId>
<scope>test</scope>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@

package com.google.cloud.spanner.r2dbc.statement;

import java.util.Arrays;
import java.util.List;

/**
* Parses SQL statements to determine what type of statement it is.
*/
public class StatementParser {

/** Matches on the Spanner SQL hints in the form @{hintName...} */
private static final String SQL_OPTIONS_REGEX = "(?m)@\\{.*\\}";

private static final List<String> SELECT_STATEMENTS = Arrays.asList("select");
private static final List<String> DDL_STATEMENTS = Arrays.asList("create", "drop", "alter");
private static final List<String> DML_STATEMENTS = Arrays.asList("insert", "update", "delete");
private static com.google.cloud.spanner.connection.StatementParser clientLibraryParser =
com.google.cloud.spanner.connection.StatementParser.INSTANCE;

private StatementParser() {
// Prevent instantiation.
Expand All @@ -41,28 +34,14 @@ private StatementParser() {
* @return the type of statement of the SQL string.
*/
public static StatementType getStatementType(String sql) {
String processedSql = processSql(sql);

if (statementStartsWith(processedSql, SELECT_STATEMENTS)) {
if (clientLibraryParser.isQuery(sql)) {
return StatementType.SELECT;
} else if (statementStartsWith(processedSql, DDL_STATEMENTS)) {
} else if (clientLibraryParser.isDdlStatement(sql)) {
return StatementType.DDL;
} else if (statementStartsWith(processedSql, DML_STATEMENTS)) {
} else if (clientLibraryParser.isUpdateStatement(sql)) {
return StatementType.DML;
} else {
return StatementType.UNKNOWN;
}
}

private static String processSql(String rawSql) {
return rawSql
.replaceAll(SQL_OPTIONS_REGEX, "")
.trim()
.toLowerCase();
}

private static boolean statementStartsWith(String sqlStatement, List<String> prefixes) {
return prefixes.stream().anyMatch(sqlPrefix -> sqlStatement.startsWith(sqlPrefix));
}
}

9 changes: 8 additions & 1 deletion cloud-spanner-spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<google-cloud-bom.version>4.1.1</google-cloud-bom.version>
<google-cloud-bom.version>5.1.0</google-cloud-bom.version>

<r2dbc.version>0.8.1.RELEASE</r2dbc.version>
<reactor.version>Dysprosium-SR5</reactor.version>
Expand All @@ -98,6 +98,13 @@

<dependencyManagement>
<dependencies>
<!-- override to snapshot until Connection API released -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>1.53.0</version>
</dependency>

<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
Expand Down

0 comments on commit 165bfe8

Please sign in to comment.