use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ClassTable method addOrderColumn.
/**
* Adds an ordering column to the element table (this) in FK list relationships.
* Used to store the position of the element in the List.
* If the <order> provides a mapped-by, this will return the existing column mapping.
* @param mmd The MetaData of the field/property with the list for the column to map to
* @return The Mapping for the order column
*/
private JavaTypeMapping addOrderColumn(AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
Class indexType = Integer.class;
JavaTypeMapping orderIndexMapping = new OrderIndexMapping();
orderIndexMapping.initialize(storeMgr, indexType.getName());
orderIndexMapping.setMemberMetaData(mmd);
orderIndexMapping.setTable(this);
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier indexColumnName = null;
ColumnMetaData colmd = null;
// Allow for any user definition in OrderMetaData
OrderMetaData omd = mmd.getOrderMetaData();
if (omd != null) {
colmd = (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 ? omd.getColumnMetaData()[0] : null);
if (omd.getMappedBy() != null) {
// User has defined ordering using the column(s) of an existing field.
// Not adding anything so just set table back to "initialised"
state = TABLE_STATE_INITIALIZED;
JavaTypeMapping orderMapping = getMemberMapping(omd.getMappedBy());
if (!(orderMapping instanceof IntegerMapping) && !(orderMapping instanceof LongMapping)) {
throw new NucleusUserException(Localiser.msg("057022", mmd.getFullFieldName(), omd.getMappedBy()));
}
return orderMapping;
}
String colName = null;
if (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 && omd.getColumnMetaData()[0].getName() != null) {
// User-defined name so create an identifier using it
colName = omd.getColumnMetaData()[0].getName();
indexColumnName = idFactory.newColumnIdentifier(colName);
}
}
if (indexColumnName == null) {
// No name defined so generate one
indexColumnName = idFactory.newForeignKeyFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(indexType), FieldRole.ROLE_INDEX);
}
Column column = addColumn(indexType.getName(), indexColumnName, orderIndexMapping, colmd);
if (colmd == null || (colmd.getAllowsNull() == null) || (colmd.getAllowsNull() != null && colmd.isAllowsNull())) {
// User either wants it nullable, or havent specified anything, so make it nullable
column.setNullable(true);
}
storeMgr.getMappingManager().createColumnMapping(orderIndexMapping, column, indexType.getName());
return orderIndexMapping;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class CollectionTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
@Override
public void initialize(ClassLoaderResolver clr) {
super.initialize(clr);
// Add column(s) for element
boolean elementPC = (mmd.hasCollection() && mmd.getCollection().elementIsPersistent());
Class elementClass = clr.classForName(getElementType());
if (isSerialisedElement() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(elementClass)) {
// Element = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
// Join table has : ownerMapping (PK), elementMapping, orderMapping (PK)
elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_COLLECTION_ELEMENT);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
} else {
// Element = PC
// Join table has : ownerMapping (PK), elementMapping, orderMapping (optional)
ColumnMetaData[] elemColmd = null;
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
ElementMetaData elemmd = mmd.getElementMetaData();
if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
elemColmd = elemmd.getColumnMetaData();
} else if (relatedMmds != null && relatedMmds[0].getJoinMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData() != null && relatedMmds[0].getJoinMetaData().getColumnMetaData().length > 0) {
// Column mappings defined at other side (M-N) on <join>
elemColmd = relatedMmds[0].getJoinMetaData().getColumnMetaData();
}
elementMapping = ColumnCreator.createColumnsForJoinTables(elementClass, mmd, elemColmd, storeMgr, this, false, false, FieldRole.ROLE_COLLECTION_ELEMENT, clr, null);
RelationType relationType = mmd.getRelationType(clr);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls()) && relationType != RelationType.MANY_TO_MANY_BI) {
// 1-N : Make all element col(s) nullable so we can store null elements
for (int i = 0; i < elementMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = elementMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
}
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
boolean pkRequired = requiresPrimaryKey();
// Add order mapping if required
boolean orderRequired = false;
if (mmd.getOrderMetaData() != null) {
if (mmd.getOrderMetaData().isIndexedList()) {
// Indexed Collection with <order>, so add index mapping
orderRequired = true;
RelationType relType = mmd.getRelationType(clr);
if (relType == RelationType.MANY_TO_MANY_BI) {
// Don't support M-N using indexed List
throw new NucleusUserException(Localiser.msg("020002", mmd.getFullFieldName())).setFatal();
}
}
} else if (List.class.isAssignableFrom(mmd.getType())) {
// Indexed List with no <order>, so has index mapping
orderRequired = true;
} else if (pkRequired && !pkColsSpecified) {
// PK is required so maybe need to add an index to form the PK
if (isEmbeddedElementPC()) {
if (mmd.getCollection().getElementClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
// Embedded PC with datastore id so we need an index to form the PK
orderRequired = true;
}
} else if (isSerialisedElement()) {
// Serialised element, so need an index to form the PK
orderRequired = true;
} else if (elementMapping instanceof ReferenceMapping) {
// ReferenceMapping, so have order if more than 1 implementation
ReferenceMapping refMapping = (ReferenceMapping) elementMapping;
if (refMapping.getJavaTypeMapping().length > 1) {
orderRequired = true;
}
} else if (!(elementMapping instanceof PersistableMapping)) {
// Non-PC, so depends if the element column can be used as part of a PK
// TODO This assumes the elementMapping has a single column but what if it is Color with 4 cols?
Column elementCol = elementMapping.getColumnMapping(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 (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
for (int i = 0; i < ownerMapping.getNumberOfColumnMappings(); i++) {
ownerMapping.getColumnMapping(i).getColumn().setPrimaryKey();
}
if (orderRequired) {
// Order column specified so owner+order are the PK
orderMapping.getColumnMapping(0).getColumn().setPrimaryKey();
} else {
// No order column specified so owner+element are the PK
for (int i = 0; i < elementMapping.getNumberOfColumnMappings(); i++) {
elementMapping.getColumnMapping(i).getColumn().setPrimaryKey();
}
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ColumnCreator method createColumnsForField.
/**
* Method to create the column(s) for a field in either a join table or for a reference field.
* @param javaType The java type of the field being stored
* @param mapping The JavaTypeMapping (if existing, otherwise created and returned by this method)
* @param table The table to insert the columns into (join table, or primary table (if ref field))
* @param storeMgr Manager for the store
* @param mmd MetaData for the field (or null if a collection field)
* @param isPrimaryKey Whether to create the columns as part of the PK
* @param isNullable Whether the columns should be nullable
* @param serialised Whether the field is serialised
* @param embedded Whether the field is embedded
* @param fieldRole The role of the field (when part of a join table)
* @param columnMetaData MetaData for the column(s)
* @param clr ClassLoader resolver
* @param isReferenceField Whether this field is part of a reference field
* @param ownerTable Table of the owner of this member (optional, for when the member is embedded)
* @return The JavaTypeMapping for the table
*/
public static JavaTypeMapping createColumnsForField(Class javaType, JavaTypeMapping mapping, Table table, RDBMSStoreManager storeMgr, AbstractMemberMetaData mmd, boolean isPrimaryKey, boolean isNullable, boolean serialised, boolean embedded, FieldRole fieldRole, ColumnMetaData[] columnMetaData, ClassLoaderResolver clr, boolean isReferenceField, Table ownerTable) {
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
if (mapping instanceof ReferenceMapping || mapping instanceof PersistableMapping) {
// PC/interface/Object mapping
JavaTypeMapping container = mapping;
if (mapping instanceof ReferenceMapping) {
// Interface/Object has child mappings for each implementation
container = storeMgr.getMappingManager().getMapping(javaType, serialised, embedded, mmd != null ? mmd.getFullFieldName() : null);
((ReferenceMapping) mapping).addJavaTypeMapping(container);
}
// Get the table that we want our column to be a FK to. This could be the owner table, element table, key table, value table etc
DatastoreClass destinationTable = null;
try {
destinationTable = storeMgr.getDatastoreClass(javaType.getName(), clr);
} catch (NoTableManagedException ntme) {
if (ownerTable != null && ownerTable instanceof DatastoreClass) {
destinationTable = (DatastoreClass) ownerTable;
} else {
throw ntme;
}
}
if (destinationTable == null) {
// Maybe the owner hasn't got its own table (e.g "subclass-table" or "complete-table"+abstract)
// Alternate is when we have an embedded type which itself has an embedded collection - not catered for at all currently
AbstractClassMetaData ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(javaType, clr);
if (ownerCmd.getBaseAbstractClassMetaData().getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// COMPLETE-TABLE but abstract root, so find one of the subclasses with a table and use that for now
Collection<String> ownerSubclassNames = storeMgr.getSubClassesForClass(javaType.getName(), true, clr);
if (ownerSubclassNames != null && ownerSubclassNames.size() > 0) {
for (String ownerSubclassName : ownerSubclassNames) {
ownerCmd = storeMgr.getMetaDataManager().getMetaDataForClass(ownerSubclassName, clr);
try {
destinationTable = storeMgr.getDatastoreClass(ownerSubclassName, clr);
} catch (NoTableManagedException ntme) {
}
if (destinationTable != null) {
break;
}
}
}
} else {
AbstractClassMetaData[] ownerCmds = storeMgr.getClassesManagingTableForClass(ownerCmd, clr);
if (ownerCmds == null || ownerCmds.length == 0) {
throw new NucleusUserException(Localiser.msg("056076", javaType.getName())).setFatal();
}
// Use the first one since they should all have the same id column(s)
destinationTable = storeMgr.getDatastoreClass(ownerCmds[0].getFullClassName(), clr);
}
}
if (destinationTable != null) {
// Foreign-Key to the destination table ID mapping
JavaTypeMapping m = destinationTable.getIdMapping();
// For each column in the destination mapping, add a column here
ColumnMetaDataContainer columnContainer = null;
if (columnMetaData != null && columnMetaData.length > 0) {
columnContainer = (ColumnMetaDataContainer) columnMetaData[0].getParent();
}
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(columnContainer, table, columnMetaData, m, true);
for (int i = 0; i < m.getNumberOfColumnMappings(); i++) {
JavaTypeMapping refDatastoreMapping = storeMgr.getMappingManager().getMapping(m.getColumnMapping(i).getJavaTypeMapping().getJavaType());
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(m.getColumnMapping(i).getColumn().getIdentifier());
try {
DatastoreIdentifier identifier = null;
if (fieldRole == FieldRole.ROLE_MAP_KEY && columnContainer == null) {
// Map KEY field and no metadata defined
if (isReferenceField) {
// Create reference identifier
identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getColumnMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
} else {
// Create join table identifier
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO If the mmd is an "embedded" type this can create invalid identifiers
// TODO Cater for more than 1 related field
identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getColumnMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
} else {
if (colmd.getName() == null) {
// User hasn't provided a name, so we use default naming
if (isReferenceField) {
// Create reference identifier
identifier = idFactory.newReferenceFieldIdentifier(mmd, storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(javaType, clr), m.getColumnMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
} else {
// Create join table identifier
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO If the mmd is an "embedded" type this can create invalid identifiers
// TODO Cater for more than 1 related field
identifier = idFactory.newJoinTableFieldIdentifier(mmd, relatedMmds != null ? relatedMmds[0] : null, m.getColumnMapping(i).getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
} else {
// User defined name, so we use that.
identifier = idFactory.newColumnIdentifier(colmd.getName());
}
}
// Only add the column if not currently present
Column column = table.addColumn(javaType.getName(), identifier, refDatastoreMapping, colmd);
m.getColumnMapping(i).getColumn().copyConfigurationTo(column);
if (isPrimaryKey) {
column.setPrimaryKey();
}
if (isNullable) {
column.setNullable(true);
}
storeMgr.getMappingManager().createColumnMapping(refDatastoreMapping, column, m.getColumnMapping(i).getJavaTypeMapping().getJavaTypeForColumnMapping(i));
} catch (DuplicateColumnException ex) {
throw new NucleusUserException("Cannot create column for field " + mmd.getFullFieldName() + " column metadata " + colmd, ex);
}
try {
((PersistableMapping) container).addJavaTypeMapping(refDatastoreMapping);
} catch (ClassCastException e) {
throw new NucleusUserException("Failed to create column for field " + mmd.getFullFieldName() + ". Cannot cast mapping to PersistableMapping.", e);
}
}
}
} else {
// Non-PC mapping
// Add column for the field
Column column = null;
ColumnMetaData colmd = null;
if (columnMetaData != null && columnMetaData.length > 0) {
colmd = columnMetaData[0];
}
DatastoreIdentifier identifier = null;
if (colmd != null && colmd.getName() != null) {
// User specified name
identifier = idFactory.newColumnIdentifier(colmd.getName());
} else {
// No user-supplied name so generate one
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(javaType), fieldRole);
}
column = table.addColumn(javaType.getName(), identifier, mapping, colmd);
storeMgr.getMappingManager().createColumnMapping(mapping, column, mapping.getJavaTypeForColumnMapping(0));
if (isNullable) {
column.setNullable(true);
}
}
return mapping;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class TableImpl method initializeColumnInfoFromDatastore.
/**
* Initialize the default value for columns if null using the values from the datastore.
* @param conn The JDBC Connection
* @throws SQLException Thrown if an error occurs in the default initialisation.
*/
public void initializeColumnInfoFromDatastore(Connection conn) throws SQLException {
Map<DatastoreIdentifier, Column> columns = new HashMap(columnsByIdentifier);
Iterator i = storeMgr.getColumnInfoForTable(this, conn).iterator();
while (i.hasNext()) {
RDBMSColumnInfo ci = (RDBMSColumnInfo) i.next();
DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, ci.getColumnName());
Column col = columns.get(colName);
if (col != null) {
col.initializeColumnInfoFromDatastore(ci);
}
}
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class AbstractTable method getDatastoreIdentifierFullyQualified.
/**
* Method that operates like toString except it returns a fully-qualified name that will always
* be fully-qualified even when the user hasnt specified the catalog/schema in PMF or MetaData.
* That is, it will add on any auto-calculated catalog/schema for the datastore.
* Note that this will never include any quoting strings required for insert/select etc.
* @return The fully qualified name
*/
public DatastoreIdentifier getDatastoreIdentifierFullyQualified() {
String catalog = identifier.getCatalogName();
if (catalog != null) {
// Remove any identifier quotes
catalog = catalog.replace(dba.getIdentifierQuoteString(), "");
}
String schema = identifier.getSchemaName();
if (schema != null) {
// Remove any identifier quotes
schema = schema.replace(dba.getIdentifierQuoteString(), "");
}
String table = identifier.getName();
table = table.replace(dba.getIdentifierQuoteString(), "");
DatastoreIdentifier di = storeMgr.getIdentifierFactory().newTableIdentifier(table);
di.setCatalogName(catalog);
di.setSchemaName(schema);
return di;
}
Aggregations