org.apache.openjpa.jdbc.schema
Class SchemaTool

java.lang.Object
  extended by org.apache.openjpa.jdbc.schema.SchemaTool

public class SchemaTool
extends Object

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.

Author:
Abe White, Patrick Linskey

Nested Class Summary
static class SchemaTool.Flags
          Run flags.
 
Field Summary
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_DROPDB
           
static String ACTION_EXPORT
           
static String ACTION_IMPORT
           
static String ACTION_REFLECT
           
static String ACTION_REFRESH
           
static String ACTION_RETAIN
           
static String[] ACTIONS
           
 
Constructor Summary
SchemaTool(JDBCConfiguration conf)
          Default constructor.
SchemaTool(JDBCConfiguration conf, String action)
          Construct a tool to perform the given action.
 
Method Summary
(package private)  void add()
          Adds any components present in the schema repository but absent from 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.
(package private)  void build()
          Re-execute all SQL used for the creation of the current database; this action is usually used when creating SQL scripts.
(package private)  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 createSequence(Sequence seq)
          Add the given sequence to the database schema.
 boolean createTable(Table table)
          Add the given table to the database schema.
(package private)  void drop()
          Drops all schema components in the schema repository that also exist in the database.
 boolean dropColumn(Column col)
          Drop the given column from the database schema.
(package private)  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.
 boolean dropSequence(Sequence seq)
          Drop the given sequence from the database schema.
 boolean dropTable(Table table)
          Drop the given table from the database schema.
 String getAction()
          The action supplied on construction.
 SchemaGroup getDBSchemaGroup()
          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.
 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.
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.
(package private)  void refresh()
          Adds any components present in the schema repository but absent from the database, and drops unused database components.
(package private)  void retain()
          Drops database components that are not mentioned in the schema repository.
 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 setSchemaGroup(SchemaGroup group)
          Set the schema group the tool will act on.
 void setSequences(boolean seqs)
          Whether sequences should be manipulated.
 void setWriter(Writer writer)
          The stream to write to for the creation of SQL scripts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACTION_ADD

public static final String ACTION_ADD
See Also:
Constant Field Values

ACTION_DROP

public static final String ACTION_DROP
See Also:
Constant Field Values

ACTION_RETAIN

public static final String ACTION_RETAIN
See Also:
Constant Field Values

ACTION_REFRESH

public static final String ACTION_REFRESH
See Also:
Constant Field Values

ACTION_BUILD

public static final String ACTION_BUILD
See Also:
Constant Field Values

ACTION_REFLECT

public static final String ACTION_REFLECT
See Also:
Constant Field Values

ACTION_CREATEDB

public static final String ACTION_CREATEDB
See Also:
Constant Field Values

ACTION_DROPDB

public static final String ACTION_DROPDB
See Also:
Constant Field Values

ACTION_IMPORT

public static final String ACTION_IMPORT
See Also:
Constant Field Values

ACTION_EXPORT

public static final String ACTION_EXPORT
See Also:
Constant Field Values

ACTION_DELETE_TABLE_CONTENTS

public static final String ACTION_DELETE_TABLE_CONTENTS
See Also:
Constant Field Values

ACTIONS

public static final String[] ACTIONS
Constructor Detail

SchemaTool

public SchemaTool(JDBCConfiguration conf)
Default constructor. Tools constructed this way will not have an action, so the run() method will be a no-op.


SchemaTool

public SchemaTool(JDBCConfiguration conf,
                  String action)
Construct a tool to perform the given action.

Method Detail

getAction

public String getAction()
The action supplied on construction.


getIgnoreErrors

public boolean getIgnoreErrors()
If true, SQLExceptions thrown during schema manipulation will be printed but ignored.


setIgnoreErrors

public void setIgnoreErrors(boolean ignoreErrs)
If true, SQLExceptions thrown during schema manipulation will be printed but ignored.


getOpenJPATables

public boolean getOpenJPATables()
Whether to act on special tables used by OpenJPA components for bookkeeping.


setOpenJPATables

public void setOpenJPATables(boolean openjpaTables)
Whether to act on special tables used by OpenJPA components for bookkeeping.


getDropTables

public boolean getDropTables()
If true, tables that appear to be unused will be dropped. Defaults to true.


setDropTables

public void setDropTables(boolean dropTables)
If true, tables that appear to be unused will be dropped. Defaults to true.


getDropSequences

public boolean getDropSequences()
If true, sequences that appear to be unused will be dropped. Defaults to true.


setDropSequences

public void setDropSequences(boolean dropSeqs)
If true, sequences that appear to be unused will be dropped. Defaults to true.


getSequences

public boolean getSequences()
Whether sequences should be manipulated. Defaults to true.


setSequences

public void setSequences(boolean seqs)
Whether sequences should be manipulated. Defaults to true.


getIndexes

public boolean getIndexes()
Whether indexes on existing tables should be manipulated. Defaults to true.


setIndexes

public void setIndexes(boolean indexes)
Whether indexes on existing tables should be manipulated. Defaults to true.


getForeignKeys

public boolean getForeignKeys()
Whether foreign keys on existing tables should be manipulated. Defaults to true.


setForeignKeys

public void setForeignKeys(boolean fks)
Whether foreign keys on existing tables should be manipulated. Defaults to true.


getPrimaryKeys

public boolean getPrimaryKeys()
Whether primary keys on existing tables should be manipulated. Defaults to true.


setPrimaryKeys

public void setPrimaryKeys(boolean pks)
Whether primary keys on existing tables should be manipulated. Defaults to true.


getWriter

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.


setWriter

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.


getSchemaGroup

public SchemaGroup getSchemaGroup()
Return the schema group the tool will act on.


setSchemaGroup

public void setSchemaGroup(SchemaGroup group)
Set the schema group the tool will act on.


run

public void run()
         throws SQLException
Run the tool action.

Throws:
SQLException

add

void add()
   throws SQLException
Adds any components present in the schema repository but absent from the database. Package-private for testing.

Throws:
SQLException

drop

void drop()
    throws SQLException
Drops all schema components in the schema repository that also exist in the database. Package-private for testing.

Throws:
SQLException

retain

void retain()
      throws SQLException
Drops database components that are not mentioned in the schema repository. Package-private for testing.

Throws:
SQLException

refresh

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.

Throws:
SQLException

createDB

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.

Throws:
SQLException

build

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.

Throws:
SQLException

dropDB

void dropDB()
      throws SQLException
Drop the current database. Package-private for testing.

Throws:
SQLException

record

public void record()
Record the changes made to the DB in the current SchemaFactory.


createTable

public boolean createTable(Table table)
                    throws SQLException
Add the given table to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropTable

public boolean dropTable(Table table)
                  throws SQLException
Drop the given table from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

createSequence

public boolean createSequence(Sequence seq)
                       throws SQLException
Add the given sequence to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropSequence

public boolean dropSequence(Sequence seq)
                     throws SQLException
Drop the given sequence from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

createIndex

public boolean createIndex(Index idx,
                           Table table)
                    throws SQLException
Add the given index to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropIndex

public boolean dropIndex(Index idx)
                  throws SQLException
Drop the given index from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

addColumn

public boolean addColumn(Column col)
                  throws SQLException
Add the given column to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropColumn

public boolean dropColumn(Column col)
                   throws SQLException
Drop the given column from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

addPrimaryKey

public boolean addPrimaryKey(PrimaryKey pk)
                      throws SQLException
Add the given primary key to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropPrimaryKey

public boolean dropPrimaryKey(PrimaryKey pk)
                       throws SQLException
Drop the given primary key from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

addForeignKey

public boolean addForeignKey(ForeignKey fk)
                      throws SQLException
Add the given foreign key to the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

dropForeignKey

public boolean dropForeignKey(ForeignKey fk)
                       throws SQLException
Drop the given foreign key from the database schema.

Returns:
true if the operation was successful, false otherwise
Throws:
SQLException

getDBSchemaGroup

public SchemaGroup getDBSchemaGroup()
Return the database schema.


setDBSchemaGroup

public void setDBSchemaGroup(SchemaGroup db)
Set the database schema.


main

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. Actions can be composed in a comma-separated list. The various actions are as follows. Examples:

Throws:
IOException
SQLException

run

public static boolean run(JDBCConfiguration conf,
                          String[] args,
                          Options opts)
                   throws IOException,
                          SQLException
Run the tool. Returns false if any invalid options were given.

Throws:
IOException
SQLException
See Also:
main(java.lang.String[])

run

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.

Throws:
IOException
SQLException


Copyright © 2006-2010 Apache Software Foundation. All Rights Reserved.