Search in sources :

Example 31 with DatastoreIdentifier

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

the class MappingManagerImpl method createColumn.

/**
 * Method to create a column for a persistable mapping.
 * @param mmd MetaData for the field whose mapping it is
 * @param table Datastore class where we create the column
 * @param mapping The Java type for this field
 * @param colmd The columnMetaData for this column
 * @param reference The column 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(METADATA_EXTENSION_SELECT_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_SELECT_FUNCTION), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension(METADATA_EXTENSION_INSERT_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_INSERT_FUNCTION), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension(METADATA_EXTENSION_UPDATE_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_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)

Example 32 with DatastoreIdentifier

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

the class MappingManagerImpl method createColumn.

/**
 * Method to create a column for a Java type mapping.
 * This is NOT used for persistable mappings - see method below.
 * @param mapping Java type mapping for the field
 * @param javaType The type of field being stored in this column
 * @param columnIndex Index of the column to use
 * @return The column
 */
@Override
public Column createColumn(JavaTypeMapping mapping, String javaType, int columnIndex) {
    AbstractMemberMetaData mmd = mapping.getMemberMetaData();
    FieldRole roleForField = mapping.getRoleForMember();
    // Take the column MetaData from the component that this mappings role relates to
    ColumnMetaData colmd = null;
    ColumnMetaDataContainer columnContainer = mmd;
    if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT || roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
        columnContainer = mmd.getElementMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
        columnContainer = mmd.getKeyMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
        columnContainer = mmd.getValueMetaData();
    }
    Column col;
    if (columnContainer != null && columnContainer.getColumnMetaData().length > columnIndex) {
        colmd = columnContainer.getColumnMetaData()[columnIndex];
    } else {
        // This column index is not present, so add one to represent it
        colmd = new ColumnMetaData();
        if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > columnIndex) {
            colmd.setName(mmd.getColumnMetaData()[columnIndex].getName());
        }
        if (columnContainer != null) {
            columnContainer.addColumn(colmd);
        }
    }
    // Generate the column identifier
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier identifier = null;
    Table tbl = mapping.getTable();
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT) {
            // Join table collection element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
            // Join table array element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
            // Join table map key
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_KEY);
        } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
            // Join table map value
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
        } else {
            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());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmd.getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
    }
    // Create the column
    col = tbl.addColumn(javaType, identifier, mapping, colmd);
    if (mmd.isPrimaryKey()) {
        col.setPrimaryKey();
    }
    if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
    // Embedded so can't be datastore-attributed
    } else {
        /*if (!mmd.getClassName(true).equals(mmd.getAbstractClassMetaData().getFullClassName()))
            {
                if (storeMgr.isStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass)
                {
                    if ((mmd.isPrimaryKey() && ((DatastoreClass)tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey())
                    {
                        NucleusLogger.GENERAL.info(">> Column addition " + mmd.getFullFieldName() + " IGNORING use of IDENTITY since override of base metadata! See RDBMSMappingManager");
                    }
                }
                // Overriding member, so ignore TODO This can be incorrect in many cases
            }
            else
            {*/
        if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass) {
            if ((mmd.isPrimaryKey() && ((DatastoreClass) tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
                // Increment any PK field if we are in base class, and increment any other field
                col.setIdentity(true);
            }
        }
    /*}*/
    }
    if (mmd.getValueForExtension(METADATA_EXTENSION_SELECT_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_SELECT_FUNCTION), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension(METADATA_EXTENSION_INSERT_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_INSERT_FUNCTION), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension(METADATA_EXTENSION_UPDATE_FUNCTION) != null) {
        col.setWrapperFunction(mmd.getValueForExtension(METADATA_EXTENSION_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 : ColumnMetaDataContainer(org.datanucleus.metadata.ColumnMetaDataContainer) Table(org.datanucleus.store.rdbms.table.Table) Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) FieldRole(org.datanucleus.metadata.FieldRole) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 33 with DatastoreIdentifier

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

the class MappingManagerImpl method createColumn.

/**
 * Method to create a column 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
 */
@Override
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();
    DatastoreIdentifier identifier = null;
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        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());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmd.getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
    }
    // Create the column
    col = tbl.addColumn(javaType, identifier, 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 34 with DatastoreIdentifier

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

the class SelectStatement method select.

/**
 * Add a select clause for the specified field (via its mapping).
 * If an alias is supplied and there are more than 1 column for this mapping then they will have
 * names like "{alias}_n" where n is the column number (starting at 0).
 * @param table The SQLTable to select from (null implies the primary table)
 * @param mapping The mapping for the field
 * @param alias optional alias
 * @param applyToUnions Whether to apply to unions
 * @return The column index(es) in the statement for the specified field (1 is first).
 */
public int[] select(SQLTable table, JavaTypeMapping mapping, String alias, boolean applyToUnions) {
    if (mapping == null) {
        throw new NucleusException("Mapping to select is null");
    } else if (table == null) {
        // Default to the primary table if not specified
        table = primaryTable;
    }
    if (mapping.getTable() != table.getTable()) {
        throw new NucleusException("Table being selected from (\"" + table.getTable() + "\") is inconsistent with the column selected (\"" + mapping.getTable() + "\")");
    }
    invalidateStatement();
    ColumnMapping[] mappings = mapping.getColumnMappings();
    int[] selected = new int[mappings.length];
    for (int i = 0; i < selected.length; i++) {
        DatastoreIdentifier colAlias = null;
        if (alias != null) {
            String name = (selected.length > 1) ? (alias + "_" + i) : alias;
            colAlias = rdbmsMgr.getIdentifierFactory().newColumnIdentifier(name);
        }
        SQLColumn col = new SQLColumn(table, mappings[i].getColumn(), colAlias);
        selected[i] = selectItem(new SQLText(col.getColumnSelectString()), alias != null ? colAlias.toString() : null, true);
    }
    if (applyToUnions && unions != null && allowUnions) {
        // Apply the select to all unions
        Iterator<SelectStatement> unionIter = unions.iterator();
        while (unionIter.hasNext()) {
            SelectStatement stmt = unionIter.next();
            stmt.select(table, mapping, alias);
        }
    }
    return selected;
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) NucleusException(org.datanucleus.exceptions.NucleusException) ColumnMapping(org.datanucleus.store.rdbms.mapping.column.ColumnMapping)

Example 35 with DatastoreIdentifier

use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier 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.getNumberOfColumnMappings(); i++) {
                            Column col = fieldMapping.getColumnMapping(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.getNumberOfColumnMappings() > 0) {
                            // Field is for a subclass and so column(s) has to either allow nulls, or have default
                            int numCols = fieldMapping.getNumberOfColumnMappings();
                            for (int colNum = 0; colNum < numCols; colNum++) {
                                Column col = fieldMapping.getColumnMapping(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;
                                    }
                                }
                            }
                            // Create identifier - use specified catalog, else take catalog of the owning table
                            DatastoreIdentifier secTableIdentifier = storeMgr.getIdentifierFactory().newTableIdentifier(mmd.getTable());
                            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)

Aggregations

DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)45 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)19 HashMap (java.util.HashMap)18 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)12 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)11 Iterator (java.util.Iterator)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)10 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)8 MacroString (org.datanucleus.util.MacroString)6 ArrayList (java.util.ArrayList)5 NucleusException (org.datanucleus.exceptions.NucleusException)5 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 Index (org.datanucleus.store.rdbms.key.Index)4 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)4 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)4 SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3