nl.topicus.jdbc.statement.DeleteWorker 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.List;
import java.util.stream.Collectors;
import nl.topicus.jdbc.shaded.com.google.rpc.Code;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.JSQLParserException;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.parser.CCJSqlParserUtil;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Table;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.delete.Delete;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.Select;
import nl.topicus.jdbc.CloudSpannerConnection;
import nl.topicus.jdbc.CloudSpannerDriver;
import nl.topicus.jdbc.MetaDataStore.TableKeyMetaData;
import nl.topicus.jdbc.exception.CloudSpannerSQLException;
public class DeleteWorker extends AbstractTablePartWorker
{
final Delete delete;
public DeleteWorker(CloudSpannerConnection connection, Delete delete, boolean allowExtendedMode) throws SQLException
{
super(connection, createSelect(connection, delete), allowExtendedMode, DMLOperation.Delete);
this.delete = delete;
}
private static Select createSelect(CloudSpannerConnection connection, Delete delete) throws SQLException
{
TableKeyMetaData table = connection.getTable(CloudSpannerDriver.unquoteIdentifier(delete.getTable().getName()));
List keyCols = table.getKeyColumns().stream()
.map(x -> CloudSpannerDriver.quoteIdentifier(delete.getTable().getName()) + "."
+ CloudSpannerDriver.quoteIdentifier(x))
.collect(Collectors.toList());
StringBuilder sql = new StringBuilder();
sql.append("SELECT ").append(String.join(", ", keyCols));
sql.append("\nFROM ").append(CloudSpannerDriver.quoteIdentifier(delete.getTable().getName()));
sql.append("\nWHERE ").append(delete.getWhere().toString());
try
{
return (Select) CCJSqlParserUtil.parse(sql.toString());
}
catch (JSQLParserException e)
{
throw new CloudSpannerSQLException("Could not parse generated SELECT statement: " + sql,
Code.INVALID_ARGUMENT);
}
}
@Override
protected List getColumnNames() throws SQLException
{
String unquotedTableName = CloudSpannerDriver.unquoteIdentifier(getTable().getName());
return ConverterUtils.getQuotedColumnNames(connection, null, null, unquotedTableName);
}
@Override
protected String createSQL() throws SQLException
{
TableKeyMetaData table = connection.getTable(CloudSpannerDriver.unquoteIdentifier(getTable().getName()));
List keyCols = table.getKeyColumns().stream().map(x -> CloudSpannerDriver.quoteIdentifier(x))
.collect(Collectors.toList());
String sql = "DELETE FROM " + CloudSpannerDriver.quoteIdentifier(delete.getTable().getName()) + " WHERE ";
boolean first = true;
for (String key : keyCols)
{
if (!first)
sql = sql + " AND ";
sql = sql + key + "=?";
first = false;
}
return sql;
}
@Override
protected Table getTable()
{
return delete.getTable();
}
}