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 _className = Object.class.getName();
private String _tableName = null; private String _tableName = null;
private String _schemaName = null;
private boolean _joined = false; private boolean _joined = false;
private Map _seconds = null; private Map _seconds = null;
private String _subStrat = null; private String _subStrat = null;
@ -104,6 +105,20 @@ public class ClassMappingInfo
_tableName = table; _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. * Whether there is a join to the superclass table.
*/ */
@ -215,7 +230,7 @@ public class ClassMappingInfo
return cls.getMappingRepository().getMappingDefaults(). return cls.getMappingRepository().getMappingDefaults().
getTableName(cls, schema); getTableName(cls, schema);
} }
}, null, _tableName, adapt); }, _schemaName, _tableName, adapt);
} }
/** /**

View File

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

View File

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

View File

@ -115,30 +115,15 @@ public class PersistenceMappingDefaults
@Override @Override
public String getTableName(ClassMapping cls, Schema schema) { public String getTableName(ClassMapping cls, Schema schema) {
String name = "";
if (StringUtils.isNotEmpty(schema.getName())) {
name += schema.getName() + '.';
}
if (cls.getTypeAlias() != null) if (cls.getTypeAlias() != null)
name += cls.getTypeAlias(); return cls.getTypeAlias();
return Strings.getClassName(cls.getDescribedType()).replace('$', '_');
else
name += Strings.getClassName(cls.getDescribedType()).replace('$',
'_');
return name;
} }
@Override @Override
public String getTableName(FieldMapping fm, Schema schema) { public String getTableName(FieldMapping fm, Schema schema) {
String name = "";
if (StringUtils.isNotEmpty(schema.getName())) {
name += schema.getName() + '.';
}
// base name is table of defining type + '_' // 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 // 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 // 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) protected void endClassMapping(ClassMetaData meta)
throws SAXException { throws SAXException {
ClassMapping cm = (ClassMapping) meta; ClassMapping cm = (ClassMapping) meta;
if (_schema != null)
cm.getMappingInfo().setSchemaName(_schema);
if (_supJoinCols != null) if (_supJoinCols != null)
cm.getMappingInfo().setColumns(_supJoinCols); cm.getMappingInfo().setColumns(_supJoinCols);
@ -769,10 +772,9 @@ public class XMLPersistenceMappingParser
private String toTableName(String schema, String table) { private String toTableName(String schema, String table) {
if (StringUtils.isEmpty(table)) if (StringUtils.isEmpty(table))
return null; return null;
schema = StringUtils.isEmpty(schema) ? _schema : schema;
if (StringUtils.isEmpty(schema)) if (StringUtils.isEmpty(schema))
return table; schema = _schema;
return schema + "." + table; return (StringUtils.isEmpty(schema)) ? table : schema + "." + table;
} }
/** /**
@ -911,18 +913,4 @@ public class XMLPersistenceMappingParser
TRUE, TRUE,
FALSE 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);
}
} }