Package org.jpox.store.mapped.expression

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


                    parameterMappingsByName.put(param.name, param.mapping);
                    parameterValuesByName.put(param.name, param.value);
                }
                else if (item instanceof QueryExpression)
                {
                    QueryExpression qe = (QueryExpression) item;
                    StatementText st = qe.toStatementText(false); // TODO use the same lock as the caller
                    statementText.append(st.toStatementString(mode));
                    if (st.parameterNames != null)
                    {
                        initParameters();
                        parameterNames.addAll(st.parameterNames);
View Full Code Here


     * @param candidateAlias Alias to use for the candidate in the query (if the native query supports it)
     * @return Query Statement
     */
    public QueryExpression newQueryStatement(Class candidateClass, DatastoreIdentifier candidateAlias)
    {
        QueryExpression stmt = ((Queryable)extent).newQueryStatement(candidateClass, candidateAlias);
        ObjectManager om = extent.getObjectManager();
        MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
        JavaTypeMapping m = storeMgr.getDatastoreClass(candidateClass.getName(),
            om.getClassLoaderResolver()).getIDMapping();

        /*
         * creates a query like WHERE ... AND (CANDIDATE_ID = ?1 OR CANDIDATE_ID =
         * ?2) or for classes with composite primary keys WHERE ... AND (
         * (CANDIDATE_IDa = ?1a AND CANDIDATE_IDb = ?1b) OR (CANDIDATE_IDa = ?2a
         * AND CANDIDATE_IDb = ?2b) )
         */
        BooleanExpression elementsExpr = null;
        for (Iterator it = userCandidates.iterator(); it.hasNext();)
        {
            Object candidateValue = it.next();
            ScalarExpression expr = m.newScalarExpression(stmt, stmt.getMainTableExpression());
            BooleanExpression keyExpr = expr.eq(m.newLiteral(stmt, candidateValue));
            if (elementsExpr == null)
            {
                elementsExpr = keyExpr;
            }
            else
            {
                elementsExpr = elementsExpr.ior(keyExpr);
            }
        }
        if (elementsExpr != null)
        {
            stmt.andCondition(elementsExpr, true);
        }
        return stmt;
    }
View Full Code Here

            RDBMSStoreData schemaDataOption = (RDBMSStoreData)optionsIter.next();
            metadata_id_len = Math.max(schemaDataOption.getName().length(), metadata_id_len);
        }

        // Form the query to find which one of these classes has the instance with this id
        QueryExpression qs_base = null;
        optionsIter = schemaDataOptions.iterator();
        while (optionsIter.hasNext())
        {
            RDBMSStoreData schemaDataOption = (RDBMSStoreData)optionsIter.next();
            ClassMetaData cmd = (ClassMetaData)schemaDataOption.getMetaData();

            QueryExpression qs = storeMgr.getDatastoreAdapter().newQueryStatement(schemaDataOption.getDatastoreContainerObject(), om.getClassLoaderResolver());
            String classname = StringUtils.leftAlignedPaddedString(schemaDataOption.getName(), metadata_id_len);
            qs.selectScalarExpression(new MetaDataStringLiteral(qs,classname));

            // LEFT OUTER JOIN to all direct subclasses
            Iterator subclass_iter = storeMgr.getSubClassesForClass(schemaDataOption.getName(),false, om.getClassLoaderResolver()).iterator();
            int subclasses_seq_id = 0;
            while (subclass_iter.hasNext())
            {
                String subclass = (String)subclass_iter.next();
                DatastoreClass subclassTable = storeMgr.getDatastoreClass(subclass, om.getClassLoaderResolver());

                // No need to LEFT OUTER JOIN for "subclass-table" and "superclass-table" cases
                // "subclass-table" objects dont exist on their own
                // "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 =
                        (((DatastoreClass)schemaDataOption.getDatastoreContainerObject()).getIDMapping()).newScalarExpression(
                            qs,qs.getMainTableExpression());
View Full Code Here

            // We have all possible candidates that are stored in this table so we can omit the restriction on the discriminator
            discrimStmt.setRestrictDiscriminator(false); // Just retrieve the discriminator for this id (accept any possible disc values)
        }

        // Create the statement
        QueryExpression stmt = discrimStmt.getQueryStatement(null);

        // WHERE (object id) = ?
        // We have to create a StateManager here just to map fields from the AppId key object to the table fields.
        // Really the table should have some way of doing this. TODO : Refactor this
        StateManager sm = StateManagerFactory.newStateManagerForHollow(om, objectClass, id);
        JavaTypeMapping idMapping = primaryTable.getIDMapping();
        ScalarExpression fieldExpr = idMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
        ScalarExpression fieldValue = idMapping.newLiteral(stmt, sm.getObject());
        stmt.andCondition(fieldExpr.eq(fieldValue), true);

        // Perform the query
        try
        {
            Transaction tx = om.getTransaction();
View Full Code Here

     * @return The QueryStatement that will return the instances
     */
    public QueryExpression newQueryStatement(Class candidateClass, DatastoreIdentifier candidateAlias)
    {
        final ClassLoaderResolver clr = om.getClassLoaderResolver();
        QueryExpression query = null;

        if (tables == null)
        {
            return null;
        }

        // In the multiple table case we pass in the class of the table rather than the candidate class since
        // the instances of that table can only be of the tables type ("subclass-table" strategy)
        for (int i = 0; i < tables.length; i++)
        {
            final int tableNo = i;

            // Load using the class loader of the candidate class where possible - JDO spec 12.5
            Class cls = null;
            try
            {
                cls = clr.classForName(tables[tableNo].getType(), this.candidateClass.getClassLoader());
            }
            catch (ClassNotResolvedException cnfe)
            {
                throw new JPOXUserException(LOCALISER_RDBMS.msg("053002",
                    candidateClass.getName(), tables[tableNo].getType())).setFatal();
            }

            if (queryUsingDiscriminator())
            {
                // Use a single select with discrim since all in 1 table, and select the discrim column for identification
                Class[] candidates = null;
                if (storeMgr.getOMFContext().getTypeManager().isReferenceType(candidateClass))
                {
                    // Take the metadata for the first implementation of the reference type
                    String[] clsNames = storeMgr.getOMFContext().getMetaDataManager().getClassesImplementingInterface(candidateClass.getName(), clr);
                    candidates = new Class[clsNames.length];
                    for (int j=0; j<clsNames.length; j++)
                    {
                        candidates[j] = clr.classForName(clsNames[j]);
                    }
                }
                else
                {
                    candidates = new Class[] {candidateClass};
                }
                query = (new DiscriminatorIteratorStatement(clr,
                    candidates, subclasses, storeMgr, true)).getQueryStatement(candidateAlias);
            }
            else
            {
                // Use a UNION since we need to join across multiple tables
                boolean completeTableInheritance =
                    cmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.COMPLETE_TABLE;
                boolean useSubclassesInStatement = subclasses;
                if (completeTableInheritance)
                {
                    // "complete-table" so ignore subclasses since each table is just that class
                    useSubclassesInStatement = false;
                }

                QueryExpression query_table = new UnionIteratorStatement(clr,
                    multipleTableCase ? cls : candidateClass, useSubclassesInStatement, storeMgr,
                    clr.classForName(tables[tableNo].getType()),
                    tables[tableNo].getIDMapping(), tables[tableNo], false, Boolean.TRUE,
                    !completeTableInheritance, false).getQueryStatement(candidateAlias);
               
View Full Code Here

        final DatastoreClass datastoreClass = storeMgr.getDatastoreClass(candidateClass.getName(), clr);
        final JavaTypeMapping m = datastoreClass.getIDMapping();
        final Class type = candidateClass;
        final boolean subclasses = this.subclasses;

        QueryExpression stmt = null;
        if (!queryUsingDiscriminator(datastoreClass) && !subclasses && hasAggregatedExpressionsOnly())
        {
            // TODO This is incorrect. Here we want to return a single row so cant use UNION statements
            // and our classes are not all in one table so shouldn't use the discriminator. We should have
            // a single SELECT like
            // SELECT ... FROM BASE
            // LEFT OUTER JOIN SUB1 ON ...
            // LEFT OUTER JOIN SUB2 ON ...
            // WHERE SUB1.ID = NULL AND SUB2.ID = NULL
            stmt = new DiscriminatorIteratorStatement(clr,
                new Class[] {type}, subclasses, storeMgr, false).getQueryStatement(candidateAlias);
        }
        else if (queryUsingDiscriminator(datastoreClass) || hasAggregatedExpressionsOnly())
        {
            // Create a statement using a single SELECT, and using the discriminator to distinguish classes
            // Don't select the discriminator column since we don't need it for aggregates
            stmt = new DiscriminatorIteratorStatement(clr,
                new Class[] {type}, subclasses, storeMgr, false).getQueryStatement(candidateAlias);
        }
        else
        {
            // Create a statement using UNIONs between the various possible types and tables
            stmt = new UnionIteratorStatement(clr, candidateClass, subclasses, storeMgr,
                type, m, datastoreClass, false, Boolean.FALSE, true, false).getQueryStatement(candidateAlias);
        }

        if (userCandidates != null)
        {
            /*
             * creates a query like WHERE ... AND (CANDIDATE_ID = ?1 OR CANDIDATE_ID =
             * ?2) or for classes with composite primary keys WHERE ... AND (
             * (CANDIDATE_IDa = ?1a AND CANDIDATE_IDb = ?1b) OR (CANDIDATE_IDa = ?2a
             * AND CANDIDATE_IDb = ?2b) )
             */
            BooleanExpression elementsExpr = null;
            for (Iterator it = userCandidates.iterator(); it.hasNext();)
            {
                Object candidateValue = it.next();
                ScalarExpression expr = m.newScalarExpression(stmt, stmt.getMainTableExpression());
                BooleanExpression keyExpr = expr.eq(m.newLiteral(stmt, candidateValue));
                if (elementsExpr == null)
                {
                    elementsExpr = keyExpr;
                }
                else
                {
                    elementsExpr = elementsExpr.ior(keyExpr);
                }
            }
            if (elementsExpr != null)
            {
                stmt.andCondition(elementsExpr, true);
            }
        }
        return stmt;
    }
View Full Code Here

     * @return The List Iterator
     **/
    public ListIterator listIterator(StateManager ownerSM, int start)
    {
        // Create the basic statement (without any selected columns)
        QueryExpression stmt = null;
        if (start >= 0)
        {
            stmt = getIteratorStatement(ownerSM, start, -1);
        }
        else
View Full Code Here

    protected java.util.List internalGetRange(StateManager sm,int start,int end)
    {
        java.util.List list = new java.util.ArrayList();

        ObjectManager om = sm.getObjectManager();
        QueryExpression stmt = getIteratorStatement(sm, start, end);
        ResultObjectFactory getROF = newResultObjectFactory(sm,stmt,false,true);
        Transaction tx = om.getTransaction();
        boolean useUpdateLock = ((Boolean)tx.getOptions().get("transaction.serializeReadObjects")).booleanValue();
        String statement = storeMgr.getStatementTextForQuery(stmt, useUpdateLock);
View Full Code Here

        {
            return null;
        }

        final ClassLoaderResolver clr = ownerSM.getObjectManager().getClassLoaderResolver();
        QueryExpression stmt = null;
        if (elementInfo.length == 1 &&
            elementInfo[0].getDatastoreClass().getDiscriminatorMetaData() != null &&
            elementInfo[0].getDatastoreClass().getDiscriminatorMetaData().getStrategy() != DiscriminatorStrategy.NONE)
        {
            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 int elementNo = i;
                Class elementCls = clr.classForName(elementType);
                QueryExpression subStmt = new UnionIteratorStatement(
                    clr, elementCls, true, this.storeMgr,
                    elementCls, elementMapping, elementInfo[elementNo].getDatastoreClass(), false,
                    null, true, false).getQueryStatement(null);
                if (stmt == null)
                {
View Full Code Here

        ClassLoaderResolver clr = sm.getObjectManager().getClassLoaderResolver();
        DatastoreClass 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

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.