com.agimatec.sql.meta.checking
Class DatabaseSchemaChecker

java.lang.Object
  extended by com.agimatec.sql.meta.checking.DatabaseSchemaChecker
Direct Known Subclasses:
MySqlSchemaChecker, OracleSchemaChecker, PostgresSchemaChecker

public abstract class DatabaseSchemaChecker
extends Object

Description: Compare a database schema against sql scripts and find differences.
Example for mysql:

       JdbcConfig config = new JdbcConfig();
 config.setDriver("com.mysql.jdbc.Driver");
 config.setConnect("jdbc:mysql://localhost:3306/mysql_db");
 config.getProperties().put("user", "root");
 config.getProperties().put("password", "");
 JdbcDatabase targetDatabase = JdbcDatabaseFactory.createInstance(config);
 targetDatabase.begin();

 DatabaseSchemaChecker checker = DatabaseSchemaChecker.forDbms("mysql");
 checker.setDatabase(targetDatabase);
 List urls = new ArrayList();
 urls.add(getClass().getClassLoader().getResource("mysql/mysql-schema.sql"));
 // add more script URLs that belong to the database schema...
 checker.checkDatabaseSchema(urls.toArray(new URL[urls.size()]));

 targetDatabase.close();
 
User: roman.stumm
Date: 24.04.2007
Time: 14:58:47


Nested Class Summary
static class DatabaseSchemaChecker.Options
           
 
Field Summary
protected  JdbcDatabase database
           
protected static org.slf4j.Logger logger
           
protected  List myFoundErrors
           
protected  Map unknownColumns
           
 
Constructor Summary
DatabaseSchemaChecker()
           
 
Method Summary
 void addUnmappedColumn(String tableName, String columnName)
          configuration - declare a column as not mapped in the database, so that it will not be treated as an error during assertSchemaComplete().
 void assertCatalogsComplete(CatalogDescription schemaConfig, CatalogDescription databaseConfig)
           
abstract  void assertObjectsValid()
          API - check for invalid objects in the database
protected  void assertTrue(String s, boolean b)
           
 void checkDatabaseSchema(List<DatabaseSchemaChecker.Options> options, URL[] scripts)
          API - check the database for compatibility with the given XML-DDL configuration.
 void checkDatabaseSchema(URL[] scripts)
          API - check the database for compatibility with the given XML-DDL configuration.
protected  void compareForeignKeyDescription(TableDescription xmlTableDescription, TableDescription databaseTableDescription)
           
protected  void compareIndexDescription(TableDescription xmlTableDescription, TableDescription databaseTableDescription)
           
protected  void compareSingleIndexDescription(IndexDescription xmlIndexDescription, IndexDescription databaseIndexDescription)
           
static DatabaseSchemaChecker forDbms(String dbms)
           
 JdbcDatabase getDatabase()
           
protected abstract  DDLScriptSqlMetaFactory getDDLScriptSqlMetaFactory()
           
 Map getUnmappedColumns()
           
 Collection getUnmappedColumns(String tableName)
           
protected  boolean isPrecisionCompatible(ColumnDescription expected, ColumnDescription actual)
           
protected  boolean isScaleCompatible(ColumnDescription expected, ColumnDescription actual)
           
protected  boolean isTypeCompatible(ColumnDescription expected, ColumnDescription actual)
           
protected  void log(Object obj)
          log INFO or Exception
protected  void print(Object obj)
          log INFO and print to console
protected abstract  CatalogDescription readDatabaseCatalog(String[] tableNames)
           
 void setDatabase(JdbcDatabase aDatabase)
           
protected  void throwAssertions()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final org.slf4j.Logger logger

myFoundErrors

protected final List myFoundErrors

database

protected JdbcDatabase database

unknownColumns

protected Map unknownColumns
Constructor Detail

DatabaseSchemaChecker

public DatabaseSchemaChecker()
Method Detail

forDbms

public static DatabaseSchemaChecker forDbms(String dbms)

getDatabase

public JdbcDatabase getDatabase()

setDatabase

public void setDatabase(JdbcDatabase aDatabase)

addUnmappedColumn

public void addUnmappedColumn(String tableName,
                              String columnName)
configuration - declare a column as not mapped in the database, so that it will not be treated as an error during assertSchemaComplete().

Parameters:
tableName -
columnName -

getUnmappedColumns

public Map getUnmappedColumns()
Returns:
a Map (read-write)

getUnmappedColumns

public Collection getUnmappedColumns(String tableName)
Parameters:
tableName -
Returns:
a collection (read-only)

throwAssertions

protected void throwAssertions()

assertObjectsValid

public abstract void assertObjectsValid()
                                 throws Exception
API - check for invalid objects in the database

Throws:
Exception

checkDatabaseSchema

public void checkDatabaseSchema(URL[] scripts)
                         throws Exception
API - check the database for compatibility with the given XML-DDL configuration. Additional add all DDL in the scripts to the schema.

Parameters:
scripts - - scripts for schema (Soll-Zustand) Der Ist-Zustand steht in der Datenbank und wird mit dem Soll-Zustand verglichen.
Throws:
Exception

checkDatabaseSchema

public void checkDatabaseSchema(List<DatabaseSchemaChecker.Options> options,
                                URL[] scripts)
                         throws Exception
API - check the database for compatibility with the given XML-DDL configuration. Additional add all DDL in the scripts to the schema.

Parameters:
options - - list with options per script (may be empty) - corresponding to index in 'scripts'
scripts - - scripts for schema (Soll-Zustand) Der Ist-Zustand steht in der Datenbank und wird mit dem Soll-Zustand verglichen.
Throws:
Exception

getDDLScriptSqlMetaFactory

protected abstract DDLScriptSqlMetaFactory getDDLScriptSqlMetaFactory()

readDatabaseCatalog

protected abstract CatalogDescription readDatabaseCatalog(String[] tableNames)
                                                   throws SQLException,
                                                          IOException
Throws:
SQLException
IOException

assertCatalogsComplete

public void assertCatalogsComplete(CatalogDescription schemaConfig,
                                   CatalogDescription databaseConfig)

compareSingleIndexDescription

protected void compareSingleIndexDescription(IndexDescription xmlIndexDescription,
                                             IndexDescription databaseIndexDescription)

compareIndexDescription

protected void compareIndexDescription(TableDescription xmlTableDescription,
                                       TableDescription databaseTableDescription)

isTypeCompatible

protected boolean isTypeCompatible(ColumnDescription expected,
                                   ColumnDescription actual)

isScaleCompatible

protected boolean isScaleCompatible(ColumnDescription expected,
                                    ColumnDescription actual)

isPrecisionCompatible

protected boolean isPrecisionCompatible(ColumnDescription expected,
                                        ColumnDescription actual)

compareForeignKeyDescription

protected void compareForeignKeyDescription(TableDescription xmlTableDescription,
                                            TableDescription databaseTableDescription)

assertTrue

protected void assertTrue(String s,
                          boolean b)

print

protected void print(Object obj)
log INFO and print to console


log

protected void log(Object obj)
log INFO or Exception

Parameters:
obj -


Copyright © 2008-2014. All Rights Reserved.