|
From: <cs...@us...> - 2002-11-04 00:55:34
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools
In directory usw-pr-cvs1:/tmp/cvs-serv31063/cirrus/hibernate/tools
Added Files:
JdbcColumnInfo.java SchemaUpdater.java
Log Message:
dynamic schema update
--- NEW FILE: JdbcColumnInfo.java ---
package cirrus.hibernate.tools;
/*
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 13.10.2002
* Time: 21:04:35
* (C) 2002 Christoph Sturm
*/
public class JdbcColumnInfo{
public String table;
public String columnName;
public String typeName;
public int columnSize;
public int decimalDigits;
public String isNullable;
}
--- NEW FILE: SchemaUpdater.java ---
package cirrus.hibernate.tools;
import cirrus.hibernate.Datastore;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.sql.Dialect;
import cirrus.hibernate.connection.ConnectionProvider;
import cirrus.hibernate.connection.ConnectionProviderFactory;
import cirrus.hibernate.tools.JdbcColumnInfo;
import java.util.*;
import java.sql.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/*
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 13.10.2002
* Time: 19:52:56
* (C) 2002 Christoph Sturm
*/
public class SchemaUpdater{
Log log = LogFactory.getLog(getClass());
private ConnectionProvider cp;
public SchemaUpdater(Datastore store) throws HibernateException, SQLException {
this.store = store;
cp = ConnectionProviderFactory.newConnectionProvider();
}
public void doSchemaUpdate() throws SQLException, HibernateException {
Connection connection = cp.getConnection();
Map columnInfo = getColumnInfo(connection);
Statement stmt;
stmt = connection.createStatement();
boolean export = true;
boolean jdbc2 = false;
String[] createSQL;
createSQL = store.generateSchemaUpdateScript(Dialect.getDialect(), columnInfo);
for(int j = 0; j < createSQL.length; j++) {
final String sql = createSQL[j];
try {
// if (script) System.out.println( createSQL[j] );
// if (outputFile != null) fileOutput.write( createSQL[j] + "\n" );
if (export) {
if (jdbc2) {
stmt.addBatch( sql );
}
else {
stmt.executeUpdate( sql );
}
}
}
catch (SQLException e) {
log.error( "Error while executing " + sql, e );
}
}
if (jdbc2) stmt.executeBatch();
stmt.close();
connection.close();
}
// the next 2 functions are highly inspired by the ofbiz entity engine
private Map getColumnInfo(Connection connection) {
DatabaseMetaData dbData = null;
try {
dbData = connection.getMetaData();
} catch (SQLException sqle) {
String message = "Unable to get database meta data... Error was:" + sqle.toString();
log.error(message);
}
log.info("Getting Column Info From Database");
Map colInfo = new HashMap();
try {
String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null;
ResultSet rsCols = dbData.getColumns(null, userName, null, null);
while (rsCols.next()) {
try {
JdbcColumnInfo info = new JdbcColumnInfo();
info.table = rsCols.getString("TABLE_NAME");
info.table = (info.table == null) ? null : info.table.toUpperCase();
String columnName = rsCols.getString("COLUMN_NAME");
info.typeName = rsCols.getString("TYPE_NAME");
info.columnSize = rsCols.getInt("COLUMN_SIZE");
info.decimalDigits = rsCols.getInt("DECIMAL_DIGITS");
info.isNullable = rsCols.getString("IS_NULLABLE");
Map tableColInfo = (Map)colInfo.get(info.table.toLowerCase());
if (tableColInfo == null) {
tableColInfo = new HashMap();
colInfo.put(info.table.toLowerCase(), tableColInfo);
}
tableColInfo.put(columnName.toLowerCase(), info);
} catch (SQLException sqlex) {
String messagex = "Error getting column info for column. Error was:" + sqlex.toString();
log.debug(messagex);
continue;
}
}
try {
rsCols.close();
} catch (SQLException sqle) {
String message = "Unable to close ResultSet for column list, continuing anyway... Error was:" + sqle.toString();
log.error(message);
}
} catch (SQLException sqle) {
String message = "Error getting column meta data for Error was:" + sqle.toString() + ". Not checking columns.";
log.error(message);
}
return colInfo;
}
private Set getTableNames(Connection connection) throws SQLException {
DatabaseMetaData dbData = connection.getMetaData();
Set tableNames = new TreeSet();
ResultSet tableSet;
try {
String[] types = {"TABLE"};
String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null;
tableSet = dbData.getTables(null, userName, null, types);
} catch (SQLException sqle) {
log.error("Unable to get list of table information, trying to create all tables.", sqle);
// we are returning an empty set here because databases like SapDB throw an exception when there are no tables in the database
return tableNames;
}
try {
while (tableSet.next()) {
try {
String tableName = tableSet.getString("TABLE_NAME");
log.debug("Found Table:" + tableName);
tableNames.add(tableName);
} catch (SQLException sqle) {
log.error("Error getting table information... Error was:", sqle);
continue;
}
}
} catch (SQLException sqle) {
log.error("Error in tableSet.next(). Error was:" , sqle);
} finally {
try {
tableSet.close();
} catch (SQLException sqle) {
String message = "Unable to close ResultSet for table list, continuing anyway... Error was:" + sqle.toString();
log.error(message);
}
}
return tableNames;
}
Datastore store;
}
|