| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Hibernate
Revision: 15139
Author: anthonyHib
Date: 22 Aug 2008 10:24:25
Changes:HBX-757 fix
Files:| ... | ...@@ -25,6 +25,8 @@ | |
| 25 | 25 | package org.hibernate.tool.hbm2ddl; |
| 26 | 26 | |
| 27 | 27 | import java.io.FileInputStream; |
| 28 | import java.io.FileWriter; | |
| 29 | import java.io.Writer; | |
| 28 | 30 | import java.sql.Connection; |
| 29 | 31 | import java.sql.SQLException; |
| 30 | 32 | import java.sql.Statement; |
| ... | ...@@ -32,14 +34,20 @@ | |
| 32 | 34 | import java.util.List; |
| 33 | 35 | import java.util.Properties; |
| 34 | 36 | |
| 35 | import org.slf4j.Logger; | |
| 36 | import org.slf4j.LoggerFactory; | |
| 37 | 37 | import org.hibernate.HibernateException; |
| 38 | import org.hibernate.JDBCException; | |
| 38 | 39 | import org.hibernate.cfg.Configuration; |
| 40 | import org.hibernate.cfg.Environment; | |
| 39 | 41 | import org.hibernate.cfg.NamingStrategy; |
| 40 | 42 | import org.hibernate.cfg.Settings; |
| 41 | 43 | import org.hibernate.dialect.Dialect; |
| 44 | import org.hibernate.jdbc.util.FormatStyle; | |
| 45 | import org.hibernate.jdbc.util.Formatter; | |
| 46 | import org.hibernate.jdbc.util.SQLStatementLogger; | |
| 47 | import org.hibernate.util.PropertiesHelper; | |
| 42 | 48 | import org.hibernate.util.ReflectHelper; |
| 49 | import org.slf4j.Logger; | |
| 50 | import org.slf4j.LoggerFactory; | |
| 43 | 51 | |
| 44 | 52 | /** |
| 45 | 53 | * A commandline tool to update a database schema. May also be called from |
| ... | ...@@ -54,6 +62,12 @@ | |
| 54 | 62 | private Configuration configuration; |
| 55 | 63 | private Dialect dialect; |
| 56 | 64 | private List exceptions; |
| 65 | private boolean haltOnError = false; | |
| 66 | private boolean format = true; | |
| 67 | private String outputFile = null; | |
| 68 | private String delimiter; | |
| 69 | private Formatter formatter; | |
| 70 | private SQLStatementLogger sqlStatementLogger; | |
| 57 | 71 | |
| 58 | 72 | public SchemaUpdate(Configuration cfg) throws HibernateException { |
| 59 | 73 | this( cfg, cfg.getProperties() ); |
| ... | ...@@ -67,6 +81,7 @@ | |
| 67 | 81 | props.putAll( connectionProperties ); |
| 68 | 82 | connectionHelper = new ManagedProviderConnectionHelper( props ); |
| 69 | 83 | exceptions = new ArrayList(); |
| 84 | formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter(); | |
| 70 | 85 | } |
| 71 | 86 | |
| 72 | 87 | public SchemaUpdate(Configuration cfg, Settings settings) throws HibernateException { |
| ... | ...@@ -76,6 +91,8 @@ | |
| 76 | 91 | settings.getConnectionProvider() |
| 77 | 92 | ); |
| 78 | 93 | exceptions = new ArrayList(); |
| 94 | sqlStatementLogger = settings.getSqlStatementLogger(); | |
| 95 | formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter(); | |
| 79 | 96 | } |
| 80 | 97 | |
| 81 | 98 | public static void main(String[] args) { |
| ... | ...@@ -139,6 +156,7 @@ | |
| 139 | 156 | |
| 140 | 157 | Connection connection = null; |
| 141 | 158 | Statement stmt = null; |
| 159 | Writer outputFileWriter = null; | |
| 142 | 160 | |
| 143 | 161 | exceptions.clear(); |
| 144 | 162 | |
| ... | ...@@ -160,20 +178,36 @@ | |
| 160 | 178 | |
| 161 | 179 | log.info( "updating schema" ); |
| 162 | 180 | |
| 181 | ||
| 182 | if ( outputFile != null ) { | |
| 183 | log.info( "writing generated schema to file: " + outputFile ); | |
| 184 | outputFileWriter = new FileWriter( outputFile ); | |
| 185 | } | |
| 186 | ||
| 163 | 187 | String[] createSQL = configuration.generateSchemaUpdateScript( dialect, meta ); |
| 164 | 188 | for ( int j = 0; j < createSQL.length; j++ ) { |
| 165 | 189 | |
| 166 | 190 | final String sql = createSQL[j]; |
| 191 | String formatted = formatter.format( sql ); | |
| 167 | 192 | try { |
| 193 | if ( delimiter != null ) { | |
| 194 | formatted += delimiter; | |
| 195 | } | |
| 168 | 196 | if ( script ) { |
| 169 | System.out.println( sql ); | |
| 197 | System.out.println( formatted ); | |
| 198 | } | |
| 199 | if ( outputFile != null ) { | |
| 200 | outputFileWriter.write( formatted + "\n" ); | |
| 170 | 201 | } |
| 171 | 202 | if ( doUpdate ) { |
| 172 | 203 | log.debug( sql ); |
| 173 | stmt.executeUpdate( sql ); | |
| 204 | stmt.executeUpdate( formatted ); | |
| 174 | 205 | } |
| 175 | 206 | } |
| 176 | 207 | catch ( SQLException e ) { |
| 208 | if ( haltOnError ) { | |
| 209 | throw new JDBCException( "Error during DDL export", e ); | |
| 210 | } | |
| 177 | 211 | exceptions.add( e ); |
| 178 | 212 | log.error( "Unsuccessful: " + sql ); |
| 179 | 213 | log.error( e.getMessage() ); |
| ... | ...@@ -199,7 +233,15 @@ | |
| 199 | 233 | exceptions.add( e ); |
| 200 | 234 | log.error( "Error closing connection", e ); |
| 201 | 235 | } |
| 202 | ||
| 236 | try { | |
| 237 | if( outputFileWriter != null ) { | |
| 238 | outputFileWriter.close(); | |
| 239 | } | |
| 240 | } | |
| 241 | catch(Exception e) { | |
| 242 | exceptions.add(e); | |
| 243 | log.error( "Error closing connection", e ); | |
| 244 | } | |
| 203 | 245 | } |
| 204 | 246 | } |
| 205 | 247 | |
| ... | ...@@ -211,4 +253,21 @@ | |
| 211 | 253 | public List getExceptions() { |
| 212 | 254 | return exceptions; |
| 213 | 255 | } |
| 256 | ||
| 257 | public void setHaltOnError(boolean haltOnError) { | |
| 258 | this.haltOnError = haltOnError; | |
| 259 | } | |
| 260 | ||
| 261 | public void setFormat(boolean format) { | |
| 262 | this.formatter = ( format ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter(); | |
| 263 | } | |
| 264 | ||
| 265 | public void setOutputFile(String outputFile) { | |
| 266 | this.outputFile = outputFile; | |
| 267 | } | |
| 268 | ||
| 269 | public void setDelimiter(String delimiter) { | |
| 270 | this.delimiter = delimiter; | |
| 271 | } | |
| 272 | ||
| 214 | 273 | } |
| ... | ...@@ -70,9 +70,13 @@ | |
| 70 | 70 | private List fileSets = new LinkedList(); |
| 71 | 71 | private File propertiesFile = null; |
| 72 | 72 | private File configurationFile = null; |
| 73 | private File outputFile = null; | |
| 73 | 74 | private boolean quiet = false; |
| 74 | 75 | private boolean text = true; |
| 76 | private boolean haltOnError = false; | |
| 77 | private String delimiter = null; | |
| 75 | 78 | private String namingStrategy = null; |
| 79 | ||
| 76 | 80 | |
| 77 | 81 | public void addFileset(FileSet set) { |
| 78 | 82 | fileSets.add(set); |
| ... | ...@@ -122,6 +126,8 @@ | |
| 122 | 126 | */ |
| 123 | 127 | public void execute() throws BuildException { |
| 124 | 128 | try { |
| 129 | log("Running Hibernate Core SchemaUpdate."); | |
| 130 | log("This is an Ant task supporting only mapping files, if you want to use annotations see http://tools.hibernate.org."); | |
| 125 | 131 | Configuration cfg = getConfiguration(); |
| 126 | 132 | getSchemaUpdate(cfg).execute(!quiet, !text); |
| 127 | 133 | } |
| ... | ...@@ -195,11 +201,39 @@ | |
| 195 | 201 | properties.load( new FileInputStream(propertiesFile) ); |
| 196 | 202 | } |
| 197 | 203 | cfg.setProperties(properties); |
| 198 | return new SchemaUpdate(cfg); | |
| 204 | SchemaUpdate su = new SchemaUpdate(cfg); | |
| 205 | su.setOutputFile( outputFile.getPath() ); | |
| 206 | su.setDelimiter(delimiter); | |
| 207 | su.setHaltOnError(haltOnError); | |
| 208 | return su; | |
| 199 | 209 | } |
| 200 | 210 | |
| 201 | 211 | public void setNamingStrategy(String namingStrategy) { |
| 202 | 212 | this.namingStrategy = namingStrategy; |
| 203 | 213 | } |
| 204 | 214 | |
| 215 | public File getOutputFile() { | |
| 216 | return outputFile; | |
| 217 | } | |
| 218 | ||
| 219 | public void setOutputFile(File outputFile) { | |
| 220 | this.outputFile = outputFile; | |
| 221 | } | |
| 222 | ||
| 223 | public boolean isHaltOnError() { | |
| 224 | return haltOnError; | |
| 225 | } | |
| 226 | ||
| 227 | public void setHaltOnError(boolean haltOnError) { | |
| 228 | this.haltOnError = haltOnError; | |
| 229 | } | |
| 230 | ||
| 231 | public String getDelimiter() { | |
| 232 | return delimiter; | |
| 233 | } | |
| 234 | ||
| 235 | public void setDelimiter(String delimiter) { | |
| 236 | this.delimiter = delimiter; | |
| 237 | } | |
| 238 | ||
| 205 | 239 | } |