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

com.sap.cds.impl.docstore.DocStoreSelectStatementBuilder 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.CqnSelect;
import com.sap.cds.reflect.CdsEntity;

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

	public DocStoreSelectStatementBuilder(Context context, CqnSelect select) {
		this.entity = context.getCdsModel().getEntity(select.ref().firstSegment());
		this.select = select;
		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("SELECT * FROM");
		builder.add(tableName);

		select.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