use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class ArrayTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr) {
super.initialize(clr);
// Add field(s) for element
boolean elementPC = (mmd.hasArray() && mmd.getArray().elementIsPersistent());
if (isSerialisedElementPC() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(mmd.getType().getComponentType())) {
// Element = PC(embedded), PC(serialised), Non-PC(embedded), Reference
elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_ARRAY_ELEMENT);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
// Make all element col(s) nullable so we can store null elements
for (int i = 0; i < elementMapping.getNumberOfDatastoreMappings(); i++) {
Column elementCol = elementMapping.getDatastoreMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
} else {
// Element = PC
ColumnMetaData[] elemColmd = null;
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();
}
elementMapping = ColumnCreator.createColumnsForJoinTables(mmd.getType().getComponentType(), mmd, elemColmd, storeMgr, this, false, true, FieldRole.ROLE_ARRAY_ELEMENT, clr, null);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
}
boolean pkRequired = requiresPrimaryKey();
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
// Add order mapping
ColumnMetaData colmd = null;
if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
// Specified "order" column info
colmd = mmd.getOrderMetaData().getColumnMetaData()[0];
} else {
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
colmd = new ColumnMetaData();
colmd.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, colmd, 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.getNumberOfDatastoreMappings(); i++) {
ownerMapping.getDatastoreMapping(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.metadata.ColumnMetaData in project datanucleus-core by datanucleus.
the class AbstractNamingFactory method getColumnName.
/* (non-Javadoc)
* @see org.datanucleus.store.schema.naming.NamingFactory#getColumnName(java.util.List, int)
*/
public String getColumnName(List<AbstractMemberMetaData> mmds, int colPosition) {
// Extract any root EmbeddedMetaData definition in case user has provided overrides
EmbeddedMetaData embmd = null;
AbstractMemberMetaData rootMmd = mmds.get(0);
if (rootMmd.hasCollection() || rootMmd.hasArray()) {
if (rootMmd.getElementMetaData() != null) {
embmd = rootMmd.getElementMetaData().getEmbeddedMetaData();
}
} else if (rootMmd.hasMap()) {
// TODO Cater for embedded map key OR values ... this just assumes the value, but could be the key
if (rootMmd.getValueMetaData() != null) {
embmd = rootMmd.getValueMetaData().getEmbeddedMetaData();
}
} else {
embmd = mmds.get(0).getEmbeddedMetaData();
}
if (embmd != null && mmds.size() > 1) {
// Try to find a user-provided column name in EmbeddedMetaData for this member
boolean checked = false;
int mmdNo = 1;
while (!checked) {
AbstractMemberMetaData[] embMmds = embmd.getMemberMetaData();
if (embMmds == null || embMmds.length == 0) {
break;
}
boolean checkedEmbmd = false;
boolean foundEmbmd = false;
for (int i = 0; i < embMmds.length; i++) {
if (embMmds[i].getFullFieldName().equals(mmds.get(mmdNo).getFullFieldName())) {
foundEmbmd = true;
if (mmds.size() == mmdNo + 1) {
// Found last embedded field, so use column data if present
checked = true;
ColumnMetaData[] colmds = embMmds[i].getColumnMetaData();
if (colmds != null && colmds.length > colPosition && !StringUtils.isWhitespace(colmds[colPosition].getName())) {
String colName = colmds[colPosition].getName();
return prepareIdentifierNameForUse(colName, SchemaComponent.COLUMN);
}
} else {
// Go to next level in embMmds if present
checkedEmbmd = true;
mmdNo++;
embmd = null;
if (embMmds[i].hasCollection() || embMmds[i].hasArray()) {
if (embMmds[i].getElementMetaData() != null) {
embmd = embMmds[i].getElementMetaData().getEmbeddedMetaData();
}
} else if (embMmds[i].hasMap()) {
// TODO Cater for embedded map key OR values ... this just assumes the value, but could be the key
if (embMmds[i].getValueMetaData() != null) {
embmd = embMmds[i].getValueMetaData().getEmbeddedMetaData();
}
} else {
embmd = embMmds[i].getEmbeddedMetaData();
}
if (embmd == null) {
// No more info specified so drop out here
checked = true;
}
}
}
if (checked || checkedEmbmd) {
break;
}
}
if (!foundEmbmd) {
// No EmbeddedMetaData definition for this member, so break out and use fallback naming
checked = true;
}
}
}
// EmbeddedMetaData not available for defining this column, so check for column info for the member itself
if (mmds.size() >= 1) {
AbstractMemberMetaData lastMmd = mmds.get(mmds.size() - 1);
ColumnMetaData[] colmds = lastMmd.getColumnMetaData();
if (colmds != null && colmds.length > colPosition && !StringUtils.isWhitespace(colmds[colPosition].getName())) {
String colName = colmds[colPosition].getName();
return prepareIdentifierNameForUse(colName, SchemaComponent.COLUMN);
}
}
// EmbeddedMetaData and member don't specify the column, so generate one based on the names of the member(s).
// TODO If columnPosition is >= 1 maybe we should append "_{colPosition}" on the column name
StringBuilder str = new StringBuilder(mmds.get(0).getName());
for (int i = 1; i < mmds.size(); i++) {
str.append(wordSeparator);
str.append(mmds.get(i).getName());
}
return prepareIdentifierNameForUse(str.toString(), SchemaComponent.COLUMN);
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-core by datanucleus.
the class CompleteClassTable method processEmbeddedMember.
protected void processEmbeddedMember(List<AbstractMemberMetaData> mmds, ClassLoaderResolver clr, EmbeddedMetaData embmd, boolean ownerNested) {
TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
MetaDataManager mmgr = storeMgr.getMetaDataManager();
NamingFactory namingFactory = storeMgr.getNamingFactory();
AbstractMemberMetaData lastMmd = mmds.get(mmds.size() - 1);
AbstractClassMetaData embCmd = null;
if (lastMmd.hasCollection()) {
// Embedded collection element
embCmd = mmgr.getMetaDataForClass(lastMmd.getCollection().getElementType(), clr);
} else if (lastMmd.hasArray()) {
// Embedded array element
embCmd = mmgr.getMetaDataForClass(lastMmd.getArray().getElementType(), clr);
} else {
// Embedded 1-1
embCmd = mmgr.getMetaDataForClass(lastMmd.getType(), clr);
}
// Go through all members of the embedded class
int[] memberPositions = embCmd.getAllMemberPositions();
for (int i = 0; i < memberPositions.length; i++) {
AbstractMemberMetaData mmd = embCmd.getMetaDataForManagedMemberAtAbsolutePosition(memberPositions[i]);
if (mmd.getPersistenceModifier() != FieldPersistenceModifier.PERSISTENT) {
// Don't need column if not persistent
continue;
}
if (mmds.size() == 1 && embmd != null && embmd.getOwnerMember() != null && embmd.getOwnerMember().equals(mmd.getName())) {
// Special case of this being a link back to the owner. TODO Repeat this for nested and their owners
continue;
}
AbstractMemberMetaData embmdMmd = null;
if (embmd != null) {
AbstractMemberMetaData[] embmdMmds = embmd.getMemberMetaData();
if (embmdMmds != null) {
for (AbstractMemberMetaData thisMmd : embmdMmds) {
if (thisMmd.getName().equals(mmd.getName())) {
embmdMmd = thisMmd;
break;
}
}
}
}
RelationType relationType = mmd.getRelationType(clr);
if (relationType != RelationType.NONE && MetaDataUtils.getInstance().isMemberEmbedded(mmgr, clr, mmd, relationType, lastMmd)) {
if (RelationType.isRelationSingleValued(relationType)) {
// Nested embedded PC, so recurse
boolean nested = false;
if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC_NESTED)) {
nested = !storeMgr.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_EMBEDDED_PC_FLAT);
String nestedStr = mmd.getValueForExtension("nested");
if (nestedStr != null && nestedStr.equalsIgnoreCase("" + !nested)) {
nested = !nested;
}
}
List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
embMmds.add(mmd);
if (nested) {
// Embedded object stored as nested under this in the owner table (where the datastore supports that)
// Add column for the owner of the embedded object, typically for the column name only
ColumnMetaData[] colmds = mmd.getColumnMetaData();
String colName = namingFactory.getColumnName(embMmds, 0);
ColumnImpl col = addEmbeddedColumn(colName, null);
col.setNested(true);
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
} else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
col.setPosition(colmds[0].getPosition());
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
} else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
col.setJdbcType(colmds[0].getJdbcType());
}
MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
col.setMemberColumnMapping(mapping);
if (schemaVerifier != null) {
schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
}
mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
// TODO Create mapping for the related info under the above column
processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, true);
} else {
// Embedded object stored flat into this table, with columns at same level as owner columns
processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, false);
}
} else {
if (mmd.hasCollection()) {
if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION_NESTED)) {
// TODO Support nested embedded collection element
}
NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
continue;
} else if (mmd.hasMap()) {
if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP_NESTED)) {
// TODO Support nested embedded map key/value
}
NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
continue;
} else if (mmd.hasArray()) {
if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_ARRAY_NESTED)) {
// TODO Support nested embedded array element
}
NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded array. Not yet supported. Ignoring");
continue;
}
}
} else {
List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
embMmds.add(mmd);
ColumnMetaData[] colmds = mmd.getColumnMetaData();
if (relationType != RelationType.NONE) {
// 1-1/N-1 stored as single column with persistable-id
// 1-N/M-N stored as single column with collection<persistable-id>
// Create column for basic type
String colName = namingFactory.getColumnName(embMmds, 0);
ColumnImpl col = addEmbeddedColumn(colName, null);
col.setNested(ownerNested);
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
} else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
col.setPosition(colmds[0].getPosition());
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
} else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
col.setJdbcType(colmds[0].getJdbcType());
}
MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
col.setMemberColumnMapping(mapping);
if (schemaVerifier != null) {
schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
}
mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
} else {
// TODO Pass in embedded colmds if they have jdbcType info?
TypeConverter typeConv = getTypeConverterForMember(mmd, colmds, typeMgr);
if (typeConv != null) {
// Create column(s) for this TypeConverter
if (typeConv instanceof MultiColumnConverter) {
Class[] colJavaTypes = ((MultiColumnConverter) typeConv).getDatastoreColumnTypes();
Column[] cols = new Column[colJavaTypes.length];
for (int j = 0; j < colJavaTypes.length; j++) {
String colName = namingFactory.getColumnName(embMmds, j);
ColumnImpl col = addEmbeddedColumn(colName, typeConv);
col.setNested(ownerNested);
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getPosition() != null) {
col.setPosition(embmdMmd.getColumnMetaData()[j].getPosition());
} else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getPosition() != null) {
col.setPosition(colmds[j].getPosition());
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getJdbcType() != null) {
col.setJdbcType(embmdMmd.getColumnMetaData()[j].getJdbcType());
} else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getJdbcType() != null) {
col.setJdbcType(colmds[j].getJdbcType());
}
cols[j] = col;
}
MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, cols, typeConv);
for (int j = 0; j < colJavaTypes.length; j++) {
((ColumnImpl) cols[j]).setMemberColumnMapping(mapping);
}
if (schemaVerifier != null) {
schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
}
mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
} else {
String colName = namingFactory.getColumnName(embMmds, 0);
ColumnImpl col = addEmbeddedColumn(colName, typeConv);
col.setNested(ownerNested);
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
} else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
col.setPosition(colmds[0].getPosition());
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
} else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
col.setJdbcType(colmds[0].getJdbcType());
}
MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
col.setMemberColumnMapping(mapping);
mapping.setTypeConverter(typeConv);
if (schemaVerifier != null) {
schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
}
mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
}
} else {
// Create column for basic type
String colName = namingFactory.getColumnName(embMmds, 0);
ColumnImpl col = addEmbeddedColumn(colName, null);
col.setNested(ownerNested);
AbstractMemberMetaData theMmd = embMmds.get(0);
if (theMmd.isPrimaryKey()) {
col.setPrimaryKey();
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
} else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
col.setPosition(colmds[0].getPosition());
}
if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
} else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
col.setJdbcType(colmds[0].getJdbcType());
}
MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
col.setMemberColumnMapping(mapping);
if (schemaVerifier != null) {
schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
}
mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
}
}
}
}
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-core by datanucleus.
the class OptionalHandler method populateMetaData.
@Override
public void populateMetaData(ClassLoaderResolver clr, ClassLoader primary, AbstractMemberMetaData mmd) {
mmd.getCollection().setSingleElement(true);
// Get columns defined metadata - not visible
List<ColumnMetaData> columns = new AbstractMemberMetaData(mmd.getParent(), mmd) {
private static final long serialVersionUID = 1L;
public List<ColumnMetaData> getColumns() {
return columns;
}
}.getColumns();
if (columns == null || columns.isEmpty()) {
// Optional should allow nullable by default
ColumnMetaData colmd = new ColumnMetaData();
colmd.setAllowsNull(Boolean.TRUE);
mmd.addColumn(colmd);
}
super.populateMetaData(clr, primary, mmd);
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-core by datanucleus.
the class ElementContainerHandler method moveColumnsToElement.
protected void moveColumnsToElement(AbstractMemberMetaData mmd) {
ColumnMetaData[] columnMetaData = mmd.getColumnMetaData();
if (!mmd.isSerialized() && !mmd.isEmbedded() && columnMetaData != null && mmd.getTypeConverterName() == null) {
// Not serialising/embedding this field, nor converting the whole field yet column info was specified. Check for specific conditions
if (mmd.getElementMetaData() == null) {
// Collection/Array with column(s) specified on field but not on element so move all column info to element
ElementMetaData elemmd = new ElementMetaData();
mmd.setElementMetaData(elemmd);
for (int i = 0; i < columnMetaData.length; i++) {
elemmd.addColumn(columnMetaData[i]);
}
mmd.clearColumns();
}
}
}
Aggregations