Package org.datanucleus.store.mapped

Examples of org.datanucleus.store.mapped.DatastoreClass


            for (int i=0;i<implMappings.length;i++)
            {
                Class implType = clr.classForName(implMappings[i].getType());
                if (type.isAssignableFrom(implType))
                {
                    DatastoreClass castTable = storeMgr.getDatastoreClass(type.getName(), clr);
                    SQLTable castSqlTbl = stmt.leftOuterJoin(table, implMappings[i], refMapping,
                        castTable, null, castTable.getIdMapping(), null, null, null);
                    return exprFactory.newExpression(stmt, castSqlTbl, castTable.getIdMapping());
                }
            }

            // No implementation matching this cast type, so return false
            NucleusLogger.QUERY.warn("Unable to process cast of interface field to " + type.getName() +
                " since it has no implementations that match that type");
            JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
            return exprFactory.newLiteral(stmt, m, false).eq(exprFactory.newLiteral(stmt, m, true));
        }
        else if (mapping instanceof PersistableMapping)
        {
            // Check if there is already the cast table in the current table group
            DatastoreClass castTable = storeMgr.getDatastoreClass(type.getName(), clr);
            SQLTable castSqlTbl = stmt.getTable(castTable, table.getGroupName());
            if (castSqlTbl == null)
            {
                // Join not present, so join to the cast table
                castSqlTbl = stmt.leftOuterJoin(table, table.getTable().getIdMapping(),
                    castTable, null, castTable.getIdMapping(), null, table.getGroupName());
            }

            // Return an expression based on the cast table
            return exprFactory.newExpression(stmt, castSqlTbl, castTable.getIdMapping());
        }
        else
        {
            // TODO Handle other casts
        }
View Full Code Here


        else if (mapping instanceof PersistableMapping || mapping instanceof ReferenceMapping)
        {
            // Field has its own table, so join to it
            AbstractClassMetaData fieldCmd = storeMgr.getMetaDataManager().getMetaDataForClass(
                mapping.getType(), clr);
            DatastoreClass memberTable = null;
            if (fieldCmd.getInheritanceMetaData().getStrategy() == 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 = storeMgr.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)
                    {
                        NucleusLogger.QUERY.warn(LOCALISER_CORE.msg("037006",
                            mapping.getMemberMetaData().getFullFieldName(), cmds[0].getFullClassName()));
                    }
                    memberTable = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr);
                }
                else
                {
                    // No subclasses with tables to join to, so throw a user error
                    throw new NucleusUserException(LOCALISER_CORE.msg("037005",
                        mapping.getMemberMetaData().getFullFieldName()));
                }
            }
            else
            {
                // Class of the field will have its own table
                memberTable = storeMgr.getDatastoreClass(mapping.getType(), clr);
            }

            DiscriminatorMetaData dismd = memberTable.getDiscriminatorMetaData();
            DiscriminatorMapping discMapping = (DiscriminatorMapping)memberTable.getDiscriminatorMapping(false);
            if (discMapping != null)
            {
                SQLTable targetSqlTbl = null;
                if (mapping.getDatastoreContainer() != memberTable)
                {
                    // FK is on source table so inner join to target table (holding the discriminator)
                    targetSqlTbl = stmt.getTable(memberTable, null);
                    if (targetSqlTbl == null)
                    {
                        targetSqlTbl = stmt.innerJoin(getSQLTable(), mapping, memberTable, null, memberTable.getIdMapping(),
                            null, null);
                    }
                }
                else
                {
                    // FK is on target side and already joined
                    targetSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(stmt, getSQLTable(), discMapping);
                }

                // Add restrict to discriminator for the instanceOf type and subclasses
                SQLTable discSqlTbl = targetSqlTbl;
                BooleanExpression discExpr =
                    SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, type.getName(),
                        dismd, discMapping, discSqlTbl, clr);

                Iterator subclassIter = storeMgr.getSubClassesForClass(type.getName(), true, clr).iterator();
                boolean hasSubclass = false;
                while (subclassIter.hasNext())
                {
                    String subclassName = (String)subclassIter.next();
                    BooleanExpression discExprSub =
                        SQLStatementHelper.getExpressionForDiscriminatorForClass(stmt, subclassName,
                            dismd, discMapping, discSqlTbl, clr);
                    discExpr = discExpr.ior(discExprSub);
                    hasSubclass = true;
                }
                if (hasSubclass)
                {
                    discExpr.encloseInParentheses();
                }
                return (not ? discExpr.not() : discExpr);
            }
            else
            {
                // Join to relevant table
                DatastoreClass table = null;
                if (fieldCmd.getInheritanceMetaData().getStrategy() == 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 = storeMgr.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)
                        {
                            NucleusLogger.QUERY.warn(LOCALISER_CORE.msg("037006",
                                mapping.getMemberMetaData().getFullFieldName(), cmds[0].getFullClassName()));
                        }
                        table = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr);
                    }
                    else
                    {
                        // No subclasses with tables to join to, so throw a user error
                        throw new NucleusUserException(LOCALISER_CORE.msg("037005",
                            mapping.getMemberMetaData().getFullFieldName()));
                    }
                }
                else
                {
                    // Class of the field will have its own table
                    table = storeMgr.getDatastoreClass(mapping.getType(), clr);
                }

                if (table.managesClass(type.getName()))
                {
                    // This type is managed in this table so must be an instance
                    JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                    return exprFactory.newLiteral(stmt, m, true).eq(
                        exprFactory.newLiteral(stmt, m, !not));
                }
                else
                {
                    if (table == stmt.getPrimaryTable().getTable())
                    {
                        // Candidate is used in instanceof, and union statement so restrict some unions
                        // Note that this is only really valid is wanting "a instanceof SUB1".
                        // It fails when we want to do "a instanceof SUB1 || a instanceof SUB2"
                        // TODO How do we handle those cases?
                        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                        if (stmt.getNumberOfUnions() > 0)
                        {
                            Class mainCandidateCls = clr.classForName(stmt.getCandidateClassName());
                            if (type.isAssignableFrom(mainCandidateCls) == not)
                            {
                                SQLExpression unionClauseExpr = exprFactory.newLiteral(stmt, m, true).eq(
                                    exprFactory.newLiteral(stmt, m, false));
                                stmt.whereAnd((BooleanExpression)unionClauseExpr, false);
                            }

                            List<SQLStatement> unionStmts = stmt.getUnions();
                            Iterator<SQLStatement> iter = unionStmts.iterator();
                            while (iter.hasNext())
                            {
                                SQLStatement unionStmt = iter.next();
                                Class unionCandidateCls = clr.classForName(unionStmt.getCandidateClassName());
                                if (type.isAssignableFrom(unionCandidateCls) == not)
                                {
                                    SQLExpression unionClauseExpr = exprFactory.newLiteral(unionStmt, m, true).eq(
                                        exprFactory.newLiteral(unionStmt, m, false));
                                    unionStmt.whereAnd((BooleanExpression)unionClauseExpr, false);
                                }
                            }

                            // Just return true since we applied the condition direct to the unions
                            SQLExpression returnExpr = exprFactory.newLiteral(stmt, m, true).eq(
                                exprFactory.newLiteral(stmt, m, true));
                            return (BooleanExpression)returnExpr;
                        }
                        else
                        {
                            Class mainCandidateCls = clr.classForName(stmt.getCandidateClassName());
                            if (!type.isAssignableFrom(mainCandidateCls))
                            {
                                return exprFactory.newLiteral(stmt, m, true).eq(
                                    exprFactory.newLiteral(stmt, m, not));
                            }
                            else
                            {
                                return exprFactory.newLiteral(stmt, m, true).eq(
                                    exprFactory.newLiteral(stmt, m, !not));
                            }
                        }
                    }
                    else
                    {
                        // Do inner join to this table to impose the instanceOf
                        DatastoreClass instanceofTable = storeMgr.getDatastoreClass(type.getName(), clr);
                        if (stmt.getNumberOfUnions() > 0)
                        {
                            // Inner join will likely not give the right result
                            NucleusLogger.QUERY.debug("InstanceOf for " + table +
                                " but no discriminator so adding inner join to " + instanceofTable +
                            " : in some cases with UNIONs this may fail");
                        }
                        stmt.innerJoin(this.table, this.table.getTable().getIdMapping(),
                            instanceofTable, null, instanceofTable.getIdMapping(), null, this.table.getGroupName());
                        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                        return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, !not));
                    }
                }
            }
View Full Code Here

        {
            public void fetchFields(ObjectProvider esm)
            {
                // Find the (element) table storing the FK back to the owner
                boolean isPersistentInterface = storeMgr.getNucleusContext().getMetaDataManager().isPersistentInterface(elementType);
                DatastoreClass elementTable = null;
                if (isPersistentInterface)
                {
                    elementTable = storeMgr.getDatastoreClass(
                        storeMgr.getNucleusContext().getMetaDataManager().getImplementationNameForPersistentInterface(elementType), clr);
                }
                else
                {
                    elementTable = storeMgr.getDatastoreClass(elementType, clr);
                }
                if (elementTable == null)
                {
                    // "subclass-table", persisted into table of other class
                    AbstractClassMetaData[] managingCmds = storeMgr.getClassesManagingTableForClass(emd, clr);
                    if (managingCmds != null && managingCmds.length > 0)
                    {
                        // Find which of these subclasses is appropriate for this element
                        for (int i=0;i<managingCmds.length;i++)
                        {
                            Class tblCls = clr.classForName(managingCmds[i].getFullClassName());
                            if (tblCls.isAssignableFrom(esm.getObject().getClass()))
                            {
                                elementTable = storeMgr.getDatastoreClass(managingCmds[i].getFullClassName(), clr);
                                break;
                            }
                        }
                    }
                }

                if (elementTable != null)
                {
                    JavaTypeMapping externalFKMapping = elementTable.getExternalMapping(ownerMemberMetaData, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
                    if (externalFKMapping != null)
                    {
                        // The element has an external FK mapping so set the value it needs to use in the INSERT
                        esm.setAssociatedValue(externalFKMapping, sm.getObject());
                    }
View Full Code Here

            int colPos = 0;
            for (int i=0; i<subclassCmds.length; i++)
            {
                Class type = clr.classForName(subclassCmds[i].getFullClassName());
                DatastoreClass dc = storeMgr.getDatastoreClass(subclassCmds[i].getFullClassName(), clr);
                JavaTypeMapping m = dc.getIdMapping();

                // obtain the column names for this type
                // TODO Fix this. The <column> elements have no way of being ordered to match the subclasses
                ColumnMetaData[] columnMetaDataForType = null;
                if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > 0)
View Full Code Here

                }
                else
                {
                    try
                    {
                        DatastoreClass dc = storeMgr.getDatastoreClass(implClass.getName(), clr);
                        m = dc.getIdMapping();
                    }
                    catch (NoTableManagedException ex)
                    {
                        // TODO Localise this message
                        throw new NucleusUserException("Cannot define columns for " + mmd.getFullFieldName() +
View Full Code Here

     * @return Element information relating to the element type
     */
    protected ElementInfo[] getElementInformationForClass()
    {
        ElementInfo[] info = null;
        DatastoreClass tbl;
        String[] clsNames;
        if (!clr.classForName(elementType).isInterface())
        {
            tbl = storeMgr.getDatastoreClass(elementType, clr);
            clsNames = new String[] {elementType};
        }
        else
        {
            clsNames = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
            tbl = storeMgr.getDatastoreClass(clsNames[0], clr);
        }
        if (tbl == null)
        {
            AbstractClassMetaData[] subclassCmds = storeMgr.getClassesManagingTableForClass(emd, clr);
            info = new ElementInfo[subclassCmds.length];
            for (int i=0;i<subclassCmds.length;i++)
            {
                DatastoreClass table = storeMgr.getDatastoreClass(subclassCmds[i].getFullClassName(), clr);
                info[i] = new ElementInfo(subclassCmds[i], table);
            }
        }
        else
        {
            info = new ElementInfo[clsNames.length];
            for (int i=0; i<clsNames.length; i++)
            {
                AbstractClassMetaData cmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(clsNames[i], clr);
                DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
                info[i] = new ElementInfo(cmd, table);
            }
        }
        return info;
    }
View Full Code Here

                String[] implNames = MetaDataUtils.getInstance().getImplementationNamesForReferenceField(ownerMemberMetaData,
                    FieldRole.ROLE_COLLECTION_ELEMENT, clr, storeMgr.getMetaDataManager());
                elementInfo = new ElementInfo[implNames.length];
                for (int i=0;i<implNames.length;i++)
                {
                    DatastoreClass table = storeMgr.getDatastoreClass(implNames[i], clr);
                    AbstractClassMetaData cmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(implNames[i], clr);
                    elementInfo[i] = new ElementInfo(cmd,table);
                }
            }
            else
View Full Code Here

        int totalFieldCount = cmd.getNoOfManagedMembers() + cmd.getNoOfInheritedManagedMembers();
        final StatementMappingIndex[] statementExpressionIndex = new StatementMappingIndex[totalFieldCount];
        int paramIndex = 0;

        final MappedStoreManager storeMgr = (MappedStoreManager)ec.getStoreManager();
        DatastoreClass datastoreClass = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
        final int[] pkFieldNumbers = cmd.getPKMemberPositions();

        for (int i=0; i<pkFieldNumbers.length; ++i)
        {
            AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNumbers[i]);
            JavaTypeMapping m = datastoreClass.getMemberMapping(fmd);
            statementExpressionIndex[fmd.getAbsoluteFieldNumber()] = new StatementMappingIndex(m);
            int expressionsIndex[] = new int[m.getNumberOfDatastoreMappings()];
            for (int j = 0; j < expressionsIndex.length; j++)
            {
                expressionsIndex[j] = param[paramIndex++];
View Full Code Here

            }
        }
        else if (relationType == Relation.ONE_TO_ONE_BI && mmd.getMappedBy() != null)
        {
            // 1-1 with FK at other side
            DatastoreClass relatedTable = storeMgr.getDatastoreClass(relatedMmds[0].getClassName(), clr);
            JavaTypeMapping relatedMapping = relatedTable.getMemberMapping(relatedMmds[0]);
            boolean isNullable = relatedMapping.isNullable();
            ObjectProvider otherSM = ec.findObjectProvider(pc);
            if (dependent)
            {
                if (isNullable)
View Full Code Here

    {
        try
        {
            // TODO This doesn't take into account serialised/embedded
            // If the type has its own table just take the id mapping of its table
            DatastoreClass datastoreClass = storeMgr.getDatastoreClass(c.getName(), clr);
            return datastoreClass.getIdMapping();
        }
        catch (NoTableManagedException ex)
        {
            // Doesn't allow for whether a field is serialised/embedded so they get the default mapping only
            Class mc = getMappingClass(c, serialised, embedded, null);
View Full Code Here

TOP

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

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.