KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > ant > SchemaMigrationTask


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.tools.ant;
13
14
15 import org.apache.tools.ant.BuildException;
16
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.Statement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
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 /**
36  */

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 JavaDoc outPutdir = null;
49     private boolean direct = false;
50
51     public void setOutputdir(String JavaDoc dir) {
52         outPutdir = dir;
53     }
54
55     public void setDirect(boolean direct) {
56         this.direct = direct;
57     }
58
59     public void setDatastore(String JavaDoc datastore) {
60         // ignore - keep property for compatibility
61
}
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 JavaDoc 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 JavaDoc getFileName(SqlDriver sqlDriver){
120         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("dd-MM-yyyy_H-m");
121         return "Schema_Migration_" + sqlDriver.getName() + "_" + formatter.format(new Date JavaDoc());
122     }
123
124     private void migrateDatabase()
125
126         throws BuildException
127
128         {
129         JdbcStorageManagerFactory jsmf = (JdbcStorageManagerFactory)innermostSmf;
130         JdbcConnectionSource conSrc = jsmf.getConnectionSource();
131         Connection JavaDoc con = null;
132         FileOutputStream JavaDoc fout = null;
133         PrintWriter JavaDoc out = null;
134         try {
135             log("Checking schema on " + conSrc.getURL());
136             StringWriter JavaDoc error = new StringWriter JavaDoc();
137             PrintWriter JavaDoc perror = new PrintWriter JavaDoc(error, false);
138
139             StringWriter JavaDoc fix = new StringWriter JavaDoc();
140             PrintWriter JavaDoc pfix = new PrintWriter JavaDoc(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 JavaDoc f = new File JavaDoc(outPutdir, getFileName(jsmf.getSqlDriver()) + ".sql");
157                     fout = new FileOutputStream JavaDoc(f);
158                     out = new PrintWriter JavaDoc(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 JavaDoc 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 JavaDoc e) {
177                     // ignore
178
}
179             }
180         }
181     }
182
183     public void runScript(Connection JavaDoc con, String JavaDoc script) throws Exception JavaDoc {
184         if (!con.getAutoCommit()) {
185             con.rollback();
186             con.setAutoCommit(true);
187         }
188
189         SQLScriptParser shredder = new SQLScriptParser();
190         ArrayList JavaDoc list = shredder.parse(script, true);
191         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
192             SQLScriptParser.SQLScriptPart scriptPart = (SQLScriptParser.SQLScriptPart) iter.next();
193             log("Executing: " + scriptPart.getSql());
194             Statement JavaDoc stat = null;
195             try {
196                 stat = con.createStatement();
197                 stat.execute(scriptPart.getSql());
198             } finally {
199                 try {
200                     stat.close();
201                 } catch (SQLException JavaDoc e) {
202                     //hide
203
}
204             }
205         }
206     }
207 }
208
Popular Tags