Search in sources :

Example 1 with BooleanLiteral

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

the class ArrayIsEmptyMethod 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) {
        throw new NucleusException(Localiser.msg("060015", "isEmpty", "ArrayExpression"));
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (expr instanceof ArrayLiteral) {
        Object arr = ((ArrayLiteral) expr).getValue();
        boolean isEmpty = (arr == null || Array.getLength(arr) == 0);
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
        return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
    }
    SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, "ARRAY", "size", expr, args);
    JavaTypeMapping mapping = exprFactory.getMappingForType(Integer.class, true);
    SQLExpression zeroExpr = exprFactory.newLiteral(stmt, mapping, 0);
    return sizeExpr.eq(zeroExpr);
}
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) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) NucleusException(org.datanucleus.exceptions.NucleusException) ArrayLiteral(org.datanucleus.store.rdbms.sql.expression.ArrayLiteral)

Example 2 with BooleanLiteral

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

the class MapContainsEntryMethod 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() != 2) {
        throw new NucleusException(Localiser.msg("060016", "containsValue", "MapExpression", 2));
    }
    MapExpression mapExpr = (MapExpression) expr;
    SQLExpression keyExpr = args.get(0);
    SQLExpression valExpr = args.get(1);
    if (keyExpr.isParameter()) {
        // Key is a parameter so make sure its type is set
        AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
        if (mmd != null && mmd.getMap() != null) {
            Class keyCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getKeyType());
            stmt.getQueryGenerator().bindParameter(keyExpr.getParameterName(), keyCls);
        }
    }
    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);
        }
    }
    if (mapExpr instanceof MapLiteral) {
        MapLiteral lit = (MapLiteral) mapExpr;
        Map map = (Map) lit.getValue();
        if (map == null || map.size() == 0) {
            return new BooleanLiteral(stmt, expr.getJavaTypeMapping(), Boolean.FALSE);
        }
        // TODO Handle this
        return lit.getValueLiteral().invoke("contains", args);
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        boolean needsSubquery = getNeedsSubquery(stmt);
        // TODO Check if *this* "containsEntry" is negated, not any of them (and remove above check)
        if (needsSubquery) {
            NucleusLogger.QUERY.debug("MapContainsEntry on " + mapExpr + "(" + keyExpr + "," + valExpr + ") using SUBQUERY");
            return containsAsSubquery(stmt, mapExpr, keyExpr, valExpr);
        }
        NucleusLogger.QUERY.debug("MapContainsEntry on " + mapExpr + "(" + keyExpr + "," + valExpr + ") using INNERJOIN");
        return containsAsInnerJoin(stmt, mapExpr, keyExpr, valExpr);
    }
    return containsAsSubquery(stmt, mapExpr, keyExpr, valExpr);
}
Also used : MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) MapLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) Map(java.util.Map)

Example 3 with BooleanLiteral

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

the class MapIsEmptyMethod 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) {
        throw new NucleusException(Localiser.msg("060015", "isEmpty", "MapExpression"));
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (expr instanceof MapLiteral) {
        Map map = (Map) ((MapLiteral) expr).getValue();
        boolean isEmpty = (map == null || map.size() == 0);
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
        return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
    }
    SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, Map.class.getName(), "size", expr, args);
    JavaTypeMapping mapping = exprFactory.getMappingForType(Integer.class, true);
    SQLExpression zeroExpr = exprFactory.newLiteral(stmt, mapping, 0);
    return sizeExpr.eq(zeroExpr);
}
Also used : MapLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 4 with BooleanLiteral

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

the class StringMatchesMethod 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() > 2) {
        throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
    } else if (!(args.get(0) instanceof StringExpression) && !(args.get(0) instanceof ParameterLiteral)) {
        throw new NucleusException("Incorrect arguments for String.matches(StringExpression)");
    }
    SQLExpression likeExpr = args.get(0);
    if (!(likeExpr instanceof StringExpression) && !(likeExpr instanceof CharacterExpression) && !(likeExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "like/matches", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    SQLExpression escapeExpr = null;
    if (args.size() > 1) {
        escapeExpr = args.get(1);
    }
    if ((likeExpr instanceof StringLiteral || likeExpr instanceof ParameterLiteral) && likeExpr.isParameter()) {
        // Argument as parameter needs translation to use SQL "LIKE" syntax, so has to be embedded as literal
        stmt.getQueryGenerator().useParameterExpressionAsLiteral((SQLLiteral) likeExpr);
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (expr instanceof StringLiteral && likeExpr instanceof StringLiteral) {
        // String.matches(String) so evaluate in-memory
        String primary = (String) ((StringLiteral) expr).getValue();
        String pattern = (String) ((StringLiteral) likeExpr).getValue();
        return new BooleanLiteral(stmt, exprFactory.getMappingForType(boolean.class, false), primary.matches(pattern));
    } else if (expr instanceof StringLiteral) {
        return getBooleanLikeExpression(stmt, expr, likeExpr, escapeExpr);
    } else if (expr instanceof StringExpression && likeExpr instanceof StringLiteral) {
        // Convert the pattern to use the regex constructs suitable for the datastore
        String pattern = (String) ((StringLiteral) likeExpr).getValue();
        if (stmt.getQueryGenerator().getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JDOQL)) {
            // JDOQL input is in java.lang.String regular expression format, so convert to SQL like
            boolean caseSensitive = false;
            if (pattern.startsWith("(?i)")) {
                caseSensitive = true;
                pattern = pattern.substring(4);
            }
            DatastoreAdapter dba = stmt.getDatastoreAdapter();
            RegularExpressionConverter converter = new RegularExpressionConverter(dba.getPatternExpressionZeroMoreCharacters().charAt(0), dba.getPatternExpressionAnyCharacter().charAt(0), dba.getEscapeCharacter().charAt(0));
            if (caseSensitive) {
                SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern).toLowerCase());
                return getBooleanLikeExpression(stmt, expr.invoke("toLowerCase", null), patternExpr, escapeExpr);
            }
            SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), converter.convert(pattern));
            return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
        }
        SQLExpression patternExpr = exprFactory.newLiteral(stmt, likeExpr.getJavaTypeMapping(), pattern);
        return getBooleanLikeExpression(stmt, expr, patternExpr, escapeExpr);
    } else if (expr instanceof StringExpression) {
        return getExpressionForStringExpressionInput(stmt, expr, likeExpr, escapeExpr);
    } else {
        throw new NucleusException(Localiser.msg("060001", "matches", expr));
    }
}
Also used : ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) RegularExpressionConverter(org.datanucleus.util.RegularExpressionConverter) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 5 with BooleanLiteral

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

the class CollectionIsEmptyMethod 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) {
        throw new NucleusException(Localiser.msg("060015", "isEmpty", "CollectionExpression"));
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (expr instanceof CollectionLiteral) {
        Collection coll = (Collection) ((CollectionLiteral) expr).getValue();
        boolean isEmpty = (coll == null || coll.size() == 0);
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, false);
        return new BooleanLiteral(stmt, m, isEmpty ? Boolean.TRUE : Boolean.FALSE);
    }
    AbstractMemberMetaData mmd = ((CollectionExpression) expr).getJavaTypeMapping().getMemberMetaData();
    if (mmd.isSerialized()) {
        throw new NucleusUserException("Cannot perform Collection.isEmpty when the collection is being serialised");
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    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.isEmpty when the collection<Non-Persistable> is not in a join table");
    }
    SQLExpression sizeExpr = exprFactory.invokeMethod(stmt, Collection.class.getName(), "size", expr, args);
    JavaTypeMapping mapping = exprFactory.getMappingForType(Integer.class, true);
    SQLExpression zeroExpr = exprFactory.newLiteral(stmt, mapping, 0);
    return sizeExpr.eq(zeroExpr);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ApiAdapter(org.datanucleus.api.ApiAdapter) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) CollectionLiteral(org.datanucleus.store.rdbms.sql.expression.CollectionLiteral) Collection(java.util.Collection) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)5 BooleanLiteral (org.datanucleus.store.rdbms.sql.expression.BooleanLiteral)5 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)5 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)4 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 Map (java.util.Map)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 MapLiteral (org.datanucleus.store.rdbms.sql.expression.MapLiteral)2 Collection (java.util.Collection)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ApiAdapter (org.datanucleus.api.ApiAdapter)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 BaseDatastoreAdapter (org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter)1 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)1 ArrayLiteral (org.datanucleus.store.rdbms.sql.expression.ArrayLiteral)1 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)1 CollectionLiteral (org.datanucleus.store.rdbms.sql.expression.CollectionLiteral)1 MapExpression (org.datanucleus.store.rdbms.sql.expression.MapExpression)1 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)1 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)1