Package org.jpox.store.mapped.expression

Examples of org.jpox.store.mapped.expression.LogicSetExpression


                    // TODO Move this hardcoded table name out into the calling class so it controls the names of all table aliases
                    DatastoreIdentifier sourceTableIdentifier =
                        storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE,
                            "SOURCECLASS" + fmd.getAbsoluteFieldNumber() + "_" + i);
                    LogicSetExpression sourceTableExpr = qs.newTableExpression(sourceTables[i], sourceTableIdentifier, true)[0];

                    // Left outer join from target ID to the mapped-by field on the source class
                    ScalarExpression sourceExpr = sourceMappings[i].newScalarExpression(qs, sourceTableExpr);
                    ScalarExpression targetExpr;
                    if (tableIdentifier != null)
                    {
                        LogicSetExpression targetTableExpr;
                        if (qs.getTableExpression(tableIdentifier) == null)
                        {
                            targetTableExpr = qs.newTableExpression(m.getDatastoreContainer(), tableIdentifier);
                        }
                        else
                        {
                            targetTableExpr = qs.getTableExpression(tableIdentifier);
                        }
                        targetExpr = m.getDatastoreContainer().getIDMapping().newScalarExpression(qs, targetTableExpr);
                    }
                    else
                    {
                        targetExpr = m.getDatastoreContainer().getIDMapping().newScalarExpression(qs,
                            qs.getMainTableExpression());
                    }
                    qs.leftOuterJoin(sourceExpr, targetExpr, sourceTableExpr, true, true);

                    // Select the Id column(s) of the source class (via the Left Outer Join)
                    int[] columnNumbersByField = qs.select(sourceTableIdentifier, sourceTables[i].getIDMapping(), true);

                    // Copy these column numbers into our overall column numbers set
                    if (sourceMappings.length == 1)
                    {
                        // We only have one source so just reference these
                        colNums = columnNumbersByField;
                    }
                    else if (colNums != null)
                    {
                        // Append to the end of where we have got to
                        int[] tmpColNums = colNums;
                        colNums = new int[tmpColNums.length + columnNumbersByField.length];
                        for (int j=0;j<tmpColNums.length;j++)
                        {
                            colNums[j] = tmpColNums[j];
                        }
                        for (int j=0;j<columnNumbersByField.length;j++)
                        {
                            colNums[tmpColNums.length+j] = columnNumbersByField[j];
                        }
                        tmpColNums = null;
                    }
                    else
                    {
                        // Add to the start of our column numbers
                        colNums = new int[columnNumbersByField.length];
                        for (int j=0;j<columnNumbersByField.length;j++)
                        {
                            colNums[j] = columnNumbersByField[j];
                        }
                    }
                }

                statementExpressionIndex.setExpressionIndex(colNums);
            }
        }
        else if (relationType == Relation.MANY_TO_ONE_BI)
        {
            AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
            // TODO Cater for more than 1 related field
            if (fmd.getJoinMetaData() != null || relatedMmds[0].getJoinMetaData() != null)
            {
                // 1-N bidirectional join table relation.
                // Do a LEFT OUTER JOIN to the join table to pick up the value.
                MappedStoreManager storeMgr = qs.getStoreManager();
                DatastoreContainerObject joinTable = storeMgr.getDatastoreContainerObject(relatedMmds[0]);
                JavaTypeMapping referenceMapping = null;
                JavaTypeMapping selectMapping = null;

                DatastoreElementContainer collTable = (DatastoreElementContainer)joinTable;
                referenceMapping = collTable.getElementMapping();
                selectMapping = collTable.getOwnerMapping();

                DatastoreObject mainTable = qs.getMainTableExpression().getMainTable();
                if (!mainTable.equals(joinTable))
                {
                    // Statement selects the element table and the owner column should be selected
                    // Join across to the join table, and select the owner mapping of the join table

                    // TODO Move this hardcoded table name out into the calling class so it controls the names of all table aliases
                    DatastoreIdentifier joinTableIdentifier =
                        storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE,
                            "JOINTABLE" + fmd.getAbsoluteFieldNumber());
                    LogicSetExpression table_expr_sub = qs.newTableExpression(joinTable, joinTableIdentifier, true)[0];

                    // Left outer join from our Id to the mapped-by field on the other class
                    ScalarExpression subExpr = referenceMapping.newScalarExpression(qs, table_expr_sub);
                    ScalarExpression schExpr = null;
                    if (tableIdentifier != null)
                    {
                        LogicSetExpression targetTableExpr;
                        if (qs.getTableExpression(tableIdentifier) == null)
                        {
                            targetTableExpr = qs.newTableExpression(m.getDatastoreContainer(), tableIdentifier);
                        }
                        else
View Full Code Here


                // TODO Cater for more than one related field
                JavaTypeMapping refMapping = targetTable.getFieldMapping(relatedMmds[0]);
                JavaTypeMapping selectMapping = targetTable.getIDMapping();
                DatastoreIdentifier targetTableIdentifier =
                    srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "RELATED" + fmd.getAbsoluteFieldNumber());
                LogicSetExpression targetTe = qs.newTableExpression(targetTable, targetTableIdentifier);
                return new ObjectExpression(qs, this, te, refMapping, targetTe, selectMapping);
            }
            else if (relationType == Relation.MANY_TO_ONE_BI)
            {
                AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
                // TODO Cater for more than one related field
                if (fmd.getJoinMetaData() != null || relatedMmds[0].getJoinMetaData() != null)
                {
                    // Join table relation - only allows for Collection/Array
                    // Create an expression this table to the join table on the element id, selecting the owner id
                    DatastoreContainerObject targetTable = srm.getDatastoreContainerObject(relatedMmds[0]);
                    JavaTypeMapping refMapping = null;
                    JavaTypeMapping selectMapping = null;
                    DatastoreElementContainer elementTable = (DatastoreElementContainer)targetTable;
                    refMapping = elementTable.getElementMapping();
                    selectMapping = elementTable.getOwnerMapping();
                    DatastoreIdentifier targetTableIdentifier =
                        srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "JOINTABLE" + fmd.getAbsoluteFieldNumber());
                    LogicSetExpression targetTe = qs.newTableExpression(targetTable, targetTableIdentifier);
                    return new ObjectExpression(qs, this, te, refMapping, targetTe, selectMapping);
                }
            }
        }
View Full Code Here

                // "superclass-table" are excluded using the discriminator clause
                if (subclassTable != null && !subclassTable.getIdentifier().equals(schemaDataOption.getDatastoreContainerObject().getIdentifier()))
                {
                    DatastoreIdentifier subclassTableIdentifier = storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "SUBCLASS" + (subclasses_seq_id++));
                    QueryExpression st = storeMgr.getDatastoreAdapter().newQueryStatement(subclassTable, subclassTableIdentifier, om.getClassLoaderResolver());
                    LogicSetExpression table_expr_sub = st.newTableExpression(subclassTable, subclassTableIdentifier);
                    JavaTypeMapping subMapping = subclassTable.getIDMapping();
                    st.select(subclassTableIdentifier, subMapping);

                    ScalarExpression subExpr = subMapping.newScalarExpression(qs, table_expr_sub);
                    ScalarExpression schExpr =
View Full Code Here

        }
    }

    private FieldQueryExpression getQueryExpression(DatastoreIdentifier alias, DatastoreField col, String colAlias)
    {
        LogicSetExpression te = (LogicSetExpression)tableExprsByAlias.get(alias);
        if (te == null)
        {
            throw new JPOXException(LOCALISER.msg("052501", alias)).setFatal();
        }
View Full Code Here

     */
    public LogicSetExpression newTableExpression(DatastoreContainerObject table, DatastoreIdentifier alias)
    {
        assertNotFrozen();

        LogicSetExpression te = (LogicSetExpression)tableExprsByAlias.get(alias);
        if (te == null)
        {
            te = ((RDBMSAdapter)storeMgr.getDatastoreAdapter()).newTableExpression(this, table, alias);
            tableExprsByAlias.put(alias, te);
        }
        else
        {
            if (!te.getMainTable().equals(table))
            {
                throw new JPOXException(LOCALISER.msg("052500", alias,
                    this.toStatementText(false))).setFatal();
            }
        }
View Full Code Here

                    subqueryCandidateExprRootAliasInfo.cls : parentExpr.getCandidateClass());
            ClassLoaderResolver clr = query.getObjectManager().getClassLoaderResolver();
            MetaDataManager mmgr = query.getObjectManager().getMetaDataManager();
            MappedStoreManager storeMgr = (MappedStoreManager)query.getStoreManager();
            AbstractClassMetaData leftCmd = mmgr.getMetaDataForClass(cls, clr);
            LogicSetExpression leftTblExpr = (subqueryCandidateExprRootAliasInfo != null ?
                    subqueryCandidateExprRootAliasInfo.tableExpression : parentExpr.getMainTableExpression());
            String leftAlias = (subqueryCandidateExprRootAliasInfo != null ?
                    subqueryCandidateExprRootAliasInfo.alias : parentExpr.getCandidateAlias());
            DatastoreClass leftTable = (DatastoreClass)leftTblExpr.getMainTable();
            for (int i=1;i<tokens.length;i++)
            {
                // Process the join from the previous token to this token
                AbstractMemberMetaData leftMmd = leftCmd.getMetaDataForMember(tokens[i]);
                AbstractClassMetaData rightCmd = null;
                int relationType = leftMmd.getRelationType(clr);
                AbstractMemberMetaData rightMmd = null;
                if (relationType == Relation.ONE_TO_ONE_BI || relationType == Relation.ONE_TO_MANY_BI ||
                    relationType == Relation.MANY_TO_MANY_BI || relationType == Relation.MANY_TO_ONE_BI)
                {
                    rightMmd = leftMmd.getRelatedMemberMetaData(clr)[0]; // Take first possible
                }

                // Find class of right hand side
                if (i == tokens.length-1)
                {
                    cls = candidateClass;
                }
                else
                {
                    if (relationType == Relation.ONE_TO_ONE_BI ||
                        relationType == Relation.ONE_TO_ONE_UNI ||
                        relationType == Relation.MANY_TO_ONE_BI)
                    {
                        cls = leftMmd.getType();
                        rightCmd = mmgr.getMetaDataForClass(cls, clr);
                    }
                    else if (relationType == Relation.ONE_TO_MANY_BI ||
                        relationType == Relation.ONE_TO_MANY_UNI ||
                        relationType == Relation.MANY_TO_MANY_BI)
                    {
                        if (leftMmd.hasCollection())
                        {
                            cls = clr.classForName(leftMmd.getCollection().getElementType());
                            rightCmd = mmgr.getMetaDataForClass(cls, clr);
                        }
                        else if (leftMmd.hasMap())
                        {
                            cls = clr.classForName(leftMmd.getMap().getValueType());
                            rightCmd = mmgr.getMetaDataForClass(cls, clr);
                        }
                    }
                    else
                    {
                        throw new JPOXUserException("Subquery has been specified with a candidate-expression that" +
                            " includes \"" + tokens[i] + "\" that isnt a relation field!!");
                    }
                }

                LogicSetExpression rightTblExpr;
                String rightAlias;
                DatastoreIdentifier rightTblAlias;
                DatastoreClass rightTable;
                if (i == tokens.length-1)
                {
                    // Candidate
                    rightTblExpr = qs.getMainTableExpression();
                    rightTblAlias = qs.getMainTableAlias();
                    rightTable = (DatastoreClass)rightTblExpr.getMainTable();
                    rightAlias = candidateAlias;
                }
                else
                {
                    // Not outer candidate, nor inner candidate so add table. Alias is "T{num}"
                    // TODO Parameterise this naming of intermediate tables
                    rightTable = storeMgr.getDatastoreClass(cls.getName(), clr);
                    rightAlias = "T" + i;
                    rightTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
                        IdentifierFactory.TABLE, rightAlias);
                    rightTblExpr = qs.newTableExpression(rightTable, rightTblAlias);
                }

                if (relationType == Relation.ONE_TO_ONE_UNI ||
                    (relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() == null) ||
                    (relationType == Relation.MANY_TO_ONE_BI &&
                     (leftMmd.getJoinMetaData() == null && rightMmd.getJoinMetaData() == null)))
                {
                    // 1-1/N-1 with FK here
                    ScalarExpression leftExpr = leftTblExpr.newFieldExpression(tokens[i]);
                    ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
                    if (i == 1)
                    {
                        // And condition to outer candidate
                        qs.andCondition(leftExpr.eq(rightExpr), true);
                    }
                    else
                    {
                        // Inner join to table
                        qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
                    }
                }
                else if (relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() != null)
                {
                    // 1-1 with FK on other side
                    ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
                    ScalarExpression rightExpr = rightTblExpr.newFieldExpression(rightMmd.getName());
                    if (i == 1)
                    {
                        // And condition to outer candidate
                        qs.andCondition(leftExpr.eq(rightExpr), true);
                    }
                    else
                    {
                        // Inner join to table
                        qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
                    }
                }
                else if ((relationType == Relation.ONE_TO_MANY_UNI && leftMmd.getJoinMetaData() == null) ||
                    (relationType == Relation.ONE_TO_MANY_BI &&
                     (leftMmd.getJoinMetaData() == null && rightMmd.getJoinMetaData() == null)))
                {
                    // 1-N FK with FK on other side
                    ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
                    ScalarExpression rightExpr = rightTblExpr.newFieldExpression(rightMmd.getName());
                    if (i == 1)
                    {
                        // And condition to outer candidate
                        qs.andCondition(leftExpr.eq(rightExpr), true);
                    }
                    else
                    {
                        // Inner join to table
                        qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
                    }
                }
                else if (relationType == Relation.ONE_TO_MANY_UNI && leftMmd.getJoinMetaData() != null)
                {
                    // 1-N uni JoinTable
                    ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
                    ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
                    ScalarExpression leftCentreExpr = null;
                    ScalarExpression rightCentreExpr = null;
                    LogicSetExpression joinTblExpr = null;
                    if (leftMmd.hasCollection())
                    {
                        CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(leftMmd);
                        DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
                            IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
                        joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
                        leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
                        rightCentreExpr = joinTbl.getElementMapping().newScalarExpression(qs, joinTblExpr);
                    }
                    else if (leftMmd.hasMap())
                    {
                        MapTable joinTbl = (MapTable)storeMgr.getDatastoreContainerObject(leftMmd);
                        DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
                            IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
                        joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
                        leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
                        rightCentreExpr = joinTbl.getValueMapping().newScalarExpression(qs, joinTblExpr);
                    }

                    if (i == 1)
                    {
                        // And condition to outer candidate
                        qs.andCondition(leftExpr.eq(leftCentreExpr), true);
                    }
                    else
                    {
                        // Inner join to table
                        qs.innerJoin(leftCentreExpr, leftExpr, leftTblExpr, true, true);
                    }
                    qs.innerJoin(rightExpr, rightCentreExpr, joinTblExpr, true, true);
                }
                else if ((relationType == Relation.ONE_TO_MANY_BI &&
                    (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)) ||
                    (relationType == Relation.MANY_TO_ONE_BI &&
                     (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)) ||
                     relationType == Relation.MANY_TO_MANY_BI)
                {
                    // 1-N/N-1 bi JoinTable
                    ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
                    ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
                    ScalarExpression leftCentreExpr = null;
                    ScalarExpression rightCentreExpr = null;
                    LogicSetExpression joinTblExpr = null;
                    if (leftMmd.hasCollection() || rightMmd.hasCollection())
                    {
                        CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(leftMmd);
                        DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
                            IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
View Full Code Here

    {
        QueryExpression stmt;

        JavaTypeMapping discriminatorMapping = null;
        DiscriminatorMetaData discriminatorMetaData = null;
        LogicSetExpression discriminatorTableExpr = null;
        if (sourceTable instanceof SCOTable)
        {
            // * Selecting the join table of a JoinTable (normal) relationship, and joining to the element table
            stmt = dba.newQueryStatement(sourceTable, candidateId, clr);

View Full Code Here

            boolean leftOuterJoin)
    {
        // TODO Make this identifier specifiable as input to the iterator statement
        DatastoreIdentifier targetTableIdentifier =
            stmt.getStoreManager().getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "ELEMENT");
        LogicSetExpression teTargetElement = stmt.newTableExpression(elementTargetTable, targetTableIdentifier);
        ScalarExpression sourceExpr = sourceMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
        ScalarExpression targetExpr =
            elementTargetTable.getIDMapping().newScalarExpression(stmt, teTargetElement);
        if (leftOuterJoin)
        {
View Full Code Here

            boolean leftOuterJoin)
    {
        // TODO Make this identifier specifiable as input to the iterator statement
        DatastoreIdentifier sourceTableIdentifier =
            stmt.getStoreManager().getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "VALUE");
        LogicSetExpression teSourceElement = stmt.newTableExpression(sourceTable, sourceTableIdentifier);
        ScalarExpression sourceExpr = sourceMapping.newScalarExpression(stmt, teSourceElement);
        ScalarExpression targetExpr =
            elementTargetTable.getIDMapping().newScalarExpression(stmt, stmt.getMainTableExpression());
        if (leftOuterJoin)
        {
View Full Code Here

            String targetSubElementType = (String) iterTargetSubElementType.next();

            QueryExpression targetQS;
            DatastoreClass cbtTargetSubElementType = storeMgr.getDatastoreClass(targetSubElementType, clr);
            DatastoreIdentifier tiTargetSubElementType;
            LogicSetExpression teTargetSubElementType;
            ScalarExpression targetSubElementTypeExpr;

            DatastoreClass[] targetSubElementTypes = null;
            if (cbtTargetSubElementType == null)
            {
View Full Code Here

TOP

Related Classes of org.jpox.store.mapped.expression.LogicSetExpression

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.