com.sap.cds.jdbc.hana.json.HanaDocStoreScalarValueResolver Maven / Gradle / Ivy
/*******************************************************************
* © 2024 SAP SE or an SAP affiliate company. All rights reserved. *
*******************************************************************/
package com.sap.cds.jdbc.hana.json;
import com.sap.cds.jdbc.generic.GenericScalarValueResolver;
import com.sap.cds.jdbc.spi.ScalarValueResolver;
import com.sap.cds.ql.cqn.CqnBooleanLiteral;
import com.sap.cds.ql.cqn.CqnLiteral;
import com.sap.cds.ql.cqn.CqnParameter;
import com.sap.cds.reflect.CdsBaseType;
public class HanaDocStoreScalarValueResolver extends GenericScalarValueResolver {
private static final String JSON_TRUE = "TO_JSON_BOOLEAN(TRUE)";
private static final String JSON_FALSE = "TO_JSON_BOOLEAN(FALSE)";
public static final ScalarValueResolver INSTANCE = new HanaDocStoreScalarValueResolver();
private HanaDocStoreScalarValueResolver() {
// empty
}
@Override
public String literal(CqnBooleanLiteral bool) {
return Boolean.TRUE.equals(bool.value()) ? JSON_TRUE : JSON_FALSE;
}
@Override
public String literal(CqnLiteral> val) {
return val.type().map(cdsType -> cast(toDocStoreType(cdsType))).orElse("?");
}
@Override
public String parameter(CqnParameter param) {
return param.type().map(cdsType -> cast(toDocStoreType(cdsType))).orElse("?");
}
@Override
public String parameter(CdsBaseType baseType) {
return cast(toDocStoreType(baseType));
}
private static String cast(String docStoreType) {
return String.format("CAST(? AS %s)", docStoreType);
}
private static String toDocStoreType(String cdsType) {
return toDocStoreType(CdsBaseType.cdsType(cdsType));
}
private static String toDocStoreType(CdsBaseType baseType) {
return switch (baseType) {
case INTEGER, INTEGER64 -> "BIGINT";
case DECIMAL, DOUBLE -> "DOUBLE";
case BOOLEAN -> "BOOLEAN";
default -> "NVARCHAR";
};
}
}