Search in sources :

Example 21 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class TableImpl method isIndexReallyNeeded.

/**
 * Utility to check if an index is necessary.
 * @param requiredIdx The index
 * @param actualIndices The actual indexes
 * @return Whether the index is needed (i.e not present in the actual indexes)
 */
private boolean isIndexReallyNeeded(Index requiredIdx, Collection actualIndices) {
    Iterator i = actualIndices.iterator();
    if (requiredIdx.getName() != null) {
        // Compare the index name since it is defined
        IdentifierFactory idFactory = requiredIdx.getTable().getStoreManager().getIdentifierFactory();
        // Allow for user input in incorrect case
        String reqdName = idFactory.getIdentifierInAdapterCase(requiredIdx.getName());
        while (i.hasNext()) {
            Index actualIdx = (Index) i.next();
            // Allow for DB returning no quotes
            String actualName = idFactory.getIdentifierInAdapterCase(actualIdx.getName());
            if (actualName.equals(reqdName) && actualIdx.getTable().getIdentifier().toString().equals(requiredIdx.getTable().getIdentifier().toString())) {
                // There already is an index of that name for the same table in the actual list so not needed
                return false;
            }
        }
    } else {
        // Compare against the index table and columns since we have no index name yet
        while (i.hasNext()) {
            Index actualIdx = (Index) i.next();
            if (actualIdx.toString().equals(requiredIdx.toString()) && actualIdx.getTable().getIdentifier().toString().equals(requiredIdx.getTable().getIdentifier().toString())) {
                // There already is an index of that name for the same table in the actual list so not needed
                return false;
            }
        }
    }
    return true;
}
Also used : Iterator(java.util.Iterator) Index(org.datanucleus.store.rdbms.key.Index) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 22 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class ClassTable method manageMembers.

/**
 * Goes through all specified members for the specified class and adds a mapping for each.
 * Ignores primary-key fields which are added elsewhere.
 * @param theCmd ClassMetaData for the class to be managed
 * @param clr The ClassLoaderResolver
 * @param mmds the fields/properties to manage
 */
private void manageMembers(AbstractClassMetaData theCmd, ClassLoaderResolver clr, AbstractMemberMetaData[] mmds) {
    // Go through the fields for this class and add columns for them
    for (int fieldNumber = 0; fieldNumber < mmds.length; fieldNumber++) {
        // Primary key fields are added by the initialisePK method
        AbstractMemberMetaData mmd = mmds[fieldNumber];
        if (!mmd.isPrimaryKey()) {
            if (managesMember(mmd.getFullFieldName())) {
                if (!mmd.getClassName(true).equals(theCmd.getFullClassName())) {
                    // Field already managed by this table so maybe we are overriding a superclass
                    JavaTypeMapping fieldMapping = getMappingForMemberName(mmd.getFullFieldName());
                    ColumnMetaData[] colmds = mmd.getColumnMetaData();
                    if (colmds != null && colmds.length > 0) {
                        // Apply this set of ColumnMetaData to the existing mapping
                        int colnum = 0;
                        IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
                        for (int i = 0; i < fieldMapping.getNumberOfDatastoreMappings(); i++) {
                            Column col = fieldMapping.getDatastoreMapping(i).getColumn();
                            col.setIdentifier(idFactory.newColumnIdentifier(colmds[colnum].getName()));
                            col.setColumnMetaData(colmds[colnum]);
                            colnum++;
                            if (colnum == colmds.length) {
                                // Reached end of specified metadata
                                break;
                            }
                        }
                        // TODO Change this to reflect that we have updated the previous mapping
                        logMapping(mmd.getFullFieldName(), fieldMapping);
                    }
                }
            } else {
                // Manage the field if not already managed (may already exist if overriding a superclass field)
                if (mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) {
                    boolean isPrimary = true;
                    if (mmd.getTable() != null && mmd.getJoinMetaData() == null) {
                        // Field has a table specified and is not a 1-N with join table
                        // so is mapped to a secondary table
                        isPrimary = false;
                    }
                    if (isPrimary) {
                        // Add the field to this table
                        JavaTypeMapping fieldMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_FIELD);
                        if (theCmd != cmd && theCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE && fieldMapping.getNumberOfDatastoreMappings() > 0) {
                            // Field is for a subclass and so column(s) has to either allow nulls, or have default
                            int numCols = fieldMapping.getNumberOfDatastoreMappings();
                            for (int colNum = 0; colNum < numCols; colNum++) {
                                Column col = fieldMapping.getDatastoreMapping(colNum).getColumn();
                                if (col.getDefaultValue() == null && !col.isNullable()) {
                                    // Column needs to be nullable
                                    NucleusLogger.DATASTORE_SCHEMA.debug("Member " + mmd.getFullFieldName() + " uses superclass-table yet the field is not marked as nullable " + " nor does it have a default value, so setting the column as nullable");
                                    col.setNullable(true);
                                }
                            }
                        }
                        addMemberMapping(fieldMapping);
                    } else {
                        // Add the field to the appropriate secondary table
                        if (secondaryTables == null) {
                            secondaryTables = new HashMap();
                        }
                        SecondaryTable secTable = secondaryTables.get(mmd.getTable());
                        if (secTable == null) {
                            // Secondary table doesnt exist yet so create it to users specifications.
                            List<JoinMetaData> joinmds = theCmd.getJoinMetaData();
                            JoinMetaData theJoinMD = null;
                            if (joinmds != null) {
                                for (JoinMetaData joinmd : joinmds) {
                                    if (joinmd.getTable().equalsIgnoreCase(mmd.getTable()) && (joinmd.getCatalog() == null || (joinmd.getCatalog() != null && joinmd.getCatalog().equalsIgnoreCase(mmd.getCatalog()))) && (joinmd.getSchema() == null || (joinmd.getSchema() != null && joinmd.getSchema().equalsIgnoreCase(mmd.getSchema())))) {
                                        theJoinMD = joinmd;
                                        break;
                                    }
                                }
                            }
                            DatastoreIdentifier secTableIdentifier = storeMgr.getIdentifierFactory().newTableIdentifier(mmd.getTable());
                            // Use specified catalog, else take catalog of the owning table
                            String catalogName = mmd.getCatalog();
                            if (catalogName == null) {
                                catalogName = getCatalogName();
                            }
                            // Use specified schema, else take schema of the owning table
                            String schemaName = mmd.getSchema();
                            if (schemaName == null) {
                                schemaName = getSchemaName();
                            }
                            secTableIdentifier.setCatalogName(catalogName);
                            secTableIdentifier.setSchemaName(schemaName);
                            secTable = new SecondaryTable(secTableIdentifier, storeMgr, this, theJoinMD, clr);
                            secTable.preInitialize(clr);
                            secTable.initialize(clr);
                            secTable.postInitialize(clr);
                            secondaryTables.put(mmd.getTable(), secTable);
                        }
                        secTable.addMemberMapping(storeMgr.getMappingManager().getMapping(secTable, mmd, clr, FieldRole.ROLE_FIELD));
                    }
                } else if (mmd.getPersistenceModifier() != FieldPersistenceModifier.TRANSACTIONAL) {
                    throw new NucleusException(Localiser.msg("057006", mmd.getName())).setFatal();
                }
                // Calculate if we need a FK adding due to a 1-N (FK) relationship
                boolean needsFKToContainerOwner = false;
                RelationType relationType = mmd.getRelationType(clr);
                if (relationType == RelationType.ONE_TO_MANY_BI) {
                    AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
                    if (mmd.getJoinMetaData() == null && relatedMmds[0].getJoinMetaData() == null) {
                        needsFKToContainerOwner = true;
                    }
                } else if (relationType == RelationType.ONE_TO_MANY_UNI && !mmd.isSingleCollection()) {
                    if (mmd.getJoinMetaData() == null) {
                        needsFKToContainerOwner = true;
                    }
                }
                if (needsFKToContainerOwner) {
                    // 1-N uni/bidirectional using FK, so update the element side with a FK
                    if ((mmd.getCollection() != null && !SCOUtils.collectionHasSerialisedElements(mmd)) || (mmd.getArray() != null && !SCOUtils.arrayIsStoredInSingleColumn(mmd, storeMgr.getMetaDataManager()))) {
                        // 1-N ForeignKey collection/array, so add FK to element table
                        AbstractClassMetaData elementCmd = null;
                        if (mmd.hasCollection()) {
                            // Collection
                            elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
                        } else {
                            // Array
                            elementCmd = storeMgr.getMetaDataManager().getMetaDataForClass(mmd.getType().getComponentType(), clr);
                        }
                        if (elementCmd == null) {
                            String[] implClassNames = storeMgr.getMetaDataManager().getClassesImplementingInterface(mmd.getCollection().getElementType(), clr);
                            if (implClassNames != null && implClassNames.length > 0) {
                                // Collection/array of interface type so apply callback to all implementation types
                                AbstractClassMetaData[] elementCmds = new AbstractClassMetaData[implClassNames.length];
                                for (int i = 0; i < implClassNames.length; i++) {
                                    elementCmds[i] = storeMgr.getMetaDataManager().getMetaDataForClass(implClassNames[i], clr);
                                }
                                // Run callbacks for each of the element classes.
                                for (int i = 0; i < elementCmds.length; i++) {
                                    storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
                                    if (dc == null) {
                                        throw new NucleusException("Unable to add foreign-key to " + elementCmds[i].getFullClassName() + " to " + this + " since element has no table!");
                                    }
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            } else {
                                // Elements that are reference types or non-PC will come through here
                                if (mmd.hasCollection()) {
                                    NucleusLogger.METADATA.warn(Localiser.msg("057016", theCmd.getFullClassName(), mmd.getCollection().getElementType()));
                                } else {
                                    NucleusLogger.METADATA.warn(Localiser.msg("057014", theCmd.getFullClassName(), mmd.getType().getComponentType().getName()));
                                }
                            }
                        } else {
                            AbstractClassMetaData[] elementCmds = null;
                            // TODO : Cater for interface elements, and get the metadata for the implementation classes here
                            if (elementCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
                                // COMPLETE-TABLE inheritance in element, so really need FK in each!
                                Collection<String> elementSubclassNames = storeMgr.getSubClassesForClass(elementCmd.getFullClassName(), true, clr);
                                elementCmds = new ClassMetaData[elementSubclassNames != null ? 1 + elementSubclassNames.size() : 1];
                                int elemNo = 0;
                                elementCmds[elemNo++] = elementCmd;
                                if (elementSubclassNames != null) {
                                    for (String elementSubclassName : elementSubclassNames) {
                                        AbstractClassMetaData elemSubCmd = storeMgr.getMetaDataManager().getMetaDataForClass(elementSubclassName, clr);
                                        elementCmds[elemNo++] = elemSubCmd;
                                    }
                                }
                            } else if (elementCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                elementCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
                            } else {
                                elementCmds = new ClassMetaData[1];
                                elementCmds[0] = elementCmd;
                            }
                            ElementMetaData elemmd = mmd.getElementMetaData();
                            if (elemmd != null && !StringUtils.isWhitespace(elemmd.getTable())) {
                                DatastoreIdentifier requiredTableId = storeMgr.getIdentifierFactory().newTableIdentifier(elemmd.getTable());
                                DatastoreClass requiredTable = storeMgr.getDatastoreClass(requiredTableId);
                                if (requiredTable != null) {
                                    // TODO Respect specification of table in ElementMetaData rather than just defaulting to table of element type
                                    // Note that this will need updates to FKListStore, FKSetStore etc to look for the table
                                    NucleusLogger.GENERAL.warn("Member=" + mmd.getFullFieldName() + " has 1-N FK with required table=" + requiredTable + " : we don't currently support specification of the element table, and always take the default table for the element type");
                                /*for (int i=0;i<elementCmds.length;i++)
                                        {
                                            AbstractClassMetaData theElementCmd = elementCmds[i];
                                            while (theElementCmd != null)
                                            {
                                                if (requiredTable.managesClass(theElementCmd.getFullClassName()))
                                                {
                                                    if (theElementCmd != elementCmds[i])
                                                    {
                                                        elementCmds = new ClassMetaData[1];
                                                        elementCmds[0] = theElementCmd;
                                                        break;
                                                    }
                                                }
                                                theElementCmd = theElementCmd.getSuperAbstractClassMetaData();
                                            }
                                        }*/
                                } else {
                                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " specified element FK in table=" + elemmd.getTable() + " but table not known. Ignoring.");
                                }
                            }
                            // Run callbacks for each of the element classes
                            for (int i = 0; i < elementCmds.length; i++) {
                                storeMgr.addSchemaCallback(elementCmds[i].getFullClassName(), mmd);
                                DatastoreClass dc = storeMgr.getDatastoreClass(elementCmds[i].getFullClassName(), clr);
                                if (// If dc is null then we assume the (possible) element is abstract so no FK needed
                                dc != null) {
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related element uses a VIEW so not remotely adding element FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        }
                    } else if (mmd.getMap() != null && !SCOUtils.mapHasSerialisedKeysAndValues(mmd)) {
                        // 1-N ForeignKey map, so add FK to value table
                        if (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getMappedBy() != null) {
                            // Key is stored in the value table so add the FK to the value table
                            AbstractClassMetaData valueCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getValueType(), clr);
                            if (valueCmd == null) {
                                // Interface elements will come through here and java.lang.String and others as well
                                NucleusLogger.METADATA.warn(Localiser.msg("057018", theCmd.getFullClassName(), mmd.getMap().getValueType()));
                            } else {
                                AbstractClassMetaData[] valueCmds = null;
                                // TODO : Cater for interface values, and get the metadata for the implementation classes here
                                if (valueCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                    valueCmds = storeMgr.getClassesManagingTableForClass(valueCmd, clr);
                                } else {
                                    valueCmds = new ClassMetaData[1];
                                    valueCmds[0] = valueCmd;
                                }
                                // Run callbacks for each of the value classes.
                                for (int i = 0; i < valueCmds.length; i++) {
                                    storeMgr.addSchemaCallback(valueCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(valueCmds[i].getFullClassName(), clr);
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related value uses a VIEW so not remotely adding value owner FK column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        } else if (mmd.getValueMetaData() != null && mmd.getValueMetaData().getMappedBy() != null) {
                            // Value is stored in the key table so add the FK to the key table
                            AbstractClassMetaData keyCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getMap().getKeyType(), clr);
                            if (keyCmd == null) {
                                // Interface elements will come through here and java.lang.String and others as well
                                NucleusLogger.METADATA.warn(Localiser.msg("057019", theCmd.getFullClassName(), mmd.getMap().getKeyType()));
                            } else {
                                AbstractClassMetaData[] keyCmds = null;
                                // TODO : Cater for interface keys, and get the metadata for the implementation classes here
                                if (keyCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
                                    keyCmds = storeMgr.getClassesManagingTableForClass(keyCmd, clr);
                                } else {
                                    keyCmds = new ClassMetaData[1];
                                    keyCmds[0] = keyCmd;
                                }
                                // Run callbacks for each of the key classes.
                                for (int i = 0; i < keyCmds.length; i++) {
                                    storeMgr.addSchemaCallback(keyCmds[i].getFullClassName(), mmd);
                                    DatastoreClass dc = storeMgr.getDatastoreClass(keyCmds[i].getFullClassName(), clr);
                                    if (dc instanceof ClassTable) {
                                        ClassTable ct = (ClassTable) dc;
                                        if (ct.isInitialized()) {
                                            // if the target table is already initialized, run the callbacks
                                            ct.runCallBacks(clr);
                                        }
                                    } else {
                                        NucleusLogger.DATASTORE_SCHEMA.info("Table " + toString() + " has to manage member " + mmd.getFullFieldName() + " yet the related key uses a VIEW so not remotely adding key FK owner column; assumed to be part of the VIEW definition");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) HashMap(java.util.HashMap) ElementMetaData(org.datanucleus.metadata.ElementMetaData) MacroString(org.datanucleus.util.MacroString) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RelationType(org.datanucleus.metadata.RelationType) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) JoinMetaData(org.datanucleus.metadata.JoinMetaData) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 23 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class SequenceGenerator method repositoryExists.

/**
 * Method to return if the repository already exists.
 * @return Whether the repository exists
 */
protected boolean repositoryExists() {
    String sequenceCatalogName = properties.getProperty(ValueGenerator.PROPERTY_SEQUENCETABLE_CATALOG);
    if (sequenceCatalogName == null) {
        sequenceCatalogName = properties.getProperty(ValueGenerator.PROPERTY_CATALOG_NAME);
    }
    if (!StringUtils.isWhitespace(sequenceCatalogName)) {
        IdentifierFactory idFactory = ((RDBMSStoreManager) storeMgr).getIdentifierFactory();
        sequenceCatalogName = idFactory.getIdentifierInAdapterCase(sequenceCatalogName);
    }
    String sequenceSchemaName = properties.getProperty(ValueGenerator.PROPERTY_SEQUENCETABLE_SCHEMA);
    if (sequenceSchemaName == null) {
        sequenceSchemaName = properties.getProperty(ValueGenerator.PROPERTY_SCHEMA_NAME);
    }
    if (!StringUtils.isWhitespace(sequenceSchemaName)) {
        IdentifierFactory idFactory = ((RDBMSStoreManager) storeMgr).getIdentifierFactory();
        sequenceSchemaName = idFactory.getIdentifierInAdapterCase(sequenceSchemaName);
    }
    String seqName = properties.getProperty(ValueGenerator.PROPERTY_SEQUENCE_NAME);
    IdentifierFactory idFactory = ((RDBMSStoreManager) storeMgr).getIdentifierFactory();
    seqName = idFactory.getIdentifierInAdapterCase(seqName);
    return ((RDBMSStoreManager) storeMgr).getDatastoreAdapter().sequenceExists((Connection) connection.getConnection(), sequenceCatalogName, sequenceSchemaName, seqName);
}
Also used : IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 24 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createColumn.

/**
 * Method to create a datastore field for a Java type mapping.
 * This is used for serialised PC elements/keys/values in a join table.
 * TODO Merge this with the method above.
 * @param mapping Java type mapping for the field
 * @param javaType The type of field being stored in this column
 * @param colmd MetaData for the column
 * @return The column
 */
public Column createColumn(JavaTypeMapping mapping, String javaType, ColumnMetaData colmd) {
    AbstractMemberMetaData mmd = mapping.getMemberMetaData();
    Table tbl = mapping.getTable();
    if (colmd == null) {
        // If column specified add one (use any column name specified on field element)
        colmd = new ColumnMetaData();
        if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length == 1) {
            colmd.setName(mmd.getColumnMetaData()[0].getName());
        }
        mmd.addColumn(colmd);
    }
    Column col;
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        DatastoreIdentifier identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
        int i = 0;
        while (tbl.hasColumn(identifier)) {
            identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
            i++;
        }
        colmd.setName(identifier.getName());
        col = tbl.addColumn(javaType, identifier, mapping, colmd);
    } else {
        // User has specified a name, so try to keep this unmodified
        col = tbl.addColumn(javaType, idFactory.newColumnIdentifier(colmd.getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true), mapping, colmd);
    }
    setColumnNullability(mmd, colmd, col);
    if (mmd.getNullValue() == NullValue.DEFAULT) {
        // Users default should be applied if a null is to be inserted
        col.setDefaultable(colmd.getDefaultValue());
    }
    return col;
}
Also used : Table(org.datanucleus.store.rdbms.table.Table) Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 25 with IdentifierFactory

use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createColumn.

/**
 * Method to create a datastore field for a persistable mapping.
 * @param mmd MetaData for the field whose mapping it is
 * @param table Datastore class where we create the datastore field
 * @param mapping The Java type for this field
 * @param colmd The columnMetaData for this datastore field
 * @param reference The datastore field we are referencing
 * @param clr ClassLoader resolver
 * @return The column
 */
public Column createColumn(AbstractMemberMetaData mmd, Table table, JavaTypeMapping mapping, ColumnMetaData colmd, Column reference, ClassLoaderResolver clr) {
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier identifier = null;
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
        identifier = idFactory.newForeignKeyFieldIdentifier(relatedMmds != null ? relatedMmds[0] : null, mmd, reference.getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), FieldRole.ROLE_OWNER);
        colmd.setName(identifier.getName());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmd.getName(), false, null, true);
    }
    Column col = table.addColumn(mmd.getType().getName(), identifier, mapping, colmd);
    // Copy the characteristics of the reference column to this one
    reference.copyConfigurationTo(col);
    if (mmd.isPrimaryKey()) {
        col.setPrimaryKey();
    }
    if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
    // Embedded so can't be datastore-attributed
    } else {
        if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber())) {
            if ((mmd.isPrimaryKey() && ((DatastoreClass) table).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
                // Increment any PK field if we are in base class, and increment any other field
                col.setIdentity(true);
            }
        }
    }
    if (mmd.getValueForExtension("select-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension("insert-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension("update-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
    }
    setColumnNullability(mmd, colmd, col);
    if (mmd.getNullValue() == NullValue.DEFAULT) {
        // Users default should be applied if a null is to be inserted
        col.setDefaultable(colmd.getDefaultValue());
    }
    return col;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Aggregations

IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)28 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)19 HashMap (java.util.HashMap)11 Iterator (java.util.Iterator)8 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)7 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)5 Column (org.datanucleus.store.rdbms.table.Column)5 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)5 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 JPOXIdentifierFactory (org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory)4 CandidateKey (org.datanucleus.store.rdbms.key.CandidateKey)4 Index (org.datanucleus.store.rdbms.key.Index)4 MacroString (org.datanucleus.util.MacroString)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3