| Method from org.apache.openjpa.jdbc.schema.SchemaTool Detail: |
void add() throws SQLException {
add(getDBSchemaGroup(false), assertSchemaGroup());
}
Adds any components present in the schema repository but absent from
the database. Package-private for testing. |
public boolean addColumn(Column col) throws SQLException {
return executeSQL(_dict.getAddColumnSQL(col));
}
Add the given column to the database schema. |
public boolean addForeignKey(ForeignKey fk) throws SQLException {
return executeSQL(_dict.getAddForeignKeySQL(fk));
}
Add the given foreign key to the database schema. |
public boolean addPrimaryKey(PrimaryKey pk) throws SQLException {
return executeSQL(_dict.getAddPrimaryKeySQL(pk));
}
Add the given primary key to the database schema. |
void build() throws SQLException {
SchemaGroup group = new SchemaGroup();
group.addSchema();
add(group, assertSchemaGroup());
}
Re-execute all SQL used for the creation of the current database;
this action is usually used when creating SQL scripts.
Package-private for testing. |
void createDB() throws SQLException {
SchemaGroup group = new SchemaGroup();
group.addSchema();
add(group, getDBSchemaGroup(true));
}
Re-execute all SQL used for the creation of the current database;
this action is usually used when creating SQL scripts.
Package-private for testing. |
public boolean createIndex(Index idx,
Table table) throws SQLException {
int max = _dict.maxIndexesPerTable;
int len = table.getIndexes().length;
if (table.getPrimaryKey() != null)
len += table.getPrimaryKey().getColumns().length;
if (len >= max) {
_log.warn(_loc.get("too-many-indexes", idx, table, max + ""));
return false;
}
return executeSQL(_dict.getCreateIndexSQL(idx));
}
Add the given index to the database schema. |
public boolean createSequence(Sequence seq) throws SQLException {
return executeSQL(_dict.getCreateSequenceSQL(seq));
}
Add the given sequence to the database schema. |
public boolean createTable(Table table) throws SQLException {
return executeSQL(_dict.getCreateTableSQL(table));
}
Add the given table to the database schema. |
void drop() throws SQLException {
drop(getDBSchemaGroup(false), assertSchemaGroup());
}
Drops all schema components in the schema repository that also exist
in the database. Package-private for testing. |
public boolean dropColumn(Column col) throws SQLException {
return executeSQL(_dict.getDropColumnSQL(col));
}
Drop the given column from the database schema. |
void dropDB() throws SQLException {
retain(getDBSchemaGroup(true), new SchemaGroup(), true, true);
}
Drop the current database. Package-private for testing. |
public boolean dropForeignKey(ForeignKey fk) throws SQLException {
return executeSQL(_dict.getDropForeignKeySQL(fk));
}
Drop the given foreign key from the database schema. |
public boolean dropIndex(Index idx) throws SQLException {
return executeSQL(_dict.getDropIndexSQL(idx));
}
Drop the given index from the database schema. |
public boolean dropPrimaryKey(PrimaryKey pk) throws SQLException {
return executeSQL(_dict.getDropPrimaryKeySQL(pk));
}
Drop the given primary key from the database schema. |
public boolean dropSequence(Sequence seq) throws SQLException {
return executeSQL(_dict.getDropSequenceSQL(seq));
}
Drop the given sequence from the database schema. |
public boolean dropTable(Table table) throws SQLException {
return executeSQL(_dict.getDropTableSQL(table));
}
Drop the given table from the database schema. |
public String getAction() {
return _action;
}
The action supplied on construction. |
public SchemaGroup getDBSchemaGroup() {
try {
return getDBSchemaGroup(true);
} catch (SQLException se) {
throw SQLExceptions.getStore(se, _dict);
}
}
Return the database schema. |
public boolean getDropSequences() {
return _dropSeqs;
}
If true, sequences that appear to be unused will be dropped. Defaults
to true. |
public boolean getDropTables() {
return _dropTables;
}
If true, tables that appear to be unused will be dropped. Defaults to
true. |
public boolean getForeignKeys() {
return _fks;
}
Whether foreign keys on existing tables should be manipulated.
Defaults to true. |
public boolean getIgnoreErrors() {
return _ignoreErrs;
}
If true, SQLExceptions thrown during schema manipulation will be
printed but ignored. |
public boolean getIndexes() {
return _indexes;
}
Whether indexes on existing tables should be manipulated.
Defaults to true. |
public boolean getOpenJPATables() {
return _openjpaTables;
}
Whether to act on special tables used by OpenJPA components
for bookkeeping. |
public boolean getPrimaryKeys() {
return _pks;
}
Whether primary keys on existing tables should be manipulated.
Defaults to true. |
public SchemaGroup getSchemaGroup() {
return _group;
}
Return the schema group the tool will act on. |
public boolean getSequences() {
return _seqs;
}
Whether sequences should be manipulated. Defaults to true. |
public Writer getWriter() {
return _writer;
}
The stream to write to for the creation of SQL scripts. If the
stream is non-null, all SQL will be written to this stream rather than
executed against the database. |
public static void main(String[] args) throws IOException, SQLException {
Options opts = new Options();
final String[] arguments = opts.setFromCmdLine(args);
boolean ret = Configurations.runAgainstAllAnchors(opts,
new Configurations.Runnable() {
public boolean run(Options opts) throws Exception {
JDBCConfiguration conf = new JDBCConfigurationImpl();
try {
return SchemaTool.run(conf, arguments, opts);
} finally {
conf.close();
}
}
});
if (!ret)
System.out.println(_loc.get("tool-usage"));
}
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.
- -properties/-p <properties file or resource>: The
path or resource name of a OpenJPA properties file containing
information such as the license key and connection data as
outlined in JDBCConfiguration . Optional.
- -<property name> <property value>: All bean
properties of the OpenJPA JDBCConfiguration can be set by
using their names and supplying a value. For example:
-licenseKey adslfja83r3lkadf
- -ignoreErrors/-i <true/t | false/f>: If false, an
exception will will be thrown if the tool encounters any database
exceptions; defaults to
false.
- -file/-f <stdout | output file or resource>: Use this
option to write a SQL script for the planned schema modifications,
rather than committing them to the database. This option also
applies to the
export and reflect actions.
- -openjpaTables/-kt <true/t | false/f>: Under the
reflect action, whether to reflect on tables with
the name OPENJPA_*. Under other actions, whether to
drop such tables. Defaults to false.
- -dropTables/-dt <true/t | false/f>: Set this option to
true to drop tables that appear to be unused during
retain and refresh actions. Defaults to
true.
- -dropSequences/-dsq <true/t | false/f>: Set this option
to true to drop sequences that appear to be unused during
retain and refresh actions. Defaults to
true.
- -primaryKeys/-pk <true/t | false/f>: Whether primary
keys on existing tables are manipulated. Defaults to true.
- -foreignKeys/-fk <true/t | false/f>: Whether foreign
keys on existing tables are manipulated. Defaults to true.
- -indexes/-ix <true/t | false/f>: Whether indexes
on existing tables are manipulated. Defaults to true.
- -sequences/-sq <true/t | false/f>: Whether to
manipulate sequences. Defaults to true.
- -record/-r <true/t | false/f>: Set this option to
false to prevent writing the schema changes to the
current SchemaFactory .
Actions can be composed in a comma-separated list. The various actions
are as follows.
- add: Bring the schema up-to-date with the latest
changes to the schema XML data by adding tables, columns,
indexes, etc. This action never drops any data. This is the
default action.
- retain: Keep all schema components in the schema XML, but
drop the rest from the database. This action never adds any data.
- drop: Drop all the schema components in the schema XML.
- refresh: Equivalent to retain, then add.
- createDB: Execute SQL to re-create the current database.
This action is typically used in conjuction with the
file option.
- build: Execute SQL to build the schema defined in the XML.
Because it doesn't take the current database schema into account,
this action is typically used in conjuction with the
file option.
- reflect: Reflect on the current database schema. Write the
schema's XML representation to the file specified with the
file option, or to stdout if no file is given.
- dropDB: Execute SQL to drop the current database. This
action implies
dropTables.
- deleteTableContents: Execute SQL to delete all rows from
all tables that OpenJPA knows about.
- import: Import the given XML schema definition into the
current SchemaFactory .
- export: Export the current SchemaFactory 's recorded
schema to an XML schema definition file.
Examples:
- Write a script to stdout to re-create the current database
schema:
java org.apache.openjpa.jdbc.schema.SchemaTool -f stdout
-a createDB
- Drop the current database schema:
java org.apache.openjpa.jdbc.schema.SchemaTool
-a dropDB
- Refresh the schema and delete all records in all tables:
java org.apache.openjpa.jdbc.schema.SchemaTool
-a refresh,deleteTableContents
- Create a schema based on an XML schema definition file:
java org.apache.openjpa.jdbc.schema.SchemaTool
myschema.xml
|
public void record() {
if (_db != null && _writer == null)
_conf.getSchemaFactoryInstance().storeSchema(_db);
}
|
void refresh() throws SQLException {
SchemaGroup local = assertSchemaGroup();
SchemaGroup db = getDBSchemaGroup(true);
retain(db, local, getDropTables(), getDropSequences());
add(db, local);
}
Adds any components present in the schema repository but absent from
the database, and drops unused database components.
Package-private for testing. |
void retain() throws SQLException {
retain(getDBSchemaGroup(true), assertSchemaGroup(),
getDropTables(), getDropSequences());
}
Drops database components that are not mentioned in the schema
repository. Package-private for testing. |
public void run() throws SQLException {
if (_action == null)
return;
if (ACTION_ADD.equals(_action))
add();
else if (ACTION_DROP.equals(_action))
drop();
else if (ACTION_RETAIN.equals(_action))
retain();
else if (ACTION_REFRESH.equals(_action))
refresh();
else if (ACTION_BUILD.equals(_action))
build();
else if (ACTION_CREATEDB.equals(_action))
createDB();
else if (ACTION_DROPDB.equals(_action))
dropDB();
else if (ACTION_DELETE_TABLE_CONTENTS.equals(_action))
deleteTableContents();
}
|
public static boolean run(JDBCConfiguration conf,
String[] args,
Options opts) throws IOException, SQLException {
Flags flags = new Flags();
flags.dropTables = opts.removeBooleanProperty
("dropTables", "dt", flags.dropTables);
flags.dropSequences = opts.removeBooleanProperty
("dropSequences", "dsq", flags.dropSequences);
flags.ignoreErrors = opts.removeBooleanProperty
("ignoreErrors", "i", flags.ignoreErrors);
flags.openjpaTables = opts.removeBooleanProperty
("openjpaTables", "ot", flags.openjpaTables);
flags.primaryKeys = opts.removeBooleanProperty
("primaryKeys", "pk", flags.primaryKeys);
flags.foreignKeys = opts.removeBooleanProperty
("foreignKeys", "fks", flags.foreignKeys);
flags.indexes = opts.removeBooleanProperty
("indexes", "ix", flags.indexes);
flags.sequences = opts.removeBooleanProperty
("sequences", "sq", flags.sequences);
flags.record = opts.removeBooleanProperty("record", "r", flags.record);
String fileName = opts.removeProperty("file", "f", null);
String schemas = opts.removeProperty("s");
if (schemas != null)
opts.setProperty("schemas", schemas);
String[] actions = opts.removeProperty("action", "a", flags.action)
.split(",");
// setup a configuration instance with cmd-line info
Configurations.populateConfiguration(conf, opts);
// create script writer
ClassLoader loader = conf.getClassResolverInstance().
getClassLoader(SchemaTool.class, null);
flags.writer = Files.getWriter(fileName, loader);
boolean returnValue = true;
for (int i = 0; i < actions.length; i++) {
flags.action = actions[i];
returnValue &= run(conf, args, flags, loader);
}
return returnValue;
}
Run the tool. Returns false if any invalid options were given. |
public static boolean run(JDBCConfiguration conf,
String[] args,
SchemaTool.Flags flags,
ClassLoader loader) throws IOException, SQLException {
Log log = conf.getLog(OpenJPAConfiguration.LOG_TOOL);
if (ACTION_REFLECT.equals(flags.action)) {
if (args.length > 0)
return false;
if (flags.writer == null)
flags.writer = new PrintWriter(System.out);
SchemaGenerator gen = new SchemaGenerator(conf);
gen.setPrimaryKeys(flags.primaryKeys);
gen.setIndexes(flags.indexes);
gen.setForeignKeys(flags.foreignKeys);
gen.setSequences(flags.sequences);
gen.setOpenJPATables(flags.openjpaTables);
String schemas = conf.getSchemas();
if (StringUtils.isEmpty(schemas))
schemas = "all";
log.info(_loc.get("sch-reflect", schemas));
gen.generateSchemas();
// record the schema
log.info(_loc.get("sch-reflect-write"));
SchemaSerializer ser = new XMLSchemaSerializer(conf);
ser.addAll(gen.getSchemaGroup());
ser.serialize(flags.writer, ser.PRETTY);
return true;
}
if (args.length == 0
&& !ACTION_CREATEDB.equals(flags.action)
&& !ACTION_DROPDB.equals(flags.action)
&& !ACTION_EXPORT.equals(flags.action)
&& !ACTION_DELETE_TABLE_CONTENTS.equals(flags.action))
return false;
// parse in the arguments
SchemaParser parser = new XMLSchemaParser(conf);
parser.setDelayConstraintResolve(true);
File file;
for (int i = 0; i < args.length; i++) {
file = Files.getFile(args[i], loader);
log.info(_loc.get("tool-running", file));
parser.parse(file);
}
parser.resolveConstraints();
if (ACTION_IMPORT.equals(flags.action)) {
log.info(_loc.get("tool-import-store"));
SchemaGroup schema = parser.getSchemaGroup();
conf.getSchemaFactoryInstance().storeSchema(schema);
return true;
}
if (ACTION_EXPORT.equals(flags.action)) {
if (flags.writer == null)
flags.writer = new PrintWriter(System.out);
log.info(_loc.get("tool-export-gen"));
SchemaGroup schema = conf.getSchemaFactoryInstance().readSchema();
log.info(_loc.get("tool-export-write"));
SchemaSerializer ser = new XMLSchemaSerializer(conf);
ser.addAll(schema);
ser.serialize(flags.writer, ser.PRETTY);
return true;
}
SchemaTool tool = new SchemaTool(conf, flags.action);
tool.setIgnoreErrors(flags.ignoreErrors);
tool.setDropTables(flags.dropTables);
tool.setSequences(flags.sequences); // set before dropseqs
tool.setDropSequences(flags.dropSequences);
tool.setPrimaryKeys(flags.primaryKeys);
tool.setForeignKeys(flags.foreignKeys);
tool.setIndexes(flags.indexes);
tool.setOpenJPATables(flags.openjpaTables);
if (args.length > 0)
tool.setSchemaGroup(parser.getSchemaGroup());
if (flags.writer != null)
tool.setWriter(flags.writer);
log.info(_loc.get("tool-action", flags.action));
try {
tool.run();
} finally {
if (flags.record) {
log.info(_loc.get("tool-record"));
tool.record();
}
}
if (flags.writer != null)
flags.writer.flush();
return true;
}
Run the tool. Return false if invalid options were given. |
public void setDBSchemaGroup(SchemaGroup db) {
_db = db;
if (db != null)
_fullDB = true;
}
|
public void setDropSequences(boolean dropSeqs) {
_dropSeqs = dropSeqs;
if (dropSeqs)
setSequences(true);
}
If true, sequences that appear to be unused will be dropped. Defaults
to true. |
public void setDropTables(boolean dropTables) {
_dropTables = dropTables;
}
If true, tables that appear to be unused will be dropped. Defaults to
true. |
public void setForeignKeys(boolean fks) {
_fks = fks;
}
Whether foreign keys on existing tables should be manipulated.
Defaults to true. |
public void setIgnoreErrors(boolean ignoreErrs) {
_ignoreErrs = ignoreErrs;
}
If true, SQLExceptions thrown during schema manipulation will be
printed but ignored. |
public void setIndexes(boolean indexes) {
_indexes = indexes;
}
Whether indexes on existing tables should be manipulated.
Defaults to true. |
public void setOpenJPATables(boolean openjpaTables) {
_openjpaTables = openjpaTables;
}
Whether to act on special tables used by OpenJPA components
for bookkeeping. |
public void setPrimaryKeys(boolean pks) {
_pks = pks;
}
Whether primary keys on existing tables should be manipulated.
Defaults to true. |
public void setSchemaGroup(SchemaGroup group) {
_group = group;
}
Set the schema group the tool will act on. |
public void setSequences(boolean seqs) {
_seqs = seqs;
}
Whether sequences should be manipulated. Defaults to true. |
public void setWriter(Writer writer) {
if (writer == null)
_writer = null;
else if (writer instanceof PrintWriter)
_writer = (PrintWriter) writer;
else
_writer = new PrintWriter(writer);
}
The stream to write to for the creation of SQL scripts. If the
stream is non-null, all SQL will be written to this stream rather than
executed against the database. |