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

com.sap.cds.impl.docstore.DocStoreUtils 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 java.util.Collections;

import com.sap.cds.impl.Context;
import com.sap.cds.impl.PreparedCqnStmt;
import com.sap.cds.impl.sql.SQLStatementBuilder;
import com.sap.cds.ql.cqn.CqnInsert;
import com.sap.cds.ql.cqn.CqnSelect;
import com.sap.cds.ql.cqn.CqnStatement;
import com.sap.cds.ql.cqn.CqnStructuredTypeRef;
import com.sap.cds.reflect.CdsBaseType;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.reflect.CdsStructuredType;
import com.sap.cds.util.CdsModelUtils;

public class DocStoreUtils {

	private final CdsModel model;

	public DocStoreUtils(CdsModel model) {
		this.model = model;
	}

	static String hanaDocStoreTypeFromCdsBaseType(CdsBaseType cdsBaseType) {
		switch (cdsBaseType) {
		case INTEGER:
		case INTEGER64:
			return "BIGINT";
		case DECIMAL:
		case DOUBLE:
			return "DOUBLE";
		case BOOLEAN:
			return "BOOLEAN";
		default:
			return "NVARCHAR";
		}
	}

	static String valueToParamCastExpression(PreparedCqnStmt.Parameter param) {
		return String.format("CAST(? AS %s)", hanaDocStoreTypeFromCdsBaseType(param.type()));
	}

	public static SQLStatementBuilder.SQLStatement getDocStoreInsertStatement(CqnInsert insert, Context context) {
		CdsEntity entity = CdsModelUtils.entity(context.getCdsModel(), insert.ref());
		String sql = String.format("INSERT INTO %s VALUES(?)",
				context.getTableNameResolver().tableName(entity));
		return new SQLStatementBuilder.SQLStatement(sql, Collections.singletonList(new PreparedCqnStmt.JsonParam()));
	}

	public boolean targetsDocStore(CqnStatement statement) {
		if (statement.isSelect()) {
			return targetsDocStore(statement.asSelect());
		}

		return targetsDocStore(statement.ref());
	}

	private boolean targetsDocStore(CqnStructuredTypeRef ref) {
		CdsEntity entity = CdsModelUtils.entity(model, ref);

		return targetsDocStore(entity);
	}

	boolean targetsDocStore(CqnSelect query) {
		if (!query.from().isRef()) {
			return false;
		}

		return targetsDocStore(query.ref());
	}

	public static boolean targetsDocStore(CdsStructuredType entity) {
		if (entity == null) {
			return false;
		}
		return entity.getAnnotationValue("cds.persistence.docstore", false);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy