com.sap.cds.impl.docstore.DocStoreSelectStatementBuilder Maven / Gradle / Ivy
/*******************************************************************
* © 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);
}
}