Search in sources :

Example 31 with SQLExpressionFactory

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

the class StringLength3Method 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 StringLiteral) {
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        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), "LENGTH", 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) 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) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 32 with SQLExpressionFactory

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

the class StringMatchesMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression likeExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = likeExpr.toSQLText();
    sql.clearStatement();
    if (Expression.OP_LIKE.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" LIKE ");
    if (Expression.OP_LIKE.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return likeExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 33 with SQLExpressionFactory

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

the class StringSimilarPostgresqlMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression similarToExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = similarToExpr.toSQLText();
    sql.clearStatement();
    if (OP_SIMILAR_TO.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" SIMILAR TO ");
    if (OP_SIMILAR_TO.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return similarToExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 34 with SQLExpressionFactory

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

the class TemporalDayOfWeekMethod4 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "DAY");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping, "dw"));
    funcArgs.add(invokedExpr);
    // Add one to the SQL "dw" (origin=0) to be compatible with Java Calendar day of week (origin=1)
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "datepart", funcArgs), Expression.OP_ADD, one);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 35 with SQLExpressionFactory

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

the class TemporalDayOfWeekMethod5 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "DAY_OF_WEEK");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping, "%w"));
    funcArgs.add(invokedExpr);
    // Add one to the SQL (origin=0) to be compatible with Java Calendar day of week (origin=1)
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs), Expression.OP_ADD, one);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)111 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)98 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)81 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)49 NucleusException (org.datanucleus.exceptions.NucleusException)42 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)40 ArrayList (java.util.ArrayList)39 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)38 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)35 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)31 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)30 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)28 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)23 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)22 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)20 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)20 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)19 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)17 BigInteger (java.math.BigInteger)16 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)16