1 package org.apache.torque.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 /***
33 * An extended Texen task used for dumping data from db into XML
34 *
35 * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
36 * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
37 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
38 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
39 * @version $Id: TorqueDataDumpTask.java 239624 2005-08-24 12:18:03Z henning $
40 */
41 public class TorqueDataDumpTask extends TorqueDataModelTask
42 {
43 /*** Database name. */
44 private String databaseName;
45
46 /*** Database URL used for JDBC connection. */
47 private String databaseUrl;
48
49 /*** Database driver used for JDBC connection. */
50 private String databaseDriver;
51
52 /*** Database user used for JDBC connection. */
53 private String databaseUser;
54
55 /*** Database password used for JDBC connection. */
56 private String databasePassword;
57
58 /*** The database connection used to retrieve the data to dump. */
59 private Connection conn;
60
61 /*** The statement used to acquire the data to dump. */
62 private Statement stmt;
63
64 /***
65 * Get the database name to dump
66 *
67 * @return The DatabaseName value
68 */
69 public String getDatabaseName()
70 {
71 return databaseName;
72 }
73
74 /***
75 * Set the database name
76 *
77 * @param v The new DatabaseName value
78 */
79 public void setDatabaseName(String v)
80 {
81 databaseName = v;
82 }
83
84 /***
85 * Get the database url
86 *
87 * @return The DatabaseUrl value
88 */
89 public String getDatabaseUrl()
90 {
91 return databaseUrl;
92 }
93
94 /***
95 * Set the database url
96 *
97 * @param v The new DatabaseUrl value
98 */
99 public void setDatabaseUrl(String v)
100 {
101 databaseUrl = v;
102 }
103
104 /***
105 * Get the database driver name
106 *
107 * @return String database driver name
108 */
109 public String getDatabaseDriver()
110 {
111 return databaseDriver;
112 }
113
114 /***
115 * Set the database driver name
116 *
117 * @param v The new DatabaseDriver value
118 */
119 public void setDatabaseDriver(String v)
120 {
121 databaseDriver = v;
122 }
123
124 /***
125 * Get the database user
126 *
127 * @return String database user
128 */
129 public String getDatabaseUser()
130 {
131 return databaseUser;
132 }
133
134 /***
135 * Set the database user
136 *
137 * @param v The new DatabaseUser value
138 */
139 public void setDatabaseUser(String v)
140 {
141 databaseUser = v;
142 }
143
144 /***
145 * Get the database password
146 *
147 * @return String database password
148 */
149 public String getDatabasePassword()
150 {
151 return databasePassword;
152 }
153
154 /***
155 * Set the database password
156 *
157 * @param v The new DatabasePassword value
158 */
159 public void setDatabasePassword(String v)
160 {
161 databasePassword = v;
162 }
163
164 /***
165 * Initializes initial context
166 *
167 * @return the context
168 * @throws Exception generic exception
169 */
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
182
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 /***
211 * Closes the db-connection, overriding the <code>cleanup()</code> hook
212 * method in <code>TexenTask</code>.
213 *
214 * @throws Exception Database problem while closing resource.
215 */
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 /***
230 * A nasty do-it-all tool class. It serves as:
231 * <ul>
232 * <li>context tool to fetch a table iterator</li>
233 * <li>the abovenamed iterator which iterates over the table</li>
234 * <li>getter for the table fields</li>
235 * </ul>
236 */
237 public class TableTool implements Iterator
238 {
239 /*** querydataset */
240 private ResultSet rs;
241
242 /***
243 * Constructor for the TableTool object
244 */
245 public TableTool()
246 {
247 }
248
249 /***
250 * Constructor for the TableTool object
251 *
252 * @param qds Description of Parameter
253 * @throws Exception Problem using database record set cursor.
254 */
255 protected TableTool(ResultSet rs) throws Exception
256 {
257 this.rs = rs;
258 }
259
260 /***
261 * Fetches an <code>Iterator</code> for the data in the named table.
262 *
263 * @param tableName Description of Parameter
264 * @return <code>Iterator</code> for the fetched data.
265 * @throws Exception Problem creating connection or executing query.
266 */
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 /***
275 * check if there are more records in the QueryDataSet
276 *
277 * @return true if there are more records
278 */
279 public boolean hasNext()
280 {
281 try
282 {
283
284
285
286 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 /***
299 * load the next record from the QueryDataSet
300 *
301 * @return Description of the Returned Value
302 * @throws NoSuchElementException Description of Exception
303 */
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 /***
321 * Returns the value for the column
322 *
323 * @param columnName name of the column
324 * @return value of the column or null if it doesn't exist
325 */
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 /***
341 * unsupported! always throws Exception
342 *
343 * @throws UnsupportedOperationException unsupported
344 */
345 public void remove() throws UnsupportedOperationException
346 {
347 throw new UnsupportedOperationException();
348 }
349 }
350 }