Search in sources :

Example 1 with FirebirdAdapter

use of org.datanucleus.store.rdbms.adapter.FirebirdAdapter in project datanucleus-rdbms by datanucleus.

the class StringLength2Method 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) {
    DatastoreAdapter dba = stmt.getDatastoreAdapter();
    if (!(dba instanceof FirebirdAdapter)) {
        throw new NucleusException("StringLength2Method being used for evaluation of String.length yet this is for Firebird ONLY. Please report this");
    }
    FirebirdAdapter fba = (FirebirdAdapter) dba;
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (fba.supportsCharLengthFunction()) {
        // Firebird v2+ support CHAR_LENGTH
        if (!expr.isParameter() && expr instanceof StringLiteral) {
            JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
            String val = (String) ((StringLiteral) expr).getValue();
            return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
        } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
            ArrayList funcArgs = new ArrayList();
            funcArgs.add(expr);
            return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHAR_LENGTH", funcArgs);
        } else {
            throw new NucleusException(Localiser.msg("060001", "length", expr));
        }
    }
    if (expr instanceof StringLiteral) {
        // Firebird v1 requires STRLEN
        JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
        String val = (String) ((StringLiteral) expr).getValue();
        return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
    } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
        ArrayList funcArgs = new ArrayList();
        funcArgs.add(expr);
        return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "STRLEN", funcArgs);
    } else {
        throw new NucleusException(Localiser.msg("060001", "length", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) FirebirdAdapter(org.datanucleus.store.rdbms.adapter.FirebirdAdapter) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Aggregations

ArrayList (java.util.ArrayList)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)1 FirebirdAdapter (org.datanucleus.store.rdbms.adapter.FirebirdAdapter)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)1 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)1 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)1 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)1 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)1 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)1