Save This Page
Home » apache-openjpa-1.1.0-source » org.apache.openjpa.jdbc » schema » [javadoc | source]
org.apache.openjpa.jdbc.schema
public class: SchemaTool [javadoc | source]
java.lang.Object
   org.apache.openjpa.jdbc.schema.SchemaTool
The SchemaTool is used to manage the database schema. Note that the tool never adds or drops unique constraints from existing tables, because JDBC DatabaseMetaData does not include information on these constraints.
Nested Class Summary:
public static class  SchemaTool.Flags  Run flags. 
Field Summary
public static final  String ACTION_ADD     
public static final  String ACTION_DROP     
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[] ACTIONS     
Constructor:
 public SchemaTool(JDBCConfiguration conf) 
    Default constructor. Tools constructed this way will not have an action, so the #run() method will be a no-op.
 public SchemaTool(JDBCConfiguration conf,
    String action) 
    Construct a tool to perform the given action.
Method from org.apache.openjpa.jdbc.schema.SchemaTool Summary:
add,   addColumn,   addForeignKey,   addPrimaryKey,   build,   createDB,   createIndex,   createSequence,   createTable,   drop,   dropColumn,   dropDB,   dropForeignKey,   dropIndex,   dropPrimaryKey,   dropSequence,   dropTable,   getAction,   getDBSchemaGroup,   getDropSequences,   getDropTables,   getForeignKeys,   getIgnoreErrors,   getIndexes,   getOpenJPATables,   getPrimaryKeys,   getSchemaGroup,   getSequences,   getWriter,   main,   record,   refresh,   retain,   run,   run,   run,   setDBSchemaGroup,   setDropSequences,   setDropTables,   setForeignKeys,   setIgnoreErrors,   setIndexes,   setOpenJPATables,   setPrimaryKeys,   setSchemaGroup,   setSequences,   setWriter
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.openjpa.jdbc.schema.SchemaTool Detail:
  void add() throws SQLException 
    Adds any components present in the schema repository but absent from the database. Package-private for testing.
 public boolean addColumn(Column col) throws SQLException 
    Add the given column to the database schema.
 public boolean addForeignKey(ForeignKey fk) throws SQLException 
    Add the given foreign key to the database schema.
 public boolean addPrimaryKey(PrimaryKey pk) throws SQLException 
    Add the given primary key to the database schema.
  void build() throws SQLException 
    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 
    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 
    Add the given index to the database schema.
 public boolean createSequence(Sequence seq) throws SQLException 
    Add the given sequence to the database schema.
 public boolean createTable(Table table) throws SQLException 
    Add the given table to the database schema.
  void drop() throws SQLException 
    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 
    Drop the given column from the database schema.
  void dropDB() throws SQLException 
    Drop the current database. Package-private for testing.
 public boolean dropForeignKey(ForeignKey fk) throws SQLException 
    Drop the given foreign key from the database schema.
 public boolean dropIndex(Index idx) throws SQLException 
    Drop the given index from the database schema.
 public boolean dropPrimaryKey(PrimaryKey pk) throws SQLException 
    Drop the given primary key from the database schema.
 public boolean dropSequence(Sequence seq) throws SQLException 
    Drop the given sequence from the database schema.
 public boolean dropTable(Table table) throws SQLException 
    Drop the given table from the database schema.
 public String getAction() 
    The action supplied on construction.
 public SchemaGroup getDBSchemaGroup() 
    Return the database schema.
 public boolean getDropSequences() 
    If true, sequences that appear to be unused will be dropped. Defaults to true.
 public boolean getDropTables() 
    If true, tables that appear to be unused will be dropped. Defaults to true.
 public boolean getForeignKeys() 
    Whether foreign keys on existing tables should be manipulated. Defaults to true.
 public boolean getIgnoreErrors() 
    If true, SQLExceptions thrown during schema manipulation will be printed but ignored.
 public boolean getIndexes() 
    Whether indexes on existing tables should be manipulated. Defaults to true.
 public boolean getOpenJPATables() 
    Whether to act on special tables used by OpenJPA components for bookkeeping.
 public boolean getPrimaryKeys() 
    Whether primary keys on existing tables should be manipulated. Defaults to true.
 public SchemaGroup getSchemaGroup() 
    Return the schema group the tool will act on.
 public boolean getSequences() 
    Whether sequences should be manipulated. Defaults to true.
 public Writer getWriter() 
    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 
    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() 
  void refresh() throws SQLException 
    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 
    Drops database components that are not mentioned in the schema repository. Package-private for testing.
 public  void run() throws SQLException 
    Run the tool action.
 public static boolean run(JDBCConfiguration conf,
    String[] args,
    Options opts) throws IOException, SQLException 
    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 
    Run the tool. Return false if invalid options were given.
 public  void setDBSchemaGroup(SchemaGroup db) 
    Set the database schema.
 public  void setDropSequences(boolean dropSeqs) 
    If true, sequences that appear to be unused will be dropped. Defaults to true.
 public  void setDropTables(boolean dropTables) 
    If true, tables that appear to be unused will be dropped. Defaults to true.
 public  void setForeignKeys(boolean fks) 
    Whether foreign keys on existing tables should be manipulated. Defaults to true.
 public  void setIgnoreErrors(boolean ignoreErrs) 
    If true, SQLExceptions thrown during schema manipulation will be printed but ignored.
 public  void setIndexes(boolean indexes) 
    Whether indexes on existing tables should be manipulated. Defaults to true.
 public  void setOpenJPATables(boolean openjpaTables) 
    Whether to act on special tables used by OpenJPA components for bookkeeping.
 public  void setPrimaryKeys(boolean pks) 
    Whether primary keys on existing tables should be manipulated. Defaults to true.
 public  void setSchemaGroup(SchemaGroup group) 
    Set the schema group the tool will act on.
 public  void setSequences(boolean seqs) 
    Whether sequences should be manipulated. Defaults to true.
 public  void setWriter(Writer 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.