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

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

There is a newer version: 3.8.0
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.sap.cds.impl.parser.token.AbstractJSONizable;
import com.sap.cds.impl.parser.token.Jsonizer;
import com.sap.cds.ql.Insert;
import com.sap.cds.ql.StructuredType;
import com.sap.cds.ql.StructuredTypeRef;
import com.sap.cds.ql.Upsert;
import com.sap.cds.ql.cqn.CqnInsert;
import com.sap.cds.ql.cqn.CqnModifier;
import com.sap.cds.ql.cqn.CqnStructuredTypeRef;
import com.sap.cds.ql.cqn.CqnUpsert;
import com.sap.cds.ql.cqn.CqnVisitor;
import com.sap.cds.ql.cqn.CqnXsert;
import com.sap.cds.util.DataUtils;

public class XsertBuilder extends AbstractJSONizable {

	private final CqnXsert.Kind kind;
	private final StructuredType entity;
	private final List> entries = new ArrayList<>();

	private XsertBuilder(CqnXsert.Kind kind, StructuredType entity) {
		this.kind = kind;
		this.entity = entity;
	}

	public static Insert insert(CqnStructuredTypeRef ref) {
		return new InsertBuilder(structuredType(ref));
	}

	public static Insert insert(StructuredType entity) {
		return new InsertBuilder(entity);
	}

	public static Upsert upsert(CqnStructuredTypeRef ref) {
		return new UpsertBuilder(structuredType(ref));
	}

	public static Upsert upsert(StructuredType entity) {
		return new UpsertBuilder(entity);
	}

	public static Insert copy(CqnInsert insert) {
		return copy(insert, ExpressionVisitor.COPY);
	}

	public static Insert copy(CqnInsert insert, CqnModifier modifier) {
		StructuredTypeRef ref = ExpressionVisitor.copy(insert.ref(), modifier);
		return insert(ref).entries(insert.entries());
	}

	public static Upsert copy(CqnUpsert upsert) {
		return copy(upsert, ExpressionVisitor.COPY);
	}

	public static Upsert copy(CqnUpsert upsert, CqnModifier modifier) {
		StructuredTypeRef ref = ExpressionVisitor.copy(upsert.ref(), modifier);
		return upsert(ref).entries(upsert.entries());
	}

	public XsertBuilder setEntries(Iterable> entries) {
		this.entries.clear();
		entries.forEach(this::addEntry);

		return this;
	}

	protected XsertBuilder addEntry(String key, Object value) {
		addEntry(Collections.singletonMap(key, value));

		return this;
	}

	protected XsertBuilder addEntry(Map entry) {
		this.entries.add(DataUtils.copyMap(entry));

		return this;
	}

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

	public List> entries() {
		return entries; // NOSONAR
	}

	public Stream elements() {
		Set all = new TreeSet<>();
		for (Map map : entries) {
			all.addAll(map.keySet());
		}

		return all.stream();
	}

	@Override
	public String toJson() {
		Jsonizer cqn = Jsonizer.object("into", entity);
		ArrayNode data = cqn.array("entries");
		entries.forEach(data::addPOJO);

		return Jsonizer.object(kind.name(), cqn).toJson();
	}

	private static class InsertBuilder extends XsertBuilder implements Insert {
		public InsertBuilder(StructuredType type) {
			super(CqnXsert.Kind.INSERT, type);
		}

		@Override
		public Insert entries(Iterable> entries) {
			setEntries(entries);
			return this;
		}

		@Override
		public Insert entry(Map entry) {
			addEntry(entry);
			return this;
		}

		@Override
		public Insert entry(String key, Object value) {
			addEntry(key, value);
			return this;
		}

		@Override
		public void accept(CqnVisitor visitor) {
			visitor.visit(this);
		}
	}

	private static class UpsertBuilder extends XsertBuilder implements Upsert {
		public UpsertBuilder(StructuredType type) {
			super(CqnXsert.Kind.UPSERT, type);
		}

		@Override
		public Upsert entries(Iterable> entries) {
			setEntries(entries);
			return this;
		}

		@Override
		public Upsert entry(Map entry) {
			addEntry(entry);
			return this;
		}

		@Override
		public Upsert entry(String key, Object value) {
			addEntry(key, value);
			return this;
		}

		@Override
		public void accept(CqnVisitor visitor) {
			visitor.visit(this);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy