1 package org.apache.torque.task; 2 3 18 19 import java.sql.Connection ; 20 import java.sql.DriverManager ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 import java.util.Iterator ; 25 import java.util.NoSuchElementException ; 26 27 import org.apache.tools.ant.Project; 28 29 import org.apache.velocity.context.Context; 30 31 32 41 public class TorqueDataDumpTask extends TorqueDataModelTask 42 { 43 44 private String databaseName; 45 46 47 private String databaseUrl; 48 49 50 private String databaseDriver; 51 52 53 private String databaseUser; 54 55 56 private String databasePassword; 57 58 59 private Connection conn; 60 61 62 private Statement stmt; 63 64 69 public String getDatabaseName() 70 { 71 return databaseName; 72 } 73 74 79 public void setDatabaseName(String v) 80 { 81 databaseName = v; 82 } 83 84 89 public String getDatabaseUrl() 90 { 91 return databaseUrl; 92 } 93 94 99 public void setDatabaseUrl(String v) 100 { 101 databaseUrl = v; 102 } 103 104 109 public String getDatabaseDriver() 110 { 111 return databaseDriver; 112 } 113 114 119 public void setDatabaseDriver(String v) 120 { 121 databaseDriver = v; 122 } 123 124 129 public String getDatabaseUser() 130 { 131 return databaseUser; 132 } 133 134 139 public void setDatabaseUser(String v) 140 { 141 databaseUser = v; 142 } 143 144 149 public String getDatabasePassword() 150 { 151 return databasePassword; 152 } 153 154 159 public void setDatabasePassword(String v) 160 { 161 databasePassword = v; 162 } 163 164 170 public Context initControlContext() throws Exception 171 { 172 super.initControlContext(); 173 174 context.put("dataset", "all"); 175 176 log("Torque - TorqueDataDump starting"); 177 log("Your DB settings are:"); 178 log("driver: " + databaseDriver); 179 log("URL: " + databaseUrl); 180 log("user: " + databaseUser); 181 183 try 184 { 185 Class.forName(databaseDriver); 186 log("DB driver instantiated sucessfully", Project.MSG_DEBUG); 187 188 conn = DriverManager.getConnection( 189 databaseUrl, databaseUser, databasePassword); 190 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 191 ResultSet.CONCUR_UPDATABLE); 192 193 log("DB connection established", Project.MSG_DEBUG); 194 context.put("tableTool", new TableTool()); 195 } 196 catch (SQLException se) 197 { 198 System.err.println("SQLException while connecting to DB:"); 199 se.printStackTrace(); 200 } 201 catch (ClassNotFoundException cnfe) 202 { 203 System.err.println("cannot load driver:"); 204 cnfe.printStackTrace(); 205 } 206 context.put("escape", new org.apache.velocity.anakia.Escape()); 207 return context; 208 } 209 210 216 protected void cleanup() throws Exception 217 { 218 if (stmt != null) 219 { 220 stmt.close(); 221 } 222 223 if (conn != null) 224 { 225 conn.close(); 226 } 227 } 228 229 237 public class TableTool implements Iterator 238 { 239 240 private ResultSet rs; 241 242 245 public TableTool() 246 { 247 } 248 249 255 protected TableTool(ResultSet rs) throws Exception 256 { 257 this.rs = rs; 258 } 259 260 267 public TableTool fetch(String tableName) throws Exception 268 { 269 log("Fetching data for table " + tableName, Project.MSG_INFO); 270 return new TableTool( 271 stmt.executeQuery("SELECT * FROM " + tableName)); 272 } 273 274 279 public boolean hasNext() 280 { 281 try 282 { 283 boolean validRow = rs.next(); 287 rs.previous(); 288 return validRow; 289 } 290 catch (Exception se) 291 { 292 System.err.println("Exception :"); 293 se.printStackTrace(); 294 } 295 return false; 296 } 297 298 304 public Object next() throws NoSuchElementException 305 { 306 try 307 { 308 System.out.print("."); 309 rs.next(); 310 } 311 catch (Exception se) 312 { 313 System.err.println("Exception while iterating:"); 314 se.printStackTrace(); 315 throw new NoSuchElementException (se.getMessage()); 316 } 317 return this; 318 } 319 320 326 public String get(String columnName) 327 { 328 try 329 { 330 return (rs.getString(columnName)); 331 } 332 catch (Exception se) 333 { 334 log("Exception fetching value " + columnName + ": " 335 + se.getMessage(), Project.MSG_ERR); 336 } 337 return null; 338 } 339 340 345 public void remove() throws UnsupportedOperationException 346 { 347 throw new UnsupportedOperationException (); 348 } 349 } 350 } 351 | Popular Tags |