Package org.jpox.store.mapped.expression

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


        {
            return null;
        }

        final ClassLoaderResolver clr = ownerSM.getObjectManager().getClassLoaderResolver();
        QueryExpression stmt = null;
        if (elementInfo[0].getDatastoreClass().getDiscriminatorMetaData() != null &&
            elementInfo[0].getDatastoreClass().getDiscriminatorMetaData().getStrategy() != DiscriminatorStrategy.NONE)
        {
            //when one uses discriminator, all must have
            if (storeMgr.getOMFContext().getTypeManager().isReferenceType(
                clr.classForName(ownerMemberMetaData.getCollection().getElementType())))
            {               
                // Take the metadata for the first implementation of the reference type
                String[] clsNames =
                    storeMgr.getOMFContext().getMetaDataManager().getClassesImplementingInterface(
                        ownerMemberMetaData.getCollection().getElementType(), clr);
                Class[] cls = new Class[clsNames.length];
                for( int i=0; i<clsNames.length; i++)
                {
                    cls[i] = clr.classForName(clsNames[i]);
                }
                stmt = new DiscriminatorIteratorStatement(clr,
                    cls, true, this.storeMgr, true).getQueryStatement(null);
            }
            else
            {
                stmt = new DiscriminatorIteratorStatement(clr, new Class[] {clr.classForName(elementInfo[0].getClassName())},
                    true, this.storeMgr, true).getQueryStatement(null);
            }
            iterateUsingDiscriminator = true;
        }
        else
        {
            for (int i=0;i<elementInfo.length;i++)
            {
                final Class elementCls = clr.classForName(this.elementInfo[i].getClassName());
                final int elementNo = i;
                QueryExpression subStmt = new UnionIteratorStatement(clr, elementCls, true, this.storeMgr,
                    elementCls, elementMapping, elementInfo[elementNo].getDatastoreClass(), false,
                    null, true, false).getQueryStatement(null);
                if (stmt == null)
                {
                    stmt = subStmt;
View Full Code Here


        ClassLoaderResolver clr = sm.getObjectManager().getClassLoaderResolver();
        DatastoreContainerObject candidateTable = storeMgr.getDatastoreClass(candidateClass, clr);

        // QueryStatement for the element table
        QueryExpression stmt = dba.newQueryStatement(candidateTable, clr);

        // Join to the owner
        ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
        ScalarExpression ownerVal = ownerMapping.newLiteral(stmt, sm.getObject());
        stmt.andCondition(ownerExpr.eq(ownerVal));

        // Select the element table ID mapping
        stmt.select(elementMapping);

        return stmt;
    }
View Full Code Here

    public QueryExpression getQueryStatement(DatastoreIdentifier candidateAlias)
    {
        if (storeMgr.getOMFContext().getTypeManager().isSupportedType(candidateFullClassName))
        {
            // SCO candidates, embedded in the source so just select the source table
            QueryExpression qs = dba.newQueryStatement(sourceTable, candidateAlias, clr);
            qs.select(sourceMapping);
            return qs;
        }
        else
        {
            // FCO candidates
            if (candidateTable == null)
            {
                // Candidate class has no table! so see if it has one subclass (with table) only and use that
                AbstractClassMetaData acmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(candidateFullClassName, clr);
                AbstractClassMetaData subclassCmds[] = storeMgr.getClassesManagingTableForClass(acmd, clr);
                if (subclassCmds != null && subclassCmds.length == 1)
                {
                    // Candidate uses "subclass-table" and only one subclass
                    candidateTable = storeMgr.getDatastoreClass(subclassCmds[0].getFullClassName(), clr);
                    candidateFullClassName = subclassCmds[0].getFullClassName();
                }
                else
                {
                    throw new JPOXUserException("Attempt to create iterator for class " + candidateFullClassName +
                        " that is using \"subclass-table\" inheritance strategy and that doesnt have only 1 subclass." +
                        " This is not currently supported");
                }
            }

            if (!includeSubclasses && withMetadata == null)
            {
                // No need to include the JPOXMETADATA if no subclasses considered
                withMetadata = Boolean.FALSE;
            }

            // Find set of possible candidates
            Set subclasses = null;
            if (includeSubclasses)
            {
                subclasses = storeMgr.getSubClassesForClass(candidateFullClassName, true, clr);
                Iterator iter = subclasses.iterator();
                while (iter.hasNext())
                {
                    String subclassName = (String)iter.next();
                    try
                    {
                        Class subclass = clr.classForName(subclassName);
                        if (Modifier.isAbstract(subclass.getModifiers()))
                        {
                            // Remove since abstract hence not instantiable
                            iter.remove();
                        }
                    }
                    catch (Exception e)
                    {
                        // Remove since class not found
                        iter.remove();
                    }
                }
            }

            // Create the main query
            String queryCandidateClassName = candidateFullClassName;
            try
            {
                Class candidateClass = clr.classForName(candidateFullClassName);
                if (Modifier.isAbstract(candidateClass.getModifiers()))
                {
                    // Candidate is abstract so not instantiable so try to find a subclass using same table
                    Iterator iter = subclasses.iterator();
                    while (iter.hasNext())
                    {
                        String subclassName = (String)iter.next();
                        DatastoreClass subclassTable = storeMgr.getDatastoreClass(subclassName, clr);
                        if (subclassTable == candidateTable)
                        {
                            // Input candidate is abstract but this subclass uses the same table so swap it for that
                            queryCandidateClassName = subclassName;
                            iter.remove(); // Not needed as subclass now since used as primary class
                            break;
                        }
                    }
                }
            }
            catch (Exception e) {}

            QueryExpression stmt = getQueryForElement(queryCandidateClassName, candidateTable, false, candidateAlias);

            if (includeSubclasses)
            {
                // Add a select for each subclass elements, and union the selects
                Iterator iterator = subclasses.iterator();
                while (iterator.hasNext())
                {
                    String subCandidateType = (String) iterator.next();
                    DatastoreClass subCandidateTable = storeMgr.getDatastoreClass(subCandidateType, clr);
                    if (subCandidateTable != null)
                    {
                        // Add UNION to any subclasses that have their own table
                        // This omits those using "subclass-table".
                        // Those will have their own subclasses where they store their objects.
                        QueryExpression stmt_subclass = getQueryForElement(subCandidateType, subCandidateTable,
                            false, candidateAlias);
                        stmt.union(stmt_subclass);
                    }
                }
            }

            if (allowsNull && sourceTable instanceof SCOTable)
            {
                // JoinTable case where we select the join table and join to the element table, and need to allow for nulls
                // Add a SELECT for nulls in the join table, and UNION it to the main query
                // TODO Add statement for nulls [CORE-2994]
                /**
                 * SELECT NULL AS JPOXMETADATA
                 * FROM JOINTBL THIS
                 * LEFT OUTER JOIN ELEM_TBL ELEM1 ON ELEM1.ID = THIS.ELEM_ID
                 * WHERE THIS.ELEM_ID IS NULL
                 */
                QueryExpression nullStmt = getQueryForElement(candidateFullClassName,
                    storeMgr.getDatastoreClass(candidateFullClassName, clr), true, candidateAlias);
                ScalarExpression elemIdExpr = sourceMapping.newScalarExpression(nullStmt, nullStmt.getMainTableExpression());
                nullStmt.andCondition(new NullLiteral(nullStmt).eq(elemIdExpr));
                stmt.union(nullStmt);
            }

            return stmt;
        }
View Full Code Here

    public QueryExpression getExistsSubquery(QueryExpression qs,
            JavaTypeMapping mapping,
            LogicSetExpression ownerTe,
            DatastoreIdentifier collectionTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(containerTable, collectionTableAlias,
            qs.getClassLoaderResolver());
        stmt.setParent(qs);

        // Join for the owner
        ScalarExpression ownerExpr = mapping.newScalarExpression(stmt, ownerTe);
        ScalarExpression ownerInCollectionExpr = ownerMapping.newScalarExpression(stmt, stmt.getTableExpression(collectionTableAlias));
        stmt.andCondition(ownerExpr.eq(ownerInCollectionExpr));

        // Select id mapping of element
        stmt.select(collectionTableAlias, elementMapping);

        return stmt;
    }
View Full Code Here

     * @return QueryStatement
     */
    private QueryExpression getQueryForElement(String targetElementType, DatastoreClass targetElementTable,
            boolean allowNull, DatastoreIdentifier candidateId)
    {
        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);

            // Add inner joins to all classes above up to elementType and across to the join table
            DatastoreIdentifier targetTableIdentifier = joinSourceToTargetElement(stmt, targetElementTable, allowNull);

            discriminatorMapping = targetElementTable.getDiscriminatorMapping(false);
            discriminatorMetaData = targetElementTable.getDiscriminatorMetaData();
            discriminatorTableExpr = stmt.getTableExpression(targetTableIdentifier);

            // Add left outer joins to exclude any target element subclasses
            if (joinToExcludeTargetSubclasses)
            {
                joinToExcludeTargetWhenSubElementsExists(stmt, sourceMapping, targetElementType);
            }
        }
        else
        {
            // * Selecting FCO objects directly (Extents etc)
            // * Selecting the element table of a ForeignKey (inverse) relationship
            stmt = dba.newQueryStatement(candidateTable, candidateId, clr);

            discriminatorMapping = sourceTable.getDiscriminatorMapping(false);
            discriminatorMetaData = sourceTable.getDiscriminatorMetaData();
            discriminatorTableExpr = stmt.getMainTableExpression();

            // in case of the elementType is a subClass of the element for the candidateTable
            // if the root (candidate) element type is not the same as the current target element type
            // joins the root to the current element
            if ((!targetElementTable.toString().equals(sourceTable.toString()) &&
                 !candidateTable.getType().equals(targetElementType)) ||
                 sourceJoin)
            {
                // Add inner joins to all classes above up to elementType and across to the join table
                if (sourceJoin)
                {
                    joinTargetToSourceElement(stmt, targetElementTable, false);
                }
                else
                {
                    joinSourceToTargetElement(stmt, targetElementTable, false);
                }
            }
            if (joinToExcludeTargetSubclasses)
            {
                joinToExcludeTargetWhenSubElementsExists(stmt, candidateTable.getIDMapping(), targetElementType);
            }
        }

        if (discriminatorMapping != null && discriminatorMetaData.getStrategy() != DiscriminatorStrategy.NONE)
        {
            // Restrict to valid discriminator values where we have a discriminator specified on this table
            String discriminatorValue = targetElementType;
            if (discriminatorMetaData.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
            {
                // Get the MetaData for the target class since that holds the "value"
                AbstractClassMetaData targetCmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(targetElementType, clr);
                discriminatorValue = targetCmd.getInheritanceMetaData().getDiscriminatorMetaData().getValue();
            }
            ScalarExpression discrExpr = discriminatorMapping.newScalarExpression(stmt, discriminatorTableExpr);
            ScalarExpression discrVal = discriminatorMapping.newLiteral(stmt, discriminatorValue);
            stmt.andCondition(discrExpr.eq(discrVal));
        }

        if (withMetadata == null || withMetadata.booleanValue())
        {
            // Select JPOXMETADATA if required
View Full Code Here

    public QueryExpression getSizeSubquery(QueryExpression qs,
            JavaTypeMapping mapping,
            LogicSetExpression ownerTe,
            DatastoreIdentifier collectionTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(containerTable, collectionTableAlias,
            qs.getClassLoaderResolver());
        stmt.setParent(qs);

        // Join for the owner
        ScalarExpression ownerExpr = mapping.newScalarExpression(stmt, ownerTe);
        ScalarExpression ownerInCollectionExpr = ownerMapping.newScalarExpression(stmt,
            stmt.getTableExpression(collectionTableAlias));
        stmt.andCondition(ownerExpr.eq(ownerInCollectionExpr));

        // Select COUNT(*)
        JavaTypeMapping m = dba.getMapping(String.class, storeMgr);
        StringLiteral lit = (StringLiteral)m.newLiteral(stmt, "COUNT(*)");
        lit.generateStatementWithoutQuotes();
        stmt.selectScalarExpression(lit);

        // TODO This needs to restrict the discriminator also where it is present (see sizeStmt)

        return stmt;
    }
View Full Code Here

        int subSequenceIdentifier = 0;
        while (iterTargetSubElementType.hasNext())
        {
            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)
            {
                AbstractClassMetaData targetSubCmd =
                    storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(targetSubElementType, stmt.getClassLoaderResolver());
                AbstractClassMetaData[] targetSubCmds = storeMgr.getClassesManagingTableForClass(targetSubCmd, clr);
                targetSubElementTypes = new DatastoreClass[targetSubCmds.length];
                for (int i=0;i<targetSubCmds.length;i++)
                {
                    targetSubElementTypes[i] = storeMgr.getDatastoreClass(targetSubCmds[i].getFullClassName(), clr);
                }
            }
            else
            {
                targetSubElementTypes = new DatastoreClass[1];
                targetSubElementTypes[0] = cbtTargetSubElementType;
            }

            for (int i=0;i<targetSubElementTypes.length;i++)
            {
                // Add leftOuterJoin to sub-target elements if they are not in the same table as the target
                if (!targetSubElementTypes[i].toString().equals(storeMgr.getDatastoreClass(targetElementType, clr).toString()))
                {
                    tiTargetSubElementType =
                        storeMgr.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE,
                            "SUBELEMENT" + (subSequenceIdentifier++));

                    // create a new statement
                    targetQS = dba.newQueryStatement(targetSubElementTypes[i], tiTargetSubElementType, stmt.getClassLoaderResolver());

                    // table expression from the table identifier
                    teTargetSubElementType = targetQS.newTableExpression(targetSubElementTypes[i], tiTargetSubElementType);

                    JavaTypeMapping targetMapping = targetSubElementTypes[i].getIDMapping();
                    ScalarExpression targetElementExpr =
                        targetElementMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
                    targetSubElementTypeExpr = targetMapping.newScalarExpression(stmt, teTargetSubElementType);
View Full Code Here

     * @param candidateAlias Alias for the candidate
     * @return The Query Statement for iterating through objects with a discriminator column
     */
    public QueryExpression getQueryStatement(DatastoreIdentifier candidateAlias)
    {
        QueryExpression stmt = null;
        DiscriminatorMetaData dismd = candidateTable.getDiscriminatorMetaData();
        JavaTypeMapping discriminatorMapping = candidateTable.getDiscriminatorMapping(true);
        boolean hasDiscriminator = (discriminatorMapping != null && dismd.getStrategy() != DiscriminatorStrategy.NONE);

        LogicSetExpression discrimTableExpr = null;
        if (selectTable != null)
        {
            // Select the required "selectTable"
            stmt = dba.newQueryStatement(selectTable, candidateAlias, clr);

            // Join from the "selectTable" to the table of our candidate
            ScalarExpression selectExpression = selectCandidateMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
            LogicSetExpression candidateTableExpression = stmt.newTableExpression(candidateTable, candidateTableIdentifier);
            ScalarExpression candidateExpression = candidateTable.getIDMapping().newScalarExpression(stmt, candidateTableExpression);
            if (allowNulls)
            {
                // Do LEFT OUTER JOIN since we need to allow nulls in the results
                stmt.leftOuterJoin(selectExpression, candidateExpression, candidateTableExpression, true);
            }
            else
            {
                // Do INNER JOIN since we don't allow nulls in the results
                stmt.innerJoin(selectExpression, candidateExpression, candidateTableExpression, true);
            }
            discrimTableExpr = stmt.getTableExpression(candidateTableIdentifier);

            if (hasDiscriminator && selectDiscriminator)
            {
                // Select the discriminator column so we can process the ResultSet
                stmt.selectScalarExpression(candidateTable.getDiscriminatorMapping(true).newScalarExpression(stmt, candidateTableExpression));
            }
        }
        else
        {
            // Select the candidate table
            stmt = dba.newQueryStatement(candidateTable, candidateAlias, clr);
            discrimTableExpr = stmt.getMainTableExpression();

            if (hasDiscriminator && selectDiscriminator)
            {
                // Select the discriminator column so we can process the ResultSet
                stmt.select(discriminatorMapping);
            }
        }

        // Check if we can omit the discriminator restriction
        if (includeSubclasses && hasDiscriminator && candidateTable.getDiscriminatorMapping(false) != null &&
            !storeMgr.getOMFContext().getMetaDataManager().isPersistentDefinitionImplementation(candidateFullClassName))
        {
            String[] managedClasses = ((ClassTable)candidateTable).getManagedClasses();
            if (managedClasses.length == 1)
            {
                // Only the candidate managed by this table and the discrim is here and we're including subclasses
                // in the SELECT so don't apply any restriction on the discriminator value
                // Note : we omit the persistent interface impl case from here for now
                restrictDiscriminator = false;
            }
        }

        if (hasDiscriminator && restrictDiscriminator)
        {
            // Add the discriminator required values to the WHERE clause
            // Loop through each possible candidate type and add the discrim values for each branch
            for (int i=0; i<candidateTypes.length; i++)
            {
                // For this candidate type, go through the possible classes persistable in this table and add an OR to the WHERE clause
                String candidateName = candidateTypes[i].getName();
                addConditionForCandidateClass(stmt, candidateName, dismd, discriminatorMapping, discrimTableExpr, storeMgr);
                if (includeSubclasses)
                {
                    Iterator iterator = storeMgr.getSubClassesForClass(candidateName, true, clr).iterator();
                    while (iterator.hasNext())
                    {
                        String subCandidateType = (String)iterator.next();
                        addConditionForCandidateClass(stmt, subCandidateType, dismd, discriminatorMapping, discrimTableExpr, storeMgr);
                    }
                }
            }
            if (allowNulls)
            {
                // Allow for null value of discriminator
                ScalarExpression discrExpr = discriminatorMapping.newScalarExpression(stmt, discrimTableExpr);
                ScalarExpression discrVal = new NullLiteral(stmt);
                stmt.iorCondition(discrExpr.eq(discrVal));
            }
        }

        return stmt;
    }
View Full Code Here

        {
            return null;
        }

        // Create the basic statement (without any selected columns)
        QueryExpression stmt = getGetStatement(sm,key);

        // Add the required field selections to the statement
        ResultObjectFactory rof = newResultObjectFactory(sm, stmt, false, true);

        ObjectManager om = sm.getObjectManager();
View Full Code Here

    public QueryExpression getExistsSubquery(QueryExpression qs,
                                            JavaTypeMapping mapping,
                                            LogicSetExpression ownerTe,
                                            DatastoreIdentifier mapTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(mapTable, mapTableAlias, qs.getClassLoaderResolver());
        stmt.setParent(qs);

        // Join to the owner
        ScalarExpression ownerExpr = mapping.newScalarExpression(stmt, ownerTe);
        ScalarExpression ownerInMapExpr = ownerMapping.newScalarExpression(stmt, stmt.getTableExpression(mapTableAlias));
        stmt.andCondition(ownerExpr.eq(ownerInMapExpr));

        stmt.select(mapTableAlias, valueMapping);

        return stmt;
    }
View Full Code Here

TOP

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

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.