nl.topicus.jdbc.statement.DeleteKeyBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
package nl.topicus.jdbc.statement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import nl.topicus.jdbc.shaded.com.google.cloud.spanner.Key;
import nl.topicus.jdbc.shaded.com.google.rpc.Code;
import nl.topicus.jdbc.MetaDataStore.TableKeyMetaData;
import nl.topicus.jdbc.exception.CloudSpannerSQLException;
public class DeleteKeyBuilder
{
private final TableKeyMetaData table;
private final boolean generateParameterMetaData;
private Map keyValues = new HashMap<>();
private String currentColumn = null;
public DeleteKeyBuilder(TableKeyMetaData table, boolean generateParameterMetaData)
{
this.table = table;
this.generateParameterMetaData = generateParameterMetaData;
}
public void set(String column)
{
currentColumn = column.toUpperCase();
}
public void to(Object value)
{
if (currentColumn == null)
throw new IllegalArgumentException("No column set");
keyValues.put(currentColumn, value);
currentColumn = null;
}
public Key.Builder getKeyBuilder() throws SQLException
{
Key.Builder builder = Key.newBuilder();
for (String key : table.getKeyColumns())
{
Object value = keyValues.get(key);
if (!generateParameterMetaData && value == null)
{
throw new CloudSpannerSQLException(
"No value supplied for key column " + key
+ ". All key columns must be specified in the WHERE-clause of a DELETE-statement.",
Code.INVALID_ARGUMENT);
}
builder.appendObject(value);
}
return builder;
}
}