Package org.datanucleus.store.rdbms.key

Examples of org.datanucleus.store.rdbms.key.CandidateKey


                {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
                    if (umd != null)
                    {
                        CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                        if (ck != null)
                        {
                            candidateKeys.add(ck);
                        }
                    }
                }
            }
            else
            {
                // Add any required candidate key for this field
                UniqueMetaData umd = fmd.getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, fieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
            }
        }

        // Add on any user-required candidate keys for the class(es) as a whole (composite keys)
        Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
        while (cmdIter.hasNext())
        {
            AbstractClassMetaData thisCmd = cmdIter.next();
            UniqueMetaData[] classCKs = thisCmd.getUniqueMetaData();
            if (classCKs != null)
            {
                for (int i=0;i<classCKs.length;i++)
                {
                    CandidateKey ck = getCandidateKeyForUniqueMetaData(classCKs[i]);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
View Full Code Here


     * @param umd The Unique MetaData
     * @return The Candidate Key
     */
    private CandidateKey getCandidateKeyForUniqueMetaData(UniqueMetaData umd)
    {
        CandidateKey ck = new CandidateKey(this);

        // Set the key name if required
        if (umd.getName() != null)
        {
            ck.setName(umd.getName());
        }

        // Class-level index so use its column definition
        ColumnMetaData[] colmds = umd.getColumnMetaData();
        AbstractMemberMetaData[] mmds = umd.getMemberMetaData();
        // a). Columns specified directly
        if (colmds != null && colmds.length > 0)
        {
            for (int i=0;i<colmds.length;i++)
            {
                DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newDatastoreFieldIdentifier(colmds[i].getName());
                Column col = columnsByName.get(colName);
                if (col == null)
                {
                    NucleusLogger.DATASTORE.warn(LOCALISER.msg("058202",
                        toString(), ck.getName(), colmds[i].getName()));
                    break;
                }
                else
                {
                    ck.addDatastoreField(col);
                }
            }
        }
        // b). Columns specified using fields
        else if (mmds != null && mmds.length > 0)
        {
            for (int i=0;i<mmds.length;i++)
            {
                // Find the metadata for the actual field with the same name as this "unique" field
                AbstractMemberMetaData realMmd = getMetaDataForMember(mmds[i].getName());
                JavaTypeMapping memberMapping = memberMappingsMap.get(realMmd);
                int countFields = memberMapping.getNumberOfDatastoreMappings();
                for (int j=0; j<countFields; j++)
                {
                    ck.addDatastoreField(memberMapping.getDatastoreMapping(j).getDatastoreField());
                }
            }
        }
        else
        {
            // We can't have an index of no columns
            NucleusLogger.DATASTORE.warn(LOCALISER.msg("058203",
                toString(), ck.getName()));
            return null;
        }

        return ck;
    }
View Full Code Here

            // User has defined a unique key on the join table
            UniqueMetaData unimd = mmd.getJoinMetaData().getUniqueMetaData();
            ColumnMetaData[] colmds = unimd.getColumnMetaData();
            if (colmds != null)
            {
                CandidateKey uniKey = new CandidateKey(this);
                IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
                for (int i=0;i<colmds.length;i++)
                {
                    DatastoreField col = getDatastoreField(idFactory.newDatastoreFieldIdentifier(colmds[i].getName()));
                    if (col != null)
                    {
                        uniKey.addDatastoreField(col);
                    }
                    else
                    {
                        throw new NucleusUserException("Unique key on join-table " + this + " has column " +
                            colmds[i].getName() + " that is not found");
View Full Code Here

        Iterator<CandidateKey> cks = candidateKeysByMapField.values().iterator();
        while (cks.hasNext())
        {
            DatastoreIdentifier ckName = idFactory.newCandidateKeyIdentifier(this, ++ckNum);
            CandidateKey ck = cks.next();
            ck.setName(ckName.getIdentifierName());

            String ckSql = dba.getAddCandidateKeyStatement(ck, idFactory);
            if (ckSql != null)
            {
                stmts.add(ckSql);
View Full Code Here

                            // a unique constraint on them. If the key field is in a superclass then we
                            // cannot do this so just omit it.
                            if (keyMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDatastoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the key field
                                int countKeyFields = keyMapping.getNumberOfDatastoreMappings();
                                for (int i = 0; i < countKeyFields; i++)
                                {
                                    Column col = (Column) keyMapping.getDatastoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        NucleusLogger.DATASTORE.warn(LOCALISER.msg("057041",
                                            ownerMmd.getName()));
                                    }
                                }

                                if (candidateKeysByMapField.put(mfmd, ck) != null)
                                {
                                    // We have multiple "mapped-by" coming to this field so give a warning that this may potentially
                                    // cause problems. For example if they have the key field defined here for 2 different relations
                                    // so you may get keys/values appearing in the other relation that shouldn't be.
                                    // Logged as a WARNING for now.
                                    // If there is a situation where this should throw an exception, please update this AND COMMENT WHY.
                                    NucleusLogger.DATASTORE.warn(LOCALISER.msg("057012",
                                        mfmd.getFullFieldName(), ownerMmd.getFullFieldName()));
                                }
                            }
                        }
                    }
                    else if (ownerMmd.getValueMetaData() != null && ownerMmd.getValueMetaData().getMappedBy() != null)
                    {
                        // Value field is stored in the key table
                        AbstractMemberMetaData vmd = null;
                        String value_field_name = ownerMmd.getValueMetaData().getMappedBy();
                        if (value_field_name != null)
                        {
                            vmd = cmd.getMetaDataForMember(value_field_name);
                        }
                        if (vmd == null)
                        {
                            throw new ClassDefinitionException(LOCALISER.msg("057008", mfmd));
                        }

                        JavaTypeMapping ownerMapping = getMemberMapping(map_field_name);
                        JavaTypeMapping valueMapping = getMemberMapping(vmd.getName());

                        if (dba.supportsOption(RDBMSAdapter.NULLS_IN_CANDIDATE_KEYS) ||
                            (!ownerMapping.isNullable() && !valueMapping.isNullable()))
                        {
                            // If the owner and value fields are represented in this table then we can impose
                            // a unique constraint on them. If the value field is in a superclass then we
                            // cannot do this so just omit it.
                            if (valueMapping.getDatastoreContainer() == this &&
                                ownerMapping.getDatastoreContainer() == this)
                            {
                                CandidateKey ck = new CandidateKey(this);

                                // This HashSet is to avoid duplicate adding of columns.
                                HashSet addedColumns = new HashSet();

                                // Add columns for the owner field
                                int countOwnerFields = ownerMapping.getNumberOfDatastoreMappings();
                                for (int i = 0; i < countOwnerFields; i++)
                                {
                                    Column col = (Column) ownerMapping.getDatastoreMapping(i).getDatastoreField();
                                    addedColumns.add(col);
                                    ck.addDatastoreField(col);
                                }

                                // Add columns for the value field
                                int countValueFields = valueMapping.getNumberOfDatastoreMappings();
                                for (int i = 0; i < countValueFields; i++)
                                {
                                    Column col = (Column) valueMapping.getDatastoreMapping(i).getDatastoreField();
                                    if (!addedColumns.contains(col))
                                    {
                                        addedColumns.add(col);
                                        ck.addDatastoreField(col);
                                    }
                                    else
                                    {
                                        NucleusLogger.DATASTORE.warn(LOCALISER.msg("057042",
                                            ownerMmd.getName()));
View Full Code Here

            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
            }
        }

        if (valueMapping instanceof EmbeddedValuePCMapping)
        {
            // Add all candidate keys required by fields of the embedded value
            EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping)valueMapping;
            for (int i=0;i<embMapping.getNumberOfJavaTypeMappings();i++)
            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
View Full Code Here

     * @param fieldMapping Mapping for the field
     * @return The Candidate Key
     */
    public static CandidateKey getCandidateKeyForField(DatastoreContainerObject table, UniqueMetaData umd, JavaTypeMapping fieldMapping)
    {
        CandidateKey ck = new CandidateKey(table);

        // Set the key name if required
        if (umd.getName() != null)
        {
            IdentifierFactory idFactory = table.getStoreManager().getIdentifierFactory();
            DatastoreIdentifier ckId = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, umd.getName());
            ck.setName(ckId.toString());
        }

        // Field-level index so use all columns for the field
        int countFields = fieldMapping.getNumberOfDatastoreMappings();
        for (int j=0; j<countFields; j++)
        {
            ck.addDatastoreField(fieldMapping.getDatastoreMapping(j).getDatastoreField());
        }

        return ck;
    }
View Full Code Here

                        continue;
                    }
   
                    String keyName = (String)indexInfo.getProperty("index_name");
                    DatastoreIdentifier idxName = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, keyName);
                    CandidateKey key = (CandidateKey) candidateKeysByName.get(idxName);
                    if (key == null)
                    {
                        key = new CandidateKey(this);
                        key.setName(keyName);
                        candidateKeysByName.put(idxName, key);
                    }
   
                    // Set the column
                    int colSeq = ((Short)indexInfo.getProperty("ordinal_position")).shortValue() - 1;
                    DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN,
                        (String)indexInfo.getProperty("column_name"));
                    Column col = columnsByName.get(colName);
                    if (col != null)
                    {
                        key.setDatastoreField(colSeq, col);
                    }
                }
            }
        }
        return candidateKeysByName;
View Full Code Here

        Iterator i = expectedCandidateKeys.iterator();
        int n = 1;
        IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
        while (i.hasNext())
        {
            CandidateKey ck = (CandidateKey) i.next();
            if (!actualCandidateKeysByName.containsValue(ck))
            {
                // If no name assigned, make one up
                if (ck.getName() == null)
                {
                    // Use the CandidateKeyIdentifier to generate the name
                    DatastoreIdentifier ckName;
                    do
                    {
                        ckName = idFactory.newCandidateKeyIdentifier(this, n++);
                    }
                    while (actualCandidateKeysByName.containsKey(ckName));
                    ck.setName(ckName.getIdentifierName());
                }
                String stmtText = dba.getAddCandidateKeyStatement(ck, idFactory);
                if (stmtText != null)
                {
                    stmtsByCKName.put(ck.getName(), stmtText);
                }
            }
        }

        return stmtsByCKName;
View Full Code Here

            {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
                if (umd != null)
                {
                    CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                    if (ck != null)
                    {
                        candidateKeys.add(ck);
                    }
                }
            }
        }

        if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getUniqueMetaData() != null)
        {
            // User has defined a unique key on the join table
            UniqueMetaData unimd = mmd.getJoinMetaData().getUniqueMetaData();
            ColumnMetaData[] colmds = unimd.getColumnMetaData();
            if (colmds != null)
            {
                CandidateKey uniKey = new CandidateKey(this);
                IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
                for (int i=0;i<colmds.length;i++)
                {
                    DatastoreField col = getDatastoreField(idFactory.newDatastoreFieldIdentifier(colmds[i].getName()));
                    if (col != null)
                    {
                        uniKey.addDatastoreField(col);
                    }
                    else
                    {
                        throw new NucleusUserException("Unique key on join-table " + this + " has column " +
                            colmds[i].getName() + " that is not found");
View Full Code Here

TOP

Related Classes of org.datanucleus.store.rdbms.key.CandidateKey

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.