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

com.sap.cds.ql.impl.DeleteBuilder Maven / Gradle / Ivy

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

import static com.sap.cds.impl.builder.model.StructuredTypeImpl.structuredType;
import static com.sap.cds.impl.builder.model.StructuredTypeRefImpl.typeRef;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.sap.cds.impl.builder.model.ExpressionImpl;
import com.sap.cds.impl.builder.model.StructuredTypeProxy;
import com.sap.cds.impl.parser.token.AbstractJSONizable;
import com.sap.cds.impl.parser.token.Jsonizer;
import com.sap.cds.ql.Delete;
import com.sap.cds.ql.StructuredType;
import com.sap.cds.ql.StructuredTypeRef;
import com.sap.cds.ql.cqn.CqnDelete;
import com.sap.cds.ql.cqn.CqnModifier;
import com.sap.cds.ql.cqn.CqnPredicate;
import com.sap.cds.ql.cqn.CqnStructuredTypeRef;

public class DeleteBuilder> extends AbstractJSONizable implements Delete {

	private final T entity;
	private CqnPredicate where;

	private DeleteBuilder(T entity) {
		this.entity = entity;
	}

	public static > DeleteBuilder from(T entity) {
		return new DeleteBuilder<>(entity);
	}

	public static > DeleteBuilder from(Class entity) {
		return from(StructuredTypeProxy.create(entity));
	}

	public static DeleteBuilder> from(CqnStructuredTypeRef ref) {
		return from(structuredType(ref));
	}

	public static DeleteBuilder> from(String entityName) {
		return from(typeRef(entityName));
	}

	public static DeleteBuilder> copy(CqnDelete delete) {
		return copy(delete, ExpressionVisitor.COPY);
	}

	public static DeleteBuilder> copy(CqnDelete delete, CqnModifier modifier) {
		StructuredTypeRef ref = ExpressionVisitor.copy(delete.ref(), modifier);
		DeleteBuilder> copy = from(ref);
		copy.where = modifier.where(ExpressionVisitor.copy(delete.where(), modifier));

		return copy;
	}

	@Override
	@SuppressWarnings("unchecked")
	public DeleteBuilder where(Function xpr) {
		where(xpr.apply((T) entity.getType()));

		return this;
	}

	@Override
	public DeleteBuilder matching(Map values) {
		return where(ExpressionImpl.matching(values));
	}

	@Override
	public DeleteBuilder byId(Object idValue) {
		return where(ExpressionImpl.byId(idValue));
	}

	@Override
	public DeleteBuilder where(CqnPredicate pred) {
		where = pred;

		return this;
	}

	@Override
	public Optional where() {
		return Optional.ofNullable(where);
	}

	@Override
	public String toJson() {
		Jsonizer cqn = Jsonizer.object("from", ref());

		where().ifPresent(w -> {
			ArrayNode whereArr = cqn.array("where");
			w.tokens().forEach(whereArr::addPOJO);
		});

		return Jsonizer.object("DELETE", cqn).toJson();
	}

	public CqnDelete build() {
		return this;
	}

	@Override
	public CqnStructuredTypeRef ref() {
		return typeRef(entity);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy