public class SchemaTool extends Object
DatabaseMetaData
does not include information on these
constraints.Modifier and Type | Class and Description |
---|---|
static class |
SchemaTool.Flags
Run flags.
|
Modifier and Type | Field and Description |
---|---|
protected JDBCConfiguration |
_conf |
protected DBDictionary |
_dict |
protected DataSource |
_ds |
protected boolean |
_fullDB |
protected static Localizer |
_loc |
protected Log |
_log |
protected String |
_scriptToExecute |
protected String |
_sqlTerminator |
static String |
ACTION_ADD |
static String |
ACTION_BUILD |
static String |
ACTION_CREATEDB |
static String |
ACTION_DELETE_TABLE_CONTENTS |
static String |
ACTION_DROP |
static String |
ACTION_DROP_SCHEMA |
static String |
ACTION_DROPDB |
static String |
ACTION_EXECUTE_SCRIPT |
static String |
ACTION_EXPORT |
static String |
ACTION_IMPORT |
static String |
ACTION_REFLECT |
static String |
ACTION_REFRESH |
static String |
ACTION_RETAIN |
static String[] |
ACTIONS |
Constructor and Description |
---|
SchemaTool(JDBCConfiguration conf)
Default constructor.
|
SchemaTool(JDBCConfiguration conf,
String action)
Construct a tool to perform the given action.
|
Modifier and Type | Method and Description |
---|---|
protected void |
add()
Adds any components present in the schema repository but absent from
the database.
|
protected void |
add(SchemaGroup db,
SchemaGroup repos)
Adds all database components in the repository schema that are not
present in the given database schema to the database.
|
boolean |
addColumn(Column col)
Add the given column to the database schema.
|
boolean |
addForeignKey(ForeignKey fk)
Add the given foreign key to the database schema.
|
boolean |
addPrimaryKey(PrimaryKey pk)
Add the given primary key to the database schema.
|
protected SchemaGroup |
assertSchemaGroup() |
protected void |
build()
Re-execute all SQL used for the creation of the current database;
this action is usually used when creating SQL scripts.
|
protected void |
buildSchema(SchemaGroup db,
SchemaGroup repos,
boolean considerDatabaseState) |
void |
clear()
Cleanup DataSource after run()/record()
|
protected void |
createDB()
Re-execute all SQL used for the creation of the current database;
this action is usually used when creating SQL scripts.
|
boolean |
createIndex(Index idx,
Table table)
Add the given index to the database schema.
|
boolean |
createIndex(Index idx,
Table table,
Unique[] uniques) |
boolean |
createSequence(Sequence seq)
Add the given sequence to the database schema.
|
boolean |
createTable(Table table)
Add the given table to the database schema.
|
protected void |
deleteTableContents()
Issue DELETE statement against all known tables.
|
protected void |
drop()
Drops all schema components in the schema repository that also exist
in the database.
|
protected void |
drop(SchemaGroup db,
SchemaGroup repos) |
boolean |
dropColumn(Column col)
Drop the given column from the database schema.
|
protected void |
dropDB()
Drop the current database.
|
boolean |
dropForeignKey(ForeignKey fk)
Drop the given foreign key from the database schema.
|
boolean |
dropIndex(Index idx)
Drop the given index from the database schema.
|
boolean |
dropPrimaryKey(PrimaryKey pk)
Drop the given primary key from the database schema.
|
protected void |
dropSchema()
Drops all schema components in the schema repository.
|
boolean |
dropSequence(Sequence seq)
Drop the given sequence from the database schema.
|
boolean |
dropTable(Table table)
Drop the given table from the database schema.
|
protected void |
dropTables(Collection<Table> tables,
SchemaGroup change)
Remove the given collection of tables from the database schema.
|
protected void |
executeScript() |
protected boolean |
executeSQL(String[] sql)
Executes the given array of non-selecting SQL statements, correctly
logging the SQL calls and optionally ignoring errors.
|
protected ForeignKey |
findForeignKey(Table dbTable,
ForeignKey fk)
Find a foreign key in the given table that matches the given one.
|
protected Index |
findIndex(Table dbTable,
Index idx)
Find an index in the given table that matches the given one.
|
String |
getAction()
The action supplied on construction.
|
SchemaGroup |
getDBSchemaGroup()
Return the database schema.
|
protected SchemaGroup |
getDBSchemaGroup(boolean full)
Return the database schema.
|
boolean |
getDropSequences()
If true, sequences that appear to be unused will be dropped.
|
boolean |
getDropTables()
If true, tables that appear to be unused will be dropped.
|
boolean |
getForeignKeys()
Whether foreign keys on existing tables should be manipulated.
|
boolean |
getIgnoreErrors()
If true, SQLExceptions thrown during schema manipulation will be
printed but ignored.
|
boolean |
getIndexes()
Whether indexes on existing tables should be manipulated.
|
boolean |
getOpenJPATables()
Whether to act on special tables used by OpenJPA components
for bookkeeping.
|
boolean |
getPrimaryKeys()
Whether primary keys on existing tables should be manipulated.
|
boolean |
getRollbackBeforeDDL()
If true, rollback will be performed before each DDL statement is executed.
|
SchemaGroup |
getSchemaGroup()
Return the schema group the tool will act on.
|
boolean |
getSequences()
Whether sequences should be manipulated.
|
Writer |
getWriter()
The stream to write to for the creation of SQL scripts.
|
protected void |
handleException(SQLException sql)
Handle the given exception, logging it and optionally ignoring it,
depending on the flags this SchemaTool was created with.
|
protected boolean |
isDroppable(Sequence seq)
Return true if the sequence is droppable.
|
protected boolean |
isDroppable(Table table)
Return true if the table is droppable.
|
static void |
main(String[] args)
Usage: java org.apache.openjpa.jdbc.schema.SchemaTool [option]*
[-action/-a <add | retain | drop | refresh | createDB | dropDB
| build | reflect | import | export>]
<.schema file or resource>*
Where the following options are recognized.
|
void |
record()
Record the changes made to the DB in the current
SchemaFactory . |
protected void |
refresh()
Adds any components present in the schema repository but absent from
the database, and drops unused database components.
|
protected void |
retain()
Drops database components that are not mentioned in the schema
repository.
|
protected void |
retain(SchemaGroup db,
SchemaGroup repos,
boolean tables,
boolean sequences)
Drops all database components that are in the given database schema
but not in the repository schema.
|
void |
run()
Run the tool action.
|
static boolean |
run(JDBCConfiguration conf,
String[] args,
Options opts)
Run the tool.
|
static boolean |
run(JDBCConfiguration conf,
String[] args,
SchemaTool.Flags flags,
ClassLoader loader)
Run the tool.
|
void |
setDBSchemaGroup(SchemaGroup db)
Set the database schema.
|
void |
setDropSequences(boolean dropSeqs)
If true, sequences that appear to be unused will be dropped.
|
void |
setDropTables(boolean dropTables)
If true, tables that appear to be unused will be dropped.
|
void |
setForeignKeys(boolean fks)
Whether foreign keys on existing tables should be manipulated.
|
void |
setIgnoreErrors(boolean ignoreErrs)
If true, SQLExceptions thrown during schema manipulation will be
printed but ignored.
|
void |
setIndexes(boolean indexes)
Whether indexes on existing tables should be manipulated.
|
void |
setOpenJPATables(boolean openjpaTables)
Whether to act on special tables used by OpenJPA components
for bookkeeping.
|
void |
setPrimaryKeys(boolean pks)
Whether primary keys on existing tables should be manipulated.
|
void |
setRollbackBeforeDDL(boolean rollbackBeforeDDL)
If true, rollback will be performed before each DDL statement is executed.
|
void |
setSchemaGroup(SchemaGroup group)
Set the schema group the tool will act on.
|
void |
setScriptToExecute(String scriptToExecute) |
void |
setSequences(boolean seqs)
Whether sequences should be manipulated.
|
void |
setSQLTerminator(String t) |
void |
setWriter(Writer writer)
The stream to write to for the creation of SQL scripts.
|
public static final String ACTION_ADD
public static final String ACTION_DROP
public static final String ACTION_DROP_SCHEMA
public static final String ACTION_RETAIN
public static final String ACTION_REFRESH
public static final String ACTION_BUILD
public static final String ACTION_REFLECT
public static final String ACTION_CREATEDB
public static final String ACTION_DROPDB
public static final String ACTION_IMPORT
public static final String ACTION_EXPORT
public static final String ACTION_DELETE_TABLE_CONTENTS
public static final String ACTION_EXECUTE_SCRIPT
public static final String[] ACTIONS
protected static final Localizer _loc
protected final JDBCConfiguration _conf
protected final DataSource _ds
protected final Log _log
protected final DBDictionary _dict
protected boolean _fullDB
protected String _sqlTerminator
protected String _scriptToExecute
public SchemaTool(JDBCConfiguration conf)
run()
method will be a no-op.public SchemaTool(JDBCConfiguration conf, String action)
public void clear()
public String getAction()
public boolean getIgnoreErrors()
public void setIgnoreErrors(boolean ignoreErrs)
public boolean getOpenJPATables()
public void setOpenJPATables(boolean openjpaTables)
public boolean getDropTables()
public void setDropTables(boolean dropTables)
public boolean getDropSequences()
public void setDropSequences(boolean dropSeqs)
public boolean getRollbackBeforeDDL()
public void setRollbackBeforeDDL(boolean rollbackBeforeDDL)
public boolean getSequences()
public void setSequences(boolean seqs)
public boolean getIndexes()
public void setIndexes(boolean indexes)
public boolean getForeignKeys()
public void setForeignKeys(boolean fks)
public boolean getPrimaryKeys()
public void setPrimaryKeys(boolean pks)
public Writer getWriter()
public void setWriter(Writer writer)
public void setSQLTerminator(String t)
public void setScriptToExecute(String scriptToExecute)
public SchemaGroup getSchemaGroup()
public void setSchemaGroup(SchemaGroup group)
public void run() throws SQLException
SQLException
protected void add() throws SQLException
SQLException
protected void drop() throws SQLException
SQLException
protected void dropSchema() throws SQLException
SQLException
protected void retain() throws SQLException
SQLException
protected void refresh() throws SQLException
SQLException
protected void createDB() throws SQLException
SQLException
protected void build() throws SQLException
SQLException
protected void dropDB() throws SQLException
SQLException
protected void deleteTableContents() throws SQLException
SQLException
protected void executeScript() throws SQLException
SQLException
public void record()
SchemaFactory
.protected void add(SchemaGroup db, SchemaGroup repos) throws SQLException
SQLException
protected void buildSchema(SchemaGroup db, SchemaGroup repos, boolean considerDatabaseState) throws SQLException
SQLException
protected void retain(SchemaGroup db, SchemaGroup repos, boolean tables, boolean sequences) throws SQLException
SQLException
protected void drop(SchemaGroup db, SchemaGroup repos) throws SQLException
SQLException
protected boolean isDroppable(Table table)
protected boolean isDroppable(Sequence seq)
protected Index findIndex(Table dbTable, Index idx)
protected ForeignKey findForeignKey(Table dbTable, ForeignKey fk)
protected void dropTables(Collection<Table> tables, SchemaGroup change) throws SQLException
SQLException
public boolean createTable(Table table) throws SQLException
SQLException
public boolean dropTable(Table table) throws SQLException
SQLException
public boolean createSequence(Sequence seq) throws SQLException
SQLException
public boolean dropSequence(Sequence seq) throws SQLException
SQLException
public boolean createIndex(Index idx, Table table) throws SQLException
SQLException
public boolean createIndex(Index idx, Table table, Unique[] uniques) throws SQLException
SQLException
public boolean dropIndex(Index idx) throws SQLException
SQLException
public boolean addColumn(Column col) throws SQLException
SQLException
public boolean dropColumn(Column col) throws SQLException
SQLException
public boolean addPrimaryKey(PrimaryKey pk) throws SQLException
SQLException
public boolean dropPrimaryKey(PrimaryKey pk) throws SQLException
SQLException
public boolean addForeignKey(ForeignKey fk) throws SQLException
SQLException
public boolean dropForeignKey(ForeignKey fk) throws SQLException
SQLException
public SchemaGroup getDBSchemaGroup()
public void setDBSchemaGroup(SchemaGroup db)
protected SchemaGroup getDBSchemaGroup(boolean full) throws SQLException
full
- if false, only the tables named in the set schema
repository will be generatedSQLException
protected SchemaGroup assertSchemaGroup()
protected boolean executeSQL(String[] sql) throws SQLException
SQLException
protected void handleException(SQLException sql) throws SQLException
SQLException
public static void main(String[] args) throws IOException, SQLException
JDBCConfiguration
. Optional.JDBCConfiguration
can be set by
using their names and supplying a value. For example:
-licenseKey adslfja83r3lkadf
false
.export
and reflect
actions.reflect
action, whether to reflect on tables with
the name OPENJPA_*
. Under other actions, whether to
drop such tables. Defaults to false
.retain
and refresh
actions. Defaults to
true
.retain
and refresh
actions. Defaults to
true
.false
to prevent writing the schema changes to the
current SchemaFactory
.file
option.file
option.file
option, or to stdout if no file is given.dropTables
.SchemaFactory
.SchemaFactory
's recorded
schema to an XML schema definition file.java org.apache.openjpa.jdbc.schema.SchemaTool -f stdout
-a createDB
java org.apache.openjpa.jdbc.schema.SchemaTool
-a dropDB
java org.apache.openjpa.jdbc.schema.SchemaTool
-a refresh,deleteTableContents
java org.apache.openjpa.jdbc.schema.SchemaTool
myschema.xml
IOException
SQLException
public static boolean run(JDBCConfiguration conf, String[] args, Options opts) throws IOException, SQLException
IOException
SQLException
main(java.lang.String[])
public static boolean run(JDBCConfiguration conf, String[] args, SchemaTool.Flags flags, ClassLoader loader) throws IOException, SQLException
IOException
SQLException
Copyright © 2006–2020 Apache Software Foundation. All rights reserved.