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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner.jdbc;

import com.google.common.collect.ImmutableList;
import com.google.rpc.Code;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/** Represents the generated-key columns requested for a JDBC statement execution. */
@NullMarked
final class GeneratedKeysColumns {
private enum Mode {
NONE,
ALL,
COLUMNS
}

static final GeneratedKeysColumns NONE = new GeneratedKeysColumns(Mode.NONE, ImmutableList.of());
static final GeneratedKeysColumns ALL = new GeneratedKeysColumns(Mode.ALL, ImmutableList.of());

private final Mode mode;
private final ImmutableList<String> columnNames;

private GeneratedKeysColumns(Mode mode, ImmutableList<String> columnNames) {
this.mode = mode;
this.columnNames = columnNames;
}

static GeneratedKeysColumns of(@Nullable String @Nullable [] columnNames) throws SQLException {
return columnNames == null ? NONE : of(Arrays.asList(columnNames));
}

static GeneratedKeysColumns of(List<? extends @Nullable String> columnNames) throws SQLException {
if (columnNames.isEmpty()) {
return NONE;
}
ImmutableList.Builder<String> builder =
ImmutableList.builderWithExpectedSize(columnNames.size());
for (String columnName : columnNames) {
builder.add(validateColumnName(columnName));
}
return new GeneratedKeysColumns(Mode.COLUMNS, builder.build());
}

private static String validateColumnName(@Nullable String columnName) throws SQLException {
if (columnName == null || columnName.isEmpty()) {
throw JdbcSqlExceptionFactory.of(
"Generated key column name cannot be null or empty", Code.INVALID_ARGUMENT);
}
if (columnName.indexOf('\0') >= 0) {
throw JdbcSqlExceptionFactory.of(
"Generated key column name cannot contain a NUL character", Code.INVALID_ARGUMENT);
}
return columnName;
}

boolean isNone() {
return mode == Mode.NONE;
}

boolean isAll() {
return mode == Mode.ALL;
}

ImmutableList<String> getColumnNames() {
return columnNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.google.cloud.spanner.jdbc;

import static com.google.cloud.spanner.jdbc.JdbcStatement.ALL_COLUMNS;
import static com.google.cloud.spanner.jdbc.JdbcStatement.isNullOrEmpty;

import com.google.api.client.util.Preconditions;
import com.google.cloud.ByteArray;
import com.google.cloud.spanner.CommitResponse;
Expand All @@ -34,7 +31,6 @@
import com.google.cloud.spanner.connection.TransactionMode;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -84,8 +80,6 @@ class JdbcConnection extends AbstractJdbcConnection {
*/
static final String LEGACY_IS_VALID_QUERY = "SELECT 1";

static final ImmutableList<String> NO_GENERATED_KEY_COLUMNS = ImmutableList.of();

private Map<String, Class<?>> typeMap = new HashMap<>();

private final boolean useLegacyIsValidCheck;
Expand Down Expand Up @@ -140,13 +134,13 @@ public Statement createStatement() throws SQLException {

@Override
public JdbcPreparedStatement prepareStatement(String sql) throws SQLException {
return prepareStatement(sql, NO_GENERATED_KEY_COLUMNS);
return prepareStatement(sql, GeneratedKeysColumns.NONE);
}

private JdbcPreparedStatement prepareStatement(
String sql, ImmutableList<String> generatedKeyColumns) throws SQLException {
String sql, GeneratedKeysColumns generatedKeysColumns) throws SQLException {
checkClosed();
return new JdbcPreparedStatement(this, sql, generatedKeyColumns);
return new JdbcPreparedStatement(this, sql, generatedKeysColumns);
}

@Override
Expand Down Expand Up @@ -386,23 +380,22 @@ public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) thr
return prepareStatement(
sql,
autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS
? ALL_COLUMNS
: NO_GENERATED_KEY_COLUMNS);
? GeneratedKeysColumns.ALL
: GeneratedKeysColumns.NONE);
}

@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// This should preferably have returned an error, but the initial version of the driver just
// accepted and ignored this. Starting to throw an error now would be a breaking change.
// TODO: Consider throwing an Unsupported error for the next major version bump.
return prepareStatement(sql, NO_GENERATED_KEY_COLUMNS);
return prepareStatement(sql, GeneratedKeysColumns.NONE);
}

@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return prepareStatement(
sql,
isNullOrEmpty(columnNames) ? NO_GENERATED_KEY_COLUMNS : ImmutableList.copyOf(columnNames));
checkClosed();
return prepareStatement(sql, GeneratedKeysColumns.of(columnNames));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class JdbcPreparedStatement extends AbstractJdbcPreparedStatement
private final String sql;
private final ParametersInfo parameters;
private JdbcParameterMetaData cachedParameterMetadata;
private final ImmutableList<String> generatedKeysColumns;
private final GeneratedKeysColumns generatedKeysColumns;

JdbcPreparedStatement(
JdbcConnection connection, String sql, ImmutableList<String> generatedKeysColumns)
JdbcConnection connection, String sql, GeneratedKeysColumns generatedKeysColumns)
throws SQLException {
super(connection);
this.sql = sql;
Expand Down
Loading
Loading