1 package org.apache.torque.task;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Iterator;
58 import java.util.NoSuchElementException;
59 import java.sql.Connection;
60 import java.sql.DriverManager;
61 import java.sql.SQLException;
62 import java.sql.Statement;
63 import org.apache.tools.ant.Project;
64 import org.apache.velocity.context.Context;
65
66 import com.workingdogs.village.QueryDataSet;
67 import com.workingdogs.village.Record;
68
69 /***
70 * An extended Texen task used for dumping data from db into XML
71 *
72 * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
73 * @author <a href="jvanzyl@zenplex.com">Jason van Zyl</a>
74 * @author <a href="dlr@finemaltcoding.com">Daniel Rall</a>
75 * @version $Id: TorqueDataDumpTask.java,v 1.3 2003/07/25 16:47:42 henning Exp $
76 */
77 public class TorqueDataDumpTask extends TorqueDataModelTask
78 {
79 /***
80 * Database name.
81 */
82 private String databaseName;
83
84 /***
85 * Database URL used for JDBC connection.
86 */
87 private String databaseUrl;
88
89 /***
90 * Database driver used for JDBC connection.
91 */
92 private String databaseDriver;
93
94 /***
95 * Database user used for JDBC connection.
96 */
97 private String databaseUser;
98
99 /***
100 * Database password used for JDBC connection.
101 */
102 private String databasePassword;
103
104 /***
105 * The database connection used to retrieve the data to dump.
106 */
107 private Connection conn;
108
109 /***
110 * The statement used to acquire the data to dump.
111 */
112 private Statement stmt;
113
114 /***
115 * Get the database name to dump
116 *
117 * @return The DatabaseName value
118 */
119 public String getDatabaseName()
120 {
121 return databaseName;
122 }
123
124 /***
125 * Set the database name
126 *
127 * @param v The new DatabaseName value
128 */
129 public void setDatabaseName(String v)
130 {
131 databaseName = v;
132 }
133
134 /***
135 * Get the database url
136 *
137 * @return The DatabaseUrl value
138 */
139 public String getDatabaseUrl()
140 {
141 return databaseUrl;
142 }
143
144 /***
145 * Set the database url
146 *
147 * @param v The new DatabaseUrl value
148 */
149 public void setDatabaseUrl(String v)
150 {
151 databaseUrl = v;
152 }
153
154 /***
155 * Get the database driver name
156 *
157 * @return String database driver name
158 */
159 public String getDatabaseDriver()
160 {
161 return databaseDriver;
162 }
163
164 /***
165 * Set the database driver name
166 *
167 * @param v The new DatabaseDriver value
168 */
169 public void setDatabaseDriver(String v)
170 {
171 databaseDriver = v;
172 }
173
174 /***
175 * Get the database user
176 *
177 * @return String database user
178 */
179 public String getDatabaseUser()
180 {
181 return databaseUser;
182 }
183
184 /***
185 * Set the database user
186 *
187 * @param v The new DatabaseUser value
188 */
189 public void setDatabaseUser(String v)
190 {
191 databaseUser = v;
192 }
193
194 /***
195 * Get the database password
196 *
197 * @return String database password
198 */
199 public String getDatabasePassword()
200 {
201 return databasePassword;
202 }
203
204 /***
205 * Set the database password
206 *
207 * @param v The new DatabasePassword value
208 */
209 public void setDatabasePassword(String v)
210 {
211 databasePassword = v;
212 }
213
214 /***
215 * Initializes initial context
216 *
217 * @return the context
218 * @throws Exception generic exception
219 */
220 public Context initControlContext() throws Exception
221 {
222 super.initControlContext();
223
224 context.put("dataset", "all");
225
226 log("Torque - TorqueDataDump starting");
227 log("Your DB settings are:");
228 log("driver: " + databaseDriver);
229 log("URL: " + databaseUrl);
230 log("user: " + databaseUser);
231 // log("password: " + databasePassword);
232
233 try
234 {
235 Class.forName(databaseDriver);
236 log("DB driver instantiated sucessfully", Project.MSG_DEBUG);
237
238 conn = DriverManager.getConnection(
239 databaseUrl, databaseUser, databasePassword);
240
241 log("DB connection established", Project.MSG_DEBUG);
242 context.put("tableTool", new TableTool());
243 }
244 catch (SQLException se)
245 {
246 System.err.println("SQLException while connecting to DB:");
247 se.printStackTrace();
248 }
249 catch (ClassNotFoundException cnfe)
250 {
251 System.err.println("cannot load driver:");
252 cnfe.printStackTrace();
253 }
254 context.put("escape", new org.apache.velocity.anakia.Escape());
255 return context;
256 }
257
258 /***
259 * Closes the db-connection, overriding the <code>cleanup()</code> hook
260 * method in <code>TexenTask</code>.
261 *
262 * @throws Exception Database problem while closing resource.
263 */
264 protected void cleanup() throws Exception
265 {
266 if (stmt != null)
267 {
268 stmt.close();
269 }
270
271 if (conn != null)
272 {
273 conn.close();
274 }
275 }
276
277 /***
278 * A nasty do-it-all tool class. It serves as:
279 * <ul>
280 * <li>context tool to fetch a table iterator</li>
281 * <li>the abovenamed iterator which iterates over the table</li>
282 * <li>getter for the table fields</li>
283 * </ul>
284 *
285 * @author fedor
286 */
287 public class TableTool implements Iterator
288 {
289 /*** querydataset */
290 private QueryDataSet qds;
291 /*** is empty */
292 private boolean isEmpty;
293 /*** current index */
294 private int curIndex = -1;
295 /*** current record */
296 private Record curRec = null;
297
298 /***
299 * Constructor for the TableTool object
300 */
301 public TableTool()
302 {
303 }
304
305 /***
306 * Constructor for the TableTool object
307 *
308 * @param qds Description of Parameter
309 * @throws Exception Problem using database record set cursor.
310 */
311 protected TableTool(QueryDataSet qds) throws Exception
312 {
313 this.qds = qds;
314 this.qds.fetchRecords();
315 this.isEmpty = !(qds.size() > 0);
316 }
317
318 /***
319 * Fetches an <code>Iterator</code> for the data in the named table.
320 *
321 * @param tableName Description of Parameter
322 * @return <code>Iterator</code> for the fetched data.
323 * @throws Exception Problem creating connection or executing query.
324 */
325 public TableTool fetch(String tableName) throws Exception
326 {
327 log("Fetching data for table " + tableName, Project.MSG_INFO);
328 // Set Statement object in associated TorqueDataDump
329 // instance
330 return new TableTool
331 (new QueryDataSet(conn, "SELECT * FROM " + tableName));
332 }
333
334 /***
335 * check if there are more records in the QueryDataSet
336 *
337 * @return true if there are more records
338 */
339 public boolean hasNext()
340 {
341 try
342 {
343 return ((this.curIndex < this.qds.size() - 1) && (!isEmpty));
344 }
345 catch (Exception se)
346 {
347 System.err.println("Exception :");
348 se.printStackTrace();
349 }
350 return false;
351 }
352
353 /***
354 * load the next record from the QueryDataSet
355 *
356 * @return Description of the Returned Value
357 * @throws NoSuchElementException Description of Exception
358 */
359 public Object next() throws NoSuchElementException
360 {
361 try
362 {
363 System.err.print(".");
364 this.curRec = this.qds.getRecord(++curIndex);
365 }
366 catch (Exception se)
367 {
368 System.err.println("Exception while iterating:");
369 se.printStackTrace();
370 throw new NoSuchElementException(se.getMessage());
371 }
372 return this;
373 }
374
375 /***
376 * Returns the value for the column
377 *
378 * @param columnName name of the column
379 * @return value of the column or null if it doesn't exist
380 */
381 public String get(String columnName)
382 {
383 try
384 {
385 return (this.curRec.getValue(columnName).asString());
386 }
387 catch (Exception se)
388 {
389 log("Exception fetching value " + columnName + ": "
390 + se.getMessage(), Project.MSG_ERR);
391 }
392 return null;
393 }
394
395 /***
396 * unsupported! always throws Exception
397 *
398 * @throws UnsupportedOperationException unsupported
399 */
400 public void remove() throws UnsupportedOperationException
401 {
402 throw new UnsupportedOperationException();
403 }
404 }
405 }
This page was automatically generated by Maven