Search in sources :

Example 21 with SQLExpression

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

Example 22 with SQLExpression

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

the class QueryToSQLMapper method compileGrouping.

/**
 * Method to compile the grouping clause of the query into the SQLStatement.
 * @param stmt SELECT statement
 */
protected void compileGrouping(SelectStatement stmt) {
    if (compilation.getExprGrouping() != null) {
        // Apply any grouping to the statement
        compileComponent = CompilationComponent.GROUPING;
        Expression[] groupExprs = compilation.getExprGrouping();
        for (Expression groupExpr : groupExprs) {
            stmt.addGroupingExpression((SQLExpression) groupExpr.evaluate(this));
        }
        compileComponent = null;
    }
}
Also used : 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)

Example 23 with SQLExpression

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

the class QueryToSQLMapper method processVariableExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processVariableExpression(org.datanucleus.query.expression.VariableExpression)
     */
@Override
protected Object processVariableExpression(VariableExpression expr) {
    String varName = expr.getId();
    Symbol varSym = expr.getSymbol();
    if (varSym != null) {
        // Use name from symbol if possible
        varName = varSym.getQualifiedName();
    }
    if (hasSQLTableMappingForAlias(varName)) {
        // Variable already found
        SQLTableMapping tblMapping = getSQLTableMappingForAlias(varName);
        SQLExpression sqlExpr = exprFactory.newExpression(tblMapping.table.getSQLStatement(), tblMapping.table, tblMapping.mapping);
        stack.push(sqlExpr);
        return sqlExpr;
    } else if (compilation.getCompilationForSubquery(varName) != null) {
        // Subquery variable
        QueryCompilation subCompilation = compilation.getCompilationForSubquery(varName);
        AbstractClassMetaData subCmd = ec.getMetaDataManager().getMetaDataForClass(subCompilation.getCandidateClass(), ec.getClassLoaderResolver());
        // Create subquery statement, using any provided alias if possible
        String subAlias = null;
        if (subCompilation.getCandidateAlias() != null && !subCompilation.getCandidateAlias().equals(candidateAlias)) {
            subAlias = subCompilation.getCandidateAlias();
        }
        StatementResultMapping subqueryResultMapping = new StatementResultMapping();
        // TODO Fix "avg(something)" arg - not essential but is a hack right now
        SQLStatement subStmt = RDBMSQueryUtils.getStatementForCandidates(storeMgr, stmt, subCmd, null, ec, subCompilation.getCandidateClass(), true, "avg(something)", subAlias, null, null);
        QueryToSQLMapper sqlMapper = new QueryToSQLMapper(subStmt, subCompilation, parameters, null, subqueryResultMapping, subCmd, true, fetchPlan, ec, importsDefinition, options, extensionsByName);
        sqlMapper.setDefaultJoinType(defaultJoinType);
        sqlMapper.setDefaultJoinTypeFilter(defaultJoinTypeFilter);
        sqlMapper.setParentMapper(this);
        sqlMapper.compile();
        if (subqueryResultMapping.getNumberOfResultExpressions() > 1) {
            throw new NucleusUserException("Number of result expressions in subquery should be 1");
        }
        SQLExpression subExpr = null;
        // TODO Cater for subquery select of its own candidate
        if (subqueryResultMapping.getNumberOfResultExpressions() == 0) {
            subExpr = new org.datanucleus.store.rdbms.sql.expression.SubqueryExpression(stmt, subStmt);
        } else {
            JavaTypeMapping subMapping = ((StatementMappingIndex) subqueryResultMapping.getMappingForResultExpression(0)).getMapping();
            if (subMapping instanceof TemporalMapping) {
                subExpr = new TemporalSubqueryExpression(stmt, subStmt);
            } else if (subMapping instanceof StringMapping) {
                subExpr = new StringSubqueryExpression(stmt, subStmt);
            } else {
                subExpr = new NumericSubqueryExpression(stmt, subStmt);
            }
            if (subExpr.getJavaTypeMapping() == null) {
                subExpr.setJavaTypeMapping(subMapping);
            }
        }
        stack.push(subExpr);
        return subExpr;
    } else if (stmt.getParentStatement() != null && parentMapper != null && parentMapper.candidateAlias != null && parentMapper.candidateAlias.equals(varName)) {
        // Variable in subquery linking back to parent query
        SQLExpression varExpr = exprFactory.newExpression(stmt.getParentStatement(), stmt.getParentStatement().getPrimaryTable(), stmt.getParentStatement().getPrimaryTable().getTable().getIdMapping());
        stack.push(varExpr);
        return varExpr;
    } else {
        // Variable never met before, so return as UnboundExpression - process later if needing binding
        NucleusLogger.QUERY.debug("QueryToSQL.processVariable (unbound) variable=" + varName + " is not yet bound so returning UnboundExpression");
        UnboundExpression unbExpr = new UnboundExpression(stmt, varName);
        stack.push(unbExpr);
        return unbExpr;
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Symbol(org.datanucleus.query.compiler.Symbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) TemporalMapping(org.datanucleus.store.rdbms.mapping.java.TemporalMapping)

Example 24 with SQLExpression

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

the class QueryToSQLMapper method processFromClauseSubquery.

/**
 * Method to process a ClassExpression where it represents a subquery.
 * User defined the candidate of the subquery as an implied join to the outer query, for example "SELECT c FROM Customer c WHERE EXISTS (SELECT o FROM c.orders o ...)"
 * so this method will add the join(s) to the outer query.
 * @param clsExpr The ClassExpression
 * @param candSqlTbl Candidate SQL Table
 * @param mmgr MetaData Manager
 */
protected void processFromClauseSubquery(ClassExpression clsExpr, SQLTable candSqlTbl, MetaDataManager mmgr) {
    String[] tokens = StringUtils.split(clsExpr.getCandidateExpression(), ".");
    String leftAlias = tokens[0];
    SQLTableMapping outerSqlTblMapping = parentMapper.getSQLTableMappingForAlias(leftAlias);
    AbstractClassMetaData leftCmd = outerSqlTblMapping.cmd;
    // Get array of the left-right sides of this expression so we can work back from the subquery candidate
    AbstractMemberMetaData[] leftMmds = new AbstractMemberMetaData[tokens.length - 1];
    AbstractMemberMetaData[] rightMmds = new AbstractMemberMetaData[tokens.length - 1];
    for (int i = 0; i < tokens.length - 1; i++) {
        String joinedField = tokens[i + 1];
        AbstractMemberMetaData leftMmd = leftCmd.getMetaDataForMember(joinedField);
        AbstractMemberMetaData rightMmd = null;
        AbstractClassMetaData rightCmd = null;
        RelationType relationType = leftMmd.getRelationType(clr);
        if (RelationType.isBidirectional(relationType)) {
            // Take first possible
            rightMmd = leftMmd.getRelatedMemberMetaData(clr)[0];
            rightCmd = rightMmd.getAbstractClassMetaData();
        } else if (relationType == RelationType.ONE_TO_ONE_UNI) {
            rightCmd = mmgr.getMetaDataForClass(leftMmd.getType(), clr);
        } else if (relationType == RelationType.ONE_TO_MANY_UNI) {
            if (leftMmd.hasCollection()) {
                rightCmd = mmgr.getMetaDataForClass(leftMmd.getCollection().getElementType(), clr);
            } else if (leftMmd.hasMap()) {
                rightCmd = mmgr.getMetaDataForClass(leftMmd.getMap().getValueType(), clr);
            }
        } else {
            throw new NucleusUserException("Subquery has been specified with a candidate-expression that includes \"" + tokens[i] + "\" that isnt a relation field!!");
        }
        leftMmds[i] = leftMmd;
        rightMmds[i] = rightMmd;
        leftCmd = rightCmd;
    }
    // Work from subquery candidate back to outer query table, adding joins and where clause as appropriate
    SQLTable rSqlTbl = candSqlTbl;
    SQLTable outerSqlTbl = outerSqlTblMapping.table;
    JoinType joinType = JoinType.INNER_JOIN;
    for (int i = leftMmds.length - 1; i >= 0; i--) {
        AbstractMemberMetaData leftMmd = leftMmds[i];
        AbstractMemberMetaData rightMmd = rightMmds[i];
        DatastoreClass leftTbl = storeMgr.getDatastoreClass(leftMmd.getClassName(true), clr);
        SQLTable lSqlTbl = null;
        RelationType relationType = leftMmd.getRelationType(clr);
        if (relationType == RelationType.ONE_TO_ONE_UNI) {
            // 1-1 with FK in left table
            if (i == 0) {
                // Add where clause right table to outer table
                SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rSqlTbl.getTable().getIdMapping());
                stmt.whereAnd(outerExpr.eq(rightExpr), false);
            } else {
                // Join to left table
                JavaTypeMapping leftMapping = leftTbl.getMemberMapping(leftMmd);
                lSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), leftTbl, null, leftMapping, null, null, true);
            }
        } else if (relationType == RelationType.ONE_TO_ONE_BI) {
            if (leftMmd.getMappedBy() != null) {
                // 1-1 with FK in right table
                JavaTypeMapping rightMapping = rSqlTbl.getTable().getMemberMapping(rightMmd);
                if (i == 0) {
                    // Add where clause right table to outer table
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                    SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rightMapping);
                    stmt.whereAnd(outerExpr.eq(rightExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, rSqlTbl, rightMapping, leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                }
            } else {
                // 1-1 with FK in left table
                if (i == 0) {
                    // Add where clause right table to outer table
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                    SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rSqlTbl.getTable().getIdMapping());
                    stmt.whereAnd(outerExpr.eq(rightExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null, true);
                }
            }
        } else if (relationType == RelationType.ONE_TO_MANY_UNI) {
            if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null) {
                // 1-N with join table to right table, so join from right to join table
                JoinTable joinTbl = (JoinTable) storeMgr.getTable(leftMmd);
                SQLTable joinSqlTbl = null;
                if (leftMmd.hasCollection()) {
                    joinSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), joinTbl, null, ((ElementContainerTable) joinTbl).getElementMapping(), null, null, true);
                } else if (leftMmd.hasMap()) {
                    joinSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), joinTbl, null, ((MapTable) joinTbl).getValueMapping(), null, null, true);
                }
                if (i == 0) {
                    // Add where clause join table (owner) to outer table (id)
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                    SQLExpression joinExpr = exprFactory.newExpression(stmt, joinSqlTbl, joinTbl.getOwnerMapping());
                    stmt.whereAnd(outerExpr.eq(joinExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getOwnerMapping(), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                }
            } else {
                // 1-N with FK in right table
                if (i == 0) {
                    // Add where clause right table to outer table
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                    SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd));
                    stmt.whereAnd(outerExpr.eq(rightExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                }
            }
        } else if (relationType == RelationType.ONE_TO_MANY_BI) {
            if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null) {
                // 1-N with join table to right table, so join from right to join table
                JoinTable joinTbl = (JoinTable) storeMgr.getTable(leftMmd);
                SQLTable joinSqlTbl = null;
                if (leftMmd.hasCollection()) {
                    joinSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), joinTbl, null, ((ElementContainerTable) joinTbl).getElementMapping(), null, null, true);
                } else if (leftMmd.hasMap()) {
                    joinSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), joinTbl, null, ((MapTable) joinTbl).getValueMapping(), null, null, true);
                }
                if (i == 0) {
                    // Add where clause join table (owner) to outer table (id)
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                    SQLExpression joinExpr = exprFactory.newExpression(stmt, joinSqlTbl, joinTbl.getOwnerMapping());
                    stmt.whereAnd(outerExpr.eq(joinExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getOwnerMapping(), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                }
            } else {
                // 1-N with FK in right table
                if (i == 0) {
                    // Add where clause right table to outer table
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                    SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd));
                    stmt.whereAnd(outerExpr.eq(rightExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                }
            }
        } else if (relationType == RelationType.MANY_TO_ONE_BI) {
            if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null) {
                // 1-N with join table to right table, so join from right to join table
                JoinTable joinTbl = (JoinTable) storeMgr.getTable(leftMmd);
                SQLTable joinSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
                if (leftMmd.hasCollection()) {
                    if (i == 0) {
                        // Add where clause join table (element) to outer table (id)
                        SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                        SQLExpression joinExpr = exprFactory.newExpression(stmt, joinSqlTbl, ((ElementContainerTable) joinTbl).getElementMapping());
                        stmt.whereAnd(outerExpr.eq(joinExpr), false);
                    } else {
                        // Join to left table
                        lSqlTbl = stmt.join(joinType, joinSqlTbl, ((ElementContainerTable) joinTbl).getElementMapping(), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                    }
                } else if (leftMmd.hasMap()) {
                    if (i == 0) {
                        // Add where clause join table (value) to outer table (id)
                        SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
                        SQLExpression joinExpr = exprFactory.newExpression(stmt, joinSqlTbl, ((MapTable) joinTbl).getValueMapping());
                        stmt.whereAnd(outerExpr.eq(joinExpr), false);
                    } else {
                        // Join to left table
                        lSqlTbl = stmt.join(joinType, joinSqlTbl, ((MapTable) joinTbl).getValueMapping(), leftTbl, null, leftTbl.getIdMapping(), null, null, true);
                    }
                }
            } else {
                if (i == 0) {
                    // Add where clause right table to outer table
                    SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(), outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                    SQLExpression rightExpr = exprFactory.newExpression(stmt, rSqlTbl, rSqlTbl.getTable().getIdMapping());
                    stmt.whereAnd(outerExpr.eq(rightExpr), false);
                } else {
                    // Join to left table
                    lSqlTbl = stmt.join(joinType, rSqlTbl, rSqlTbl.getTable().getIdMapping(), leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null, true);
                }
            }
        }
        rSqlTbl = lSqlTbl;
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JoinType(org.datanucleus.store.rdbms.sql.SQLJoin.JoinType) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) MapTable(org.datanucleus.store.rdbms.table.MapTable) RelationType(org.datanucleus.metadata.RelationType) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ElementContainerTable(org.datanucleus.store.rdbms.table.ElementContainerTable) JoinTable(org.datanucleus.store.rdbms.table.JoinTable)

Example 25 with SQLExpression

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

the class QueryToSQLMapper method processIsExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processIsExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processIsExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    SQLExpression instanceofExpr = left.is(right, false);
    stack.push(instanceofExpr);
    return instanceofExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression)

Aggregations

SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)199 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)98 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)95 ArrayList (java.util.ArrayList)75 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)74 NucleusException (org.datanucleus.exceptions.NucleusException)63 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)56 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)47 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)44 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)41 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)40 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)35 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)34 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)34 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)34 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)31 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)29 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)28 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)28 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)25