Package org.datanucleus.store.mapped.expression

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


     * @return The query statement
     */
    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 The query statement
     */
    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 = storeMgr.getMappingManager().getMapping(String.class);
        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

     * @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);
        if (discriminatorMapping != null)
        {
            // Use discriminator metadata from the place where the discriminator mapping is defined
            dismd = discriminatorMapping.getDatastoreContainer().getDiscriminatorMetaData();
        }
        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 = getCandidateTableManagedClasses();
            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
            boolean multipleDiscriminatorValues = candidateTypes.length > 1 ? true: false;
            BooleanExpression discExpr = null;
            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();
                BooleanExpression discExprCand = getExpressionForDiscriminatorForClass(stmt, candidateName,
                    dismd, discriminatorMapping, discrimTableExpr, storeMgr);
                if (discExpr != null)
                {
                    discExpr = discExpr.ior(discExprCand);
                }
                else
                {
                    discExpr = discExprCand;
                }
                if (includeSubclasses)
                {
                    Iterator iterator = storeMgr.getSubClassesForClass(candidateName, true, clr).iterator();
                    while (iterator.hasNext())
                    {
                        String subCandidateType = (String)iterator.next();
                        BooleanExpression discExprSub = getExpressionForDiscriminatorForClass(stmt,
                            subCandidateType, dismd, discriminatorMapping, discrimTableExpr, storeMgr);
                        discExpr = discExpr.ior(discExprSub);
                        if (!multipleDiscriminatorValues)
                        {
                            multipleDiscriminatorValues = true;
                        }
                    }
                }
            }

            if (allowNulls)
            {
                // Allow for null value of discriminator
                ScalarExpression expr = discriminatorMapping.newScalarExpression(stmt, discrimTableExpr);
                ScalarExpression val = new NullLiteral(stmt);
                BooleanExpression nullDiscExpr = expr.eq(val);
                discExpr = discExpr.ior(nullDiscExpr);
                if (!multipleDiscriminatorValues)
                {
                    multipleDiscriminatorValues = true;
                }
            }

            // Apply the discriminator to the query statement
            if (multipleDiscriminatorValues)
            {
                discExpr.encloseWithInParentheses();
            }
            stmt.andCondition(discExpr);
        }

        return stmt;
    }
View Full Code Here

     * @return A subquery
     */
    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

     * @return The query statement
     */
    public QueryExpression getSizeSubquery(QueryExpression qs, JavaTypeMapping mapping,
            LogicSetExpression ownerTe, DatastoreIdentifier mapTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(mapTable, mapTableAlias, qs.getClassLoaderResolver());
        stmt.setParent(qs);

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

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

        return stmt;
    }
View Full Code Here

     * @return The JDOQL query statement
     */
    public QueryExpression getExistsSubquery(QueryExpression qs, JavaTypeMapping mapping, LogicSetExpression ownerTe,
            DatastoreIdentifier arrayTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(containerTable, arrayTableAlias, qs.getClassLoaderResolver());
        stmt.setParent(qs);

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

        stmt.select(arrayTableAlias, elementMapping);

        return stmt;
    }
View Full Code Here

     * @return The query statement
     */
    public QueryExpression getSizeSubquery(QueryExpression qs, JavaTypeMapping mapping,
            LogicSetExpression ownerTe, DatastoreIdentifier arrayTableAlias)
    {
        QueryExpression stmt = dba.newQueryStatement(containerTable, arrayTableAlias, qs.getClassLoaderResolver());
        stmt.setParent(qs);

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

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

        return stmt;
    }
View Full Code Here

    public QueryExpression getQueryStatement(DatastoreIdentifier candidateAlias)
    {
        if (storeMgr.getMappedTypeManager().isSupportedMappedType(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 NucleusUserException("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 NUCLEUS_TYPE 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 && sourceTableIsJoinTable())
            {
                // 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 NUCLEUS_TYPE
                 * 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

     * @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 (sourceTableIsJoinTable())
        {
            // * Selecting the join table of a JoinTable relationship, and joining to the element table
            stmt = dba.newQueryStatement(sourceTable, candidateId, clr);
            IdentifierFactory idFactory = stmt.getStoreManager().getIdentifierFactory();

            // Add join from the join table to the root element table
            DatastoreIdentifier targetTableIdentifier = null;
            DatastoreIdentifier rootElementTblId = idFactory.newIdentifier(IdentifierType.TABLE, "ELEMENT");
            LogicSetExpression rootElementTblExpr = stmt.newTableExpression(candidateTable, rootElementTblId);
            ScalarExpression sourceExpr = sourceMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
            ScalarExpression rootElementExpr =
                candidateTable.getIDMapping().newScalarExpression(stmt, rootElementTblExpr);
            if (allowNull)
            {
                stmt.leftOuterJoin(sourceExpr, rootElementExpr, rootElementTblExpr, true);
            }
            else
            {
                stmt.innerJoin(sourceExpr, rootElementExpr, rootElementTblExpr, true);
            }
            targetTableIdentifier = rootElementTblId;

            if (targetElementTable != candidateTable)
            {
                // Add join from the root element table to the target element table
                DatastoreIdentifier tgtElementTblId = idFactory.newIdentifier(IdentifierType.TABLE, "ELMNTSUB");
                LogicSetExpression tgtElementTblExpr = stmt.newTableExpression(targetElementTable, tgtElementTblId);
                ScalarExpression targetExpr =
                    targetElementTable.getIDMapping().newScalarExpression(stmt, tgtElementTblExpr);
                if (allowNull)
                {
                    stmt.leftOuterJoin(rootElementExpr, targetExpr, tgtElementTblExpr, true);
                }
                else
                {
                    stmt.innerJoin(rootElementExpr, targetExpr, tgtElementTblExpr, true);
                }
                targetTableIdentifier = tgtElementTblId;
            }

            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 NUCLEUS_TYPE if required
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(IdentifierType.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

TOP

Related Classes of org.datanucleus.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.