jp.sf.amateras.mirage.updater
Class SchemaUpdater

java.lang.Object
  extended by jp.sf.amateras.mirage.updater.SchemaUpdater

public class SchemaUpdater
extends Object

Execute SQL files which located a specific package to update database schema. You can use this to update database schema automatically with application updating.

By the default, you have to locate SQLs within META-INF as follows:

 /META-INF
   mysql_1.sql
   mysql_2.sql
   mysql_3.sql
   ...
 

You can change a package where locate SQL files by setPackageName(String).

SQLs will be executed in the order of version number which contained in the filename. The version number (a part of filename) have to be a continued integer value. Filename can contain the database type such as "mysql" also. It is omittable. For examples, if the schema updater can not find a "mysql_1.sql", uses "1.sql" instead of it.

Author:
Naoki Takezoe

Field Summary
protected  String packageName
           
protected  SqlManager sqlManager
           
protected  String tableName
           
 
Constructor Summary
SchemaUpdater()
           
 
Method Summary
protected  void createTable()
          Creates table which manages schema version and insert an initial record as version 0.
protected  boolean existsTable()
          Checks the table which manages a schema version exists or not exisis.
protected  int getCurrentVersion()
          Returns the current version number.
protected  String getSql(int version)
          Returns the SQL which located within a package specified by the packageName as "dialectname_version.sql" or "version.sql".
 void setPackageName(String packageName)
          Sets the package name where locate SQL files.
 void setSqlManager(SqlManager sqlManager)
          Sets the SqlManager to use for schema updating.
 void setTableName(String tableName)
          Sets the table name which manages the schema version.
 void update()
          Updates database schema to the latest state.
protected  void updateVersion(int version)
          Updates the version number by the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlManager

protected SqlManager sqlManager

tableName

protected String tableName

packageName

protected String packageName
Constructor Detail

SchemaUpdater

public SchemaUpdater()
Method Detail

setSqlManager

public void setSqlManager(SqlManager sqlManager)
Sets the SqlManager to use for schema updating.

Parameters:
sqlManager - the SqlManager

setTableName

public void setTableName(String tableName)
Sets the table name which manages the schema version. The default is "SCHEMA_VERSION".

Parameters:
tableName - the table name

setPackageName

public void setPackageName(String packageName)
Sets the package name where locate SQL files. The default is "META-INF".

Parameters:
packageName - the package name

update

public void update()
Updates database schema to the latest state.

Note: Before calling this method, Connection might have to be auto-commit mode.


getSql

protected String getSql(int version)
Returns the SQL which located within a package specified by the packageName as "dialectname_version.sql" or "version.sql".

Parameters:
version - the version number
Returns:
SQL or null if the SQL file does not exist

existsTable

protected boolean existsTable()
Checks the table which manages a schema version exists or not exisis.

Returns:
if the table exists then returns true; otherwise false

createTable

protected void createTable()
Creates table which manages schema version and insert an initial record as version 0.


getCurrentVersion

protected int getCurrentVersion()
Returns the current version number.

Returns:
the current version number

updateVersion

protected void updateVersion(int version)
Updates the version number by the given value.

Parameters:
version - the version number


Copyright © 2010-2012 Project Amateras. All Rights Reserved.