1 2 12 package com.versant.core.jdo.tools.ant; 13 14 15 import org.apache.tools.ant.BuildException; 16 17 18 import java.sql.Connection ; 19 import java.sql.Statement ; 20 import java.sql.SQLException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Date ; 24 import java.io.StringWriter ; 25 import java.io.PrintWriter ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.text.SimpleDateFormat ; 29 30 import com.versant.core.jdbc.sql.diff.ControlParams; 31 import com.versant.core.jdbc.sql.SqlDriver; 32 import com.versant.core.jdbc.JdbcConnectionSource; 33 import com.versant.core.jdbc.JdbcStorageManagerFactory; 34 35 37 public class SchemaMigrationTask extends JdoTaskBase { 38 39 private ControlParams params = null; 40 private boolean checkLength = true; 41 private boolean checkType = true; 42 private boolean checkScale = true; 43 private boolean checkNulls = true; 44 private boolean checkPK = true; 45 private boolean checkIndex = true; 46 private boolean checkConstraint = true; 47 private boolean checkExtraColumns = true; 48 private String outPutdir = null; 49 private boolean direct = false; 50 51 public void setOutputdir(String dir) { 52 outPutdir = dir; 53 } 54 55 public void setDirect(boolean direct) { 56 this.direct = direct; 57 } 58 59 public void setDatastore(String datastore) { 60 } 62 63 public void setCheckConstraint(boolean checkConstraint) { 64 this.checkConstraint = checkConstraint; 65 } 66 67 public void setCheckExtraColumns(boolean checkExtraColumns) { 68 this.checkExtraColumns = checkExtraColumns; 69 } 70 71 public void setCheckIndex(boolean checkIndex) { 72 this.checkIndex = checkIndex; 73 } 74 75 public void setCheckLength(boolean checkLength) { 76 this.checkLength = checkLength; 77 } 78 79 public void setCheckNulls(boolean checkNulls) { 80 this.checkNulls = checkNulls; 81 } 82 83 public void setCheckPK(boolean checkPK) { 84 this.checkPK = checkPK; 85 } 86 87 public void setCheckScale(boolean checkScale) { 88 this.checkScale = checkScale; 89 } 90 91 public void setCheckType(boolean checkType) { 92 this.checkType = checkType; 93 } 94 95 public void setOutPutdir(String outPutdir) { 96 this.outPutdir = outPutdir; 97 } 98 99 public void execute() 100 101 throws BuildException 102 103 { 104 super.execute(); 105 106 params = new ControlParams(); 107 params.setCheckConstraint(checkConstraint); 108 params.setCheckExtraColumns(checkExtraColumns); 109 params.setCheckIndex(checkIndex); 110 params.setCheckLength(checkLength); 111 params.setCheckNulls(checkNulls); 112 params.setCheckPK(checkPK); 113 params.setCheckScale(checkScale); 114 params.setCheckType(checkType); 115 116 migrateDatabase(); 117 } 118 119 private String getFileName(SqlDriver sqlDriver){ 120 SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy_H-m"); 121 return "Schema_Migration_" + sqlDriver.getName() + "_" + formatter.format(new Date ()); 122 } 123 124 private void migrateDatabase() 125 126 throws BuildException 127 128 { 129 JdbcStorageManagerFactory jsmf = (JdbcStorageManagerFactory)innermostSmf; 130 JdbcConnectionSource conSrc = jsmf.getConnectionSource(); 131 Connection con = null; 132 FileOutputStream fout = null; 133 PrintWriter out = null; 134 try { 135 log("Checking schema on " + conSrc.getURL()); 136 StringWriter error = new StringWriter (); 137 PrintWriter perror = new PrintWriter (error, false); 138 139 StringWriter fix = new StringWriter (); 140 PrintWriter pfix = new PrintWriter (fix, false); 141 142 143 con = conSrc.getConnection(false, false); 144 boolean valid = jsmf.getSqlDriver().checkDDL( 145 jsmf.getJdbcMetaData().getTables(), con, perror,pfix, params); 146 perror.close(); 147 pfix.close(); 148 if (valid) { 149 log("Schema is valid."); 150 } else { 151 log("Schema has errors."); 152 log(error.toString()); 153 error.close(); 154 155 if (outPutdir != null) { 156 File f = new File (outPutdir, getFileName(jsmf.getSqlDriver()) + ".sql"); 157 fout = new FileOutputStream (f); 158 out = new PrintWriter (fout); 159 out.write(fix.toString()); 160 out.flush(); 161 out.close(); 162 } 163 164 if (direct){ 165 runScript(con, fix.toString()); 166 } 167 168 } 169 } catch (Exception x) { 170 throwBuildException(x.getClass().getName() + ": " + 171 x.getMessage(), x); 172 } finally { 173 if (con != null) { 174 try { 175 conSrc.returnConnection(con); 176 } catch (SQLException e) { 177 } 179 } 180 } 181 } 182 183 public void runScript(Connection con, String script) throws Exception { 184 if (!con.getAutoCommit()) { 185 con.rollback(); 186 con.setAutoCommit(true); 187 } 188 189 SQLScriptParser shredder = new SQLScriptParser(); 190 ArrayList list = shredder.parse(script, true); 191 for (Iterator iter = list.iterator(); iter.hasNext();) { 192 SQLScriptParser.SQLScriptPart scriptPart = (SQLScriptParser.SQLScriptPart) iter.next(); 193 log("Executing: " + scriptPart.getSql()); 194 Statement stat = null; 195 try { 196 stat = con.createStatement(); 197 stat.execute(scriptPart.getSql()); 198 } finally { 199 try { 200 stat.close(); 201 } catch (SQLException e) { 202 } 204 } 205 } 206 } 207 } 208 | Popular Tags |