Package org.datanucleus.store.mapped

Examples of org.datanucleus.store.mapped.MappedStoreManager


        StateManager sm = om.findStateManager(value);

        try
        {
            ClassLoaderResolver clr = om.getClassLoaderResolver();
            MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();

            // Check if the field is attributed in the datastore
            boolean hasDatastoreAttributedPrimaryKeyValues = hasDatastoreAttributedPrimaryKeyValues(
                om.getMetaDataManager(), storeMgr, clr);

            boolean inserted = false;
            if (ownerFieldNumber >= 0)
            {
                // Field mapping : is this field of the related object present in the datastore?
                inserted = storeMgr.isObjectInserted(sm, ownerFieldNumber);
            }
            else if (mmd == null)
            {
                // Identity mapping : is the object inserted far enough to be considered of this mapping type?
                inserted = storeMgr.isObjectInserted(sm, type);
            }

            if (sm != null)
            {
                if (om.getApiAdapter().isDetached(value) && sm.getReferencedPC() != null && ownerSM != null && mmd != null)
View Full Code Here


     * @return The Persistence Capable object
     */
    public Object getObject(ObjectManager om, final Object rs, int[] param)
    {
        // Check for null FK
        MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
        if (storeMgr.getResultValueAtPosition(rs, this, param[0]) == null)
        {
            // if the first param is null, then the field is null
            return null;
        }

View Full Code Here

            return new ObjectExpression(qs, this, te);
        }
        else
        {
            ClassLoaderResolver clr = qs.getClassLoaderResolver();
            MappedStoreManager srm = qs.getStoreManager();
            int relationType = mmd.getRelationType(clr);
            if (relationType == Relation.ONE_TO_ONE_BI)
            {
                // Create an expression joining to the related field in the related table
                DatastoreClass targetTable = srm.getDatastoreClass(mmd.getTypeName(), clr);
                AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                // TODO Cater for more than one related field
                JavaTypeMapping refMapping = targetTable.getMemberMapping(relatedMmds[0]);
                JavaTypeMapping selectMapping = targetTable.getIDMapping();
                DatastoreIdentifier targetTableIdentifier =
                    srm.getIdentifierFactory().newIdentifier(IdentifierType.TABLE, "RELATED" + mmd.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 = mmd.getRelatedMemberMetaData(clr);
                // TODO Cater for more than one related field
                if (mmd.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(IdentifierType.TABLE, "JOINTABLE" + mmd.getAbsoluteFieldNumber());
                    LogicSetExpression targetTe = qs.newTableExpression(targetTable, targetTableIdentifier);
                    return new ObjectExpression(qs, this, te, refMapping, targetTe, selectMapping);
                }
            }
        }
View Full Code Here

        {
            bExpr = expr.eq(new NullLiteral(qs));
        }
        else
        {
            MappedStoreManager storeMgr = qs.getStoreManager();
            ClassLoaderResolver clr = qs.getClassLoaderResolver();
            if (value instanceof OID)
            {
                // Object is an OID
                JavaTypeMapping m = storeMgr.getMappingManager().getMappingWithDatastoreMapping(
                    ((OID)value).getKeyValue().getClass(), false, false, clr);
                ScalarExpression oidExpr =  m.newLiteral(qs,((OID)value).getKeyValue());
                bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
            }
            else
            {
                ApiAdapter api = qs.getStoreManager().getApiAdapter();
                AbstractClassMetaData cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(value.getClass(), clr);
                if (cmd == null)
                {
                    // if there is no metadata, we either have an SingleFieldIdentity, application identity, or any object
                    if (storeMgr.getApiAdapter().isSingleFieldIdentityClass(value.getClass().getName()))
                    {
                        // Object is SingleFieldIdentity
                        JavaTypeMapping m = storeMgr.getMappingManager().getMappingWithDatastoreMapping(
                            api.getTargetClassForSingleFieldIdentity(value), false, false, clr);
                        ScalarExpression oidExpr =  m.newLiteral(qs, api.getTargetKeyForSingleFieldIdentity(value));
                        bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
                    }
                    else
                    {
                        String pcClassName = storeMgr.getClassNameForObjectID(value, clr, null);
                        if (pcClassName != null)
                        {
                            // Object is an application identity
                            cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(pcClassName, clr);
                            bExpr = eqApplicationIdentity(value, null, expr, null, storeMgr, clr, cmd);
                        }
                        else
                        {
                            // Value not PersistenceCapable nor an identity, so return nothing "(1 = 0)"
                            bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                        }
                    }
                }
                else
                {
                    // Value is a PersistenceCapable
                    if (cmd.getIdentityType() == IdentityType.APPLICATION)
                    {
                        // Application identity
                        if (api.getIdForObject(value) != null)
                        {
                            // Persistent PC object (FCO)
                            // Cater for composite PKs and parts of PK being PC mappings, and recursion
                            JavaTypeMapping[] pkMappingsApp = new JavaTypeMapping[expr.expressionList.size()];
                            Object[] pkFieldValues = new Object[expr.expressionList.size()];
                            int position = 0;
                            for (int i=0;i<cmd.getNoOfPrimaryKeyMembers();i++)
                            {
                                AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
                                Object fieldValue = getFieldValue(mmd, value);
                                JavaTypeMapping mapping = storeMgr.getMappingManager().getMappingWithDatastoreMapping(
                                    fieldValue.getClass(), false, false, clr);
                                if (mapping instanceof PersistenceCapableMapping)
                                {
                                    position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappingsApp,
                                        pkFieldValues, position, (PersistenceCapableMapping)mapping,
                                        cmd, mmd, fieldValue, storeMgr, clr);
                                }
                                else
                                {
                                    pkMappingsApp[position] = mapping;
                                    pkFieldValues[position] = fieldValue;
                                    position++;
                                }
                            }

                            for (int i=0; i<expr.expressionList.size(); i++)
                            {
                                ScalarExpression source = expr.expressionList.getExpression(i);
                                ScalarExpression target = pkMappingsApp[i].newLiteral(qs, pkFieldValues[i]);
                                if (bExpr == null)
                                {
                                    bExpr = source.eq(target);
                                }
                                else
                                {
                                    bExpr = bExpr.and(source.eq(target));
                                }
                            }
                        }
                        else
                        {
                            // PC object with no id (embedded, or transient maybe)
                            for (int i=0; i<expr.expressionList.size(); i++)
                            {
                                // Query should return nothing (so just do "(1 = 0)")
                                NucleusLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                                // It is arguable that we should compare the id with null (as below)
                                /*bExpr = expr.eq(new NullLiteral(qs));*/
                            }
                        }
                    }
                    else
                    {
                        // Datastore identity
                        for (int i=0; i<expr.expressionList.size(); i++)
                        {
                            ScalarExpression source = expr.expressionList.getExpression(i);
                            OID objectId = (OID)api.getIdForObject(value);
                            if (objectId == null)
                            {
                                // PC object with no id (embedded, or transient maybe)
                                // Query should return nothing (so just do "(1 = 0)")
                                NucleusLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                                // It is arguable that we should compare the id with null (as below)
                                /*bExpr = expr.eq(new NullLiteral(qs));*/
                            }
                            else
                            {
                                JavaTypeMapping m = storeMgr.getMappingManager().getMappingWithDatastoreMapping(
                                    objectId.getKeyValue().getClass(), false, false, clr);
                                ScalarExpression oidExpr = m.newLiteral(qs, objectId.getKeyValue());
                                bExpr = source.eq(oidExpr);
                            }
                        }
View Full Code Here

                createPerImplementationColumnsForReferenceField(false, false, false, false,
                    roleForMember, colmds, clr);
            }
            else
            {
                MappedStoreManager storeMgr = datastoreContainer.getStoreManager();
                if (mmd.getMappedBy() == null)
                {
                    // Unidirectional 1-1
                    createPerImplementationColumnsForReferenceField(false, true, false,
                        mmd.isEmbedded() || mmd.getElementMetaData() != null,
                        roleForMember, mmd.getColumnMetaData(), clr);
                }
                else
                {
                    // Bidirectional 1-1/N-1
                    AbstractClassMetaData refCmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForInterface(mmd.getType(), clr);
                    if (refCmd != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                    {
                        // TODO Is this block actually reachable ? Would we specify "inheritance" under "interface" elements?
                        // Find the actual tables storing the other end (can be multiple subclasses)
                        AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
                        if (cmds != null && cmds.length > 0)
                        {
                            if (cmds.length > 1)
                            {
                                NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() +
                                    " represents either a 1-1 relation, or a N-1 relation where the other end uses" +
                                    " \"subclass-table\" inheritance strategy and more than 1 subclasses with a table. " +
                                    "This is not fully supported currently");
                            }
                        }
                        else
                        {
                            // No subclasses of the class using "subclasses-table" so no mapping!
                            // TODO Throw an exception ?
                            return;
                        }
                        // TODO We need a mapping for each of the possible subclass tables
                        /*JavaTypeMapping referenceMapping = */storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIDMapping();
                    }
                    else
                    {
                        String[] implTypes = MetaDataUtils.getInstance().getImplementationNamesForReferenceField(mmd,
                            FieldRole.ROLE_FIELD, clr);
                        for (int j=0; j<implTypes.length; j++)
                        {
                            JavaTypeMapping refMapping = storeMgr.getDatastoreClass(implTypes[j], clr).getIDMapping();
                            JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(clr.classForName(implTypes[j]));
                            mapping.setReferenceMapping(refMapping);
                            this.addJavaTypeMapping(mapping);
                        }
                    }
                }
View Full Code Here

            {
                fieldTypeName = mmd.getMap().getValueType();
            }
        }

        MappedStoreManager storeMgr = datastoreContainer.getStoreManager();
        boolean isPersistentInterfaceField = storeMgr.getOMFContext().getMetaDataManager().isPersistentInterface(fieldTypeName);

        // Set the PK and nullability of column(s) for the implementations (based on the number of impls etc)
        if (implTypes.length > 1)
        {
            pk = false; // Cannot be part of PK if more than 1 implementation
        }
        if (implTypes.length > 1 && !pk)
        {
            nullable = true; // Must be nullable if more than 1 impl (since only 1 impl can have value at a time)
        }

        // Create list of classes that require columns.
        // We only add columns for the implementation that is the root of a particular inheritance tree
        // e.g if we have A implements I1, and B extends A then they both are valid implementations
        // but we only want to create column(s) for A.
        Collection implClasses = new ArrayList();
        for (int i=0;i<implTypes.length;i++)
        {
            Class type = clr.classForName(implTypes[i]);
            if (type == null)
            {
                throw new NucleusUserException(LOCALISER_MAPPED.msg("020189", mmd.getTypeName(), implTypes[i]));
            }
            else if (type.isInterface())
            {
                throw new NucleusUserException(LOCALISER_MAPPED.msg("020190", mmd.getFullFieldName(),
                    mmd.getTypeName(), implTypes[i]));
            }

            Iterator iter = implClasses.iterator();
            boolean toBeAdded = true;
            Class clsToSwap = null;
            while (iter.hasNext())
            {
                Class cls = (Class)iter.next();
                if (cls == type)
                {
                    // Implementation already present
                    toBeAdded = false;
                    break;
                }
                else
                {
                    if (type.isAssignableFrom(cls))
                    {
                        // "type" is superclass of "cls" so swap subclass for this class
                        clsToSwap = cls;
                        toBeAdded = false;
                        break;
                    }
                    else if (cls.isAssignableFrom(type))
                    {
                        toBeAdded = false;
                        break;
                    }
                }
            }
            if (toBeAdded)
            {
                implClasses.add(type);
            }
            else if (clsToSwap != null)
            {
                implClasses.remove(clsToSwap);
                implClasses.add(type);
            }
        }

        // Add columns for each of these implementations
        int colPos = 0;
        Iterator implClsIter = implClasses.iterator();
        while (implClsIter.hasNext())
        {
            Class implClass = (Class)implClsIter.next();

            boolean present = false;
            int numJavaTypeMappings = getJavaTypeMapping().length;
            for (int i=0;i<numJavaTypeMappings;i++)
            {
                JavaTypeMapping implMapping = getJavaTypeMapping()[i];
                if (implClass.getName().equals(implMapping.getType()))
                {
                    present = true;
                }
            }
            if (present)
            {
                // Implementation already present in mapping (e.g reinitialising) so skip this
                continue;
            }

            boolean columnsNeeded = true;
            if (isPersistentInterfaceField &&
                !storeMgr.getOMFContext().getMetaDataManager().isPersistentInterfaceImplementation(fieldTypeName, implClass.getName()))
            {
                // We have a "persistent-interface" field yet this is not a generated implementation so ignore it
                // It is arguable if we should allow the real implementations of this interface here, but the JDO2 TCK doesn't
                // make that assumption so we don't either
                columnsNeeded = false;
            }

            if (columnsNeeded)
            {
                // Get the mapping for this implementation
                JavaTypeMapping m;
                if (storeMgr.getMappedTypeManager().isSupportedMappedType(implClass.getName()))
                {
                    m = storeMgr.getMappingManager().getMapping(implClass, serialised, embedded,
                        mmd.getFullFieldName());
                }
                else
                {
                    try
                    {
                        DatastoreClass dc = storeMgr.getDatastoreClass(implClass.getName(), clr);
                        m = dc.getIDMapping();
                    }
                    catch (NoTableManagedException ex)
                    {
                        // TODO Localise this message
View Full Code Here

                 * {                              {
                 *    B b;                            A a;
                 * }                              }
                 */
                int[] colNums = null;
                MappedStoreManager storeMgr = qs.getStoreManager();
                JavaTypeMapping[] sourceMappings = null;
                DatastoreClass[] sourceTables = null;
                if (m instanceof ReferenceMapping)
                {
                    // Reference field (with "n" implementations)
                    JavaTypeMapping[] refMappings = ((ReferenceMapping)m).getJavaTypeMapping();
                    sourceTables = new DatastoreClass[refMappings.length];
                    sourceMappings = new JavaTypeMapping[refMappings.length];
                    for (int j=0;j<refMappings.length;j++)
                    {
                        sourceTables[j] = storeMgr.getDatastoreClass(refMappings[j].getType(), clr);
                        sourceMappings[j] = sourceTables[j].getMemberMapping(fmd.getMappedBy());
                    }
                }
                else
                {
                    // PC field
                    sourceTables = new DatastoreClass[1];
                    sourceMappings = new JavaTypeMapping[1];
                    sourceTables[0] = storeMgr.getDatastoreClass(fmd.getTypeName(), clr);
                    sourceMappings[0] = sourceTables[0].getMemberMapping(fmd.getMappedBy());
                }

                for (int j=0;j<sourceMappings.length;j++)
                {
                    // Do a LEFT OUTER JOIN for this sourceMapping back to the target id column(s)

                    // 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(IdentifierType.TABLE,
                            "SOURCECLASS" + fmd.getAbsoluteFieldNumber() + "_" + j);
                    LogicSetExpression sourceTableExpr = qs.newTableExpression(sourceTables[j], sourceTableIdentifier, true)[0];

                    // Left outer join from target ID to the mapped-by field on the source class
                    ScalarExpression sourceExpr = sourceMappings[j].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[j].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 k=0;k<tmpColNums.length;k++)
                        {
                            colNums[k] = tmpColNums[k];
                        }
                        for (int k=0;k<columnNumbersByField.length;k++)
                        {
                            colNums[tmpColNums.length+k] = columnNumbersByField[k];
                        }
                        tmpColNums = null;
                    }
                    else
                    {
                        // Add to the start of our column numbers
                        colNums = new int[columnNumbersByField.length];
                        for (int k=0;k<columnNumbersByField.length;k++)
                        {
                            colNums[k] = columnNumbersByField[k];
                        }
                    }
                }

                stmtMapping.setColumnPositions(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]);
                    DatastoreElementContainer collTable = (DatastoreElementContainer)joinTable;
                    JavaTypeMapping referenceMapping = collTable.getElementMapping();
                    JavaTypeMapping selectMapping = collTable.getOwnerMapping();

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

            // 1-1 bidirectional "mapped-by" relation, so use ID mappings of related class to retrieve the value
          if (referenceMapping != null) //TODO why is it null for PC concrete classes?
          {
                return referenceMapping.getObject(om, rs, param);
          }
          MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
            Class fieldType = mmd.getType();
            JavaTypeMapping referenceMapping = storeMgr.getDatastoreClass(fieldType.getName(), om.getClassLoaderResolver()).getIDMapping();
            value = referenceMapping.getDataStoreMapping(0).getObject(rs, param[0]);
        }

        if (value != null)
        {
View Full Code Here

    {
        // SingleFieldIdentity
        int paramNumber = param[0];
        try
        {
            MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
            Object idObj = storeMgr.getResultValueAtPosition(rs, this, paramNumber);
            if (idObj == null)
            {
                throw new NucleusException(LOCALISER.msg("041039")).setFatal();
            }
            else
View Full Code Here

            for (int i=0; i<cmd.getPKMemberPositions().length; ++i)
            {
                AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
                Field field = objectIdClass.getField(fmd.getName());

                MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
                JavaTypeMapping m = storeMgr.getDatastoreClass(cmd.getFullClassName(), om.getClassLoaderResolver()).getMemberMapping(fmd);
                // NOTE This assumes that each field has one datastore column.
                for (int j = 0; j < m.getNumberOfDatastoreFields(); j++)
                {
                    Object obj = storeMgr.getResultValueAtPosition(rs, this, param[paramIndex++]);
                    if ((obj instanceof BigDecimal))
                    {
                        BigDecimal bigDecimal = (BigDecimal) obj;
                        // Oracle 10g returns BigDecimal for NUMBER columns,
                        // resulting in IllegalArgumentException when reflective
View Full Code Here

TOP

Related Classes of org.datanucleus.store.mapped.MappedStoreManager

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.