com.sap.cds.jdbc.hana.HanaDbContext Maven / Gradle / Ivy
/************************************************************************
* © 2020-2024 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cds.jdbc.hana;
import static com.sap.cds.DataStoreConfiguration.HANA_COMPATIBILITY_MODE;
import static com.sap.cds.DataStoreConfiguration.HANA_COMPATIBILITY_MODE_HEX;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.sap.cds.CdsDataStoreConnector;
import com.sap.cds.CdsDataStoreConnector.Capabilities;
import com.sap.cds.CqnTableFunction;
import com.sap.cds.DataStoreConfiguration;
import com.sap.cds.impl.Context;
import com.sap.cds.jdbc.generic.GenericDbContext;
import com.sap.cds.jdbc.hana.hierarchies.HierarchyFunctionMapper;
import com.sap.cds.jdbc.hana.json.HanaDocStoreDbContext;
import com.sap.cds.jdbc.hana.search.HanaSearchResolver;
import com.sap.cds.jdbc.spi.DbContext;
import com.sap.cds.jdbc.spi.ExceptionAnalyzer;
import com.sap.cds.jdbc.spi.FunctionMapper;
import com.sap.cds.jdbc.spi.PredicateMapper;
import com.sap.cds.jdbc.spi.ScalarValueResolver;
import com.sap.cds.jdbc.spi.SearchResolver;
import com.sap.cds.jdbc.spi.SessionVariableSetter;
import com.sap.cds.jdbc.spi.StatementResolver;
import com.sap.cds.jdbc.spi.TableFunctionMapper;
import com.sap.cds.jdbc.spi.ValueBinder;
import com.sap.cds.ql.hana.CqnHierarchy;
import com.sap.cds.reflect.CdsModel;
public final class HanaDbContext extends GenericDbContext {
public static final String USE_HEX_PLAN = "hdb.USE_HEX_PLAN";
public static final String NO_USE_HEX_PLAN = "hdb.NO_USE_HEX_PLAN";
private final int majorVersion;
private boolean optimizeForHexEngine;
public HanaDbContext(DataStoreConfiguration dataStoreConfiguration, int majorVersion) {
super(dataStoreConfiguration);
this.majorVersion = majorVersion;
this.optimizeForHexEngine = majorVersion >= 4
&& HANA_COMPATIBILITY_MODE_HEX.equals(config.getProperty(HANA_COMPATIBILITY_MODE));
}
@Override
public SearchResolver createSearchResolver(CdsModel model, Locale locale, Map hints) {
if (hints.get(NO_USE_HEX_PLAN) == Boolean.TRUE) {
return HanaSearchResolver.forLegacyEngine(config, model, locale);
}
if (optimizeForHexEngine || hints.get(USE_HEX_PLAN) == Boolean.TRUE) {
return HanaSearchResolver.forHexEngine(config, model, locale);
}
return HanaSearchResolver.forLegacyEngine(config, model, locale);
}
@Override
public StatementResolver getStatementResolver() {
return new HanaStatementResolver(config, majorVersion, optimizeForHexEngine);
}
@Override
public PredicateMapper getPredicateMapper() {
return new HanaPredicateMapper();
}
@Override
public ExceptionAnalyzer getExceptionAnalyzer() {
return new HanaExceptionAnalyzer();
}
@Override
public Capabilities getCapabilities() {
return CdsDataStoreConnector.Capabilities.ALL;
}
@Override
public ValueBinder getBinder(TimeZone timeZone) {
return new HanaBinder(timeZone);
}
@Override
public SessionVariableSetter getSessionVariableSetter() {
return new HanaSessionVariableSetter();
}
@Override
public FunctionMapper getFunctionMapper() {
return new HanaFunctionMapper();
}
@Override
public DbContext subDialect(String name) {
if ("JsonDocStore".equalsIgnoreCase(name)) {
return new HanaDocStoreDbContext(this);
}
return this;
}
@Override
public ScalarValueResolver getScalarValueResolver() {
return HanaScalarValueResolver.INSTANCE;
}
@SuppressWarnings("unchecked")
@Override
public TableFunctionMapper getTableFunctionMapper(Object context, @SuppressWarnings("rawtypes") List params,
CqnTableFunction tableFunction) {
if (tableFunction instanceof CqnHierarchy hierarchy) {
return HierarchyFunctionMapper.create((Context) context, params, hierarchy);
}
return super.getTableFunctionMapper(context, params, tableFunction);
}
}