Search in sources :

Example 1 with NullLiteral

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

the class DiscriminatorStatementGenerator method getStatement.

/**
 * Accessor for the SelectStatement.
 * @param ec ExecutionContext
 * @return The SelectStatement for iterating through objects with a discriminator column
 */
public SelectStatement getStatement(ExecutionContext ec) {
    SelectStatement stmt = null;
    SQLTable discrimSqlTbl = null;
    if (joinTable == null) {
        // Select of candidate table
        stmt = new SelectStatement(parentStmt, storeMgr, candidateTable, candidateTableAlias, candidateTableGroupName);
        stmt.setClassLoaderResolver(clr);
        discrimSqlTbl = stmt.getPrimaryTable();
    } else {
        // Select of join table, with join to element table
        stmt = new SelectStatement(parentStmt, storeMgr, joinTable, joinTableAlias, candidateTableGroupName);
        stmt.setClassLoaderResolver(clr);
        JavaTypeMapping candidateIdMapping = candidateTable.getIdMapping();
        if (hasOption(OPTION_ALLOW_NULLS)) {
            // Put element table in same table group since all relates to the elements
            discrimSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, null, joinElementMapping, candidateTable, null, candidateIdMapping, null, stmt.getPrimaryTable().getGroupName(), true);
        } else {
            // Put element table in same table group since all relates to the elements
            discrimSqlTbl = stmt.join(JoinType.INNER_JOIN, null, joinElementMapping, candidateTable, null, candidateIdMapping, null, stmt.getPrimaryTable().getGroupName(), true);
        }
    }
    JavaTypeMapping discMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, true);
    if (discMapping != null) {
        // Allow for discriminator being in super-table of the candidate table
        discrimSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(stmt, discrimSqlTbl, discMapping);
    }
    DiscriminatorMetaData dismd = discrimSqlTbl.getTable().getDiscriminatorMetaData();
    boolean hasDiscriminator = (discMapping != null && dismd != null && dismd.getStrategy() != DiscriminatorStrategy.NONE);
    // Check if we can omit the discriminator restriction
    boolean restrictDiscriminator = hasOption(OPTION_RESTRICT_DISCRIM);
    if (hasDiscriminator && restrictDiscriminator) {
        // Add the discriminator expression to restrict accepted values
        boolean multipleCandidates = false;
        BooleanExpression discExpr = null;
        if (candidates != null) {
            // Multiple candidates
            if (candidates.length > 1) {
                multipleCandidates = true;
            }
            for (int i = 0; i < candidates.length; i++) {
                if (Modifier.isAbstract(candidates[i].getModifiers())) {
                    // No point selecting this candidate since can't be instantiated
                    continue;
                }
                BooleanExpression discExprCandidate = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, candidates[i].getName(), dismd, discMapping, discrimSqlTbl, clr);
                if (discExpr != null) {
                    discExpr = discExpr.ior(discExprCandidate);
                } else {
                    discExpr = discExprCandidate;
                }
                if (includeSubclasses) {
                    Collection<String> subclassNames = storeMgr.getSubClassesForClass(candidateType.getName(), true, clr);
                    Iterator<String> subclassIter = subclassNames.iterator();
                    if (!multipleCandidates) {
                        multipleCandidates = (subclassNames.size() > 0);
                    }
                    while (subclassIter.hasNext()) {
                        String subclassName = subclassIter.next();
                        BooleanExpression discExprSub = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, subclassName, dismd, discMapping, discrimSqlTbl, clr);
                        discExpr = discExpr.ior(discExprSub);
                    }
                }
            }
        } else {
            // Single candidate
            if (!Modifier.isAbstract(candidateType.getModifiers())) {
                discExpr = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, candidateType.getName(), dismd, discMapping, discrimSqlTbl, clr);
            }
            if (includeSubclasses) {
                Collection<String> subclassNames = storeMgr.getSubClassesForClass(candidateType.getName(), true, clr);
                Iterator<String> subclassIter = subclassNames.iterator();
                multipleCandidates = (subclassNames.size() > 0);
                while (subclassIter.hasNext()) {
                    String subclassName = subclassIter.next();
                    Class subclass = clr.classForName(subclassName);
                    if ((Modifier.isAbstract(subclass.getModifiers()))) {
                        continue;
                    }
                    BooleanExpression discExprCandidate = SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, subclassName, dismd, discMapping, discrimSqlTbl, clr);
                    if (discExpr == null) {
                        discExpr = discExprCandidate;
                    } else {
                        discExpr = discExpr.ior(discExprCandidate);
                    }
                }
            }
            if (discExpr == null) {
                // No possible candidates, so set expression as "1=0"
                SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
                JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                discExpr = exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
            }
        }
        if (discExpr != null) {
            if (hasOption(OPTION_ALLOW_NULLS)) {
                // Allow for null value of discriminator
                SQLExpression expr = stmt.getSQLExpressionFactory().newExpression(stmt, discrimSqlTbl, discMapping);
                SQLExpression val = new NullLiteral(stmt, null, null, null);
                BooleanExpression nullDiscExpr = expr.eq(val);
                discExpr = discExpr.ior(nullDiscExpr);
                if (!multipleCandidates) {
                    multipleCandidates = true;
                }
            }
            // Apply the discriminator to the query statement
            if (multipleCandidates) {
                discExpr.encloseInParentheses();
            }
            stmt.whereAnd(discExpr, true);
        }
    }
    JavaTypeMapping multitenancyMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.MULTITENANCY, false);
    if (multitenancyMapping != null) {
        // Multi-tenancy restriction
        AbstractClassMetaData cmd = candidateTable.getClassMetaData();
        SQLTable tenantSqlTbl = stmt.getTable(multitenancyMapping.getTable(), discrimSqlTbl.getGroupName());
        SQLExpression tenantExpr = stmt.getSQLExpressionFactory().newExpression(stmt, tenantSqlTbl, multitenancyMapping);
        SQLExpression tenantVal = stmt.getSQLExpressionFactory().newLiteral(stmt, multitenancyMapping, ec.getNucleusContext().getMultiTenancyId(ec, cmd));
        stmt.whereAnd(tenantExpr.eq(tenantVal), true);
    }
    JavaTypeMapping softDeleteMapping = candidateTable.getSurrogateMapping(SurrogateColumnType.SOFTDELETE, false);
    if (softDeleteMapping != null && !hasOption(OPTION_INCLUDE_SOFT_DELETES)) {
        // Soft-delete restriction
        SQLTable softDeleteSqlTbl = stmt.getTable(softDeleteMapping.getTable(), discrimSqlTbl.getGroupName());
        SQLExpression softDeleteExpr = stmt.getSQLExpressionFactory().newExpression(stmt, softDeleteSqlTbl, softDeleteMapping);
        SQLExpression softDeleteVal = stmt.getSQLExpressionFactory().newLiteral(stmt, softDeleteMapping, Boolean.FALSE);
        stmt.whereAnd(softDeleteExpr.eq(softDeleteVal), true);
    }
    return stmt;
}
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) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Example 2 with NullLiteral

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

the class MathAbsMethod 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 Math.abs without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    } else if (expr instanceof SQLLiteral) {
        if (expr instanceof ByteLiteral) {
            int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
            BigInteger absValue = new BigInteger(String.valueOf(Math.abs(originalValue)));
            return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof IntegerLiteral) {
            int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
            Integer absValue = Integer.valueOf(Math.abs(originalValue));
            return new IntegerLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof FloatingPointLiteral) {
            double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
            Double absValue = new Double(Math.abs(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.abs()", expr);
    } else {
        // Relay to the equivalent "abs(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "abs", null, args);
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) BigInteger(java.math.BigInteger) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) FloatingPointLiteral(org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral) ByteLiteral(org.datanucleus.store.rdbms.sql.expression.ByteLiteral) BigInteger(java.math.BigInteger) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 3 with NullLiteral

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

the class MathAsinMethod 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 Math.asin without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    } else if (expr instanceof SQLLiteral) {
        if (expr instanceof ByteLiteral) {
            int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
            BigInteger absValue = new BigInteger(String.valueOf(Math.asin(originalValue)));
            return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof IntegerLiteral) {
            int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
            Double absValue = new Double(Math.asin(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof FloatingPointLiteral) {
            double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
            Double absValue = new Double(Math.asin(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.asin()", expr);
    } else {
        // Relay to the equivalent "asin(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "asin", null, args);
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) FloatingPointLiteral(org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral) ByteLiteral(org.datanucleus.store.rdbms.sql.expression.ByteLiteral) BigInteger(java.math.BigInteger) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 4 with NullLiteral

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

the class ListGetMethod 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", "get", "CollectionExpression", 1));
    }
    CollectionExpression listExpr = (CollectionExpression) expr;
    AbstractMemberMetaData mmd = listExpr.getJavaTypeMapping().getMemberMetaData();
    if (!List.class.isAssignableFrom(mmd.getType())) {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not a List!");
    } else if (mmd.getOrderMetaData() != null && !mmd.getOrderMetaData().isIndexedList()) {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not an 'indexed' List!");
    }
    SQLExpression idxExpr = args.get(0);
    if (idxExpr instanceof SQLLiteral) {
        if (!(((SQLLiteral) idxExpr).getValue() instanceof Number)) {
            throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
        }
    } else {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
    }
    if (listExpr instanceof CollectionLiteral && idxExpr instanceof SQLLiteral) {
        CollectionLiteral lit = (CollectionLiteral) expr;
        if (lit.getValue() == null) {
            return new NullLiteral(stmt, null, null, null);
        }
        return lit.invoke("get", args);
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        return getAsInnerJoin(stmt, listExpr, idxExpr);
    } else if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.ORDERING || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
        return getAsSubquery(stmt, listExpr, idxExpr);
    }
    throw new NucleusException("List.get() is not supported for " + listExpr + " with argument " + idxExpr + " for query component " + stmt.getQueryGenerator().getCompilationComponent());
}
Also used : CollectionLiteral(org.datanucleus.store.rdbms.sql.expression.CollectionLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) List(java.util.List) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Example 5 with NullLiteral

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

the class MathCosMethod 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 Math.cos without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    } else if (expr instanceof SQLLiteral) {
        if (expr instanceof ByteLiteral) {
            int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
            BigInteger absValue = new BigInteger(String.valueOf(Math.cos(originalValue)));
            return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof IntegerLiteral) {
            int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
            Double absValue = new Double(Math.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof FloatingPointLiteral) {
            double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
            Double absValue = new Double(Math.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.cos()", expr);
    } else {
        // Relay to the equivalent "cos(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "cos", null, args);
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) FloatingPointLiteral(org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral) ByteLiteral(org.datanucleus.store.rdbms.sql.expression.ByteLiteral) BigInteger(java.math.BigInteger) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Aggregations

NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)24 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)24 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)20 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)19 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)19 IllegalExpressionOperationException (org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException)17 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)16 BigInteger (java.math.BigInteger)15 ByteLiteral (org.datanucleus.store.rdbms.sql.expression.ByteLiteral)15 FloatingPointLiteral (org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral)15 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 NucleusException (org.datanucleus.exceptions.NucleusException)5 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)3 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)3 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)3 List (java.util.List)2 ApiAdapter (org.datanucleus.api.ApiAdapter)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)2