Package org.jpox.store.mapped

Examples of org.jpox.store.mapped.DatastoreContainerObject


     * @param innerJoin whether to inner join
     * @return The field expression representing the required field of this object
     */
    public ScalarExpression accessField(String subfieldName, boolean innerJoin)
    {
        DatastoreContainerObject table;
        ClassLoaderResolver clr = qs.getClassLoaderResolver();
        try
        {
            if (mapping instanceof EmbeddedMapping)
            {
                // Any embedded fields can go straight to the main table if embedded there
                table = mapping.getDatastoreContainer();
                if (te.getMainTable().equals(table))
                {
                    // Provide the full field name so we can allow for nested embeddings
                    return te.newFieldExpression(fieldName + "." + subfieldName);
                }
            }
            else if (mapping instanceof PersistenceCapableMapping || mapping instanceof ReferenceMapping)
            {
                AbstractClassMetaData otherCmd = qs.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForClass(
                    mapping.getType(), clr);
                if (otherCmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.SUBCLASS_TABLE)
                {
                    // Field is a PC class that uses "subclass-table" inheritance strategy (and so has multiple possible tables to join to)
                    AbstractClassMetaData[] cmds = qs.getStoreManager().getClassesManagingTableForClass(otherCmd, clr);
                    if (cmds != null)
                    {
                        // Join to the first table
                        // TODO Allow for all possible tables. Can we do an OR of the tables ? How ?
                        if (cmds.length > 1)
                        {
                            JPOXLogger.QUERY.warn(LOCALISER.msg("037006",
                                mapping.getFieldMetaData().getFullFieldName(), cmds[0].getFullClassName()));
                        }
                        table = qs.getStoreManager().getDatastoreClass(cmds[0].getFullClassName(), clr);
                    }
                    else
                    {
                        // No subclasses with tables to join to, so throw a user error
                        throw new JPOXUserException(LOCALISER.msg("037005",
                            mapping.getFieldMetaData().getFullFieldName()));
                    }
                }
                else
                {
                    // Class of the field will have its own table
                    table = qs.getStoreManager().getDatastoreClass(mapping.getType(), clr);
                    ApiAdapter api = qs.getStoreManager().getApiAdapter();

                    if (fieldName != null && subfieldName != null)
                    {
                        AbstractMemberMetaData subfieldMetaData = otherCmd.getMetaDataForMember(subfieldName);
                        if (subfieldMetaData != null && subfieldMetaData.isPrimaryKey() &&
                            !api.isPersistable(subfieldMetaData.getType()))
                        {
                            // Selecting a non-PC field in the other class that is part of its PK mapping (so we have a column here for it)
                            // Avoids the extra join to the other table
                            JavaTypeMapping[] subMappings = ((PersistenceCapableMapping)mapping).getJavaTypeMapping();
                            if (subMappings.length == 1)
                            {
                                // TODO Cater for a field of a composite PK being selected
                                return subMappings[0].newScalarExpression(qs, te);
                            }
                        }
                    }
                }
            }
            else
            {
                table = qs.getStoreManager().getDatastoreClass(mapping.getType(), clr);
            }
        }
        catch (ClassNotPersistenceCapableException cnpce)
        {
            return te.newFieldExpression(subfieldName);
        }

        if (fieldType != null && !fieldType.equals(mapping.getType()))
        {
            // The field relation is to a table that allows multiple types to be stored (and has a discriminator)
            // and the type we want is not the base type, so we need to restrict the values of the discriminator.
            DiscriminatorMetaData dismd = table.getDiscriminatorMetaData();
            DiscriminatorMapping discriminatorMapping = (DiscriminatorMapping)table.getDiscriminatorMapping(false);
            if (dismd != null && dismd.getStrategy() != DiscriminatorStrategy.NONE)
            {
                // Start with the required class
                BooleanExpression discrExpr = booleanConditionForClassInDiscriminator(qs, fieldType, dismd, discriminatorMapping, te);

                // Add "or" condition for any of its possible subclasses (if any)
                Iterator subclassIter = qs.getStoreManager().getSubClassesForClass(fieldType, true, clr).iterator();
                while (subclassIter.hasNext())
                {
                    String subCandidateType = (String)subclassIter.next();
                    discrExpr.ior(booleanConditionForClassInDiscriminator(qs, subCandidateType, dismd, discriminatorMapping, te));
                }

                discrExpr.encloseWithInParentheses();

                // Add the discriminator restrictions as an "and" condition to the Query Statement
                qs.andCondition(discrExpr);
            }
        }

        if (te.getMainTable().equals(table) && usingRelatedTable && fieldName==null)
        {
            //TODO fieldname==null, QUESTION is it "<candidateAlias>" namespace? debug and see
            // We are already in the same table (left outer join in the constructor) and it isn't a self reference so just return
            // the field expression. This can happen when we have a 1-1 bidir single FK and to generate the ObjectExpression we
            // had to join across to the related field
            return te.newFieldExpression(subfieldName);
        }

        // jt... = "joined table"
        String jtIdentifier = te.getAlias().getIdentifier();
        if (fieldName != null)
        {
            jtIdentifier += '.' + fieldName;
        }
        if (!subfieldName.equals("this")) // TODO Use qs.getCandidateAlias()
        {
            jtIdentifier += '.' + subfieldName;
        }

        if (castType != null && !castType.getName().equals(mapping.getType()))
        {
            String castTypeName = castType.getName();
            jtIdentifier += '.' + castTypeName.substring(castTypeName.lastIndexOf('.') + 1);
        }

        DatastoreIdentifier jtRangeVar = qs.getStoreManager().getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, jtIdentifier);
        LogicSetExpression jtTblExpr = qs.getTableExpression(jtRangeVar);
        if (jtTblExpr == null)
        {
            // We can't join further (this subfield is not an object with a table expression)
            if (te.getAlias().getIdentifier().equalsIgnoreCase("this") && // TODO Use qs.getCandidateAlias()
                table == qs.getMainTableExpression().getMainTable() &&
                fieldName == null)
            {
                // Query contains "this.field" so just provide the associated field expression for that field
                return qs.getMainTableExpression().newFieldExpression(subfieldName);
            }

            jtTblExpr = qs.newTableExpression(table, jtRangeVar);

            ScalarExpression jtExpr = table.getIDMapping().newScalarExpression(qs, jtTblExpr);
            ScalarExpression expr = mapping.newScalarExpression(qs, te);
            if (mapping.isNullable())
            {
                if (innerJoin)
                {
View Full Code Here


            {
                // Field type and instanceof type are totally incompatible, so just return false
                return new BooleanLiteral(qs, mapping, true).eq(new BooleanLiteral(qs, mapping, false));
            }

            DatastoreContainerObject table;
            try
            {
                if (mapping instanceof EmbeddedMapping)
                {
                    // Field is embedded in this table
                    // TODO Enable instanceof on non-PC fields (currently just return "true")
                    return new BooleanLiteral(qs, mapping, true).eq(new BooleanLiteral(qs, mapping, true));
                }
                else if (mapping instanceof PersistenceCapableMapping || mapping instanceof ReferenceMapping)
                {
                    // Field has its own table, so join to it
                    AbstractClassMetaData fieldCmd = qs.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForClass(
                        mapping.getType(), clr);
                    if (fieldCmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.SUBCLASS_TABLE)
                    {
                        // Field is a PC class that uses "subclass-table" inheritance strategy (and so has multiple possible tables to join to)
                        AbstractClassMetaData[] cmds = qs.getStoreManager().getClassesManagingTableForClass(fieldCmd, clr);
                        if (cmds != null)
                        {
                            // Join to the first table
                            // TODO Allow for all possible tables. Can we do an OR of the tables ? How ?
                            if (cmds.length > 1)
                            {
                                JPOXLogger.QUERY.warn(LOCALISER.msg("037006",
                                    mapping.getFieldMetaData().getFullFieldName(), cmds[0].getFullClassName()));
                            }
                            table = qs.getStoreManager().getDatastoreClass(cmds[0].getFullClassName(), clr);
                        }
                        else
                        {
                            // No subclasses with tables to join to, so throw a user error
                            throw new JPOXUserException(LOCALISER.msg("037005",
                                mapping.getFieldMetaData().getFullFieldName()));
                        }
                    }
                    else
                    {
                        // Class of the field will have its own table
                        table = qs.getStoreManager().getDatastoreClass(mapping.getType(), clr);
                    }
                }
                else
                {
                    table = qs.getStoreManager().getDatastoreClass(mapping.getType(), clr);
                }
            }
            catch (ClassNotPersistenceCapableException cnpce)
            {
                // Field is not PersistenceCapable
                // TODO Enable instanceof on non-PC fields (currently just return "true")
                return new BooleanLiteral(qs, mapping, true).eq(new BooleanLiteral(qs, mapping, true));
            }

            // Check if the table of the field has a discriminator
            IdentifierFactory idFactory = qs.getStoreManager().getIdentifierFactory();
            DiscriminatorMetaData dismd = table.getDiscriminatorMetaData();
            DiscriminatorMapping discriminatorMapping = (DiscriminatorMapping)table.getDiscriminatorMapping(false);
            if (discriminatorMapping != null)
            {
                // Has a discriminator so do a join to the table of the field and apply a constraint on its discriminator
                LogicSetExpression fieldTblExpr = null;
                if (fieldName == null)
                {
                    // Using THIS so use default table expression
                    fieldTblExpr = qs.getMainTableExpression();
                }
                else
                {
                    // Using field, so our real table will have an identifier of "THIS_{fieldName}" via INNER JOIN
                    String fieldIdentifier = te.getAlias().getIdentifier();
                    fieldIdentifier += '.' + fieldName;
                    DatastoreIdentifier fieldRangeVar = idFactory.newIdentifier(IdentifierFactory.TABLE, fieldIdentifier);
                    fieldTblExpr = qs.getTableExpression(fieldRangeVar);
                    if (fieldTblExpr == null)
                    {
                        fieldTblExpr = qs.newTableExpression(table, fieldRangeVar);
                    }
                    ScalarExpression fieldExpr = table.getIDMapping().newScalarExpression(qs, fieldTblExpr);
                    expr = mapping.newScalarExpression(qs, te);
                    qs.innerJoin(fieldExpr, expr, fieldTblExpr, true, true);
                }

                // Return a constraint on the discriminator for this table to get the right instances
                // This allows all discriminator values for the instanceof class and all of its subclasses
                // DISCRIM = 'baseVal' OR DISCRIM = 'sub1Val' OR DISCRIM = 'sub2Val' ... etc
                BooleanExpression discrExpr =
                    booleanConditionForClassInDiscriminator(qs, instanceofClass.getName(), dismd,
                        discriminatorMapping, fieldTblExpr);
                Iterator subclassIter = qs.getStoreManager().getSubClassesForClass(instanceofClass.getName(),
                    true, clr).iterator();
                while (subclassIter.hasNext())
                {
                    String subCandidateType = (String)subclassIter.next();
                    discrExpr.ior(booleanConditionForClassInDiscriminator(qs, subCandidateType, dismd,
                        discriminatorMapping, fieldTblExpr));
                }
                discrExpr.encloseWithInParentheses();

                return discrExpr;
            }
            else
            {
                // No discriminator so maybe union, or just a SELECT
                // Need to join to the instanceof class (where appropriate)
                // TODO RDBMS-71 Only join on the UNION select that it is applicable to
                if (table instanceof DatastoreClass)
                {
                    DatastoreClass ct = (DatastoreClass)table;
                    if (ct.managesClass(instanceofClass.getName()))
                    {
                        // This type is managed in this table so must be an instance
                        return new BooleanLiteral(qs, mapping, true).eq(new BooleanLiteral(qs, mapping, true));
                    }
                    else
                    {
                        // The instanceof type is not managed here
                        DatastoreClass instanceofTable = qs.getStoreManager().getDatastoreClass(
                            instanceofClass.getName(), clr);
                        String fieldIdentifier = te.getAlias().getIdentifier();
                        if (fieldName == null)
                        {
                            // Using THIS, so our real table will have an identifier of "THIS_INST"
                            fieldIdentifier += ".INST";
                        }
                        else
                        {
                            // Using field, so our real table will have an identifier of "THIS_{fieldName}"
                            fieldIdentifier += '.' + fieldName;
                        }
                        DatastoreIdentifier fieldRangeVar = idFactory.newIdentifier(IdentifierFactory.TABLE, fieldIdentifier);
                        LogicSetExpression fieldTblExpr = qs.newTableExpression(instanceofTable, fieldRangeVar);
                        ScalarExpression fieldExpr = table.getIDMapping().newScalarExpression(qs, te);
                        if (fieldName == null)
                        {
                            expr = instanceofTable.getIDMapping().newScalarExpression(qs, fieldTblExpr);
                        }
                        else
View Full Code Here

            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();
View Full Code Here

    public String referenceColumn(DatastoreField col)
    {
        assertNotFrozen();

        DatastoreContainerObject table = col.getDatastoreContainerObject();
        DatastoreIdentifier alias = (DatastoreIdentifier)aliasesByTable.get(table);

        if (alias == null)
        {
            if (!(mainTable instanceof DatastoreClass) || !(table instanceof DatastoreClass))
View Full Code Here

                // 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();
View Full Code Here

     * @param col The column
     * @return The column statement text
     */
    public String referenceDatastoreField(DatastoreField col)
    {
        DatastoreContainerObject t = col.getDatastoreContainerObject();
        String alias = null;
        if (t.equals(mainTable))
        {
            alias = "THIS";
        }
        else
        {
            if (alias == null)
            {
                // Check if we have an inner join to this table
                Iterator iter = innerTableMappings.entrySet().iterator();
                while (iter.hasNext())
                {
                    Map.Entry entry = (Map.Entry)iter.next();
                    InnerJoinDefinition join = (InnerJoinDefinition)entry.getValue();
                    if (t.equals(join.getTable()))
                    {
                        alias = (String)entry.getKey();
                        break;
                    }
                }
            }
        }
        if (alias == null)
        {
            // No join present so create INNER join
            alias = "INNER" + (innerTableMappings.size()+1);
            innerTableMappings.put(alias, new InnerJoinDefinition(t, t.getIDMapping()));
        }

        return col.applySelectFunction(alias + '.' + col.getIdentifier().toString());
    }
View Full Code Here

                    else if (imd.getStrategyValue() == InheritanceStrategy.SUPERCLASS_TABLE)
                    {
                        // Table mapped into table of superclass
                        // Find the superclass - should have been created first
                        AbstractClassMetaData[] managingCmds = getClassesManagingTableForClass(cmd, clr);
                        DatastoreContainerObject superTable = null;
                        if (managingCmds != null && managingCmds.length == 1)
                        {
                            RDBMSStoreData superData = (RDBMSStoreData) storeDataMgr.get(managingCmds[0].getFullClassName());
                            if (superData != null)
                            {
View Full Code Here

            List result = new ArrayList();
            Set uniqueTables = new TreeSet(new Comparator()
            {
                public int compare(Object o1, Object o2)
                {
                    DatastoreContainerObject t1 = (DatastoreContainerObject) o1;
                    DatastoreContainerObject t2 = (DatastoreContainerObject) o2;
                    return StringUtils.toJVMIDString(t1).compareTo(StringUtils.toJVMIDString(t2));
                }
            });
            uniqueTables.addAll(newTables);
            result.addAll(uniqueTables);
View Full Code Here

        private boolean hasDuplicateTablesFromList(List newTables)
        {
            Map map = new HashMap();
            for (int i=0; i<newTables.size(); i++)
            {
                DatastoreContainerObject t1 = (DatastoreContainerObject) newTables.get(i);
                if (map.containsKey(t1.getIdentifier().getIdentifier()))
                {
                    return true;
                }
                map.put(t1.getIdentifier().getIdentifier(), t1);
            }
            return false;
        }
View Full Code Here

         * @param type The type of the join table
         */
        private DatastoreContainerObject addJoinTableForContainer(AbstractMemberMetaData fmd, ClassLoaderResolver clr, int type)
        {
            DatastoreIdentifier tableName = getTableIdentifier(fmd, clr);
            DatastoreContainerObject join = null;
            if (type == JOIN_TABLE_COLLECTION)
            {
                join = new CollectionTable(tableName, fmd, RDBMSManager.this);
            }
            else if (type == JOIN_TABLE_MAP)
View Full Code Here

TOP

Related Classes of org.jpox.store.mapped.DatastoreContainerObject

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.