use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class SerialisedKeyPCMapping 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.getKeyMetaData() != null && mmd.getKeyMetaData().getColumnMetaData() != null && mmd.getKeyMetaData().getColumnMetaData().length > 0) {
colmd = mmd.getKeyMetaData().getColumnMetaData()[0];
}
Column col = mmgr.createColumn(this, getType(), colmd);
mmgr.createDatastoreMapping(this, mmd, 0, col);
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class SerialisedValuePCMapping 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.getValueMetaData() != null && mmd.getValueMetaData().getColumnMetaData() != null && mmd.getValueMetaData().getColumnMetaData().length > 0) {
colmd = mmd.getValueMetaData().getColumnMetaData()[0];
}
Column col = mmgr.createColumn(this, getType(), colmd);
mmgr.createDatastoreMapping(this, mmd, 0, col);
}
use of org.datanucleus.metadata.ColumnMetaData 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;
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class ElementContainerTable method initialize.
/**
* Method to initialise the table definition. Adds the owner mapping.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr) {
assertIsUninitialized();
// Add owner mapping
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
ColumnMetaData[] columnMetaData = null;
if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
// When specified at this side they use the <join> tag
columnMetaData = mmd.getJoinMetaData().getColumnMetaData();
} else if (relatedMmds != null && relatedMmds[0].getElementMetaData() != null && relatedMmds[0].getElementMetaData().getColumnMetaData() != null && relatedMmds[0].getElementMetaData().getColumnMetaData().length > 0) {
// Column mappings defined at other side (M-N)
// When specified at other side they use the <element> tag
// ** This is really only for Collections/Sets since M-N doesnt make sense for indexed Lists/arrays **
columnMetaData = relatedMmds[0].getElementMetaData().getColumnMetaData();
}
try {
ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, columnMetaData, storeMgr, this, false, false, FieldRole.ROLE_OWNER, clr, ownerTable);
} catch (NoTableManagedException ntme) {
// Maybe this is a join table from an embedded object, so no table to link back to
throw new NucleusUserException("Table " + toString() + " for member=" + mmd.getFullFieldName() + " needs a column to link back to its owner, yet the owner type (" + ownerType + ") has no table of its own (embedded?)");
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
}
// Add any distinguisher column
if (mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN) || mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_VALUE)) {
// Generate some columnMetaData for our new column
String colName = mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN);
if (colName == null) {
// No column defined so use a fallback name
colName = "RELATION_DISCRIM";
}
ColumnMetaData colmd = new ColumnMetaData();
colmd.setName(colName);
boolean relationDiscriminatorPk = false;
if (mmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_PK) && mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_PK).equalsIgnoreCase("true")) {
// Default this to not be part of the PK of the join table, but allow the user to override it
relationDiscriminatorPk = true;
}
if (!relationDiscriminatorPk) {
// Allow for elements not in any discriminated collection (when not PK)
colmd.setAllowsNull(Boolean.TRUE);
}
// Create the mapping and its datastore column (only support String relation discriminators here)
relationDiscriminatorMapping = storeMgr.getMappingManager().getMapping(String.class);
ColumnCreator.createIndexColumn(relationDiscriminatorMapping, storeMgr, clr, this, colmd, relationDiscriminatorPk);
relationDiscriminatorValue = mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_VALUE);
if (relationDiscriminatorValue == null) {
// No value defined so just use the field name
relationDiscriminatorValue = mmd.getFullFieldName();
}
}
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class MapTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr) {
assertIsUninitialized();
MapMetaData mapmd = mmd.getMap();
if (mapmd == null) {
throw new NucleusUserException(Localiser.msg("057017", mmd));
}
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null && pkmd.getColumnMetaData() != null);
boolean pkRequired = requiresPrimaryKey();
// Add owner mapping
ColumnMetaData[] ownerColmd = null;
if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
// When specified at this side they use the <join> tag
ownerColmd = mmd.getJoinMetaData().getColumnMetaData();
}
ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, ownerColmd, storeMgr, this, pkRequired, false, FieldRole.ROLE_OWNER, clr, null);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
}
String keyValueFieldName = (mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getMappedBy() : null);
String valueKeyFieldName = (mmd.getValueMetaData() != null ? mmd.getValueMetaData().getMappedBy() : null);
// Add key mapping
boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
Class keyCls = clr.classForName(mapmd.getKeyType());
if (keyValueFieldName != null && isEmbeddedValuePC()) {
// Added in value code
} else if (isSerialisedKey() || isEmbeddedKeyPC() || (isEmbeddedKey() && !keyPC) || ClassUtils.isReferenceType(keyCls)) {
// Key = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
keyMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_KEY);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
// Make all key col(s) nullable so we can store null elements
for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
}
if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
// Key (PC) is embedded and value is a field of the key
EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
valueMapping = embMapping.getJavaTypeMapping(valueKeyFieldName);
}
} else {
// Key = PC
ColumnMetaData[] keyColmd = null;
KeyMetaData keymd = mmd.getKeyMetaData();
if (keymd != null && keymd.getColumnMetaData() != null && keymd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
keyColmd = keymd.getColumnMetaData();
}
keyMapping = ColumnCreator.createColumnsForJoinTables(keyCls, mmd, keyColmd, storeMgr, this, false, false, FieldRole.ROLE_MAP_KEY, clr, null);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all key col(s) nullable so we can store null elements
for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = keyMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
}
}
// Add value mapping
boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
Class valueCls = clr.classForName(mapmd.getValueType());
if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
// Added in key code
} else if (isSerialisedValue() || isEmbeddedValuePC() || (isEmbeddedValue() && !valuePC) || ClassUtils.isReferenceType(valueCls)) {
// Value = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
valueMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_VALUE);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all value col(s) nullable so we can store null elements
for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
}
if (keyValueFieldName != null && isEmbeddedValuePC()) {
// Value (PC) is embedded and key is a field of the value
EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
keyMapping = embMapping.getJavaTypeMapping(keyValueFieldName);
}
} else {
// Value = PC
ColumnMetaData[] valueColmd = null;
ValueMetaData valuemd = mmd.getValueMetaData();
if (valuemd != null && valuemd.getColumnMetaData() != null && valuemd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
valueColmd = valuemd.getColumnMetaData();
}
valueMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(mapmd.getValueType()), mmd, valueColmd, storeMgr, this, false, true, FieldRole.ROLE_MAP_VALUE, clr, null);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all value col(s) nullable so we can store null elements
for (int i = 0; i < valueMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = valueMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
}
}
// Add order mapping if required
boolean orderRequired = false;
if (mmd.getOrderMetaData() != null) {
// User requested order column so add one
orderRequired = true;
} else if (requiresPrimaryKey() && !pkColsSpecified) {
// PK is required so maybe need to add an index to form the PK
if (isEmbeddedKeyPC()) {
if (mmd.hasExtension("surrogate-pk-column") && mmd.getValueForExtension("surrogate-pk-column").equalsIgnoreCase("true")) {
// Allow user to request surrogate pk column be added (for use with JPA)
orderRequired = true;
} else if (storeMgr.getApiAdapter().getName().equalsIgnoreCase("JDO") && mmd.getMap().getKeyClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
// Embedded key PC with datastore id so we need an index to form the PK TODO It is arguable that we can just use all embedded key fields as part of PK here always
orderRequired = true;
}
} else if (isSerialisedKey()) {
// Serialised key, so need an index to form the PK
orderRequired = true;
} else if (keyMapping instanceof ReferenceMapping) {
// ReferenceMapping, so have order if more than 1 implementation
ReferenceMapping refMapping = (ReferenceMapping) keyMapping;
if (refMapping.getJavaTypeMapping().length > 1) {
orderRequired = true;
}
} else if (!(keyMapping instanceof PersistableMapping)) {
// Non-PC, so depends if the key column can be used as part of a PK
// TODO This assumes the keyMapping has a single column but what if it is Color with 4 cols?
Column elementCol = keyMapping.getDatastoreMapping(0).getColumn();
if (!storeMgr.getDatastoreAdapter().isValidPrimaryKeyType(elementCol.getJdbcType())) {
// Not possible to use this Non-PC type as part of the PK
orderRequired = true;
}
}
}
if (orderRequired) {
// Order/Adapter (index) column is required (integer based)
ColumnMetaData orderColmd = null;
if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
// Specified "order" column info
orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
if (orderColmd.getName() == null) {
// No column name so use default
orderColmd = new ColumnMetaData(orderColmd);
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd.setName(id.getName());
}
} else {
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd = new ColumnMetaData();
orderColmd.setName(id.getName());
}
// JDO2 spec [18.5] order column is assumed to be "int"
orderMapping = storeMgr.getMappingManager().getMapping(int.class);
ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ORDER]", orderMapping);
}
}
// Define primary key of the join table (if any)
if (pkRequired) {
if (pkColsSpecified) {
// Apply the users PK specification
applyUserPrimaryKeySpecification(pkmd);
} else {
// Define PK using internal rules
if (orderRequired) {
// Order column specified so owner+order are the PK
orderMapping.getDatastoreMapping(0).getColumn().setPrimaryKey();
} else {
// No order column specified so owner+key are the PK
for (int i = 0; i < keyMapping.getNumberOfDatastoreMappings(); i++) {
keyMapping.getDatastoreMapping(i).getColumn().setPrimaryKey();
}
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
Aggregations