Search in sources :

Example 61 with SQLExpression

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

the class StringIndexOfMethod 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 (args == null || args.size() == 0 || args.size() > 2) {
        throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    // {stringExpr}.indexOf(strExpr1 [,numExpr2])
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(substrExpr);
    funcArgs.add(expr);
    if (args.size() == 2) {
        SQLExpression fromExpr = args.get(1);
        if (!(fromExpr instanceof NumericExpression)) {
            throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
        }
        funcArgs.add(fromExpr.add(one));
    }
    NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "LOCATE", funcArgs);
    return new NumericExpression(locateExpr, Expression.OP_SUB, one).encloseInParentheses();
}
Also used : ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) 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) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 62 with SQLExpression

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

the class StringStartsWith3Method 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 (args == null || args.size() == 0 || args.size() > 2) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    // {stringExpr}.indexOf(strExpr1 [,numExpr2])
    ArrayList funcArgs = new ArrayList();
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    if (args.size() > 1) {
        SQLExpression numExpr = args.get(1);
        funcArgs.add(substrExpr);
        funcArgs.add(expr);
        return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one.add(numExpr));
    }
    funcArgs.add(substrExpr);
    funcArgs.add(expr);
    return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one);
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 63 with SQLExpression

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

the class StringStartsWithMethod 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 (args == null || args.size() == 0 || args.size() > 2) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    if (args.size() > 1) {
        // TODO Use numExpr in a SUBSTRING
        if (substrExpr.isParameter()) {
            // Any pattern expression cannot be a parameter here
            SQLLiteral substrLit = (SQLLiteral) substrExpr;
            stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
            if (substrLit.getValue() == null) {
                return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
            }
        }
        SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
        return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
    }
    // Create a new StringExpression and manually update its SQL
    if (substrExpr.isParameter()) {
        // Any pattern expression cannot be a parameter here
        SQLLiteral substrLit = (SQLLiteral) substrExpr;
        stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
        if (substrLit.getValue() == null) {
            return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
        }
    }
    SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
    return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 64 with SQLExpression

use of org.datanucleus.store.rdbms.sql.expression.SQLExpression 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 65 with SQLExpression

use of org.datanucleus.store.rdbms.sql.expression.SQLExpression 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

SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)199 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)98 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)95 ArrayList (java.util.ArrayList)75 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)74 NucleusException (org.datanucleus.exceptions.NucleusException)63 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)56 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)47 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)44 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)41 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)40 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)35 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)34 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)34 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)34 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)31 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)29 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)28 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)28 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)25