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.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
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;
import nl.topicus.jdbc.util.CloudSpannerConversionUtil;

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 && !keyValues.containsKey(key)) {
        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(convert(value));
    }
    return builder;
  }

  private Object convert(Object value) {
    if (value != null) {
      if (Date.class.isAssignableFrom(value.getClass())) {
        Date dateValue = (Date) value;
        return CloudSpannerConversionUtil.toCloudSpannerDate(dateValue);
      } else if (Timestamp.class.isAssignableFrom(value.getClass())) {
        Timestamp timeValue = (Timestamp) value;
        return CloudSpannerConversionUtil.toCloudSpannerTimestamp(timeValue);
      }
    }
    return value;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy