From: <one...@us...> - 2002-11-27 13:47:38
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools In directory sc8-pr-cvs1:/tmp/cvs-serv24254/cirrus/hibernate/tools Modified Files: SchemaExport.java Log Message: correctly propagate exceptions via proxies fixed uncompilable calendar types for JDK1.2 added --format and --delimiter options to SchemaExport Index: SchemaExport.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/SchemaExport.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** SchemaExport.java 31 Aug 2002 14:23:43 -0000 1.10 --- SchemaExport.java 27 Nov 2002 13:47:34 -0000 1.11 *************** *** 61,65 **** */ public void create(boolean script, boolean export) throws HibernateException { ! execute(script, export, false); } --- 61,65 ---- */ public void create(boolean script, boolean export) throws HibernateException { ! execute(script, export, false, true, null); } *************** *** 71,78 **** */ public void drop(boolean script, boolean export) throws HibernateException { ! execute(script, export, true); } ! private void execute(boolean script, boolean export, boolean justDrop) throws HibernateException { Connection connection = null; FileWriter fileOutput = null; --- 71,78 ---- */ public void drop(boolean script, boolean export) throws HibernateException { ! execute(script, export, true, true, null); } ! private void execute(boolean script, boolean export, boolean justDrop, boolean format, String delimiter) throws HibernateException { Connection connection = null; FileWriter fileOutput = null; *************** *** 122,127 **** for(int j = 0; j < createSQL.length; j++) { try { ! if (script) System.out.println( createSQL[j] ); ! if (outputFile != null) fileOutput.write( createSQL[j] + "\n" ); if (export) { if (jdbc2) { --- 122,128 ---- for(int j = 0; j < createSQL.length; j++) { try { ! String formatted = format ? format( createSQL[j], delimiter ) : createSQL[j]; ! if (script) System.out.println( formatted ); ! if (outputFile != null) fileOutput.write( formatted + "\n" ); if (export) { if (jdbc2) { *************** *** 172,175 **** --- 173,223 ---- } + /** + * Format an SQL statement using simple rules: + * a) Insert newline after each comma; + * b) Indent three spaces after each inserted newline; + * c) Append an optional delimiter to the SQL statement. + * If the statement contains single/double quotes return unchanged, + * it is too complex and could be broken by simple formatting. + */ + private static String format(String sql, String delimiter) { + + if ( sql.indexOf("\"") > 0 || sql.indexOf("'") > 0) { + return sql; + } + + final String formatted; + + if ( sql.toLowerCase().startsWith("create table") ) { + + StringBuffer result = new StringBuffer(60); + StringTokenizer tokens = new StringTokenizer( sql, "(,)", true ); + + int depth = 0; + + while ( tokens.hasMoreTokens() ) { + String tok = tokens.nextToken(); + if ( ")".equals(tok) ) { + depth--; + if (depth==0) result.append("\n"); + } + result.append(tok); + if ( ",".equals(tok) && depth==1 ) result.append("\n "); + if ( "(".equals(tok) ) { + depth++; + if (depth==1) result.append("\n "); + } + } + + formatted = result.toString(); + + } + else { + formatted = sql; + } + + return (delimiter!=null) ? formatted + delimiter : formatted; + } + public static void main(String[] args) { try { *************** *** 181,184 **** --- 229,234 ---- String outputFile = null; String propFile = null; + boolean formatSQL = false; + String delimiter = null; for ( int i=0; i<args.length; i++ ) { *************** *** 199,202 **** --- 249,258 ---- propFile = args[i].substring(13); } + else if( args[i].equals("--format") ) { + formatSQL = true; + } + else if ( args[i].startsWith("--delimiter=") ) { + delimiter = args[i].substring(12); + } } else { *************** *** 208,215 **** Properties props = new Properties(); props.load( new FileInputStream(propFile) ); ! new SchemaExport(ds, props).setOutputFile(outputFile).execute(script, export, drop); } else { ! new SchemaExport(ds).setOutputFile(outputFile).execute(script, export, drop); } } --- 264,271 ---- Properties props = new Properties(); props.load( new FileInputStream(propFile) ); ! new SchemaExport(ds, props).setOutputFile(outputFile).execute(script, export, drop, formatSQL, delimiter); } else { ! new SchemaExport(ds).setOutputFile(outputFile).execute(script, export, drop, formatSQL, delimiter); } } |