Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make compiled statements immutable #843

Merged
merged 1 commit into from Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,26 +16,13 @@

package com.google.cloud.spanner.connection;

import com.google.cloud.spanner.connection.ClientSideStatementImpl.CompileException;

/**
* A {@link ClientSideStatementExecutor} is used to compile {@link ClientSideStatement}s from the
* json source file, and to execute these against a {@link Connection} (through a {@link
* ConnectionStatementExecutor}).
*/
interface ClientSideStatementExecutor {

/**
* Compiles the given {@link ClientSideStatementImpl} and registers this statement with this
* executor. A statement must be compiled before it can be executed. The parser automatically
* compiles all available statements during initialization.
*
* @param statement The statement to compile.
* @throws CompileException If the statement could not be compiled. This should never happen, as
* it would indicate that an invalid statement has been defined in the source file.
*/
void compile(ClientSideStatementImpl statement) throws CompileException;

/**
* Executes the {@link ClientSideStatementImpl} that has been compiled and registered with this
* executor on the specified connection.
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.connection.StatementResult.ResultType;
import com.google.common.base.Preconditions;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -143,10 +144,12 @@ public String getMessage() {
ClientSideStatementImpl compile() throws CompileException {
try {
this.pattern = Pattern.compile(regex);
this.executor =
(ClientSideStatementExecutor)
Class.forName(getClass().getPackage().getName() + "." + executorName).newInstance();
this.executor.compile(this);
@SuppressWarnings("unchecked")
Constructor<ClientSideStatementExecutor> constructor =
(Constructor<ClientSideStatementExecutor>)
Class.forName(getClass().getPackage().getName() + "." + executorName)
.getDeclaredConstructor(ClientSideStatementImpl.class);
this.executor = constructor.newInstance(this);
return this;
} catch (Exception e) {
throw new CompileException(e, this);
Expand Down
Expand Up @@ -24,12 +24,16 @@
* SHOW AUTOCOMMIT. The executor just calls a method with no parameters.
*/
class ClientSideStatementNoParamExecutor implements ClientSideStatementExecutor {
private Method method;
private final Method method;

ClientSideStatementNoParamExecutor() {}

@Override
public void compile(ClientSideStatementImpl statement) throws CompileException {
/**
* Creates and compiles the given {@link ClientSideStatementImpl}.
*
* @param statement The statement to compile.
* @throws CompileException If the statement could not be compiled. This should never happen, as
* it would indicate that an invalid statement has been defined in the source file.
*/
ClientSideStatementNoParamExecutor(ClientSideStatementImpl statement) throws CompileException {
try {
this.method = ConnectionStatementExecutor.class.getDeclaredMethod(statement.getMethodName());
} catch (NoSuchMethodException | SecurityException e) {
Expand Down
Expand Up @@ -30,14 +30,20 @@
* AUTOCOMMIT=TRUE.
*/
class ClientSideStatementSetExecutor<T> implements ClientSideStatementExecutor {
private ClientSideStatementImpl statement;
private Method method;
private ClientSideStatementValueConverter<T> converter;
private Pattern allowedValuesPattern;
private final ClientSideStatementImpl statement;
private final Method method;
private final ClientSideStatementValueConverter<T> converter;
private final Pattern allowedValuesPattern;

/**
* Creates and compiles the given {@link ClientSideStatementImpl}.
*
* @param statement The statement to compile.
* @throws CompileException If the statement could not be compiled. This should never happen, as
* it would indicate that an invalid statement has been defined in the source file.
*/
@SuppressWarnings("unchecked")
@Override
public void compile(ClientSideStatementImpl statement) throws CompileException {
ClientSideStatementSetExecutor(ClientSideStatementImpl statement) throws CompileException {
Preconditions.checkNotNull(statement.getSetStatement());
try {
this.statement = statement;
Expand Down