use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHandlerTest method testParseDefaultNamespace.
public void testParseDefaultNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package1.jdo"), "jdo");
assertNotNull(md);
}
use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.
the class PersistenceFileMetaDataHandlerTest method testParseNamespace.
public void testParseNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence2.xml"), "persistence");
assertNotNull(md);
}
use of org.datanucleus.metadata.MetaData in project datanucleus-rdbms by datanucleus.
the class AbstractTable method addColumn.
/**
* Creates a new column in the table.
* Will add the new Column and return it. If the new column clashes in name with an existing column of the
* required name will throw a DuplicateColumnNameException except when :-
* <ul>
* <li>The 2 columns are for same named fields in the class or its subclasses with the subclass(es) using
* "superclass-table" inheritance strategy. One of the columns has to come from a subclass - cant have
* both from the same class.</li>
* </ul>
* @param storedJavaType the java type of the datastore field
* @param name the SQL identifier for the column to be added
* @param mapping the mapping for the column to be added
* @param colmd ColumnMetaData for the column to be added to the table
* @return the new Column
* @throws DuplicateColumnException if a column already exists with same name and not a supported situation.
*/
public synchronized Column addColumn(String storedJavaType, DatastoreIdentifier name, JavaTypeMapping mapping, ColumnMetaData colmd) {
// TODO If already initialized and this is called we should check if exists in current representation
// then check if exists in datastore, and then create it if necessary
boolean duplicateName = false;
if (hasColumnName(name)) {
duplicateName = true;
}
// Create the column
Column col = new ColumnImpl(this, storedJavaType, name, colmd);
if (duplicateName && colmd != null) {
// Verify if a duplicate column is valid. A duplicate column name is (currently) valid when :-
// 1. subclasses defining the duplicated column are using "super class table" strategy
//
// Find the MetaData for the existing column
Column existingCol = columnsByIdentifier.get(name);
MetaData md = existingCol.getColumnMetaData().getParent();
while (!(md instanceof AbstractClassMetaData)) {
if (md == null) {
// ColumnMetaData for existing column has no parent class somehow!
throw new NucleusUserException(Localiser.msg("057043", name.getName(), getDatastoreIdentifierFullyQualified()));
}
md = md.getParent();
}
// Find the MetaData for the column to be added
MetaData dupMd = colmd.getParent();
while (!(dupMd instanceof AbstractClassMetaData)) {
dupMd = dupMd.getParent();
if (dupMd == null) {
// ColumnMetaData for required column has no parent class somehow!
throw new NucleusUserException(Localiser.msg("057044", name.getName(), getDatastoreIdentifierFullyQualified(), colmd.toString()));
}
}
boolean reuseColumns = storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_ALLOW_COLUMN_REUSE);
if (!reuseColumns) {
if (((AbstractClassMetaData) md).getFullClassName().equals(((AbstractClassMetaData) dupMd).getFullClassName())) {
// much sense in most of the cases. (this whole block of duplicated column check, could be optional, like a pmf property)
throw new DuplicateColumnException(this.toString(), existingCol, col);
}
// Make sure the field JavaTypeMappings are compatible
if (mapping != null && !mapping.getClass().isAssignableFrom(existingCol.getJavaTypeMapping().getClass()) && !existingCol.getJavaTypeMapping().getClass().isAssignableFrom(mapping.getClass())) {
// the mapping class must be the same (not really required, but to avoid user mistakes)
throw new DuplicateColumnException(this.toString(), existingCol, col);
}
} else {
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
if (mapping != null && mapping.getMemberMetaData() != null) {
NucleusLogger.DATASTORE_SCHEMA.debug("Column " + existingCol + " has already been defined but needing to reuse it for " + mapping.getMemberMetaData().getFullFieldName());
} else {
NucleusLogger.DATASTORE_SCHEMA.debug("Column " + existingCol + " has already been defined but needing to reuse it");
}
}
}
// Make sure the field java types are compatible
Class fieldStoredJavaTypeClass = null;
Class existingColStoredJavaTypeClass = null;
try {
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
fieldStoredJavaTypeClass = clr.classForName(storedJavaType);
existingColStoredJavaTypeClass = clr.classForName(col.getStoredJavaType());
} catch (RuntimeException cnfe) {
// Do nothing
}
if (fieldStoredJavaTypeClass != null && existingColStoredJavaTypeClass != null && !fieldStoredJavaTypeClass.isAssignableFrom(existingColStoredJavaTypeClass) && !existingColStoredJavaTypeClass.isAssignableFrom(fieldStoredJavaTypeClass)) {
// the stored java type must be the same (not really required, but to avoid user mistakes)
throw new DuplicateColumnException(this.toString(), existingCol, col);
}
}
if (!duplicateName) {
// Only add to our internal list when it is not a dup (since it would try to create a table with the same col twice)
addColumnInternal(col);
}
if (isInitialized()) {
// Set state to modified
state = TABLE_STATE_INITIALIZED_MODIFIED;
}
return col;
}
use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHandlerTest method testParseNamespace.
public void testParseNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), "jdo");
assertNotNull(md);
}
use of org.datanucleus.metadata.MetaData in project datanucleus-api-jdo by datanucleus.
the class PersistenceFileMetaDataHandlerTest method testParseDefaultNamespace.
public void testParseDefaultNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence1.xml"), "persistence");
assertNotNull(md);
}
Aggregations