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

com.sap.cds.impl.sql.DeleteStatementBuilder Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/************************************************************************
 * © 2019-2023 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cds.impl.sql;

import static com.sap.cds.impl.sql.SQLStatementBuilder.aliasResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import com.sap.cds.impl.Context;
import com.sap.cds.impl.PreparedCqnStmt.Parameter;
import com.sap.cds.jdbc.spi.SqlMapping;
import com.sap.cds.ql.cqn.CqnDelete;
import com.sap.cds.ql.cqn.CqnElementRef;
import com.sap.cds.reflect.CdsEntity;

public class DeleteStatementBuilder implements SQLStatementBuilder {

	private final CqnDelete delete;
	private final SqlMapping sqlMapping;
	private final String tableName;
	private final TokenToSQLTransformer toSQL;
	private final List params = new ArrayList<>();

	public DeleteStatementBuilder(Context context, CqnDelete delete) {
		this.delete = delete;
		CdsEntity entity = context.getCdsModel().getEntity(delete.ref().firstSegment());
		sqlMapping = context.getDbContext().getSqlMapping(entity);
		tableName = sqlMapping.tableName();
		Function aliasResolver = aliasResolver(context, entity);
		toSQL = new TokenToSQLTransformer(context, aliasResolver, entity, tableName, params);
	}

	public SQLStatement build() {
		StringBuilder builder = new StringBuilder();
		builder.append("DELETE FROM ");
		builder.append(tableName);
		delete.where().map(toSQL::toSQL).ifPresent(s -> {
			builder.append(" WHERE ");
			builder.append(s);
		});

		String sql = builder.toString();

		return new SQLStatement(sql, params);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy