Search in sources :

Example 1 with EnumLiteral

use of org.datanucleus.store.rdbms.sql.expression.EnumLiteral in project datanucleus-rdbms by datanucleus.

the class EnumToStringMethod method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    if (expr instanceof EnumLiteral) {
        Enum val = (Enum) ((EnumLiteral) expr).getValue();
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return new StringLiteral(stmt, exprFactory.getMappingForType(String.class, false), val.toString(), null);
    } else if (expr instanceof EnumExpression) {
        EnumExpression enumExpr = (EnumExpression) expr;
        JavaTypeMapping m = enumExpr.getJavaTypeMapping();
        if (m.getJavaTypeForDatastoreMapping(0).equals(ClassNameConstants.JAVA_LANG_STRING)) {
            return enumExpr.getDelegate();
        }
        throw new NucleusException("EnumExpression.toString is not supported when the enum is stored as a numeric");
    } else {
        throw new NucleusException(Localiser.msg("060001", "ordinal", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EnumExpression(org.datanucleus.store.rdbms.sql.expression.EnumExpression) EnumLiteral(org.datanucleus.store.rdbms.sql.expression.EnumLiteral) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 EnumExpression (org.datanucleus.store.rdbms.sql.expression.EnumExpression)1 EnumLiteral (org.datanucleus.store.rdbms.sql.expression.EnumLiteral)1 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)1 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)1