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

com.sap.cds.impl.parser.UpdateParser 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.impl.parser;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.TypeRef;
import com.sap.cds.ql.StructuredType;
import com.sap.cds.ql.Update;
import com.sap.cds.ql.cqn.CqnPredicate;
import com.sap.cds.ql.cqn.CqnStructuredTypeRef;
import com.sap.cds.ql.impl.UpdateBuilder;

public class UpdateParser extends CqnParser {

	private static final TypeRef>> LIST_OF_MAPS = new TypeRef>>() {
	};
	private static final TypeRef> MAP = new TypeRef>() {
	};

	private UpdateParser(DocumentContext cqn) {
		super(cqn);
	}

	public static UpdateParser of(String cqn) {
		return new UpdateParser(getDocumentContext(cqn));
	}

	public static Update> parse(String cqn) {
		return of(cqn).parse();
	}

	public Update> parse() {
		CqnStructuredTypeRef ref = TokenParser.ref(cqn.read("$.UPDATE.entity"));
		UpdateBuilder> update = UpdateBuilder.entity(ref);
		readData().ifPresent(update::data);
		readEntries().ifPresent(update::entries);

		return update.where(where());
	}

	private CqnPredicate where() {
		JsonNode where = cqn.read("$.UPDATE.where");
		return ExpressionParser.parsePredicate(where);
	}

	private Optional> readData() {
		Map data = cqn.read("$.UPDATE.data", MAP);

		return Optional.ofNullable(data);
	}

	private Optional>> readEntries() {
		List> entries = cqn.read("$.UPDATE.entries", LIST_OF_MAPS);

		return Optional.ofNullable(entries);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy