Cleanup bugfix relating to use of a default schema declared in orm.xml:

- Removed prepending of schema name in PersistenceMappingDefaults.getTableName
  methods as unnecessary.
- Moved storage of default schema name to ClassMappingInfo as that's the only
  place we use it for now.
- Moved setting of default schema name into the more efficient endClassMapping
  method of XMLPersistenceMappingParser.
- Fixed MappingInfo.createTable logic to pass the full table name to 
  SchemaGroup.findTable.  This is important b/c the DynamicSchemaGroup (used
  during the mappingtool's buildSchema action, the default for JPA) dynamically
  adds the table to itself during the findTable operation.  Without the full
  table name it would always add the table to the default schema.



git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@525606 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2007-04-04 21:13:19 +00:00
parent db2a53ff2d
commit 80f7795fef
6 changed files with 53 additions and 75 deletions

View File

@ -54,6 +54,7 @@ public class ClassMappingInfo
private String _className = Object.class.getName();
private String _tableName = null;
private String _schemaName = null;
private boolean _joined = false;
private Map _seconds = null;
private String _subStrat = null;
@ -104,6 +105,20 @@ public class ClassMappingInfo
_tableName = table;
}
/**
* The default schema name for unqualified tables.
*/
public String getSchemaName() {
return _schemaName;
}
/**
* The default schema name for unqualified tables.
*/
public void setSchemaName(String schema) {
_schemaName = schema;
}
/**
* Whether there is a join to the superclass table.
*/
@ -215,7 +230,7 @@ public class ClassMappingInfo
return cls.getMappingRepository().getMappingDefaults().
getTableName(cls, schema);
}
}, null, _tableName, adapt);
}, _schemaName, _tableName, adapt);
}
/**

View File

@ -118,7 +118,8 @@ public class FieldMappingInfo
return null;
Table table = field.getDefiningMapping().getTable();
Schema schema = (table == null) ? null : table.getSchema();
String schemaName = (table == null) ? null
: table.getSchema().getName();
// if we have no join columns defined, there may be class-level join
// information with a more fully-qualified name for our table
@ -134,7 +135,7 @@ public class FieldMappingInfo
return field.getMappingRepository().getMappingDefaults().
getTableName(field, schema);
}
}, schema, tableName, adapt);
}, schemaName, tableName, adapt);
}
/**

View File

@ -70,7 +70,6 @@ public abstract class MappingInfo
private boolean _canFK = true;
private int _join = JOIN_NONE;
private ColumnIO _io = null;
private String _defaultSchemaName = null;
/**
* Mapping strategy name.
@ -423,58 +422,56 @@ public abstract class MappingInfo
*
* @param context the mapping that uses the table
* @param def default table name provider
* @param schema default schema if known, or null
* @param schemaName default schema if known, or null
* @param given given table name
* @param adapt whether we can alter the schema or mappings
*/
public Table createTable(MetaDataContext context, TableDefaults def,
Schema schema, String given, boolean adapt) {
String schemaName, String given, boolean adapt) {
MappingRepository repos = (MappingRepository) context.getRepository();
if (given == null && (def == null || (!adapt
&& !repos.getMappingDefaults().defaultMissingInfo())))
throw new MetaDataException(_loc.get("no-table", context));
if (schemaName == null)
schemaName = Schemas.getNewTableSchema((JDBCConfiguration)
repos.getConfiguration());
// if no given and adapting or defaulting missing info, use template
SchemaGroup group = repos.getSchemaGroup();
String schemaName = null;
Schema schema = null;
if (given == null) {
if (schema == null) {
schemaName = Schemas.getNewTableSchema((JDBCConfiguration)
repos.getConfiguration());
if (StringUtils.isEmpty(schemaName)) {
schemaName = _defaultSchemaName;
}
schema = group.getSchema(schemaName);
if (schema == null)
schema = group.addSchema(schemaName);
}
schema = group.getSchema(schemaName);
if (schema == null)
schema = group.addSchema(schemaName);
given = def.get(schema);
}
// look for named table
Table table = group.findTable(given);
String fullName;
int dotIdx = given.lastIndexOf('.');
if (dotIdx == -1)
fullName = (schemaName == null) ? given : schemaName + "." + given;
else {
fullName = given;
schema = null;
schemaName = given.substring(0, dotIdx);
given = given.substring(dotIdx + 1);
}
// look for named table using full name and findTable, which allows
// the dynamic schema factory to create the table if needed
Table table = group.findTable(fullName);
if (table != null)
return table;
if (!adapt)
throw new MetaDataException(_loc.get("bad-table", given, context));
// named table doesn't exist; figure out what schema to create new
// table in
int dotIdx = given.lastIndexOf('.');
if (dotIdx != -1) {
schema = null;
schemaName = given.substring(0, dotIdx);
given = given.substring(dotIdx + 1);
} else if (schema == null)
schemaName = Schemas.getNewTableSchema((JDBCConfiguration)
repos.getConfiguration());
// named table doesn't exist; create it
if (schema == null) {
schema = group.getSchema(schemaName);
if (schema == null)
schema = group.addSchema(schemaName);
}
table = schema.getTable(given);
if (table == null)
table = schema.addTable(given);
@ -1769,12 +1766,4 @@ public abstract class MappingInfo
public void populate(Table local, Table foreign, Column col,
Object target, boolean inverse, int pos, int cols);
}
public String getDefaultSchemaName() {
return _defaultSchemaName;
}
public void setDefaultSchemaName(String schemaName) {
_defaultSchemaName = schemaName;
}
}

View File

@ -47,7 +47,7 @@ public class FlatClassStrategy
info.assertNoSchemaComponents(cls, true);
if (info.getTableName() != null) {
Table table = info.createTable(cls, null, null,
Table table = info.createTable(cls, null, info.getSchemaName(),
info.getTableName(), false);
if (table != sup.getTable())
throw new MetaDataException(_loc.get("flat-table", cls,

View File

@ -115,30 +115,15 @@ public class PersistenceMappingDefaults
@Override
public String getTableName(ClassMapping cls, Schema schema) {
String name = "";
if (StringUtils.isNotEmpty(schema.getName())) {
name += schema.getName() + '.';
}
if (cls.getTypeAlias() != null)
name += cls.getTypeAlias();
else
name += Strings.getClassName(cls.getDescribedType()).replace('$',
'_');
return name;
return cls.getTypeAlias();
return Strings.getClassName(cls.getDescribedType()).replace('$', '_');
}
@Override
public String getTableName(FieldMapping fm, Schema schema) {
String name = "";
if (StringUtils.isNotEmpty(schema.getName())) {
name += schema.getName() + '.';
}
// base name is table of defining type + '_'
name += fm.getDefiningMapping().getTable().getName() + "_";
String name = fm.getDefiningMapping().getTable().getName() + "_";
// if this is an assocation table, spec says to suffix with table of
// the related type. spec doesn't cover other cases; we're going to

View File

@ -304,6 +304,9 @@ public class XMLPersistenceMappingParser
protected void endClassMapping(ClassMetaData meta)
throws SAXException {
ClassMapping cm = (ClassMapping) meta;
if (_schema != null)
cm.getMappingInfo().setSchemaName(_schema);
if (_supJoinCols != null)
cm.getMappingInfo().setColumns(_supJoinCols);
@ -769,10 +772,9 @@ public class XMLPersistenceMappingParser
private String toTableName(String schema, String table) {
if (StringUtils.isEmpty(table))
return null;
schema = StringUtils.isEmpty(schema) ? _schema : schema;
if (StringUtils.isEmpty(schema))
return table;
return schema + "." + table;
schema = _schema;
return (StringUtils.isEmpty(schema)) ? table : schema + "." + table;
}
/**
@ -911,18 +913,4 @@ public class XMLPersistenceMappingParser
TRUE,
FALSE
}
@Override
protected void endClass(String elem)
throws SAXException {
if (StringUtils.isNotEmpty(_schema)) {
Class cls = classForName(currentClassName());
MetaDataRepository repos = getRepository();
ClassMapping meta = (ClassMapping) repos.getCachedMetaData(cls);
meta.getMappingInfo().setDefaultSchemaName(_schema);
}
super.endClass(elem);
}
}