001: package org.apache.torque.task;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027: import java.util.Iterator;
028: import java.util.NoSuchElementException;
029:
030: import org.apache.tools.ant.Project;
031:
032: import org.apache.velocity.context.Context;
033:
034: /**
035: * An extended Texen task used for dumping data from db into XML
036: *
037: * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
038: * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
039: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
040: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
041: * @version $Id: TorqueDataDumpTask.java 473814 2006-11-11 22:30:30Z tv $
042: */
043: public class TorqueDataDumpTask extends TorqueDataModelTask {
044: /** Database name. */
045: private String databaseName;
046:
047: /** Database URL used for JDBC connection. */
048: private String databaseUrl;
049:
050: /** Database driver used for JDBC connection. */
051: private String databaseDriver;
052:
053: /** Database user used for JDBC connection. */
054: private String databaseUser;
055:
056: /** Database password used for JDBC connection. */
057: private String databasePassword;
058:
059: /** The database connection used to retrieve the data to dump. */
060: private Connection conn;
061:
062: /** The statement used to acquire the data to dump. */
063: private Statement stmt;
064:
065: /**
066: * Get the database name to dump
067: *
068: * @return The DatabaseName value
069: */
070: public String getDatabaseName() {
071: return databaseName;
072: }
073:
074: /**
075: * Set the database name
076: *
077: * @param v The new DatabaseName value
078: */
079: public void setDatabaseName(String v) {
080: databaseName = v;
081: }
082:
083: /**
084: * Get the database url
085: *
086: * @return The DatabaseUrl value
087: */
088: public String getDatabaseUrl() {
089: return databaseUrl;
090: }
091:
092: /**
093: * Set the database url
094: *
095: * @param v The new DatabaseUrl value
096: */
097: public void setDatabaseUrl(String v) {
098: databaseUrl = v;
099: }
100:
101: /**
102: * Get the database driver name
103: *
104: * @return String database driver name
105: */
106: public String getDatabaseDriver() {
107: return databaseDriver;
108: }
109:
110: /**
111: * Set the database driver name
112: *
113: * @param v The new DatabaseDriver value
114: */
115: public void setDatabaseDriver(String v) {
116: databaseDriver = v;
117: }
118:
119: /**
120: * Get the database user
121: *
122: * @return String database user
123: */
124: public String getDatabaseUser() {
125: return databaseUser;
126: }
127:
128: /**
129: * Set the database user
130: *
131: * @param v The new DatabaseUser value
132: */
133: public void setDatabaseUser(String v) {
134: databaseUser = v;
135: }
136:
137: /**
138: * Get the database password
139: *
140: * @return String database password
141: */
142: public String getDatabasePassword() {
143: return databasePassword;
144: }
145:
146: /**
147: * Set the database password
148: *
149: * @param v The new DatabasePassword value
150: */
151: public void setDatabasePassword(String v) {
152: databasePassword = v;
153: }
154:
155: /**
156: * Initializes initial context
157: *
158: * @return the context
159: * @throws Exception generic exception
160: */
161: public Context initControlContext() throws Exception {
162: super .initControlContext();
163:
164: context.put("dataset", "all");
165:
166: log("Torque - TorqueDataDump starting");
167: log("Your DB settings are:");
168: log("driver: " + databaseDriver);
169: log("URL: " + databaseUrl);
170: log("user: " + databaseUser);
171: // log("password: " + databasePassword);
172:
173: try {
174: Class.forName(databaseDriver);
175: log("DB driver instantiated sucessfully", Project.MSG_DEBUG);
176:
177: conn = DriverManager.getConnection(databaseUrl,
178: databaseUser, databasePassword);
179: stmt = conn.createStatement(
180: ResultSet.TYPE_SCROLL_INSENSITIVE,
181: ResultSet.CONCUR_UPDATABLE);
182:
183: log("DB connection established", Project.MSG_DEBUG);
184: context.put("tableTool", new TableTool());
185: } catch (SQLException se) {
186: System.err.println("SQLException while connecting to DB:");
187: se.printStackTrace();
188: } catch (ClassNotFoundException cnfe) {
189: System.err.println("cannot load driver:");
190: cnfe.printStackTrace();
191: }
192: context.put("escape", new org.apache.velocity.anakia.Escape());
193: return context;
194: }
195:
196: /**
197: * Closes the db-connection, overriding the <code>cleanup()</code> hook
198: * method in <code>TexenTask</code>.
199: *
200: * @throws Exception Database problem while closing resource.
201: */
202: protected void cleanup() throws Exception {
203: if (stmt != null) {
204: stmt.close();
205: }
206:
207: if (conn != null) {
208: conn.close();
209: }
210: }
211:
212: /**
213: * A nasty do-it-all tool class. It serves as:
214: * <ul>
215: * <li>context tool to fetch a table iterator</li>
216: * <li>the abovenamed iterator which iterates over the table</li>
217: * <li>getter for the table fields</li>
218: * </ul>
219: */
220: public class TableTool implements Iterator {
221: /** querydataset */
222: private ResultSet rs;
223:
224: /**
225: * Constructor for the TableTool object.
226: */
227: public TableTool() {
228: }
229:
230: /**
231: * Constructor for the TableTool object.
232: *
233: * @param rs a query result set
234: * @throws Exception Problem using database record set cursor.
235: */
236: protected TableTool(ResultSet rs) throws Exception {
237: this .rs = rs;
238: }
239:
240: /**
241: * Fetches an <code>Iterator</code> for the data in the named table.
242: *
243: * @param tableName Description of Parameter
244: * @return <code>Iterator</code> for the fetched data.
245: * @throws Exception Problem creating connection or executing query.
246: */
247: public TableTool fetch(String tableName) throws Exception {
248: log("Fetching data for table " + tableName,
249: Project.MSG_INFO);
250: return new TableTool(stmt.executeQuery("SELECT * FROM "
251: + tableName));
252: }
253:
254: /**
255: * check if there are more records in the QueryDataSet.
256: *
257: * @return true if there are more records
258: */
259: public boolean hasNext() {
260: try {
261: // TODO optimize this
262: // i tried to use rs.isLast() but this returns wrong results
263: // for empty tables :-(
264: boolean validRow = rs.next();
265: rs.previous();
266: return validRow;
267: } catch (Exception se) {
268: System.err.println("Exception :");
269: se.printStackTrace();
270: }
271: return false;
272: }
273:
274: /**
275: * load the next record from the QueryDataSet.
276: *
277: * @return Description of the Returned Value
278: * @throws NoSuchElementException Description of Exception
279: */
280: public Object next() throws NoSuchElementException {
281: try {
282: System.out.print(".");
283: rs.next();
284: } catch (Exception se) {
285: System.err.println("Exception while iterating:");
286: se.printStackTrace();
287: throw new NoSuchElementException(se.getMessage());
288: }
289: return this ;
290: }
291:
292: /**
293: * Returns the value for the column.
294: *
295: * @param columnName name of the column
296: * @return value of the column or null if it doesn't exist
297: */
298: public String get(String columnName) {
299: try {
300: return (rs.getString(columnName));
301: } catch (Exception se) {
302: log("Exception fetching value " + columnName + ": "
303: + se.getMessage(), Project.MSG_ERR);
304: }
305: return null;
306: }
307:
308: /**
309: * Dummy implementation of the remove() method of the iterator
310: * interface. This implementation always throws a
311: * UnsupportedOperationException
312: *
313: * @throws UnsupportedOperationException always.
314: */
315: public void remove() throws UnsupportedOperationException {
316: throw new UnsupportedOperationException();
317: }
318: }
319: }
|