Package org.datanucleus.store.rdbms.sql

Examples of org.datanucleus.store.rdbms.sql.SQLTable


                    sqlStmt.union(elementStmt);
                }
            }

            // Select the required fields
            SQLTable elementSqlTbl = sqlStmt.getTable(elementInfo[0].getDatastoreClass(),
                sqlStmt.getPrimaryTable().getGroupName());
            SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingClass,
                ownerSM.getExecutionContext().getFetchPlan(), elementSqlTbl, emd, 0);
        }

        // Apply condition on join-table owner field to filter by owner
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);

        if (relationDiscriminatorMapping != null)
        {
            // Apply condition on distinguisher field to filter by distinguisher (when present)
            SQLTable distSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), relationDiscriminatorMapping);
            SQLExpression distExpr = exprFactory.newExpression(sqlStmt, distSqlTbl, relationDiscriminatorMapping);
            SQLExpression distVal = exprFactory.newLiteral(sqlStmt, relationDiscriminatorMapping, relationDiscriminatorValue);
            sqlStmt.whereAnd(distExpr.eq(distVal), true);
        }

        if (orderMapping != null)
        {
            // Order by the ordering column, when present
            SQLTable orderSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
            SQLExpression[] orderExprs = new SQLExpression[orderMapping.getNumberOfDatastoreMappings()];
            boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreMappings()];
            orderExprs[0] = exprFactory.newExpression(sqlStmt, orderSqlTbl, orderMapping);
            sqlStmt.setOrdering(orderExprs, descendingOrder);
View Full Code Here


                    sqlStmt.union(elementStmt);
                }
            }

            // Select the required fields
            SQLTable elementSqlTbl = sqlStmt.getTable(elementInfo[0].getDatastoreClass(),
                sqlStmt.getPrimaryTable().getGroupName());
            SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, resultMapping,
                ownerSM.getExecutionContext().getFetchPlan(), elementSqlTbl, emd, 0);
        }

        // Apply condition on join-table owner field to filter by owner
        SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);

        if (relationDiscriminatorMapping != null)
        {
            // Apply condition on distinguisher field to filter by distinguisher (when present)
            SQLTable distSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), relationDiscriminatorMapping);
            SQLExpression distExpr = exprFactory.newExpression(sqlStmt, distSqlTbl, relationDiscriminatorMapping);
            SQLExpression distVal = exprFactory.newLiteral(sqlStmt, relationDiscriminatorMapping, relationDiscriminatorValue);
            sqlStmt.whereAnd(distExpr.eq(distVal), true);
        }

        if (indexedList)
        {
            // "Indexed List" so allow restriction on returned indexes
            boolean needsOrdering = true;
            if (startIdx == -1 && endIdx == -1)
            {
                // Just restrict to >= 0 so we don't get any disassociated elements
                SQLExpression indexExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                SQLExpression indexVal = exprFactory.newLiteral(sqlStmt, orderMapping, 0);
                sqlStmt.whereAnd(indexExpr.ge(indexVal), true);
            }
            else if (startIdx >= 0 && endIdx == startIdx)
            {
                // Particular index required so add restriction
                needsOrdering = false;
                SQLExpression indexExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                SQLExpression indexVal = exprFactory.newLiteral(sqlStmt, orderMapping, startIdx);
                sqlStmt.whereAnd(indexExpr.eq(indexVal), true);
            }
            else
            {
                // Add restrictions on start/end indices as required
                if (startIdx >= 0)
                {
                    SQLExpression indexExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                    SQLExpression indexVal = exprFactory.newLiteral(sqlStmt, orderMapping, startIdx);
                    sqlStmt.whereAnd(indexExpr.ge(indexVal), true);
                }
                else
                {
                    // Just restrict to >= 0 so we don't get any disassociated elements
                    SQLExpression indexExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                    SQLExpression indexVal = exprFactory.newLiteral(sqlStmt, orderMapping, 0);
                    sqlStmt.whereAnd(indexExpr.ge(indexVal), true);
                }

                if (endIdx >= 0)
                {
                    SQLExpression indexExpr2 = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                    SQLExpression indexVal2 = exprFactory.newLiteral(sqlStmt, orderMapping, endIdx);
                    sqlStmt.whereAnd(indexExpr2.lt(indexVal2), true);
                }
            }

            if (needsOrdering)
            {
                // Order by the ordering column, when present
                SQLTable orderSqlTbl =
                    SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
                SQLExpression[] orderExprs = new SQLExpression[orderMapping.getNumberOfDatastoreMappings()];
                boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreMappings()];
                orderExprs[0] = exprFactory.newExpression(sqlStmt, orderSqlTbl, orderMapping);
                sqlStmt.setOrdering(orderExprs, descendingOrder);
            }
        }
        else
        {
            // Apply ordering defined by <order-by>
            if (elementInfo != null && elementInfo.length > 0)
            {
                DatastoreClass elementTbl = elementInfo[0].getDatastoreClass();
                FieldOrder[] orderComponents = ownerMemberMetaData.getOrderMetaData().getFieldOrders();
                SQLExpression[] orderExprs = new SQLExpression[orderComponents.length];
                boolean[] orderDirs = new boolean[orderComponents.length];

                for (int i=0;i<orderComponents.length;i++)
                {
                    String fieldName = orderComponents[i].getFieldName();
                    JavaTypeMapping fieldMapping = elementTbl.getMemberMapping(elementInfo[0].getAbstractClassMetaData().getMetaDataForMember(fieldName));
                    orderDirs[i] = !orderComponents[i].isForward();
                    SQLTable fieldSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), fieldMapping);
                    orderExprs[i] = exprFactory.newExpression(sqlStmt, fieldSqlTbl, fieldMapping);
                }

                sqlStmt.setOrdering(orderExprs, orderDirs);
            }
View Full Code Here

                }
            }
        }

        // Apply condition to filter by owner
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);

        if (relationDiscriminatorMapping != null)
        {
            // Apply condition on distinguisher field to filter by distinguisher (when present)
            SQLTable distSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), relationDiscriminatorMapping);
            SQLExpression distExpr = exprFactory.newExpression(sqlStmt, distSqlTbl, relationDiscriminatorMapping);
            SQLExpression distVal = exprFactory.newLiteral(sqlStmt, relationDiscriminatorMapping, relationDiscriminatorValue);
            sqlStmt.whereAnd(distExpr.eq(distVal), true);
        }

        if (orderMapping != null)
        {
            // Order by the ordering column, when present
            SQLTable orderSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
            SQLExpression[] orderExprs = new SQLExpression[orderMapping.getNumberOfDatastoreMappings()];
            boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreMappings()];
            orderExprs[0] = exprFactory.newExpression(sqlStmt, orderSqlTbl, orderMapping);
            sqlStmt.setOrdering(orderExprs, descendingOrder);
View Full Code Here

                    sqlStmt.union(elementStmt);
                }
            }

            // Select the required fields
            SQLTable elementSqlTbl = sqlStmt.getTable(elementInfo[0].getDatastoreClass(),
                sqlStmt.getPrimaryTable().getGroupName());
            SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingClass,
                ownerSM.getExecutionContext().getFetchPlan(), elementSqlTbl, emd, 0);
        }

        // Apply condition on join-table owner field to filter by owner
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);

        if (relationDiscriminatorMapping != null)
        {
            // Apply condition on distinguisher field to filter by distinguisher (when present)
            SQLTable distSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), relationDiscriminatorMapping);
            SQLExpression distExpr = exprFactory.newExpression(sqlStmt, distSqlTbl, relationDiscriminatorMapping);
            SQLExpression distVal = exprFactory.newLiteral(sqlStmt, relationDiscriminatorMapping, relationDiscriminatorValue);
            sqlStmt.whereAnd(distExpr.eq(distVal), true);
        }

        if (orderMapping != null)
        {
            // Order by the ordering column, when present
            SQLTable orderSqlTbl =
                SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
            SQLExpression[] orderExprs = new SQLExpression[orderMapping.getNumberOfDatastoreMappings()];
            boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreMappings()];
            orderExprs[0] = exprFactory.newExpression(sqlStmt, orderSqlTbl, orderMapping);
            sqlStmt.setOrdering(orderExprs, descendingOrder);
View Full Code Here

        SQLStatement sqlStmt = null;

        final ClassLoaderResolver clr = ownerSM.getExecutionContext().getClassLoaderResolver();
        final Class keyCls = clr.classForName(elementType);
        RDBMSStoreManager storeMgr = (RDBMSStoreManager)this.storeMgr;
        SQLTable containerSqlTbl = null;
        MapType mapType = getOwnerMemberMetaData().getMap().getMapType();
        if (emd != null && emd.getDiscriminatorStrategyForTable() != null &&
            emd.getDiscriminatorStrategyForTable() != DiscriminatorStrategy.NONE)
        {
            // Map<PC, ?> where key has discriminator
            if (ClassUtils.isReferenceType(keyCls))
            {
                // Take the metadata for the first implementation of the reference type
                String[] clsNames = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
                Class[] cls = new Class[clsNames.length];
                for (int j=0; j<clsNames.length; j++)
                {
                    cls[j] = clr.classForName(clsNames[j]);
                }
                sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr, cls, true, null, null).getStatement();
            }
            else
            {
                sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr,
                    clr.classForName(elementInfo[0].getClassName()), true, null, null).getStatement();
            }
            containerSqlTbl = sqlStmt.getPrimaryTable();
            iterateUsingDiscriminator = true;

            if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY)
            {
                // Select key fields
                containerSqlTbl = sqlStmt.getPrimaryTable();

                iteratorMappingDef = new StatementClassMapping();
                SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
                    ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
            }
            else
            {
                // MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
                // Join to join table and select key fields
                JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
                containerSqlTbl = sqlStmt.innerJoin(sqlStmt.getPrimaryTable(), keyIdMapping,
                    containerTable, null, elementMapping, null, null);

                iteratorMappingDef = new StatementClassMapping();
                SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
                    ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
            }
        }
        else
        {
            if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY)
            {
                // Select of key in key table (allow union of possible key types)
                iteratorMappingDef = new StatementClassMapping();
                UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
                stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
                iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
                sqlStmt = stmtGen.getStatement();
                containerSqlTbl = sqlStmt.getPrimaryTable();

                SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
                    ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
            }
            else
            {
                // MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
                if (emd != null)
                {
                    // Select of key table, joining to join table
                    iteratorMappingDef = new StatementClassMapping();
                    UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
                    stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
                    iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
                    sqlStmt = stmtGen.getStatement();

                    JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
                    containerSqlTbl = sqlStmt.innerJoin(sqlStmt.getPrimaryTable(), keyIdMapping,
                        containerTable, null, elementMapping, null, null);

                    SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
                        ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
                }
                else
                {
                    // Select of key in join table
                    sqlStmt = new SQLStatement(storeMgr, containerTable, null, null);
                    sqlStmt.setClassLoaderResolver(clr);
                    containerSqlTbl = sqlStmt.getPrimaryTable();
                    sqlStmt.select(sqlStmt.getPrimaryTable(), elementMapping, null);
                }
            }
        }

        // Apply condition on owner field to filter by owner
        SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, containerSqlTbl, ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
View Full Code Here

                ClassTable verTable = table.getTableManagingMapping(verMapping);
                VersionMetaData vermd = candidateCmd.getVersionMetaData();
                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,
                        stmt.getSQLExpressionFactory().getMappingForType(Integer.class, false), new Integer(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;
                }
                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 = VersionHelper.getNextVersion(vermd.getVersionStrategy(), null);
                    JavaTypeMapping valMapping = stmt.getSQLExpressionFactory().getMappingForType(newVersion.getClass(), false);
                    SQLExpression valExpr = new TemporalLiteral(stmt, valMapping, newVersion, null);
View Full Code Here

     */
    protected void compileFromClassExpression(ClassExpression clsExpr)
    {
        Symbol clsExprSym = clsExpr.getSymbol();
        Class baseCls = (clsExprSym != null ? clsExprSym.getValueType() : null);
        SQLTable candSqlTbl = stmt.getPrimaryTable();
        MetaDataManager mmgr = storeMgr.getMetaDataManager();
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(baseCls, clr);
        if (baseCls != null && baseCls != compilation.getCandidateClass())
        {
            // Not candidate class so must be cross join (JPA spec 4.4.5)
            DatastoreClass candTbl = storeMgr.getDatastoreClass(baseCls.getName(), clr);
            candSqlTbl = stmt.crossJoin(candTbl, clsExpr.getAlias(), null);
            SQLTableMapping tblMapping = new SQLTableMapping(candSqlTbl, cmd, candTbl.getIdMapping());
            setSQLTableMappingForAlias(clsExpr.getAlias(), tblMapping);
        }

        if (clsExpr.getCandidateExpression() != null && parentMapper != null)
        {
            // User defined the candidate of the subquery as an implied join to the outer query
            // e.g SELECT c FROM Customer c WHERE EXISTS (SELECT o FROM c.orders o ...)
            // so add the join(s) to the outer query
            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;
                int relationType = leftMmd.getRelationType(clr);

                switch (relationType)
                {
                    case Relation.ONE_TO_ONE_BI :
                    case Relation.ONE_TO_MANY_BI :
                    case Relation.MANY_TO_ONE_BI :
                    case Relation.MANY_TO_MANY_BI :
                        rightMmd = leftMmd.getRelatedMemberMetaData(clr)[0]; // Take first possible
                        rightCmd = rightMmd.getAbstractClassMetaData();
                        break;
                    case Relation.ONE_TO_ONE_UNI :
                        rightCmd = mmgr.getMetaDataForClass(leftMmd.getType(), clr);
                        rightMmd = null;
                        break;
                    case Relation.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);
                        }
                        break;
                    default :
                        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;
            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;
                int relationType = leftMmd.getRelationType(clr);

                switch (relationType)
                {
                    case Relation.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.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                leftTbl, null, leftMapping, null, null);
                        }

                        break;
                    }
                    case Relation.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.innerJoin(rSqlTbl, rightMapping,
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        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.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                    leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null);
                            }
                        }
                        break;
                    }
                    case Relation.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
                            ElementContainerTable joinTbl =
                                (ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
                            SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                joinTbl, null, joinTbl.getElementMapping(), null, null);

                            if (i == 0)
                            {
                                // Add where clause join table to outer table
                                SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
                                    outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                                SQLExpression joinExpr = exprFactory.newExpression(stmt,
                                    joinSqlTbl, joinTbl.getOwnerMapping());
                                stmt.whereAnd(outerExpr.eq(joinExpr), false);
                            }
                            else
                            {
                                // Join to left table
                                lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        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.innerJoin(rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd),
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        break;
                    }
                    case Relation.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
                            ElementContainerTable joinTbl =
                                (ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
                            SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                joinTbl, null, joinTbl.getElementMapping(), null, null);

                            if (i == 0)
                            {
                                // Add where clause join table to outer table
                                SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
                                    outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                                SQLExpression joinExpr = exprFactory.newExpression(stmt,
                                    joinSqlTbl, joinTbl.getOwnerMapping());
                                stmt.whereAnd(outerExpr.eq(joinExpr), false);
                            }
                            else
                            {
                                // Join to left table
                                lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        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.innerJoin(rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd),
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        break;
                    }
                    case Relation.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
                            ElementContainerTable joinTbl =
                                (ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
                            SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                joinTbl, null, joinTbl.getOwnerMapping(), null, null);

                            if (i == 0)
                            {
                                // Add where clause join table to outer table
                                SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
                                    outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
                                SQLExpression joinExpr = exprFactory.newExpression(stmt,
                                    joinSqlTbl, joinTbl.getElementMapping());
                                stmt.whereAnd(outerExpr.eq(joinExpr), false);
                            }
                            else
                            {
                                // Join to left table
                                lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                    leftTbl, null, leftTbl.getIdMapping(), null, null);
                            }
                        }
                        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.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
                                    leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null);
                            }
                        }
                    }
                    default:
                        break;
                }
                rSqlTbl = lSqlTbl;
            }
        }

        // Process all join expressions
        Expression rightExpr = clsExpr.getRight();
        SQLTable sqlTbl = candSqlTbl;
        while (rightExpr != null)
        {
            if (rightExpr instanceof JoinExpression)
            {
                JoinExpression joinExpr = (JoinExpression)rightExpr;
                JoinType joinType = joinExpr.getType();
                String joinAlias = joinExpr.getAlias();
                PrimaryExpression joinPrimExpr = joinExpr.getPrimaryExpression();

                Iterator<String> iter = joinPrimExpr.getTuples().iterator();
                String rootId = iter.next();
                String joinTableGroupName = null;
                if (rootId.equalsIgnoreCase(candidateAlias))
                {
                    // Join relative to the candidate
                    // Name table group of joined-to as per the relation
                    // Note : this will only work for one level out from the candidate TODO Extend this
                    joinTableGroupName = joinPrimExpr.getId();
                }
                else
                {
                    // Join relative to some other alias
                    SQLTableMapping sqlTblMapping = getSQLTableMappingForAlias(rootId);
                    if (sqlTblMapping != null)
                    {
                        cmd = sqlTblMapping.cmd;
                        joinTableGroupName = sqlTblMapping.table.getGroupName();
                        sqlTbl = sqlTblMapping.table;
                    }
                    else
                    {
                        throw new NucleusUserException("Query has " + joinPrimExpr.getId() + " yet the first component "+
                            rootId + " is unknown!");
                    }
                }

                while (iter.hasNext())
                {
                    String id = iter.next();
                    AbstractMemberMetaData mmd = cmd.getMetaDataForMember(id);
                    int relationType = mmd.getRelationType(clr);
                    DatastoreClass relTable = null;
                    AbstractMemberMetaData relMmd = null;
                    // TODO Cater for map in 1-N relations?
                    switch (relationType)
                    {
                        case Relation.ONE_TO_ONE_UNI:
                            relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
                            cmd = mmgr.getMetaDataForClass(mmd.getType(), clr);
                            if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                            {
                                sqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getMemberMapping(mmd),
                                    relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                            }
                            else
                            {
                                sqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getMemberMapping(mmd),
                                    relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                            }
                            break;
                        case Relation.ONE_TO_ONE_BI:
                            relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
                            cmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
                            if (mmd.getMappedBy() != null)
                            {
                                relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    sqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relTable.getMemberMapping(relMmd), null, joinTableGroupName);
                                }
                                else
                                {
                                    sqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relTable.getMemberMapping(relMmd), null, joinTableGroupName);
                                }
                            }
                            else
                            {
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    sqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getMemberMapping(mmd), relTable, null,
                                        relTable.getIdMapping(), null, joinTableGroupName);
                                }
                                else
                                {
                                    sqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getMemberMapping(mmd), relTable, null,
                                        relTable.getIdMapping(), null, joinTableGroupName);
                                }
                            }
                            break;
                        case Relation.ONE_TO_MANY_BI:
                            relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
                            cmd = mmd.getCollection().getElementClassMetaData(clr, mmgr);
                            relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                            if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null)
                            {
                                // Join to join table, then to related table
                                ElementContainerTable joinTbl = (ElementContainerTable)storeMgr.getDatastoreContainerObject(mmd);
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    SQLTable joinSqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                    sqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                                else
                                {
                                    SQLTable joinSqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                    sqlTbl = stmt.leftOuterJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                            }
                            else
                            {
                                // Join to related table
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    sqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relTable.getMemberMapping(relMmd), null, joinTableGroupName);
                                }
                                else
                                {
                                    sqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relTable.getMemberMapping(relMmd), null, joinTableGroupName);
                                }
                            }
                            break;
                        case Relation.ONE_TO_MANY_UNI:
                            relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
                            cmd = mmd.getCollection().getElementClassMetaData(clr, mmgr);
                            if (mmd.getJoinMetaData() != null)
                            {
                                // Join to join table, then to related table
                                ElementContainerTable joinTbl = (ElementContainerTable)storeMgr.getDatastoreContainerObject(mmd);
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    SQLTable joinSqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                    sqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                                else
                                {
                                    SQLTable joinSqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                    sqlTbl = stmt.leftOuterJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                            }
                            else
                            {
                                // Join to related table
                                JavaTypeMapping relMapping = relTable.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    sqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relMapping, null, joinTableGroupName);
                                }
                                else
                                {
                                    sqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        relTable, null, relMapping, null, joinTableGroupName);
                                }
                            }
                            break;
                        case Relation.MANY_TO_MANY_BI:
                            relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
                            cmd = mmd.getCollection().getElementClassMetaData(clr, mmgr);
                            relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                            // Join to join table, then to related table
                            CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(mmd);
                            if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                            {
                                SQLTable joinSqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                    joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                sqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                    relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                            }
                            else
                            {
                                SQLTable joinSqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                    joinTbl, null, joinTbl.getOwnerMapping(), null, null);
                                sqlTbl = stmt.leftOuterJoin(joinSqlTbl, joinTbl.getElementMapping(),
                                    relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                            }
                            break;
                        case Relation.MANY_TO_ONE_BI:
                            relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
                            cmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
                            relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                            if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null)
                            {
                                // Join to join table, then to related table
                                joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(relMmd);
                                if (joinType == JoinType.JOIN_INNER || joinType == JoinType.JOIN_INNER_FETCH)
                                {
                                    SQLTable joinSqlTbl = stmt.innerJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getElementMapping(), null, null);
                                    sqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                                else
                                {
                                    SQLTable joinSqlTbl = stmt.leftOuterJoin(sqlTbl, sqlTbl.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getElementMapping(), null, null);
                                    sqlTbl = stmt.leftOuterJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
                                        relTable, null, relTable.getIdMapping(), null, joinTableGroupName);
                                }
                            }
View Full Code Here

            sqlStmt.setClassLoaderResolver(clr);

            if (vmd != null)
            {
                // Left outer join to value table (so we allow for null values)
                SQLTable valueSqlTbl = sqlStmt.leftOuterJoin(sqlStmt.getPrimaryTable(), valueMapping,
                    valueTable, null, valueTable.getIdMapping(), null, null);

                // Select the value field(s)
                SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, getMappingDef,
                    ownerSM.getExecutionContext().getFetchPlan(), valueSqlTbl, vmd, 0);
            }
            else
            {
                sqlStmt.select(sqlStmt.getPrimaryTable(), valueMapping, null);
            }
        }

        // Apply condition on owner field to filter by owner
        SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
        SQLTable ownerSqlTbl =
            SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
        SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
        SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
        sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
View Full Code Here

                {
                    throw new NucleusUserException("Field "+ mmd.getFullFieldName() + " is not marked as persistent so cannot be queried");
                }

                // Find the table and the mapping for this field in the table
                SQLTable sqlTbl = null;
                if (mapping instanceof EmbeddedMapping)
                {
                    // Embedded into the current table
                    sqlTbl = sqlMapping.table;
                    mapping = ((EmbeddedMapping)mapping).getJavaTypeMapping(component);
                }
                else
                {
                    DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
                    if (table == null)
                    {
                        AbstractClassMetaData[] subCmds = storeMgr.getClassesManagingTableForClass(cmd, clr);
                        if (subCmds.length == 1)
                        {
                            table = storeMgr.getDatastoreClass(subCmds[0].getFullClassName(), clr);
                        }
                        else
                        {
                            throw new NucleusUserException("Unable to find table for primary " + primaryName +
                                " since the class " + cmd.getFullClassName() + " is managed in multiple tables");
                        }
                    }
                    mapping = table.getMemberMapping(mmd);
                    sqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(theStmt, sqlMapping.table, mapping);
                }

                int relationType = mmd.getRelationType(clr);
                switch (relationType)
                {
                    case Relation.NONE :
                        sqlMappingNew = new SQLTableMapping(sqlTbl, cmd, mapping);
                        cmd = sqlMappingNew.cmd;
                        setSQLTableMappingForAlias(primaryName, sqlMappingNew);
                        break;

                    case Relation.ONE_TO_ONE_UNI :
                    case Relation.ONE_TO_ONE_BI :
                        if (mmd.getMappedBy() != null)
                        {
                            // FK in other table so join to that first
                            AbstractMemberMetaData relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                            if (relMmd.getAbstractClassMetaData().isEmbeddedOnly())
                            {
                                // Member is embedded, so keep same SQL table mapping
                                sqlMappingNew = sqlMapping;
                                cmd = relMmd.getAbstractClassMetaData();
                            }
                            else
                            {
                                // Member is in own table, so move to that SQL table mapping
                                DatastoreClass relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
                                JavaTypeMapping relMapping = relTable.getMemberMapping(relMmd);
                                // Join to related table unless we already have the join in place
                                sqlTbl = theStmt.getTable(relTable, primaryName);
                                if (sqlTbl == null)
                                {
                                    sqlTbl = SQLStatementHelper.addJoinForOneToOneRelation(theStmt,
                                        sqlMapping.table.getTable().getIdMapping(), sqlMapping.table,
                                        relMapping, relTable, null, null, primaryName, defaultJoinType);
                                }

                                if (iter.hasNext())
                                {
                                    sqlMappingNew = new SQLTableMapping(sqlTbl, relMmd.getAbstractClassMetaData(),
                                        relTable.getIdMapping());
                                    cmd = sqlMappingNew.cmd;
                                }
                                else
                                {
                                    sqlMappingNew = new SQLTableMapping(sqlTbl, cmd, relTable.getIdMapping());
                                    cmd = sqlMappingNew.cmd;
                                }
                            }
                        }
                        else
                        {
                            // FK is at this side, so only join if further component provided, or if forcing
                            if (forceJoin == null && !iter.hasNext())
                            {
                                if (primExpr.getParent() != null &&
                                    primExpr.getParent().getOperator() == Expression.OP_CAST)
                                {
                                    // Cast and not an interface field, so do a join to the table of the persistable object
                                    if (!(mapping instanceof ReferenceMapping))
                                    {
                                        // Don't join with interface field since represents multiple implementations
                                        // and the cast will be a restrict on which implementation to join to
                                        forceJoin = Boolean.TRUE;
                                    }
                                }
                            }

                            if (iter.hasNext() || Boolean.TRUE.equals(forceJoin))
                            {
                                AbstractClassMetaData relCmd = null;
                                JavaTypeMapping relMapping = null;
                                DatastoreClass relTable = null;
                                if (relationType == Relation.ONE_TO_ONE_BI)
                                {
                                    AbstractMemberMetaData relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                                    relCmd = relMmd.getAbstractClassMetaData();
                                }
                                else
                                {
                                    relCmd = ec.getMetaDataManager().getMetaDataForClass(mmd.getTypeName(), clr);
                                }

                                if (relCmd != null && relCmd.isEmbeddedOnly())
                                {
                                    // Member is embedded so keep same SQL table mapping
                                    sqlMappingNew = sqlMapping;
                                    cmd = relCmd;
                                }
                                else
                                {
                                    // Member is in own table, so move to that SQL table mapping
                                    relTable = storeMgr.getDatastoreClass(relCmd.getFullClassName(), clr);
                                    relMapping = relTable.getIdMapping();

                                    // Join to other table unless we already have the join in place
                                    sqlTbl = theStmt.getTable(relTable, primaryName);
                                    if (sqlTbl == null)
                                    {
                                        sqlTbl = SQLStatementHelper.addJoinForOneToOneRelation(theStmt, mapping,
                                            sqlMapping.table, relMapping, relTable, null, null, primaryName,
                                            defaultJoinType);
                                    }

                                    sqlMappingNew = new SQLTableMapping(sqlTbl, relCmd, relMapping);
                                    cmd = sqlMappingNew.cmd;
                                    setSQLTableMappingForAlias(primaryName, sqlMappingNew);
                                }
                            }
                            else
                            {
                                sqlMappingNew = new SQLTableMapping(sqlTbl, cmd, mapping);
                                cmd = sqlMappingNew.cmd;
                                // Don't register the SQLTableMapping for this alias since only using FK
                            }
                        }
                        break;

                    case Relation.MANY_TO_ONE_BI :
                        AbstractMemberMetaData relMmd = mmd.getRelatedMemberMetaData(clr)[0];
                        DatastoreClass relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
                        if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null)
                        {
                            // Has join table so use that
                            sqlTbl = theStmt.getTable(relTable, primaryName);
                            if (sqlTbl == null)
                            {
                                // Join to the join table
                                CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(relMmd);
                                if (defaultJoinType == org.datanucleus.store.rdbms.sql.SQLJoin.JoinType.INNER_JOIN)
                                {
                                    SQLTable joinSqlTbl = theStmt.innerJoin(
                                        sqlMapping.table, sqlMapping.table.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getElementMapping(),
                                        null, null);
                                    sqlTbl = theStmt.innerJoin(
                                        joinSqlTbl, joinTbl.getOwnerMapping(),
                                        relTable, null, relTable.getIdMapping(),
                                        null, primaryName);
                                }
                                else if (defaultJoinType == org.datanucleus.store.rdbms.sql.SQLJoin.JoinType.LEFT_OUTER_JOIN ||
                                        defaultJoinType == null)
                                {
                                    SQLTable joinSqlTbl = theStmt.leftOuterJoin(
                                        sqlMapping.table, sqlMapping.table.getTable().getIdMapping(),
                                        joinTbl, null, joinTbl.getElementMapping(),
                                        null, null);
                                    sqlTbl = theStmt.leftOuterJoin(
                                        joinSqlTbl, joinTbl.getOwnerMapping(),
View Full Code Here

        AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(type, clr);
        if (cmd != null)
        {
            // Variable is persistent type, so add cross join (may need changing later on in compilation)
            DatastoreClass varTable = storeMgr.getDatastoreClass(varSym.getValueType().getName(), clr);
            SQLTable varSqlTbl = stmt.crossJoin(varTable, "VAR_" + varName, null);
            SQLTableMapping varSqlTblMapping = new SQLTableMapping(varSqlTbl, cmd, varTable.getIdMapping());
            setSQLTableMappingForAlias(varName, varSqlTblMapping);
            return exprFactory.newExpression(stmt, varSqlTbl, varTable.getIdMapping());
        }
        return null;
View Full Code Here

TOP

Related Classes of org.datanucleus.store.rdbms.sql.SQLTable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.