Search in sources :

Example 1 with SqlTimestampMapping

use of org.datanucleus.store.rdbms.mapping.java.SqlTimestampMapping 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
 */
@Override
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);
    // 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.getMemberName() == null) {
        if (versionMetaData.getStrategy() == VersionStrategy.NONE) {
            // Equate NONE to an integral column (TODO Allow for using timestamp when not checking the version?)
            // See also LockManagerImpl
            versionMapping = new VersionMapping.VersionLongMapping(this, storeMgr.getMappingManager().getMapping(Long.class));
        } else if (versionMetaData.getStrategy() == VersionStrategy.VERSION_NUMBER) {
            versionMapping = new VersionMapping.VersionLongMapping(this, storeMgr.getMappingManager().getMapping(Long.class));
        } else if (versionMetaData.getStrategy() == 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, storeMgr.getMappingManager().getMapping(Timestamp.class));
        } else if (versionMetaData.getStrategy() == VersionStrategy.STATE_IMAGE) {
            // TODO Support state-image strategy
            throw new NucleusUserException("DataNucleus doesnt currently support version strategy \"state-image\"");
        }
        if (versionMapping != null) {
            logMapping("VERSION", versionMapping);
        }
    }
    DiscriminatorMetaData dismd = cmd.getDiscriminatorMetaDataForTable();
    if (dismd != null) {
        // Surrogate discriminator
        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)
    MultitenancyMetaData mtmd = cmd.getMultitenancyMetaData();
    if (mtmd != null) {
        // Surrogate multi-tenancy discriminator
        ColumnMetaData colmd = mtmd.getColumnMetaData();
        if (colmd == null) {
            colmd = new ColumnMetaData();
            if (mtmd.getColumnName() != null) {
                colmd.setName(mtmd.getColumnName());
            }
        }
        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().createColumnMapping(multitenancyMapping, tenantColumn, typeName);
        logMapping("MULTITENANCY", multitenancyMapping);
    }
    SoftDeleteMetaData sdmd = cmd.getSoftDeleteMetaData();
    if (sdmd != null) {
        // Surrogate "SoftDelete" flag column
        ColumnMetaData colmd = sdmd.getColumnMetaData();
        if (colmd == null) {
            colmd = new ColumnMetaData();
            if (sdmd.getColumnName() != null) {
                colmd.setName(sdmd.getColumnName());
            }
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "DELETED";
        // TODO Allow integer using JDBC type
        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().createColumnMapping(softDeleteMapping, tenantColumn, typeName);
        logMapping("SOFTDELETE", softDeleteMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_CREATEUSER)) {
        // Surrogate "create user" column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_CREATEUSER_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_CREATEUSER_COLUMN_NAME));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_CREATEUSER_COLUMN_LENGTH)) {
            colmd.setLength(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_CREATEUSER_COLUMN_LENGTH));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "CREATE_USER";
        String typeName = String.class.getName();
        createUserMapping = new StringMapping();
        createUserMapping.setTable(this);
        createUserMapping.initialize(storeMgr, typeName);
        Column auditColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), createUserMapping, colmd);
        storeMgr.getMappingManager().createColumnMapping(createUserMapping, auditColumn, typeName);
        logMapping("CREATEUSER", createUserMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_CREATETIMESTAMP)) {
        // Surrogate "create timestamp" column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_CREATETIMESTAMP_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_CREATETIMESTAMP_COLUMN_NAME));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "CREATE_TIMESTAMP";
        String typeName = Timestamp.class.getName();
        createTimestampMapping = new SqlTimestampMapping();
        createTimestampMapping.setTable(this);
        createTimestampMapping.initialize(storeMgr, typeName);
        Column auditColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), createTimestampMapping, colmd);
        storeMgr.getMappingManager().createColumnMapping(createTimestampMapping, auditColumn, typeName);
        logMapping("CREATETIMESTAMP", createTimestampMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_UPDATEUSER)) {
        // Surrogate "update user" column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_UPDATEUSER_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_UPDATEUSER_COLUMN_NAME));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_UPDATEUSER_COLUMN_LENGTH)) {
            colmd.setLength(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_UPDATEUSER_COLUMN_LENGTH));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "UPDATE_USER";
        String typeName = String.class.getName();
        updateUserMapping = new StringMapping();
        updateUserMapping.setTable(this);
        updateUserMapping.initialize(storeMgr, typeName);
        Column auditColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), updateUserMapping, colmd);
        storeMgr.getMappingManager().createColumnMapping(updateUserMapping, auditColumn, typeName);
        logMapping("UPDATEUSER", updateUserMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_UPDATETIMESTAMP)) {
        // Surrogate "update timestamp" column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_UPDATETIMESTAMP_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_UPDATETIMESTAMP_COLUMN_NAME));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "UPDATE_TIMESTAMP";
        String typeName = Timestamp.class.getName();
        updateTimestampMapping = new SqlTimestampMapping();
        updateTimestampMapping.setTable(this);
        updateTimestampMapping.initialize(storeMgr, typeName);
        Column auditColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), updateTimestampMapping, colmd);
        storeMgr.getMappingManager().createColumnMapping(updateTimestampMapping, auditColumn, typeName);
        logMapping("UPDATETIMESTAMP", updateTimestampMapping);
    }
    // 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 : SoftDeleteMetaData(org.datanucleus.metadata.SoftDeleteMetaData) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) 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) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) SqlTimestampMapping(org.datanucleus.store.rdbms.mapping.java.SqlTimestampMapping) NucleusException(org.datanucleus.exceptions.NucleusException) MultitenancyMetaData(org.datanucleus.metadata.MultitenancyMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)1 MultitenancyMetaData (org.datanucleus.metadata.MultitenancyMetaData)1 SoftDeleteMetaData (org.datanucleus.metadata.SoftDeleteMetaData)1 BooleanMapping (org.datanucleus.store.rdbms.mapping.java.BooleanMapping)1 IntegerMapping (org.datanucleus.store.rdbms.mapping.java.IntegerMapping)1 SqlTimestampMapping (org.datanucleus.store.rdbms.mapping.java.SqlTimestampMapping)1 StringMapping (org.datanucleus.store.rdbms.mapping.java.StringMapping)1 VersionMapping (org.datanucleus.store.rdbms.mapping.java.VersionMapping)1 MacroString (org.datanucleus.util.MacroString)1