Commandline tool to export table schema to the database. This class may also be called from inside an application.
| Constructor: |
public SchemaExport(Configuration cfg) throws HibernateException {
this( cfg, cfg.getProperties() );
}
Create a schema exporter for the given Configuration Parameters:
cfg - The configuration from which to build a schema export.
Throws:
HibernateException - Indicates problem preparing for schema export.
|
public SchemaExport(Configuration cfg,
Settings settings) throws HibernateException {
dialect = settings.getDialect();
connectionHelper = new SuppliedConnectionProviderConnectionHelper( settings.getConnectionProvider() );
dropSQL = cfg.generateDropSchemaScript( dialect );
createSQL = cfg.generateSchemaCreationScript( dialect );
sqlStatementLogger = settings.getSqlStatementLogger();
formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
Create a schema exporter for the given Configuration and given settings Parameters:
cfg - The configuration from which to build a schema export.
settings - The 'parsed' settings.
Throws:
HibernateException - Indicates problem preparing for schema export.
|
public SchemaExport(Configuration cfg,
Properties properties) throws HibernateException {
dialect = Dialect.getDialect( properties );
Properties props = new Properties();
props.putAll( dialect.getDefaultProperties() );
props.putAll( properties );
connectionHelper = new ManagedProviderConnectionHelper( props );
dropSQL = cfg.generateDropSchemaScript( dialect );
createSQL = cfg.generateSchemaCreationScript( dialect );
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
Create a schema exporter for the given Configuration, with the given
database connection properties. Parameters:
cfg - The configuration from which to build a schema export.
properties - The properties from which to configure connectivity etc.
Throws:
HibernateException - Indicates problem preparing for schema export.
|
public SchemaExport(Configuration cfg,
Connection connection) throws HibernateException {
this.connectionHelper = new SuppliedConnectionHelper( connection );
dialect = Dialect.getDialect( cfg.getProperties() );
dropSQL = cfg.generateDropSchemaScript( dialect );
createSQL = cfg.generateSchemaCreationScript( dialect );
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, cfg.getProperties() ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
}
Create a schema exporter for the given Configuration, using the supplied connection for connectivity. Parameters:
cfg - The configuration to use.
connection - The JDBC connection to use.
Throws:
HibernateException - Indicates problem preparing for schema export.
|
| Method from org.hibernate.tool.hbm2ddl.SchemaExport Detail: |
public void create(boolean script,
boolean export) {
execute( script, export, false, false );
}
Run the schema creation script. |
public void drop(boolean script,
boolean export) {
execute( script, export, true, false );
}
Run the drop schema script. |
public void execute(boolean script,
boolean export,
boolean justDrop,
boolean justCreate) {
log.info( "Running hbm2ddl schema export" );
Connection connection = null;
Writer outputFileWriter = null;
Reader importFileReader = null;
Statement statement = null;
exceptions.clear();
try {
try {
InputStream stream = ConfigHelper.getResourceAsStream( importFile );
importFileReader = new InputStreamReader( stream );
}
catch ( HibernateException e ) {
log.debug( "import file not found: " + importFile );
}
if ( outputFile != null ) {
log.info( "writing generated schema to file: " + outputFile );
outputFileWriter = new FileWriter( outputFile );
}
if ( export ) {
log.info( "exporting generated schema to database" );
connectionHelper.prepare( true );
connection = connectionHelper.getConnection();
statement = connection.createStatement();
}
if ( !justCreate ) {
drop( script, export, outputFileWriter, statement );
}
if ( !justDrop ) {
create( script, export, outputFileWriter, statement );
if ( export && importFileReader != null ) {
importScript( importFileReader, statement );
}
}
log.info( "schema export complete" );
}
catch ( Exception e ) {
exceptions.add( e );
log.error( "schema export unsuccessful", e );
}
finally {
try {
if ( statement != null ) {
statement.close();
}
if ( connection != null ) {
connectionHelper.release();
}
}
catch ( Exception e ) {
exceptions.add( e );
log.error( "Could not close connection", e );
}
try {
if ( outputFileWriter != null ) {
outputFileWriter.close();
}
if ( importFileReader != null ) {
importFileReader.close();
}
}
catch ( IOException ioe ) {
exceptions.add( ioe );
log.error( "Error closing output file: " + outputFile, ioe );
}
}
}
|
public List getExceptions() {
return exceptions;
}
Returns a List of all Exceptions which occured during the export. |
public static void main(String[] args) {
try {
Configuration cfg = new Configuration();
boolean script = true;
boolean drop = false;
boolean create = false;
boolean halt = false;
boolean export = true;
String outFile = null;
String importFile = "/import.sql";
String propFile = null;
boolean format = false;
String delim = null;
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].startsWith( "--" ) ) {
if ( args[i].equals( "--quiet" ) ) {
script = false;
}
else if ( args[i].equals( "--drop" ) ) {
drop = true;
}
else if ( args[i].equals( "--create" ) ) {
create = true;
}
else if ( args[i].equals( "--haltonerror" ) ) {
halt = true;
}
else if ( args[i].equals( "--text" ) ) {
export = false;
}
else if ( args[i].startsWith( "--output=" ) ) {
outFile = args[i].substring( 9 );
}
else if ( args[i].startsWith( "--import=" ) ) {
importFile = args[i].substring( 9 );
}
else if ( args[i].startsWith( "--properties=" ) ) {
propFile = args[i].substring( 13 );
}
else if ( args[i].equals( "--format" ) ) {
format = true;
}
else if ( args[i].startsWith( "--delimiter=" ) ) {
delim = args[i].substring( 12 );
}
else if ( args[i].startsWith( "--config=" ) ) {
cfg.configure( args[i].substring( 9 ) );
}
else if ( args[i].startsWith( "--naming=" ) ) {
cfg.setNamingStrategy(
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) )
.newInstance()
);
}
}
else {
String filename = args[i];
if ( filename.endsWith( ".jar" ) ) {
cfg.addJar( new File( filename ) );
}
else {
cfg.addFile( filename );
}
}
}
if ( propFile != null ) {
Properties props = new Properties();
props.putAll( cfg.getProperties() );
props.load( new FileInputStream( propFile ) );
cfg.setProperties( props );
}
SchemaExport se = new SchemaExport( cfg )
.setHaltOnError( halt )
.setOutputFile( outFile )
.setImportFile( importFile )
.setDelimiter( delim );
if ( format ) {
se.setFormat( true );
}
se.execute( script, export, drop, create );
}
catch ( Exception e ) {
log.error( "Error creating schema ", e );
e.printStackTrace();
}
}
|
public SchemaExport setDelimiter(String delimiter) {
this.delimiter = delimiter;
return this;
}
Set the end of statement delimiter |
public SchemaExport setFormat(boolean format) {
this.formatter = ( format ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
return this;
}
Should we format the sql strings? |
public SchemaExport setHaltOnError(boolean haltOnError) {
this.haltOnError = haltOnError;
return this;
}
Should we stop once an error occurs? |
public SchemaExport setImportFile(String filename) {
importFile = filename;
return this;
}
An import file, containing raw SQL statements to be executed. |
public SchemaExport setOutputFile(String filename) {
outputFile = filename;
return this;
}
For generating a export script file, this is the file which will be written. |