%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.task.TorqueDataDumpTask |
|
|
1 | package org.apache.torque.task; |
|
2 | ||
3 | /* |
|
4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
5 | * or more contributor license agreements. See the NOTICE file |
|
6 | * distributed with this work for additional information |
|
7 | * regarding copyright ownership. The ASF licenses this file |
|
8 | * to you under the Apache License, Version 2.0 (the |
|
9 | * "License"); you may not use this file except in compliance |
|
10 | * with the License. You may obtain a copy of the License at |
|
11 | * |
|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
13 | * |
|
14 | * Unless required by applicable law or agreed to in writing, |
|
15 | * software distributed under the License is distributed on an |
|
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
17 | * KIND, either express or implied. See the License for the |
|
18 | * specific language governing permissions and limitations |
|
19 | * under the License. |
|
20 | */ |
|
21 | ||
22 | import java.sql.Connection; |
|
23 | import java.sql.DriverManager; |
|
24 | import java.sql.ResultSet; |
|
25 | import java.sql.SQLException; |
|
26 | import java.sql.Statement; |
|
27 | import java.util.Iterator; |
|
28 | import java.util.NoSuchElementException; |
|
29 | ||
30 | import org.apache.tools.ant.Project; |
|
31 | ||
32 | import org.apache.velocity.context.Context; |
|
33 | ||
34 | ||
35 | /** |
|
36 | * An extended Texen task used for dumping data from db into XML |
|
37 | * |
|
38 | * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a> |
|
39 | * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a> |
|
40 | * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> |
|
41 | * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> |
|
42 | * @version $Id: TorqueDataDumpTask.java 473814 2006-11-11 22:30:30Z tv $ |
|
43 | */ |
|
44 | 0 | public class TorqueDataDumpTask extends TorqueDataModelTask |
45 | { |
|
46 | /** Database name. */ |
|
47 | private String databaseName; |
|
48 | ||
49 | /** Database URL used for JDBC connection. */ |
|
50 | private String databaseUrl; |
|
51 | ||
52 | /** Database driver used for JDBC connection. */ |
|
53 | private String databaseDriver; |
|
54 | ||
55 | /** Database user used for JDBC connection. */ |
|
56 | private String databaseUser; |
|
57 | ||
58 | /** Database password used for JDBC connection. */ |
|
59 | private String databasePassword; |
|
60 | ||
61 | /** The database connection used to retrieve the data to dump. */ |
|
62 | private Connection conn; |
|
63 | ||
64 | /** The statement used to acquire the data to dump. */ |
|
65 | private Statement stmt; |
|
66 | ||
67 | /** |
|
68 | * Get the database name to dump |
|
69 | * |
|
70 | * @return The DatabaseName value |
|
71 | */ |
|
72 | public String getDatabaseName() |
|
73 | { |
|
74 | 0 | return databaseName; |
75 | } |
|
76 | ||
77 | /** |
|
78 | * Set the database name |
|
79 | * |
|
80 | * @param v The new DatabaseName value |
|
81 | */ |
|
82 | public void setDatabaseName(String v) |
|
83 | { |
|
84 | 0 | databaseName = v; |
85 | 0 | } |
86 | ||
87 | /** |
|
88 | * Get the database url |
|
89 | * |
|
90 | * @return The DatabaseUrl value |
|
91 | */ |
|
92 | public String getDatabaseUrl() |
|
93 | { |
|
94 | 0 | return databaseUrl; |
95 | } |
|
96 | ||
97 | /** |
|
98 | * Set the database url |
|
99 | * |
|
100 | * @param v The new DatabaseUrl value |
|
101 | */ |
|
102 | public void setDatabaseUrl(String v) |
|
103 | { |
|
104 | 0 | databaseUrl = v; |
105 | 0 | } |
106 | ||
107 | /** |
|
108 | * Get the database driver name |
|
109 | * |
|
110 | * @return String database driver name |
|
111 | */ |
|
112 | public String getDatabaseDriver() |
|
113 | { |
|
114 | 0 | return databaseDriver; |
115 | } |
|
116 | ||
117 | /** |
|
118 | * Set the database driver name |
|
119 | * |
|
120 | * @param v The new DatabaseDriver value |
|
121 | */ |
|
122 | public void setDatabaseDriver(String v) |
|
123 | { |
|
124 | 0 | databaseDriver = v; |
125 | 0 | } |
126 | ||
127 | /** |
|
128 | * Get the database user |
|
129 | * |
|
130 | * @return String database user |
|
131 | */ |
|
132 | public String getDatabaseUser() |
|
133 | { |
|
134 | 0 | return databaseUser; |
135 | } |
|
136 | ||
137 | /** |
|
138 | * Set the database user |
|
139 | * |
|
140 | * @param v The new DatabaseUser value |
|
141 | */ |
|
142 | public void setDatabaseUser(String v) |
|
143 | { |
|
144 | 0 | databaseUser = v; |
145 | 0 | } |
146 | ||
147 | /** |
|
148 | * Get the database password |
|
149 | * |
|
150 | * @return String database password |
|
151 | */ |
|
152 | public String getDatabasePassword() |
|
153 | { |
|
154 | 0 | return databasePassword; |
155 | } |
|
156 | ||
157 | /** |
|
158 | * Set the database password |
|
159 | * |
|
160 | * @param v The new DatabasePassword value |
|
161 | */ |
|
162 | public void setDatabasePassword(String v) |
|
163 | { |
|
164 | 0 | databasePassword = v; |
165 | 0 | } |
166 | ||
167 | /** |
|
168 | * Initializes initial context |
|
169 | * |
|
170 | * @return the context |
|
171 | * @throws Exception generic exception |
|
172 | */ |
|
173 | public Context initControlContext() throws Exception |
|
174 | { |
|
175 | 0 | super.initControlContext(); |
176 | ||
177 | 0 | context.put("dataset", "all"); |
178 | ||
179 | 0 | log("Torque - TorqueDataDump starting"); |
180 | 0 | log("Your DB settings are:"); |
181 | 0 | log("driver: " + databaseDriver); |
182 | 0 | log("URL: " + databaseUrl); |
183 | 0 | log("user: " + databaseUser); |
184 | // log("password: " + databasePassword); |
|
185 | ||
186 | try |
|
187 | { |
|
188 | 0 | Class.forName(databaseDriver); |
189 | 0 | log("DB driver instantiated sucessfully", Project.MSG_DEBUG); |
190 | ||
191 | 0 | conn = DriverManager.getConnection( |
192 | databaseUrl, databaseUser, databasePassword); |
|
193 | 0 | stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, |
194 | ResultSet.CONCUR_UPDATABLE); |
|
195 | ||
196 | 0 | log("DB connection established", Project.MSG_DEBUG); |
197 | 0 | context.put("tableTool", new TableTool()); |
198 | } |
|
199 | 0 | catch (SQLException se) |
200 | { |
|
201 | 0 | System.err.println("SQLException while connecting to DB:"); |
202 | 0 | se.printStackTrace(); |
203 | } |
|
204 | 0 | catch (ClassNotFoundException cnfe) |
205 | { |
|
206 | 0 | System.err.println("cannot load driver:"); |
207 | 0 | cnfe.printStackTrace(); |
208 | 0 | } |
209 | 0 | context.put("escape", new org.apache.velocity.anakia.Escape()); |
210 | 0 | return context; |
211 | } |
|
212 | ||
213 | /** |
|
214 | * Closes the db-connection, overriding the <code>cleanup()</code> hook |
|
215 | * method in <code>TexenTask</code>. |
|
216 | * |
|
217 | * @throws Exception Database problem while closing resource. |
|
218 | */ |
|
219 | protected void cleanup() throws Exception |
|
220 | { |
|
221 | 0 | if (stmt != null) |
222 | { |
|
223 | 0 | stmt.close(); |
224 | } |
|
225 | ||
226 | 0 | if (conn != null) |
227 | { |
|
228 | 0 | conn.close(); |
229 | } |
|
230 | 0 | } |
231 | ||
232 | /** |
|
233 | * A nasty do-it-all tool class. It serves as: |
|
234 | * <ul> |
|
235 | * <li>context tool to fetch a table iterator</li> |
|
236 | * <li>the abovenamed iterator which iterates over the table</li> |
|
237 | * <li>getter for the table fields</li> |
|
238 | * </ul> |
|
239 | */ |
|
240 | 0 | public class TableTool implements Iterator |
241 | { |
|
242 | /** querydataset */ |
|
243 | private ResultSet rs; |
|
244 | ||
245 | /** |
|
246 | * Constructor for the TableTool object. |
|
247 | */ |
|
248 | public TableTool() |
|
249 | { |
|
250 | } |
|
251 | ||
252 | /** |
|
253 | * Constructor for the TableTool object. |
|
254 | * |
|
255 | * @param rs a query result set |
|
256 | * @throws Exception Problem using database record set cursor. |
|
257 | */ |
|
258 | protected TableTool(ResultSet rs) throws Exception |
|
259 | { |
|
260 | this.rs = rs; |
|
261 | } |
|
262 | ||
263 | /** |
|
264 | * Fetches an <code>Iterator</code> for the data in the named table. |
|
265 | * |
|
266 | * @param tableName Description of Parameter |
|
267 | * @return <code>Iterator</code> for the fetched data. |
|
268 | * @throws Exception Problem creating connection or executing query. |
|
269 | */ |
|
270 | public TableTool fetch(String tableName) throws Exception |
|
271 | { |
|
272 | log("Fetching data for table " + tableName, Project.MSG_INFO); |
|
273 | return new TableTool( |
|
274 | stmt.executeQuery("SELECT * FROM " + tableName)); |
|
275 | } |
|
276 | ||
277 | /** |
|
278 | * check if there are more records in the QueryDataSet. |
|
279 | * |
|
280 | * @return true if there are more records |
|
281 | */ |
|
282 | public boolean hasNext() |
|
283 | { |
|
284 | try |
|
285 | { |
|
286 | // TODO optimize this |
|
287 | // i tried to use rs.isLast() but this returns wrong results |
|
288 | // for empty tables :-( |
|
289 | boolean validRow = rs.next(); |
|
290 | rs.previous(); |
|
291 | return validRow; |
|
292 | } |
|
293 | catch (Exception se) |
|
294 | { |
|
295 | System.err.println("Exception :"); |
|
296 | se.printStackTrace(); |
|
297 | } |
|
298 | return false; |
|
299 | } |
|
300 | ||
301 | /** |
|
302 | * load the next record from the QueryDataSet. |
|
303 | * |
|
304 | * @return Description of the Returned Value |
|
305 | * @throws NoSuchElementException Description of Exception |
|
306 | */ |
|
307 | public Object next() throws NoSuchElementException |
|
308 | { |
|
309 | try |
|
310 | { |
|
311 | System.out.print("."); |
|
312 | rs.next(); |
|
313 | } |
|
314 | catch (Exception se) |
|
315 | { |
|
316 | System.err.println("Exception while iterating:"); |
|
317 | se.printStackTrace(); |
|
318 | throw new NoSuchElementException(se.getMessage()); |
|
319 | } |
|
320 | return this; |
|
321 | } |
|
322 | ||
323 | /** |
|
324 | * Returns the value for the column. |
|
325 | * |
|
326 | * @param columnName name of the column |
|
327 | * @return value of the column or null if it doesn't exist |
|
328 | */ |
|
329 | public String get(String columnName) |
|
330 | { |
|
331 | try |
|
332 | { |
|
333 | return (rs.getString(columnName)); |
|
334 | } |
|
335 | catch (Exception se) |
|
336 | { |
|
337 | log("Exception fetching value " + columnName + ": " |
|
338 | + se.getMessage(), Project.MSG_ERR); |
|
339 | } |
|
340 | return null; |
|
341 | } |
|
342 | ||
343 | /** |
|
344 | * Dummy implementation of the remove() method of the iterator |
|
345 | * interface. This implementation always throws a |
|
346 | * UnsupportedOperationException |
|
347 | * |
|
348 | * @throws UnsupportedOperationException always. |
|
349 | */ |
|
350 | public void remove() throws UnsupportedOperationException |
|
351 | { |
|
352 | throw new UnsupportedOperationException(); |
|
353 | } |
|
354 | } |
|
355 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |