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

com.sap.cds.impl.docstore.DocStoreDeleteStatementBuilder Maven / Gradle / Ivy

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

import static com.sap.cds.impl.sql.SpaceSeparatedCollector.joining;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.sap.cds.impl.Context;
import com.sap.cds.impl.PreparedCqnStmt;
import com.sap.cds.impl.sql.SQLStatementBuilder;
import com.sap.cds.impl.sql.TokenToSQLTransformer;
import com.sap.cds.jdbc.spi.SqlMapping;
import com.sap.cds.ql.cqn.CqnDelete;
import com.sap.cds.reflect.CdsEntity;

public class DocStoreDeleteStatementBuilder implements DocStoreStatementBuilder {
	private final CqnDelete delete;
	private final TokenToSQLTransformer toSql;
	private final SqlMapping sqlMapping;
	private final String tableName;
	private final CdsEntity entity;
	private final List params = new ArrayList<>();

	public DocStoreDeleteStatementBuilder(Context context, CqnDelete delete) {
		this.delete = delete;
		this.entity = context.getCdsModel().getEntity(this.delete.ref().firstSegment());
		this.sqlMapping = context.getDbContext().getSqlMapping(entity);
		this.tableName = this.sqlMapping.tableName();
		this.toSql = new TokenToSQLTransformer(context, sqlMapping::jsonObjectPath, entity, this.tableName, params,
				DocStoreUtils::valueToParamCastExpression);
	}

	@Override
	public SQLStatementBuilder.SQLStatement build() {
		Stream.Builder builder = Stream.builder();
		builder.add("DELETE FROM");
		builder.add(tableName);

		delete.where().map(toSql::toSQL).ifPresent(whereSql -> {
			builder.add("WHERE");
			builder.add(whereSql);
		});
		String sql = builder.build().collect(joining());

		return new SQLStatementBuilder.SQLStatement(sql, params);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy