All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.topicus.jdbc.statement.DeleteKeyBuilder Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy