Search in sources :

Example 36 with BooleanExpression

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

the class MapContainsValueMethod 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() > 1) {
        throw new NucleusException(Localiser.msg("060016", "containsValue", "MapExpression", 1));
    }
    MapExpression mapExpr = (MapExpression) expr;
    SQLExpression valExpr = args.get(0);
    if (valExpr.isParameter()) {
        // Value is a parameter so make sure its type is set
        AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
        if (mmd != null && mmd.getMap() != null) {
            Class valCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getValueType());
            stmt.getQueryGenerator().bindParameter(valExpr.getParameterName(), valCls);
        }
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (mapExpr instanceof MapLiteral) {
        MapLiteral lit = (MapLiteral) mapExpr;
        Map map = (Map) lit.getValue();
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
        if (map == null || map.size() == 0) {
            return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
        }
        boolean useInExpression = false;
        List<SQLExpression> mapValExprs = lit.getValueLiteral().getValueExpressions();
        if (mapValExprs != null && !mapValExprs.isEmpty()) {
            // Make sure the the map key(s) are compatible with the keyExpr
            boolean incompatible = true;
            Class elemtype = clr.classForName(valExpr.getJavaTypeMapping().getType());
            Iterator<SQLExpression> mapKeyExprIter = mapValExprs.iterator();
            while (mapKeyExprIter.hasNext()) {
                SQLExpression mapKeyExpr = mapKeyExprIter.next();
                Class mapKeyType = clr.classForName(mapKeyExpr.getJavaTypeMapping().getType());
                if (valueTypeCompatible(elemtype, mapKeyType)) {
                    incompatible = false;
                    break;
                }
            }
            if (incompatible) {
                // The provided element type isn't assignable to any of the input collection elements!
                return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
            }
            // Check if we should compare using an "IN (...)" expression
            SQLExpression mapKeyExpr = mapValExprs.get(0);
            if (mapKeyExpr instanceof StringExpression || mapKeyExpr instanceof NumericExpression || mapKeyExpr instanceof TemporalExpression || mapKeyExpr instanceof CharacterExpression || mapKeyExpr instanceof EnumExpression) {
                useInExpression = true;
            }
        }
        if (useInExpression) {
            // Return "key IN (val1, val2, ...)"
            SQLExpression[] exprs = (mapValExprs != null ? mapValExprs.toArray(new SQLExpression[mapValExprs.size()]) : null);
            return new InExpression(valExpr, exprs);
        }
        // TODO If valExpr is a parameter and mapExpr is derived from a parameter ?
        MapValueLiteral mapValueLiteral = lit.getValueLiteral();
        BooleanExpression bExpr = null;
        List<SQLExpression> elementExprs = mapValueLiteral.getValueExpressions();
        for (int i = 0; i < elementExprs.size(); i++) {
            if (bExpr == null) {
                bExpr = (elementExprs.get(i)).eq(valExpr);
            } else {
                bExpr = bExpr.ior((elementExprs.get(i)).eq(valExpr));
            }
        }
        if (bExpr != null) {
            bExpr.encloseInParentheses();
        }
        return bExpr;
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        boolean useSubquery = getNeedsSubquery(stmt);
        JoinType joinType = JoinType.INNER_JOIN;
        if (valExpr instanceof UnboundExpression) {
            // See if the user has defined what should be used
            String varName = ((UnboundExpression) valExpr).getVariableName();
            String extensionName = "datanucleus.query.jdoql." + varName + ".join";
            String extensionValue = (String) stmt.getQueryGenerator().getValueForExtension(extensionName);
            if (extensionValue != null) {
                if (extensionValue.equalsIgnoreCase("SUBQUERY")) {
                    useSubquery = true;
                } else if (extensionValue.equalsIgnoreCase("INNERJOIN")) {
                    useSubquery = false;
                } else if (extensionValue.equalsIgnoreCase("LEFTOUTERJOIN")) {
                    joinType = JoinType.LEFT_OUTER_JOIN;
                }
            }
        }
        // TODO Check if *this* "containsValue" is negated, not any of them (and remove above check)
        if (useSubquery) {
            return containsAsSubquery(stmt, mapExpr, valExpr);
        }
        return containsAsJoin(stmt, mapExpr, valExpr, joinType);
    }
    return containsAsSubquery(stmt, mapExpr, valExpr);
}
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) InExpression(org.datanucleus.store.rdbms.sql.expression.InExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) JoinType(org.datanucleus.store.rdbms.sql.SQLJoin.JoinType) EnumExpression(org.datanucleus.store.rdbms.sql.expression.EnumExpression) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression) MapLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) MapValueLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral.MapValueLiteral) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) Map(java.util.Map)

Example 37 with BooleanExpression

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

the class SQLBooleanMethod 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 ignore, List<SQLExpression> args) {
    if (args == null || args.size() != 1) {
        throw new NucleusUserException("Cannot invoke SQL_boolean() without a string argument");
    }
    SQLExpression expr = args.get(0);
    if (!(expr instanceof StringLiteral)) {
        throw new NucleusUserException("Cannot use SQL_boolean() without string argument");
    }
    String sql = (String) ((StringLiteral) expr).getValue();
    JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(boolean.class, false);
    BooleanExpression retExpr = new BooleanExpression(stmt, m, sql);
    return retExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 38 with BooleanExpression

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

the class StringStartsWith2Method 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])
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    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"));
    }
    if (args.size() == 2) {
        NumericExpression numExpr = (NumericExpression) args.get(1);
        funcArgs.add(substrExpr);
        funcArgs.add(expr);
        return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHARINDEX", funcArgs), Expression.OP_EQ, one.add(numExpr));
    }
    funcArgs.add(substrExpr);
    funcArgs.add(expr);
    return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHARINDEX", 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) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 39 with BooleanExpression

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

the class CollectionContainsMethod 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() > 1) {
        throw new NucleusException(Localiser.msg("060016", "contains", "CollectionExpression", 1));
    }
    CollectionExpression collExpr = (CollectionExpression) expr;
    AbstractMemberMetaData mmd = collExpr.getJavaTypeMapping().getMemberMetaData();
    SQLExpression elemExpr = args.get(0);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (elemExpr.isParameter()) {
        // Element is a parameter so make sure its type is set
        if (mmd != null && mmd.getCollection() != null) {
            Class elementCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getCollection().getElementType());
            stmt.getQueryGenerator().bindParameter(elemExpr.getParameterName(), elementCls);
        }
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    if (collExpr instanceof CollectionLiteral) {
        // Literal collection
        CollectionLiteral lit = (CollectionLiteral) collExpr;
        Collection coll = (Collection) lit.getValue();
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
        if (coll == null || coll.isEmpty()) {
            return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
        }
        if (collExpr.isParameter()) {
            stmt.getQueryGenerator().useParameterExpressionAsLiteral((CollectionLiteral) collExpr);
        }
        boolean useInExpression = false;
        List<SQLExpression> collElementExprs = lit.getElementExpressions();
        if (collElementExprs != null && !collElementExprs.isEmpty()) {
            // Make sure the the collection element(s) are compatible with the elemExpr
            boolean incompatible = true;
            Class elemtype = clr.classForName(elemExpr.getJavaTypeMapping().getType());
            Iterator<SQLExpression> collElementExprIter = collElementExprs.iterator();
            while (collElementExprIter.hasNext()) {
                SQLExpression collElementExpr = collElementExprIter.next();
                Class collElemType = clr.classForName(collElementExpr.getJavaTypeMapping().getType());
                if (elementTypeCompatible(elemtype, collElemType)) {
                    incompatible = false;
                    break;
                }
            }
            if (incompatible) {
                // The provided element type isn't assignable to any of the input collection elements!
                return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
            }
            // Check if we should compare using an "IN (...)" expression
            SQLExpression collElementExpr = collElementExprs.get(0);
            if (collElementExpr instanceof StringExpression || collElementExpr instanceof NumericExpression || collElementExpr instanceof TemporalExpression || collElementExpr instanceof CharacterExpression || collElementExpr instanceof EnumExpression) {
                useInExpression = true;
            }
        }
        if (useInExpression) {
            // Return "elem IN (val1, val2, ...)"
            SQLExpression[] exprs = (collElementExprs != null ? collElementExprs.toArray(new SQLExpression[collElementExprs.size()]) : null);
            return new InExpression(elemExpr, exprs);
        }
        // Return "elem == val1 || elem == val2 || elem == val3 ..."
        BooleanExpression bExpr = null;
        if (collElementExprs != null) {
            for (int i = 0; i < collElementExprs.size(); i++) {
                if (bExpr == null) {
                    bExpr = (collElementExprs.get(i)).eq(elemExpr);
                } else {
                    bExpr = bExpr.ior((collElementExprs.get(i)).eq(elemExpr));
                }
            }
        }
        if (bExpr != null) {
            bExpr.encloseInParentheses();
        }
        return bExpr;
    }
    if (mmd == null) {
        throw new NucleusUserException("Cannot perform Collection.contains when the field metadata is not provided");
    }
    if (mmd.isSerialized()) {
        throw new NucleusUserException("Cannot perform Collection.contains when the collection is being serialised");
    }
    ApiAdapter api = stmt.getRDBMSManager().getApiAdapter();
    Class elementType = clr.classForName(mmd.getCollection().getElementType());
    if (!api.isPersistable(elementType) && mmd.getJoinMetaData() == null) {
        throw new NucleusUserException("Cannot perform Collection.contains when the collection<Non-Persistable> is not in a join table");
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        boolean useSubquery = getNeedsSubquery(stmt, collExpr, elemExpr);
        JoinType joinType = JoinType.INNER_JOIN;
        if (elemExpr instanceof UnboundExpression) {
            // See if the user has defined what should be used
            String varName = ((UnboundExpression) elemExpr).getVariableName();
            String extensionName = "datanucleus.query.jdoql." + varName + ".join";
            String extensionValue = (String) stmt.getQueryGenerator().getValueForExtension(extensionName);
            if (extensionValue != null) {
                if (extensionValue.equalsIgnoreCase("SUBQUERY")) {
                    useSubquery = true;
                } else if (extensionValue.equalsIgnoreCase("INNERJOIN")) {
                    useSubquery = false;
                } else if (extensionValue.equalsIgnoreCase("LEFTOUTERJOIN")) {
                    joinType = JoinType.LEFT_OUTER_JOIN;
                }
            }
        }
        if (useSubquery) {
            return containsAsSubquery(stmt, collExpr, elemExpr);
        }
        return containsAsJoin(stmt, collExpr, elemExpr, joinType);
    }
    return containsAsSubquery(stmt, collExpr, elemExpr);
}
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) InExpression(org.datanucleus.store.rdbms.sql.expression.InExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) CollectionLiteral(org.datanucleus.store.rdbms.sql.expression.CollectionLiteral) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ApiAdapter(org.datanucleus.api.ApiAdapter) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) JoinType(org.datanucleus.store.rdbms.sql.SQLJoin.JoinType) EnumExpression(org.datanucleus.store.rdbms.sql.expression.EnumExpression) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) Collection(java.util.Collection) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)39 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)33 NucleusException (org.datanucleus.exceptions.NucleusException)14 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)14 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)14 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)13 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)12 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)11 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)11 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)10 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)8 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)8 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)7 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 BooleanSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression)6 MapExpression (org.datanucleus.store.rdbms.sql.expression.MapExpression)6 ArrayList (java.util.ArrayList)5 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)5 CollectionExpression (org.datanucleus.store.rdbms.sql.expression.CollectionExpression)5