1 56 package org.objectstyle.cayenne.tools; 57 58 import java.io.File ; 59 import java.sql.Driver ; 60 61 import org.apache.tools.ant.BuildException; 62 import org.apache.tools.ant.Project; 63 import org.objectstyle.cayenne.access.DbGenerator; 64 import org.objectstyle.cayenne.conn.DriverDataSource; 65 import org.objectstyle.cayenne.dba.DbAdapter; 66 import org.objectstyle.cayenne.dba.JdbcAdapter; 67 import org.objectstyle.cayenne.map.DataMap; 68 import org.objectstyle.cayenne.map.MapLoader; 69 import org.objectstyle.cayenne.util.Util; 70 import org.xml.sax.InputSource ; 71 72 79 public class DbGeneratorTask extends CayenneTask { 81 82 protected DbAdapter adapter; 83 protected File map; 84 protected String driver; 85 protected String url; 86 protected String userName; 87 protected String password; 88 89 protected boolean dropTables; 92 protected boolean dropPK; 93 protected boolean createTables = true; 94 protected boolean createPK = true; 95 protected boolean createFK = true; 96 97 98 public void execute() { 99 configureLogging(); 100 101 if (adapter == null) { 103 adapter = new JdbcAdapter(); 104 } 105 106 log("connection settings - [driver: " 107 + driver 108 + ", url: " 109 + url 110 + ", username: " 111 + userName 112 + "]", Project.MSG_VERBOSE); 113 114 log("generator options - [dropTables: " 115 + dropTables 116 + ", dropPK: " 117 + dropPK 118 + ", createTables: " 119 + createTables 120 + ", createPK: " 121 + createPK 122 + ", createFK: " 123 + createFK 124 + "]", Project.MSG_VERBOSE); 125 126 validateAttributes(); 127 128 try { 129 130 DataMap dataMap = loadDataMap(); 132 DbGenerator generator = new DbGenerator(adapter, dataMap); 133 generator.setShouldCreateFKConstraints(createFK); 134 generator.setShouldCreatePKSupport(createPK); 135 generator.setShouldCreateTables(createTables); 136 generator.setShouldDropPKSupport(dropPK); 137 generator.setShouldDropTables(dropTables); 138 139 DriverDataSource dataSource = new DriverDataSource((Driver ) Class.forName( 141 driver).newInstance(), url, userName, password); 142 143 generator.runGenerator(dataSource); 144 } 145 catch (Exception ex) { 146 Throwable th = Util.unwindException(ex); 147 148 String message = "Error generating database"; 149 150 if (th.getLocalizedMessage() != null) { 151 message += ": " + th.getLocalizedMessage(); 152 } 153 154 super.log(message); 155 throw new BuildException(message, th); 156 } 157 } 158 159 163 protected void validateAttributes() throws BuildException { 164 StringBuffer error = new StringBuffer (""); 165 166 if (map == null) { 167 error.append("The 'map' attribute must be set.\n"); 168 } 169 170 if (driver == null) { 171 error.append("The 'driver' attribute must be set.\n"); 172 } 173 174 if (url == null) { 175 error.append("The 'adapter' attribute must be set.\n"); 176 } 177 178 if (error.length() > 0) { 179 throw new BuildException(error.toString()); 180 } 181 } 182 183 184 protected DataMap loadDataMap() throws Exception { 185 InputSource in = new InputSource (map.getCanonicalPath()); 186 return new MapLoader().loadDataMap(in); 187 } 188 189 public void setCreateFK(boolean createFK) { 190 this.createFK = createFK; 191 } 192 193 public void setCreatePK(boolean createPK) { 194 this.createPK = createPK; 195 } 196 197 public void setCreateTables(boolean createTables) { 198 this.createTables = createTables; 199 } 200 201 public void setDropPK(boolean dropPK) { 202 this.dropPK = dropPK; 203 } 204 205 public void setDropTables(boolean dropTables) { 206 this.dropTables = dropTables; 207 } 208 209 214 public void setMap(File map) { 215 this.map = map; 216 } 217 218 223 public void setAdapter(String adapter) { 224 225 if (adapter != null) { 226 try { 228 Class c = Class.forName(adapter); 229 this.adapter = (DbAdapter) c.newInstance(); 230 } 231 catch (Exception e) { 232 throw new BuildException("Can't load DbAdapter: " + adapter); 233 } 234 } 235 } 236 237 242 public void setDriver(String driver) { 243 this.driver = driver; 244 } 245 246 251 public void setUrl(String url) { 252 this.url = url; 253 } 254 255 260 public void setUserName(String username) { 261 this.userName = username; 262 } 263 264 269 public void setPassword(String password) { 270 this.password = password; 271 } 272 273 } | Popular Tags |