Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>google-cloud-spanner-jdbc</artifactId>
<version>2.17.1-SNAPSHOT</version><!-- {x-version-update:google-cloud-spanner-jdbc:current} -->
<version>2.17.2-SNAPSHOT</version><!-- {x-version-update:google-cloud-spanner-jdbc:current} -->
<packaging>jar</packaging>
<name>Google Cloud Spanner JDBC</name>
<url>https://github.com/googleapis/java-spanner-jdbc</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ public JdbcConnection getConnection() {
return connection;
}

String maybeAddWhereClause(String sql) {
String lowerCaseSql = sql.toLowerCase();
if (lowerCaseSql.startsWith("delete") || lowerCaseSql.startsWith("update")) {
if (!lowerCaseSql.contains(" where ")) {
return sql + " where true";
}
}
return sql;
}

boolean isAllowedDdlStatementInTransaction(String sql) throws SQLException {
if (!getConnection().getAutoCommit() && getConnection().getParser().isDdlStatement(sql)) {
if (!getConnection().isTransactionStarted()) {
return true;
}
}
return false;
}

private Options.QueryOption[] getQueryOptions(QueryOption... options) throws SQLException {
QueryOption[] res = options == null ? new QueryOption[0] : options;
if (getFetchSize() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
JdbcConnection connection, String sql, ImmutableList<String> generatedKeysColumns)
throws SQLException {
super(connection);
this.sql = sql;
this.sql = maybeAddWhereClause(sql);
try {
// The PostgreSQL parser allows comments to be present in the SQL string that is used to parse
// the query parameters.
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/google/cloud/spanner/jdbc/JdbcStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public long executeLargeUpdate(String sql) throws SQLException {

private long executeLargeUpdate(String sql, ImmutableList<String> generatedKeysColumns)
throws SQLException {
return executeLargeUpdate(Statement.of(sql), generatedKeysColumns);
return executeLargeUpdate(Statement.of(maybeAddWhereClause(sql)), generatedKeysColumns);
}

protected long executeLargeUpdate(Statement statement, ImmutableList<String> generatedKeysColumns)
Expand All @@ -112,7 +112,19 @@ protected long executeLargeUpdate(Statement statement, ImmutableList<String> gen
checkClosed();
Statement statementWithReturningClause =
addReturningToStatement(statement, generatedKeysColumns);
StatementResult result = execute(statementWithReturningClause);
boolean allowedDdlStatementInTransaction =
isAllowedDdlStatementInTransaction(statement.getSql());
StatementResult result;
if (allowedDdlStatementInTransaction) {
getConnection().setAutoCommit(true);
}
try {
result = execute(statementWithReturningClause);
} finally {
if (allowedDdlStatementInTransaction) {
getConnection().setAutoCommit(false);
}
}
switch (result.getResultType()) {
case RESULT_SET:
if (generatedKeysColumns.isEmpty()) {
Expand Down Expand Up @@ -256,7 +268,7 @@ String quoteColumn(String column) {

@Override
public boolean execute(String sql) throws SQLException {
return executeStatement(Statement.of(sql), NO_GENERATED_KEY_COLUMNS);
return executeStatement(Statement.of(maybeAddWhereClause(sql)), NO_GENERATED_KEY_COLUMNS);
}

boolean executeStatement(Statement statement, ImmutableList<String> generatedKeysColumns)
Expand Down