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

nl.topicus.jdbc.statement.DeleteWorker 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.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();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy