%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.task.TorqueDataDumpTask |
|
|
1 | package org.apache.torque.task; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 0 | 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 | 0 | 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 | 0 | databaseName = v; |
82 | 0 | } |
83 | ||
84 | /** |
|
85 | * Get the database url |
|
86 | * |
|
87 | * @return The DatabaseUrl value |
|
88 | */ |
|
89 | public String getDatabaseUrl() |
|
90 | { |
|
91 | 0 | 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 | 0 | databaseUrl = v; |
102 | 0 | } |
103 | ||
104 | /** |
|
105 | * Get the database driver name |
|
106 | * |
|
107 | * @return String database driver name |
|
108 | */ |
|
109 | public String getDatabaseDriver() |
|
110 | { |
|
111 | 0 | 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 | 0 | databaseDriver = v; |
122 | 0 | } |
123 | ||
124 | /** |
|
125 | * Get the database user |
|
126 | * |
|
127 | * @return String database user |
|
128 | */ |
|
129 | public String getDatabaseUser() |
|
130 | { |
|
131 | 0 | 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 | 0 | databaseUser = v; |
142 | 0 | } |
143 | ||
144 | /** |
|
145 | * Get the database password |
|
146 | * |
|
147 | * @return String database password |
|
148 | */ |
|
149 | public String getDatabasePassword() |
|
150 | { |
|
151 | 0 | 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 | 0 | databasePassword = v; |
162 | 0 | } |
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 | 0 | super.initControlContext(); |
173 | ||
174 | 0 | context.put("dataset", "all"); |
175 | ||
176 | 0 | log("Torque - TorqueDataDump starting"); |
177 | 0 | log("Your DB settings are:"); |
178 | 0 | log("driver: " + databaseDriver); |
179 | 0 | log("URL: " + databaseUrl); |
180 | 0 | log("user: " + databaseUser); |
181 | // log("password: " + databasePassword); |
|
182 | ||
183 | try |
|
184 | { |
|
185 | 0 | Class.forName(databaseDriver); |
186 | 0 | log("DB driver instantiated sucessfully", Project.MSG_DEBUG); |
187 | ||
188 | 0 | conn = DriverManager.getConnection( |
189 | databaseUrl, databaseUser, databasePassword); |
|
190 | 0 | stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
191 | ResultSet.CONCUR_UPDATABLE); |
|
192 | ||
193 | 0 | log("DB connection established", Project.MSG_DEBUG); |
194 | 0 | context.put("tableTool", new TableTool()); |
195 | } |
|
196 | 0 | catch (SQLException se) |
197 | { |
|
198 | 0 | System.err.println("SQLException while connecting to DB:"); |
199 | 0 | se.printStackTrace(); |
200 | } |
|
201 | 0 | catch (ClassNotFoundException cnfe) |
202 | { |
|
203 | 0 | System.err.println("cannot load driver:"); |
204 | 0 | cnfe.printStackTrace(); |
205 | 0 | } |
206 | 0 | context.put("escape", new org.apache.velocity.anakia.Escape()); |
207 | 0 | 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 | 0 | if (stmt != null) |
219 | { |
|
220 | 0 | stmt.close(); |
221 | } |
|
222 | ||
223 | 0 | if (conn != null) |
224 | { |
|
225 | 0 | conn.close(); |
226 | } |
|
227 | 0 | } |
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 | 0 | 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 | // TODO optimize this |
|
284 | // i tried to use rs.isLast() but this returns wrong results |
|
285 | // for empty tables :-( |
|
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 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |