Search in sources :

Example 11 with TemporalExpression

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

the class TemoralMinuteMethod6 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 args) {
    if (!(expr instanceof TemporalExpression)) {
        throw new NucleusException(Localiser.msg("060001", "getMinute()", expr));
    }
    expr.toSQLText().prepend("MINUTE FROM ");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(expr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "EXTRACT", funcArgs);
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 12 with TemporalExpression

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

Example 13 with TemporalExpression

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

the class CurrentDateFunction 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 == null) {
        // Assume that we have something like "CURRENT_DATE()"
        SQLExpression dateExpr = new TemporalExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(getClassForMapping(), true), getFunctionName(), args);
        // Update the SQL manually since the default is to add brackets after the name
        dateExpr.toSQLText().clearStatement();
        dateExpr.toSQLText().append(getFunctionName());
        return dateExpr;
    }
    throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 14 with TemporalExpression

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

the class CurrentTimestampFunction 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 args) {
    if (expr == null) {
        // Assume that we have something like "CURRENT_DATE()"
        SQLExpression dateExpr = new TemporalExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(getClassForMapping(), true), getFunctionName(), args);
        // Update the SQL manually since the default is to add brackets after the name
        dateExpr.toSQLText().clearStatement();
        dateExpr.toSQLText().append(getFunctionName());
        return dateExpr;
    }
    throw new NucleusException(Localiser.msg("060002", getFunctionName(), expr));
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 15 with TemporalExpression

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

the class JDOHelperGetVersionMethod 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() == 0) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getVersion without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getVersion on null expression");
    }
    if (expr instanceof SQLLiteral) {
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        ApiAdapter api = storeMgr.getApiAdapter();
        Object obj = ((SQLLiteral) expr).getValue();
        if (obj == null || !api.isPersistable(obj)) {
            return new NullLiteral(stmt, null, null, null);
        }
        Object ver = stmt.getRDBMSManager().getApiAdapter().getVersionForObject(obj);
        JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(ver.getClass(), true);
        return new ObjectLiteral(stmt, m, ver, null);
    } else if (ObjectExpression.class.isAssignableFrom(expr.getClass())) {
        if (((ObjectExpression) expr).getJavaTypeMapping() instanceof PersistableMapping) {
            JavaTypeMapping mapping = ((ObjectExpression) expr).getJavaTypeMapping();
            DatastoreClass table = (DatastoreClass) expr.getSQLTable().getTable();
            if (// Version of candidate
            table.getIdMapping() == mapping) {
                mapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
                if (mapping == null) {
                    throw new NucleusUserException("Cannot use JDOHelper.getVersion on object that has no version information");
                }
                if (table.getVersionMetaData().getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
                    return new NumericExpression(stmt, expr.getSQLTable(), mapping);
                }
                return new TemporalExpression(stmt, expr.getSQLTable(), mapping);
            }
            throw new NucleusUserException("Dont currently support JDOHelper.getVersion(ObjectExpression) for expr=" + expr + " on table=" + expr.getSQLTable());
        // TODO Implement this
        }
        return expr;
    }
    throw new IllegalExpressionOperationException("JDOHelper.getVersion", expr);
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ApiAdapter(org.datanucleus.api.ApiAdapter) ObjectLiteral(org.datanucleus.store.rdbms.sql.expression.ObjectLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Aggregations

TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)15 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)14 NucleusException (org.datanucleus.exceptions.NucleusException)11 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)11 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)9 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)9 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)5 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)4 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)4 EnumExpression (org.datanucleus.store.rdbms.sql.expression.EnumExpression)4 InExpression (org.datanucleus.store.rdbms.sql.expression.InExpression)4 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)4 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)4