mirror of https://github.com/apache/openjpa.git
OPENJPA-2275 - Enhancements for SchemaTool extension. Contributed by Francesco Chicchiriccò.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1396145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
94969374f6
commit
4ca0cdc92c
|
@ -43,8 +43,6 @@ import org.apache.openjpa.jdbc.identifier.DBIdentifier;
|
||||||
import org.apache.openjpa.jdbc.sql.DBDictionary;
|
import org.apache.openjpa.jdbc.sql.DBDictionary;
|
||||||
import org.apache.openjpa.jdbc.sql.SQLExceptions;
|
import org.apache.openjpa.jdbc.sql.SQLExceptions;
|
||||||
import org.apache.openjpa.lib.conf.Configurations;
|
import org.apache.openjpa.lib.conf.Configurations;
|
||||||
import org.apache.openjpa.lib.identifier.IdentifierUtil;
|
|
||||||
import org.apache.openjpa.lib.jdbc.DecoratingDataSource;
|
|
||||||
import org.apache.openjpa.lib.jdbc.DelegatingDataSource;
|
import org.apache.openjpa.lib.jdbc.DelegatingDataSource;
|
||||||
import org.apache.openjpa.lib.log.Log;
|
import org.apache.openjpa.lib.log.Log;
|
||||||
import org.apache.openjpa.lib.meta.MetaDataSerializer;
|
import org.apache.openjpa.lib.meta.MetaDataSerializer;
|
||||||
|
@ -90,12 +88,12 @@ public class SchemaTool {
|
||||||
ACTION_DELETE_TABLE_CONTENTS,
|
ACTION_DELETE_TABLE_CONTENTS,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final Localizer _loc = Localizer.forPackage(SchemaTool.class);
|
protected static final Localizer _loc = Localizer.forPackage(SchemaTool.class);
|
||||||
|
|
||||||
private final JDBCConfiguration _conf;
|
protected final JDBCConfiguration _conf;
|
||||||
private final DataSource _ds;
|
protected final DataSource _ds;
|
||||||
private final Log _log;
|
protected final Log _log;
|
||||||
private final DBDictionary _dict;
|
protected final DBDictionary _dict;
|
||||||
private final String _action;
|
private final String _action;
|
||||||
private boolean _ignoreErrs = false;
|
private boolean _ignoreErrs = false;
|
||||||
private boolean _openjpaTables = false;
|
private boolean _openjpaTables = false;
|
||||||
|
@ -108,8 +106,8 @@ public class SchemaTool {
|
||||||
private PrintWriter _writer = null;
|
private PrintWriter _writer = null;
|
||||||
private SchemaGroup _group = null;
|
private SchemaGroup _group = null;
|
||||||
private SchemaGroup _db = null;
|
private SchemaGroup _db = null;
|
||||||
private boolean _fullDB = false;
|
protected boolean _fullDB = false;
|
||||||
private String _sqlTerminator = ";";
|
protected String _sqlTerminator = ";";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor. Tools constructed this way will not have an
|
* Default constructor. Tools constructed this way will not have an
|
||||||
|
@ -359,27 +357,27 @@ public class SchemaTool {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds any components present in the schema repository but absent from
|
* Adds any components present in the schema repository but absent from
|
||||||
* the database. Package-private for testing.
|
* the database.
|
||||||
*/
|
*/
|
||||||
void add()
|
protected void add()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
add(getDBSchemaGroup(false), assertSchemaGroup());
|
add(getDBSchemaGroup(false), assertSchemaGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drops all schema components in the schema repository that also exist
|
* Drops all schema components in the schema repository that also exist
|
||||||
* in the database. Package-private for testing.
|
* in the database.
|
||||||
*/
|
*/
|
||||||
void drop()
|
protected void drop()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
drop(getDBSchemaGroup(false), assertSchemaGroup());
|
drop(getDBSchemaGroup(false), assertSchemaGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drops database components that are not mentioned in the schema
|
* Drops database components that are not mentioned in the schema
|
||||||
* repository. Package-private for testing.
|
* repository.
|
||||||
*/
|
*/
|
||||||
void retain()
|
protected void retain()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
retain(getDBSchemaGroup(true), assertSchemaGroup(),
|
retain(getDBSchemaGroup(true), assertSchemaGroup(),
|
||||||
getDropTables(), getDropSequences());
|
getDropTables(), getDropSequences());
|
||||||
|
@ -388,9 +386,8 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Adds any components present in the schema repository but absent from
|
* Adds any components present in the schema repository but absent from
|
||||||
* the database, and drops unused database components.
|
* the database, and drops unused database components.
|
||||||
* Package-private for testing.
|
|
||||||
*/
|
*/
|
||||||
void refresh()
|
protected void refresh()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
SchemaGroup local = assertSchemaGroup();
|
SchemaGroup local = assertSchemaGroup();
|
||||||
SchemaGroup db = getDBSchemaGroup(true);
|
SchemaGroup db = getDBSchemaGroup(true);
|
||||||
|
@ -401,9 +398,8 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Re-execute all SQL used for the creation of the current database;
|
* Re-execute all SQL used for the creation of the current database;
|
||||||
* this action is usually used when creating SQL scripts.
|
* this action is usually used when creating SQL scripts.
|
||||||
* Package-private for testing.
|
|
||||||
*/
|
*/
|
||||||
void createDB()
|
protected void createDB()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
SchemaGroup group = new SchemaGroup();
|
SchemaGroup group = new SchemaGroup();
|
||||||
group.addSchema();
|
group.addSchema();
|
||||||
|
@ -413,9 +409,8 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Re-execute all SQL used for the creation of the current database;
|
* Re-execute all SQL used for the creation of the current database;
|
||||||
* this action is usually used when creating SQL scripts.
|
* this action is usually used when creating SQL scripts.
|
||||||
* Package-private for testing.
|
|
||||||
*/
|
*/
|
||||||
void build()
|
protected void build()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
SchemaGroup group = new SchemaGroup();
|
SchemaGroup group = new SchemaGroup();
|
||||||
group.addSchema();
|
group.addSchema();
|
||||||
|
@ -423,9 +418,9 @@ public class SchemaTool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop the current database. Package-private for testing.
|
* Drop the current database.
|
||||||
*/
|
*/
|
||||||
void dropDB()
|
protected void dropDB()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
retain(getDBSchemaGroup(true), new SchemaGroup(), true, true);
|
retain(getDBSchemaGroup(true), new SchemaGroup(), true, true);
|
||||||
}
|
}
|
||||||
|
@ -433,7 +428,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Issue DELETE statement against all known tables.
|
* Issue DELETE statement against all known tables.
|
||||||
*/
|
*/
|
||||||
private void deleteTableContents()
|
protected void deleteTableContents()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
SchemaGroup group = getSchemaGroup();
|
SchemaGroup group = getSchemaGroup();
|
||||||
Schema[] schemas = group.getSchemas();
|
Schema[] schemas = group.getSchemas();
|
||||||
|
@ -462,7 +457,7 @@ public class SchemaTool {
|
||||||
* Adds all database components in the repository schema that are not
|
* Adds all database components in the repository schema that are not
|
||||||
* present in the given database schema to the database.
|
* present in the given database schema to the database.
|
||||||
*/
|
*/
|
||||||
private void add(SchemaGroup db, SchemaGroup repos)
|
protected void add(SchemaGroup db, SchemaGroup repos)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
// add sequences
|
// add sequences
|
||||||
Schema[] schemas = repos.getSchemas();
|
Schema[] schemas = repos.getSchemas();
|
||||||
|
@ -643,7 +638,7 @@ public class SchemaTool {
|
||||||
* Drops all database components that are in the given database schema
|
* Drops all database components that are in the given database schema
|
||||||
* but not in the repository schema.
|
* but not in the repository schema.
|
||||||
*/
|
*/
|
||||||
private void retain(SchemaGroup db, SchemaGroup repos, boolean tables,
|
protected void retain(SchemaGroup db, SchemaGroup repos, boolean tables,
|
||||||
boolean sequences)
|
boolean sequences)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
Schema[] schemas = db.getSchemas();
|
Schema[] schemas = db.getSchemas();
|
||||||
|
@ -769,7 +764,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Drops all database components in the given repository schema.
|
* Drops all database components in the given repository schema.
|
||||||
*/
|
*/
|
||||||
private void drop(SchemaGroup db, SchemaGroup repos)
|
protected void drop(SchemaGroup db, SchemaGroup repos)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
// drop sequences
|
// drop sequences
|
||||||
Schema[] schemas = repos.getSchemas();
|
Schema[] schemas = repos.getSchemas();
|
||||||
|
@ -902,7 +897,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Return true if the table is droppable.
|
* Return true if the table is droppable.
|
||||||
*/
|
*/
|
||||||
private boolean isDroppable(Table table) {
|
protected boolean isDroppable(Table table) {
|
||||||
return _openjpaTables
|
return _openjpaTables
|
||||||
|| (!DBIdentifier.toUpper(table.getIdentifier()).getName().startsWith("OPENJPA_")
|
|| (!DBIdentifier.toUpper(table.getIdentifier()).getName().startsWith("OPENJPA_")
|
||||||
&& !DBIdentifier.toUpper(table.getIdentifier()).getName().startsWith("JDO_")); // legacy
|
&& !DBIdentifier.toUpper(table.getIdentifier()).getName().startsWith("JDO_")); // legacy
|
||||||
|
@ -911,7 +906,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Return true if the sequence is droppable.
|
* Return true if the sequence is droppable.
|
||||||
*/
|
*/
|
||||||
private boolean isDroppable(Sequence seq) {
|
protected boolean isDroppable(Sequence seq) {
|
||||||
return _openjpaTables
|
return _openjpaTables
|
||||||
|| (!DBIdentifier.toUpper(seq.getIdentifier()).getName().startsWith("OPENJPA_")
|
|| (!DBIdentifier.toUpper(seq.getIdentifier()).getName().startsWith("OPENJPA_")
|
||||||
&& !DBIdentifier.toUpper(seq.getIdentifier()).getName().startsWith("JDO_")); // legacy
|
&& !DBIdentifier.toUpper(seq.getIdentifier()).getName().startsWith("JDO_")); // legacy
|
||||||
|
@ -920,7 +915,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Find an index in the given table that matches the given one.
|
* Find an index in the given table that matches the given one.
|
||||||
*/
|
*/
|
||||||
private Index findIndex(Table dbTable, Index idx) {
|
protected Index findIndex(Table dbTable, Index idx) {
|
||||||
Index[] idxs = dbTable.getIndexes();
|
Index[] idxs = dbTable.getIndexes();
|
||||||
for (int i = 0; i < idxs.length; i++)
|
for (int i = 0; i < idxs.length; i++)
|
||||||
if (idx.columnsMatch(idxs[i].getColumns()))
|
if (idx.columnsMatch(idxs[i].getColumns()))
|
||||||
|
@ -931,7 +926,7 @@ public class SchemaTool {
|
||||||
/**
|
/**
|
||||||
* Find a foreign key in the given table that matches the given one.
|
* Find a foreign key in the given table that matches the given one.
|
||||||
*/
|
*/
|
||||||
private ForeignKey findForeignKey(Table dbTable, ForeignKey fk) {
|
protected ForeignKey findForeignKey(Table dbTable, ForeignKey fk) {
|
||||||
if (fk.getConstantColumns().length > 0
|
if (fk.getConstantColumns().length > 0
|
||||||
|| fk.getConstantPrimaryKeyColumns().length > 0)
|
|| fk.getConstantPrimaryKeyColumns().length > 0)
|
||||||
return null;
|
return null;
|
||||||
|
@ -947,7 +942,7 @@ public class SchemaTool {
|
||||||
* Remove the given collection of tables from the database schema. Orders
|
* Remove the given collection of tables from the database schema. Orders
|
||||||
* the removals according to foreign key constraints on the tables.
|
* the removals according to foreign key constraints on the tables.
|
||||||
*/
|
*/
|
||||||
private void dropTables(Collection<Table> tables, SchemaGroup change)
|
protected void dropTables(Collection<Table> tables, SchemaGroup change)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
if (tables.isEmpty())
|
if (tables.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
@ -1133,7 +1128,7 @@ public class SchemaTool {
|
||||||
* @param full if false, only the tables named in the set schema
|
* @param full if false, only the tables named in the set schema
|
||||||
* repository will be generated
|
* repository will be generated
|
||||||
*/
|
*/
|
||||||
private SchemaGroup getDBSchemaGroup(boolean full)
|
protected SchemaGroup getDBSchemaGroup(boolean full)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
if (_db == null || (full && !_fullDB)) {
|
if (_db == null || (full && !_fullDB)) {
|
||||||
SchemaGenerator gen = new SchemaGenerator(_conf);
|
SchemaGenerator gen = new SchemaGenerator(_conf);
|
||||||
|
@ -1171,7 +1166,7 @@ public class SchemaTool {
|
||||||
return _db;
|
return _db;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SchemaGroup assertSchemaGroup() {
|
protected SchemaGroup assertSchemaGroup() {
|
||||||
SchemaGroup local = getSchemaGroup();
|
SchemaGroup local = getSchemaGroup();
|
||||||
if (local == null)
|
if (local == null)
|
||||||
throw new InvalidStateException(_loc.get("tool-norepos"));
|
throw new InvalidStateException(_loc.get("tool-norepos"));
|
||||||
|
@ -1189,7 +1184,7 @@ public class SchemaTool {
|
||||||
* @return true if there was SQL to execute and the calls were
|
* @return true if there was SQL to execute and the calls were
|
||||||
* successful, false otherwise
|
* successful, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean executeSQL(String[] sql)
|
protected boolean executeSQL(String[] sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
// if no sql, probably b/c dictionary doesn't support operation
|
// if no sql, probably b/c dictionary doesn't support operation
|
||||||
if (sql.length == 0)
|
if (sql.length == 0)
|
||||||
|
@ -1265,7 +1260,7 @@ public class SchemaTool {
|
||||||
* Handle the given exception, logging it and optionally ignoring it,
|
* Handle the given exception, logging it and optionally ignoring it,
|
||||||
* depending on the flags this SchemaTool was created with.
|
* depending on the flags this SchemaTool was created with.
|
||||||
*/
|
*/
|
||||||
private void handleException(SQLException sql)
|
protected void handleException(SQLException sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
if (!_ignoreErrs)
|
if (!_ignoreErrs)
|
||||||
throw sql;
|
throw sql;
|
||||||
|
|
Loading…
Reference in New Issue