use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class DiscriminatorMapping method createDiscriminatorMapping.
/**
* Convenience method to create a discriminator mapping in the specified table, using the provided
* discriminator metadata.
* @param table The table
* @param dismd The discriminator metadata
* @return Discriminator mapping
*/
public static DiscriminatorMapping createDiscriminatorMapping(Table table, DiscriminatorMetaData dismd) {
RDBMSStoreManager storeMgr = table.getStoreManager();
MappingManager mapMgr = storeMgr.getMappingManager();
if (dismd.getStrategy() == DiscriminatorStrategy.CLASS_NAME) {
return new DiscriminatorStringMapping(table, mapMgr.getMapping(String.class), dismd);
} else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP) {
ColumnMetaData disColmd = dismd.getColumnMetaData();
if (disColmd != null && disColmd.getJdbcType() != null) {
if (MetaDataUtils.isJdbcTypeNumeric(disColmd.getJdbcType())) {
return new DiscriminatorLongMapping(table, mapMgr.getMapping(Long.class), dismd);
}
return new DiscriminatorStringMapping(table, mapMgr.getMapping(String.class), dismd);
}
return new DiscriminatorStringMapping(table, mapMgr.getMapping(String.class), dismd);
} else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP_ENTITY_NAME) {
ColumnMetaData disColmd = dismd.getColumnMetaData();
if (disColmd != null && disColmd.getJdbcType() != null) {
if (MetaDataUtils.isJdbcTypeNumeric(disColmd.getJdbcType())) {
return new DiscriminatorLongMapping(table, mapMgr.getMapping(Long.class), dismd);
}
return new DiscriminatorStringMapping(table, mapMgr.getMapping(String.class), dismd);
}
return new DiscriminatorStringMapping(table, mapMgr.getMapping(String.class), dismd);
}
return null;
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class UUIDMapping method initialize.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping#initialize(org.datanucleus.metadata.AbstractMemberMetaData, org.datanucleus.store.rdbms.table.Table, org.datanucleus.ClassLoaderResolver)
*/
@Override
public void initialize(AbstractMemberMetaData mmd, Table table, ClassLoaderResolver clr) {
boolean useConverter = true;
if (mmd != null) {
ColumnMetaData[] colmds = mmd.getColumnMetaData();
if (colmds != null && colmds.length == 1) {
ColumnMetaData colmd = colmds[0];
if (colmd.getSqlType() != null) {
useConverter = false;
}
}
if (useConverter) {
if (mmd.getTypeConverterName() != null) {
// Use specified converter (if found)
converter = table.getStoreManager().getNucleusContext().getTypeManager().getTypeConverterForName(mmd.getTypeConverterName());
if (converter == null) {
throw new NucleusUserException(Localiser.msg("044062", mmd.getFullFieldName(), mmd.getTypeConverterName()));
}
} else {
converter = table.getStoreManager().getNucleusContext().getTypeManager().getTypeConverterForType(mmd.getType(), String.class);
}
}
}
super.initialize(mmd, table, clr);
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class UUIDMapping method initialize.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping#initialize(org.datanucleus.store.rdbms.RDBMSStoreManager, java.lang.String)
*/
@Override
public void initialize(RDBMSStoreManager storeMgr, String type) {
boolean useConverter = true;
if (mmd != null) {
ColumnMetaData[] colmds = mmd.getColumnMetaData();
if (colmds != null && colmds.length == 1) {
ColumnMetaData colmd = colmds[0];
if (colmd.getSqlType() != null) {
useConverter = false;
}
}
}
if (useConverter) {
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
Class fieldType = clr.classForName(type);
converter = storeMgr.getNucleusContext().getTypeManager().getDefaultTypeConverterForType(fieldType);
if (converter == null) {
throw new NucleusUserException("Unable to find TypeConverter for converting " + fieldType + " to String");
}
}
super.initialize(storeMgr, type);
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method createColumn.
/**
* Method to create a datastore field for a Java type mapping.
* This is used for serialised PC elements/keys/values in a join table.
* TODO Merge this with the method above.
* @param mapping Java type mapping for the field
* @param javaType The type of field being stored in this column
* @param colmd MetaData for the column
* @return The column
*/
public Column createColumn(JavaTypeMapping mapping, String javaType, ColumnMetaData colmd) {
AbstractMemberMetaData mmd = mapping.getMemberMetaData();
Table tbl = mapping.getTable();
if (colmd == null) {
// If column specified add one (use any column name specified on field element)
colmd = new ColumnMetaData();
if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length == 1) {
colmd.setName(mmd.getColumnMetaData()[0].getName());
}
mmd.addColumn(colmd);
}
Column col;
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
if (colmd.getName() == null) {
// No name specified, so generate the identifier from the field name
DatastoreIdentifier identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
int i = 0;
while (tbl.hasColumn(identifier)) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
i++;
}
colmd.setName(identifier.getName());
col = tbl.addColumn(javaType, identifier, mapping, colmd);
} else {
// User has specified a name, so try to keep this unmodified
col = tbl.addColumn(javaType, idFactory.newColumnIdentifier(colmd.getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true), mapping, colmd);
}
setColumnNullability(mmd, colmd, col);
if (mmd.getNullValue() == NullValue.DEFAULT) {
// Users default should be applied if a null is to be inserted
col.setDefaultable(colmd.getDefaultValue());
}
return col;
}
use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method createColumn.
/**
* Method to create a column for a Java type mapping.
* This is NOT used for persistable mappings - see method below.
* @param mapping Java type mapping for the field
* @param javaType The type of field being stored in this column
* @param datastoreFieldIndex Index of the datastore field to use
* @return The datastore field
*/
public Column createColumn(JavaTypeMapping mapping, String javaType, int datastoreFieldIndex) {
AbstractMemberMetaData mmd = mapping.getMemberMetaData();
FieldRole roleForField = mapping.getRoleForMember();
Table tbl = mapping.getTable();
// Take the column MetaData from the component that this mappings role relates to
ColumnMetaData colmd = null;
ColumnMetaDataContainer columnContainer = mmd;
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT || roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
columnContainer = mmd.getElementMetaData();
} else if (roleForField == FieldRole.ROLE_MAP_KEY) {
columnContainer = mmd.getKeyMetaData();
} else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
columnContainer = mmd.getValueMetaData();
}
Column col;
ColumnMetaData[] colmds;
if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex) {
colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
colmds = columnContainer.getColumnMetaData();
} else {
// If column specified add one (use any column name specified on field element)
colmd = new ColumnMetaData();
if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > datastoreFieldIndex) {
colmd.setName(mmd.getColumnMetaData()[datastoreFieldIndex].getName());
}
if (columnContainer != null) {
columnContainer.addColumn(colmd);
colmds = columnContainer.getColumnMetaData();
} else {
colmds = new ColumnMetaData[1];
colmds[0] = colmd;
}
}
// Generate the column identifier
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier identifier = null;
if (colmd.getName() == null) {
// No name specified, so generate the identifier from the field name
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT) {
// Join table collection element
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
} else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
// Join table array element
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
} else if (roleForField == FieldRole.ROLE_MAP_KEY) {
// Join table map key
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_KEY);
} else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
// Join table map value
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
} else {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
int i = 0;
while (tbl.hasColumn(identifier)) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
i++;
}
}
colmd.setName(identifier.getName());
} else {
// User has specified a name, so try to keep this unmodified
identifier = idFactory.newColumnIdentifier(colmds[datastoreFieldIndex].getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
}
// Create the column
col = tbl.addColumn(javaType, identifier, mapping, colmd);
if (mmd.isPrimaryKey()) {
col.setPrimaryKey();
}
if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
// Embedded so can't be datastore-attributed
} else {
/*if (!mmd.getClassName(true).equals(mmd.getAbstractClassMetaData().getFullClassName()))
{
if (storeMgr.isStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass)
{
if ((mmd.isPrimaryKey() && ((DatastoreClass)tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey())
{
NucleusLogger.GENERAL.info(">> Column addition " + mmd.getFullFieldName() + " IGNORING use of IDENTITY since override of base metadata! See RDBMSMappingManager");
}
}
// Overriding member, so ignore TODO This can be incorrect in many cases
}
else
{*/
if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass) {
if ((mmd.isPrimaryKey() && ((DatastoreClass) tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
// Increment any PK field if we are in base class, and increment any other field
col.setIdentity(true);
}
}
/*}*/
}
if (mmd.getValueForExtension("select-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
}
if (mmd.getValueForExtension("insert-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
}
if (mmd.getValueForExtension("update-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
}
setColumnNullability(mmd, colmd, col);
if (mmd.getNullValue() == NullValue.DEFAULT) {
// Users default should be applied if a null is to be inserted
col.setDefaultable(colmd.getDefaultValue());
}
return col;
}
Aggregations