1 2 12 package com.versant.core.jdo.tools.ant; 13 14 import com.versant.core.jdbc.JdbcStorageManager; 15 import com.versant.core.jdbc.JdbcConnectionSource; 16 import com.versant.core.jdbc.JdbcStorageManagerFactory; 17 import com.versant.core.jdbc.metadata.JdbcTable; 18 import com.versant.core.util.BeanUtils; 19 20 import java.io.*; 21 import java.sql.Connection ; 22 import java.sql.SQLException ; 23 import java.util.*; 24 25 import com.versant.core.logging.LogEventStore; 26 27 36 public class CreateJdbcSchemaTask extends JdoTaskBase { 37 38 public static void main(String [] args) { 39 try { 40 CreateJdbcSchemaTask t = new CreateJdbcSchemaTask(); 41 BeanUtils.setCommandLineArgs(t, args, new String []{ 42 "config", "project", "outputdir", "destdir", "direct", 43 "droptables", "createtables", "validate", "comments", 44 "out" 45 }); 46 t.execute(); 47 } catch (IllegalArgumentException x) { 48 System.err.println(x.getMessage()); 49 System.err.println(HELP); 50 } catch (Exception x) { 51 x.printStackTrace(); 52 } 53 } 54 55 private static final String HELP = 56 "Usage: java com.versant.core.jdo.tools.ant.CreateJdbcSchemaTask\n" + 57 " [-out <name of file for SQL script, default versant.sql>]\n" + 58 " [-droptables <true | false>]\n" + 59 " [-createtables <true | false>]\n\n" + 60 " [-validate <true | false>]\n\n" + 61 " [-comments <true | false>]\n\n" + 62 " [-project <name of project file, default versant.properties>]\n" + 63 "WARNING: The droptables option will drop all tables with the same\n" + 64 " names (case insensitive) as tables in the generated schema!\n\n" + 65 "This Class can also be used as an Ant task. The command line arguments\n" + 66 "have the same names and functions as the task attributes.\n"; 67 68 protected String outName = "versant.sql"; 69 protected boolean direct; 70 protected boolean droptables; 71 protected boolean validate; 72 protected boolean comments = true; 73 private String logEvents = LogEventStore.LOG_EVENTS_ERRORS; 74 75 public void setOut(String out) { 76 this.outName = out; 77 } 78 79 public void setOutputdir(String dir) { 80 setDestdir(dir); 81 } 82 83 public void setDestdir(String destdir) { 84 this.outName = destdir + "/versant.sql"; 85 } 86 87 public void setCreatetables(String direct) { 88 setDirect(direct); 89 } 90 91 public void setDirect(String s) { 92 direct = isTrue(s); 93 } 94 95 private static boolean isTrue(String s) { 96 return "*".equals(s) || "true".equals(s); 97 } 98 99 public void setDroptables(String s) { 100 droptables = isTrue(s); 101 } 102 103 public void setValidate(String s) { 104 validate = isTrue(s); 105 } 106 107 public void setLogEvents(String logEvents) { 108 this.logEvents = logEvents; 109 } 110 111 114 public void setComments(boolean comments) { 115 this.comments = comments; 116 } 117 118 public void execute() { 119 super.execute(); 120 if (!(innermostSmf instanceof JdbcStorageManagerFactory)) { 121 return; 122 } 123 pes.setLogEvents(logEvents); 124 JdbcStorageManager sm = (JdbcStorageManager)innermostSmf.getStorageManager(); 125 if (droptables) dropAllTables(sm); 126 generateDatabase(sm, direct); 127 if (validate) validateDatabase(sm); 128 innermostSmf.returnStorageManager(sm); 129 } 130 131 private void dropAllTables(JdbcStorageManager sm) { 132 JdbcConnectionSource conSrc = sm.getJdbcConnectionSource(); 133 Connection con = null; 134 try { 135 log("Dropping tables in schema on " + conSrc.getURL()); 136 con = conSrc.getConnection(false, true); 137 HashMap dbTableNames = sm.getDatabaseTableNames(con); 138 ArrayList a = sm.getJdbcMetaData().getTables(); 139 for (int i = 0; i < a.size(); i++) { 140 JdbcTable t = (JdbcTable)a.get(i); 141 String name = (String )dbTableNames.get(t.name.toLowerCase()); 142 if (name != null) { 143 sm.getSqlDriver().dropTable(con, name); 144 } 145 } 146 } catch (SQLException x) { 147 throwBuildException(x.getClass().getName() + ": " + 148 x.getMessage(), x); 149 } finally { 150 if (con != null) { 151 try { 152 conSrc.returnConnection(con); 153 } catch (SQLException e) { 154 } 156 } 157 } 158 } 159 160 private void generateDatabase(JdbcStorageManager sm, boolean direct) { 161 JdbcConnectionSource conSrc = sm.getJdbcConnectionSource(); 162 Connection con = null; 163 FileOutputStream fout = null; 164 PrintWriter out = null; 165 try { 166 File f = new File(outName); 167 log("Creating " + f); 168 fout = new FileOutputStream(f); 169 out = new PrintWriter(fout); 170 if (direct) { 171 log("Creating schema on " + conSrc.getURL()); 172 con = conSrc.getConnection(false, true); 173 } 174 sm.getSqlDriver().generateDDL(sm.getJdbcMetaData().getTables(), 175 con, out, comments); 176 if (out != null) { 177 out.flush(); 178 } 179 } catch (Exception x) { 180 throwBuildException(x.getClass().getName() + ": " + 181 x.getMessage(), x); 182 } finally { 183 if (fout != null) { 184 try { 185 fout.close(); 186 } catch (IOException e) { 187 } 189 } 190 if (con != null) { 191 try { 192 conSrc.returnConnection(con); 193 } catch (SQLException e) { 194 } 196 } 197 } 198 } 199 200 private void validateDatabase(JdbcStorageManager sm) { 201 JdbcConnectionSource conSrc = sm.getJdbcConnectionSource(); 202 Connection con = null; 203 try { 204 log("Validating mapping for " + conSrc.getURL()); 205 con = conSrc.getConnection(false, false); 206 StringWriter error = new StringWriter(); 207 PrintWriter perror = new PrintWriter(error, false); 208 StringWriter fix = new StringWriter(); 209 PrintWriter pfix = new PrintWriter(fix, false); 210 if (!sm.getSqlDriver().checkDDL( 211 sm.getJdbcMetaData().getTables(true), con, perror, pfix, 212 sm.getJdbcMetaData().getMigrationParams())) { 213 log(error.toString()); 214 throwBuildException("Mapping for " + 215 conSrc.getURL() + " has errors"); 216 } 217 } catch (Exception x) { 218 throwBuildException(x.getClass().getName() + ": " + 219 x.getMessage(), x); 220 } finally { 221 if (con != null) { 222 try { 223 conSrc.returnConnection(con); 224 } catch (SQLException e) { 225 } 227 } 228 } 229 } 230 231 } 232 | Popular Tags |