Search in sources :

Example 6 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class SingleFieldMultiMapping method addColumns.

/**
 * Convenience method to add a datastore field for this mapping.
 * If this mapping is a "full" mapping (for a field in a table) then a Column will be added,
 * otherwise (mapping representing a parameter in a query) will just add a datastore mapping.
 * The datastore mapping is added to the end of the datastoreMappings.
 * @param typeName Java type of the field to add the column for.
 */
protected void addColumns(String typeName) {
    MappingManager mgr = storeMgr.getMappingManager();
    Column column = null;
    if (table != null) {
        // Full mapping, so add column to back the datastore mapping
        column = mgr.createColumn(this, typeName, getNumberOfDatastoreMappings());
    }
    mgr.createDatastoreMapping(this, column, typeName);
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager)

Example 7 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class ClassTable method initialize.

/**
 * Method to initialise the table.
 * This adds the columns based on the MetaData representation for the class being represented by this table.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    // if already initialized, we have nothing further to do here
    if (isInitialized()) {
        return;
    }
    // we may inherit from that table are initialized at the point at which we may need them
    if (supertable != null) {
        supertable.initialize(clr);
    }
    // Add the fields for this class (and any other superclasses that we need to manage the
    // fields for (inheritance-strategy="subclass-table" in the superclass)
    initializeForClass(cmd, clr);
    MappingManager mapMgr = storeMgr.getMappingManager();
    // Add Version where specified in MetaData
    // TODO If there is a superclass table that has a version we should omit from here even if in MetaData
    // See "getTableWithDiscriminator()" for the logic
    versionMetaData = cmd.getVersionMetaDataForTable();
    if (versionMetaData != null && versionMetaData.getFieldName() == null) {
        if (versionMetaData.getVersionStrategy() == VersionStrategy.NONE || versionMetaData.getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
            // No optimistic locking but the idiot wants a column for that :-)
            versionMapping = new VersionMapping.VersionLongMapping(this, mapMgr.getMapping(Long.class));
        } else if (versionMetaData.getVersionStrategy() == VersionStrategy.DATE_TIME) {
            if (!dba.supportsOption(DatastoreAdapter.DATETIME_STORES_MILLISECS)) {
                // TODO Localise this
                throw new NucleusException("Class " + cmd.getFullClassName() + " is defined " + "to use date-time versioning, yet this datastore doesnt support storing " + "milliseconds in DATETIME/TIMESTAMP columns. Use version-number");
            }
            versionMapping = new VersionMapping.VersionTimestampMapping(this, mapMgr.getMapping(Timestamp.class));
        }
        if (versionMapping != null) {
            logMapping("VERSION", versionMapping);
        }
    }
    // Add Discriminator where specified in MetaData
    DiscriminatorMetaData dismd = cmd.getDiscriminatorMetaDataForTable();
    if (dismd != null) {
        discriminatorMetaData = dismd;
        if (storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_DISCRIM_PER_SUBCLASS_TABLE)) {
            // Backwards compatibility only. Creates discriminator in all subclass tables even though not needed
            // TODO Remove this in the future
            discriminatorMapping = DiscriminatorMapping.createDiscriminatorMapping(this, dismd);
        } else {
            // Create discriminator column only in top most table that needs it
            ClassTable tableWithDiscrim = getTableWithDiscriminator();
            if (tableWithDiscrim == this) {
                // No superclass with a discriminator so add it in this table
                discriminatorMapping = DiscriminatorMapping.createDiscriminatorMapping(this, dismd);
            }
        }
        if (discriminatorMapping != null) {
            logMapping("DISCRIMINATOR", discriminatorMapping);
        }
    }
    // TODO Only put on root table (i.e "if (supertable != null)" then omit)
    if (storeMgr.getNucleusContext().isClassMultiTenant(cmd)) {
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_NAME));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_JDBC_TYPE)) {
            colmd.setJdbcType(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_JDBC_TYPE));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_LENGTH)) {
            colmd.setLength(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_LENGTH));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "TENANT_ID";
        String typeName = (colmd.getJdbcType() == JdbcType.INTEGER) ? Integer.class.getName() : String.class.getName();
        multitenancyMapping = (typeName.equals(Integer.class.getName())) ? new IntegerMapping() : new StringMapping();
        multitenancyMapping.setTable(this);
        multitenancyMapping.initialize(storeMgr, typeName);
        Column tenantColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), multitenancyMapping, colmd);
        storeMgr.getMappingManager().createDatastoreMapping(multitenancyMapping, tenantColumn, typeName);
        logMapping("MULTITENANCY", multitenancyMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE)) {
        // SoftDelete flag column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_SOFTDELETE_COLUMN_NAME));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "DELETED";
        // TODO Allow integer?
        String typeName = Boolean.class.getName();
        softDeleteMapping = new BooleanMapping();
        softDeleteMapping.setTable(this);
        softDeleteMapping.initialize(storeMgr, typeName);
        Column tenantColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), softDeleteMapping, colmd);
        storeMgr.getMappingManager().createDatastoreMapping(softDeleteMapping, tenantColumn, typeName);
        logMapping("SOFTDELETE", softDeleteMapping);
    }
    // Initialise any SecondaryTables
    if (secondaryTables != null) {
        Iterator<Map.Entry<String, SecondaryTable>> secondaryTableEntryIter = secondaryTables.entrySet().iterator();
        while (secondaryTableEntryIter.hasNext()) {
            Map.Entry<String, SecondaryTable> secondaryTableEntry = secondaryTableEntryIter.next();
            SecondaryTable second = secondaryTableEntry.getValue();
            if (!second.isInitialized()) {
                second.initialize(clr);
            }
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
    }
    storeMgr.registerTableInitialized(this);
    state = TABLE_STATE_INITIALIZED;
}
Also used : VersionMapping(org.datanucleus.store.rdbms.mapping.java.VersionMapping) MacroString(org.datanucleus.util.MacroString) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) BooleanMapping(org.datanucleus.store.rdbms.mapping.java.BooleanMapping) Timestamp(java.sql.Timestamp) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) NucleusException(org.datanucleus.exceptions.NucleusException) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class SchemaTable method initialize.

/**
 * Method to initialise the table.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    assertIsUninitialized();
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    MappingManager mapMgr = getStoreManager().getMappingManager();
    classMapping = mapMgr.getMapping(String.class);
    Column class_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("CLASS_NAME"), classMapping, null);
    mapMgr.createDatastoreMapping(classMapping, class_column, String.class.getName());
    class_column.getColumnMetaData().setLength(128);
    class_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    class_column.setPrimaryKey();
    tableMapping = mapMgr.getMapping(String.class);
    Column table_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TABLE_NAME"), tableMapping, null);
    mapMgr.createDatastoreMapping(tableMapping, table_column, String.class.getName());
    table_column.getColumnMetaData().setLength(128);
    table_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    typeMapping = mapMgr.getMapping(String.class);
    Column type_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TYPE"), typeMapping, null);
    mapMgr.createDatastoreMapping(typeMapping, type_column, String.class.getName());
    type_column.getColumnMetaData().setLength(4);
    type_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    // TODO Change type to SMALLINT/BIT
    ownerMapping = mapMgr.getMapping(String.class);
    Column owner_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("OWNER"), ownerMapping, null);
    mapMgr.createDatastoreMapping(ownerMapping, owner_column, String.class.getName());
    owner_column.getColumnMetaData().setLength(2);
    owner_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    versionMapping = mapMgr.getMapping(String.class);
    Column version_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("VERSION"), versionMapping, null);
    mapMgr.createDatastoreMapping(versionMapping, version_column, String.class.getName());
    version_column.getColumnMetaData().setLength(20);
    version_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    interfaceNameMapping = mapMgr.getMapping(String.class);
    Column interfaceName_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("INTERFACE_NAME"), interfaceNameMapping, null);
    mapMgr.createDatastoreMapping(interfaceNameMapping, interfaceName_column, String.class.getName());
    interfaceName_column.getColumnMetaData().setLength(255);
    interfaceName_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
    interfaceName_column.setNullable(true);
    // Set up JDBC statements for supported operations
    insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + ") VALUES (?,?,?,?,?,?)";
    deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + "=?";
    deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
    fetchAllStmt = "SELECT " + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + table_column.getIdentifier();
    fetchStmt = "SELECT 1 FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + " = ? ";
    state = TABLE_STATE_INITIALIZED;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory)

Example 9 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class PersistableMapping method prepareDatastoreMapping.

/**
 * Method to prepare the PC mapping and add its associated datastore mappings.
 * @param clr The ClassLoaderResolver
 */
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
    if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
    // TODO Handle creation of columns in join table for collection of PCs
    } else if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
    // TODO Handle creation of columns in join table for array of PCs
    } else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
    // TODO Handle creation of columns in join table for map of PCs as keys
    } else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
    // TODO Handle creation of columns in join table for map of PCs as values
    } else {
        // Either one end of a 1-1 relation, or the N end of a N-1
        AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
        JavaTypeMapping referenceMapping = null;
        if (refCmd == null) {
            // User stupidity
            throw new NucleusUserException("You have a field " + mmd.getFullFieldName() + " that has type " + mmd.getTypeName() + " but this type has no known metadata. Your mapping is incorrect");
        }
        if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
            // Find the actual tables storing the other end (can be multiple subclasses)
            AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
            if (cmds != null && cmds.length > 0) {
                if (cmds.length > 1) {
                    // TODO Only log this when it is really necessary. In some situations it is fine
                    NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"subclass-table\" inheritance strategy and more " + "than 1 subclasses with a table. This is not fully supported");
                }
            } else {
                // TODO Throw an exception ?
                return;
            }
            // TODO We need a mapping for each of the possible subclass tables
            referenceMapping = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
        } else if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
            // Find the other side of the relation
            DatastoreClass refTable = null;
            if (refCmd instanceof ClassMetaData && !((ClassMetaData) refCmd).isAbstract()) {
                refTable = storeMgr.getDatastoreClass(refCmd.getFullClassName(), clr);
            } else {
                Collection<String> refSubclasses = storeMgr.getSubClassesForClass(refCmd.getFullClassName(), true, clr);
                if (refSubclasses != null && !refSubclasses.isEmpty()) {
                    // if only 1 subclass then use that
                    String refSubclassName = refSubclasses.iterator().next();
                    refTable = storeMgr.getDatastoreClass(refSubclassName, clr);
                    if (refSubclasses.size() > 1) {
                        NucleusLogger.DATASTORE_SCHEMA.info("Field " + mmd.getFullFieldName() + " is a 1-1/N-1 relation and the other side had multiple possible classes " + "to which to create a foreign-key. Using first possible (" + refSubclassName + ")");
                    }
                }
            }
            if (refTable != null) {
                referenceMapping = refTable.getIdMapping();
            } else {
                throw new NucleusUserException("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"complete-table\" inheritance strategy and either no table was found, or multiple possible tables!");
            }
        } else {
            // Default is to use the ID of the related object
            // TODO Add option to use a natural-id in the other class. Find the mapping using the targetColumnName
            referenceMapping = storeMgr.getDatastoreClass(mmd.getType().getName(), clr).getIdMapping();
        }
        // Generate a mapping from the columns of the referenced object to this mapping's ColumnMetaData
        CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(mmd, table, referenceMapping, true);
        // Find any related field where this is part of a bidirectional relation
        RelationType relationType = mmd.getRelationType(clr);
        boolean createDatastoreMappings = true;
        if (relationType == RelationType.MANY_TO_ONE_BI) {
            AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
            // TODO Cater for more than 1 related field
            createDatastoreMappings = (relatedMmds[0].getJoinMetaData() == null);
        } else if (// TODO If join table then don't need this
        relationType == RelationType.ONE_TO_ONE_BI) {
            // Put the FK at the end without "mapped-by"
            createDatastoreMappings = (mmd.getMappedBy() == null);
        }
        if (mmd.getJoinMetaData() != null && (relationType == RelationType.MANY_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI)) {
            if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI) {
                throw new NucleusUserException("We do not currently support 1-1 relations via join table : " + mmd.getFullFieldName());
            }
            // create join table
            storeMgr.newJoinTable(table, mmd, clr);
        } else {
            // Loop through the datastore fields in the referenced class and create a datastore field for each
            for (int i = 0; i < referenceMapping.getNumberOfDatastoreMappings(); i++) {
                DatastoreMapping refDatastoreMapping = referenceMapping.getDatastoreMapping(i);
                JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType());
                this.addJavaTypeMapping(mapping);
                // Create physical datastore columns where we require a FK link to the related table.
                if (createDatastoreMappings) {
                    // Find the Column MetaData that maps to the referenced datastore field
                    ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refDatastoreMapping.getColumn().getIdentifier());
                    if (colmd == null) {
                        throw new NucleusUserException(Localiser.msg("041038", refDatastoreMapping.getColumn().getIdentifier(), toString())).setFatal();
                    }
                    // Create a Datastore field to equate to the referenced classes datastore field
                    MappingManager mmgr = storeMgr.getMappingManager();
                    Column col = mmgr.createColumn(mmd, table, mapping, colmd, refDatastoreMapping.getColumn(), clr);
                    // Add its datastore mapping
                    DatastoreMapping datastoreMapping = mmgr.createDatastoreMapping(mapping, col, refDatastoreMapping.getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
                    this.addDatastoreMapping(datastoreMapping);
                } else {
                    mapping.setReferenceMapping(referenceMapping);
                }
            }
        }
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) Column(org.datanucleus.store.rdbms.table.Column) RelationType(org.datanucleus.metadata.RelationType) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) CorrespondentColumnsMapper(org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper)

Example 10 with MappingManager

use of org.datanucleus.store.rdbms.mapping.MappingManager in project datanucleus-rdbms by datanucleus.

the class SerialisedElementPCMapping method prepareDatastoreMapping.

/**
 * Method to prepare a field mapping for use in the datastore.
 * This creates the column in the table.
 */
protected void prepareDatastoreMapping() {
    MappingManager mmgr = storeMgr.getMappingManager();
    ColumnMetaData colmd = null;
    if (mmd.getElementMetaData() != null && mmd.getElementMetaData().getColumnMetaData() != null && mmd.getElementMetaData().getColumnMetaData().length > 0) {
        colmd = mmd.getElementMetaData().getColumnMetaData()[0];
    }
    Column col = mmgr.createColumn(this, getType(), colmd);
    mmgr.createDatastoreMapping(this, mmd, 0, col);
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Aggregations

MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)12 Column (org.datanucleus.store.rdbms.table.Column)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 DatastoreMapping (org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping)2 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)1 ElementMetaData (org.datanucleus.metadata.ElementMetaData)1 KeyMetaData (org.datanucleus.metadata.KeyMetaData)1 RelationType (org.datanucleus.metadata.RelationType)1 ValueMetaData (org.datanucleus.metadata.ValueMetaData)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 CorrespondentColumnsMapper (org.datanucleus.store.rdbms.mapping.CorrespondentColumnsMapper)1