From: <one...@us...> - 2002-11-11 10:09:59
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools In directory usw-pr-cvs1:/tmp/cvs-serv1678/cirrus/hibernate/tools Modified Files: JdbcColumnInfo.java SchemaUpdater.java Log Message: made SchemaUpdater commandline-invokable added latest cglib jar got SchemaUpdater working with DB2 removed bcel.jar Index: JdbcColumnInfo.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/JdbcColumnInfo.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** JdbcColumnInfo.java 4 Nov 2002 00:55:30 -0000 1.1 --- JdbcColumnInfo.java 11 Nov 2002 10:09:56 -0000 1.2 *************** *** 1,2 **** --- 1,3 ---- + //$Id$ package cirrus.hibernate.tools; /* *************** *** 7,11 **** * (C) 2002 Christoph Sturm */ ! public class JdbcColumnInfo{ public String table; public String columnName; --- 8,12 ---- * (C) 2002 Christoph Sturm */ ! public class JdbcColumnInfo { public String table; public String columnName; *************** *** 14,16 **** --- 15,22 ---- public int decimalDigits; public String isNullable; + + public String toString() { + return table + '.' + columnName; + } + } Index: SchemaUpdater.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/SchemaUpdater.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SchemaUpdater.java 4 Nov 2002 00:55:30 -0000 1.1 --- SchemaUpdater.java 11 Nov 2002 10:09:56 -0000 1.2 *************** *** 1,12 **** 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.*; --- 1,14 ---- + //$Id$ package cirrus.hibernate.tools; import cirrus.hibernate.Datastore; + import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; import cirrus.hibernate.sql.Dialect; import cirrus.hibernate.connection.ConnectionProvider; import cirrus.hibernate.connection.ConnectionProviderFactory; import java.util.*; + import java.io.FileInputStream; import java.sql.*; *************** *** 22,37 **** */ 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); --- 24,81 ---- */ public class SchemaUpdater{ ! ! Log log = LogFactory.getLog( getClass() ); private ConnectionProvider cp; ! Datastore store; ! public SchemaUpdater(Datastore store) throws HibernateException, SQLException { this.store = store; cp = ConnectionProviderFactory.newConnectionProvider(); } ! public SchemaUpdater(Datastore store, Properties connectionProperties) throws HibernateException { ! this.store = store; ! cp = ConnectionProviderFactory.newConnectionProvider(connectionProperties); ! } ! ! public static void main(String[] args) { ! try { ! ! Datastore ds = Hibernate.createDatastore(); ! ! boolean script = true; ! String propFile = null; ! ! for ( int i=0; i<args.length; i++ ) { ! if( args[i].startsWith("--") ) { ! if( args[i].equals("--quiet") ) { ! script = false; ! } ! else if( args[i].startsWith("--properties=") ) { ! propFile = args[i].substring(13); ! } ! } ! else { ! ds.storeFile(args[i]); ! } ! ! } ! if(propFile!=null) { ! Properties props = new Properties(); ! props.load( new FileInputStream(propFile) ); ! new SchemaUpdater(ds, props).execute(script); ! } ! else { ! new SchemaUpdater(ds).execute(script); ! } ! } ! catch(Exception e) { ! System.err.println( "Error updating schema " + e.getMessage() ); ! e.printStackTrace(); ! } ! } ! ! public void execute(boolean script) throws SQLException, HibernateException { Connection connection = cp.getConnection(); Map columnInfo = getColumnInfo(connection); *************** *** 39,65 **** 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(); --- 83,109 ---- stmt = connection.createStatement(); 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 (jdbc2) { ! stmt.addBatch( sql ); } + else { + stmt.executeUpdate( sql ); + } + + } + catch (SQLException e) { + log.error( "Error while executing " + sql, e ); + } + } + if (jdbc2) stmt.executeBatch(); stmt.close(); *************** *** 91,95 **** try { String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null; ! ResultSet rsCols = dbData.getColumns(null, userName, null, null); while (rsCols.next()) { --- 135,141 ---- try { String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null; ! if (userName!=null) log.info("Schema: " + userName); ! //ResultSet rsCols = dbData.getColumns(null, userName, "%", "%"); ! ResultSet rsCols = dbData.getColumns(null, "%", "%", "%"); while (rsCols.next()) { *************** *** 100,104 **** info.table = (info.table == null) ? null : info.table.toUpperCase(); ! String columnName = rsCols.getString("COLUMN_NAME"); info.typeName = rsCols.getString("TYPE_NAME"); --- 146,150 ---- info.table = (info.table == null) ? null : info.table.toUpperCase(); ! info.columnName = rsCols.getString("COLUMN_NAME"); info.typeName = rsCols.getString("TYPE_NAME"); *************** *** 107,110 **** --- 153,158 ---- info.isNullable = rsCols.getString("IS_NULLABLE"); + + log.info("Existing column: " + info); Map tableColInfo = (Map)colInfo.get(info.table.toLowerCase()); *************** *** 114,118 **** 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(); --- 162,166 ---- colInfo.put(info.table.toLowerCase(), tableColInfo); } ! tableColInfo.put( info.columnName.toLowerCase(), info ); } catch (SQLException sqlex) { String messagex = "Error getting column info for column. Error was:" + sqlex.toString(); *************** *** 139,144 **** --- 187,195 ---- private Set getTableNames(Connection connection) throws SQLException { + DatabaseMetaData dbData = connection.getMetaData(); + log.info("Getting Table Info From Database"); + Set tableNames = new TreeSet(); ResultSet tableSet; *************** *** 162,165 **** --- 213,219 ---- log.debug("Found Table:" + tableName); tableNames.add(tableName); + + log.info("Existing table: " + tableName); + } catch (SQLException sqle) { *************** *** 182,185 **** } ! Datastore store; } --- 236,239 ---- } ! } |