Search in sources :

Example 1 with TemporalLiteral

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

the class QueryToSQLMapper method compileUpdate.

/**
 * Method to compile the result clause of the query into the SQLStatement.
 * @param stmt UPDATE statement
 */
protected void compileUpdate(UpdateStatement stmt) {
    if (compilation.getExprUpdate() != null) {
        // Update statement, so generate update expression(s)
        compileComponent = CompilationComponent.UPDATE;
        Expression[] updateExprs = compilation.getExprUpdate();
        SQLExpression[] updateSqlExprs = new SQLExpression[updateExprs.length];
        // TODO If the field being set is in a different table omit it
        boolean performingUpdate = false;
        for (int i = 0; i < updateExprs.length; i++) {
            // "field = value"
            DyadicExpression updateExpr = (DyadicExpression) updateExprs[i];
            // Left-side has to be PrimaryExpression
            SQLExpression leftSqlExpr = null;
            if (updateExpr.getLeft() instanceof PrimaryExpression) {
                processPrimaryExpression((PrimaryExpression) updateExpr.getLeft());
                leftSqlExpr = stack.pop();
                if (leftSqlExpr.getSQLTable() != stmt.getPrimaryTable()) {
                    // Set left to null to signify that it is not applicable to the table of this UPDATE statement
                    leftSqlExpr = null;
                }
            } else {
                throw new NucleusException("Dont currently support update clause containing left expression of type " + updateExpr.getLeft());
            }
            if (leftSqlExpr != null) {
                if (!stmt.getDatastoreAdapter().supportsOption(DatastoreAdapter.UPDATE_STATEMENT_ALLOW_TABLE_ALIAS_IN_SET_CLAUSE)) {
                    // This datastore doesn't allow table alias in UPDATE SET clause, so just use column name
                    for (int j = 0; j < leftSqlExpr.getNumberOfSubExpressions(); j++) {
                        ColumnExpression colExpr = leftSqlExpr.getSubExpression(j);
                        colExpr.setOmitTableFromString(true);
                    }
                }
                performingUpdate = true;
                SQLExpression rightSqlExpr = null;
                if (updateExpr.getRight() instanceof Literal) {
                    processLiteral((Literal) updateExpr.getRight());
                    rightSqlExpr = stack.pop();
                } else if (updateExpr.getRight() instanceof ParameterExpression) {
                    ParameterExpression paramExpr = (ParameterExpression) updateExpr.getRight();
                    paramMappingForName.put(paramExpr.getId(), leftSqlExpr.getJavaTypeMapping());
                    processParameterExpression(paramExpr);
                    rightSqlExpr = stack.pop();
                } else if (updateExpr.getRight() instanceof PrimaryExpression) {
                    processPrimaryExpression((PrimaryExpression) updateExpr.getRight());
                    rightSqlExpr = stack.pop();
                } else if (updateExpr.getRight() instanceof DyadicExpression) {
                    updateExpr.getRight().evaluate(this);
                    rightSqlExpr = stack.pop();
                } else if (updateExpr.getRight() instanceof CaseExpression) {
                    CaseExpression caseExpr = (CaseExpression) updateExpr.getRight();
                    processCaseExpression(caseExpr, leftSqlExpr);
                    rightSqlExpr = stack.pop();
                } else if (updateExpr.getRight() instanceof VariableExpression) {
                    // Subquery?
                    processVariableExpression((VariableExpression) updateExpr.getRight());
                    rightSqlExpr = stack.pop();
                    if (rightSqlExpr instanceof UnboundExpression) {
                        // TODO Support whatever this is
                        throw new NucleusException("Found UnboundExpression in UPDATE clause!");
                    }
                } else {
                    throw new NucleusException("Dont currently support update clause containing right expression of type " + updateExpr.getRight());
                }
                if (rightSqlExpr != null) {
                    updateSqlExprs[i] = leftSqlExpr.eq(rightSqlExpr);
                }
            }
        }
        if (candidateCmd.isVersioned() && options.contains(OPTION_BULK_UPDATE_VERSION)) {
            SQLExpression updateSqlExpr = null;
            ClassTable table = (ClassTable) stmt.getPrimaryTable().getTable();
            JavaTypeMapping verMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
            ClassTable verTable = table.getTableManagingMapping(verMapping);
            if (verTable == stmt.getPrimaryTable().getTable()) {
                VersionMetaData vermd = candidateCmd.getVersionMetaDataForClass();
                if (vermd.getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
                    // Increment the version
                    SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
                    SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
                    SQLExpression incrExpr = verExpr.add(new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(1), null));
                    updateSqlExpr = verExpr.eq(incrExpr);
                    SQLExpression[] oldArray = updateSqlExprs;
                    updateSqlExprs = new SQLExpression[oldArray.length + 1];
                    System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
                    updateSqlExprs[oldArray.length] = updateSqlExpr;
                    performingUpdate = true;
                } else if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
                    // Set version to the time of update
                    SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
                    SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
                    Object newVersion = ec.getLockManager().getNextVersion(vermd, null);
                    JavaTypeMapping valMapping = exprFactory.getMappingForType(newVersion.getClass(), false);
                    SQLExpression valExpr = new TemporalLiteral(stmt, valMapping, newVersion, null);
                    updateSqlExpr = verExpr.eq(valExpr);
                    SQLExpression[] oldArray = updateSqlExprs;
                    updateSqlExprs = new SQLExpression[oldArray.length + 1];
                    System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
                    updateSqlExprs[oldArray.length] = updateSqlExpr;
                    performingUpdate = true;
                }
            }
        }
        if (performingUpdate) {
            // Only set the updates component of the SQLStatement if anything to update in this table
            stmt.setUpdates(updateSqlExprs);
        }
    }
    compileComponent = null;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) VersionMetaData(org.datanucleus.metadata.VersionMetaData) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) TemporalLiteral(org.datanucleus.store.rdbms.sql.expression.TemporalLiteral) VariableExpression(org.datanucleus.query.expression.VariableExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Expression(org.datanucleus.query.expression.Expression) TypeExpression(org.datanucleus.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) ClassTable(org.datanucleus.store.rdbms.table.ClassTable) TemporalLiteral(org.datanucleus.store.rdbms.sql.expression.TemporalLiteral) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral) Literal(org.datanucleus.query.expression.Literal) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)1 VersionMetaData (org.datanucleus.metadata.VersionMetaData)1 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)1 CaseExpression (org.datanucleus.query.expression.CaseExpression)1 ClassExpression (org.datanucleus.query.expression.ClassExpression)1 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)1 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)1 Expression (org.datanucleus.query.expression.Expression)1 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)1 JoinExpression (org.datanucleus.query.expression.JoinExpression)1 Literal (org.datanucleus.query.expression.Literal)1 OrderExpression (org.datanucleus.query.expression.OrderExpression)1 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)1 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)1 TypeExpression (org.datanucleus.query.expression.TypeExpression)1 VariableExpression (org.datanucleus.query.expression.VariableExpression)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)1 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)1