KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > sql > SqlJob


1 /*
2  * Copyright (c) 2005, Rob Gordon.
3  */

4 package org.oddjob.sql;
5
6 import java.io.IOException JavaDoc;
7 import java.io.ObjectInputStream JavaDoc;
8 import java.io.ObjectOutputStream JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.Statement JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.apache.commons.beanutils.RowSetDynaClass;
17 import org.apache.log4j.Logger;
18
19
20 /**
21  * @oddjob.description Runs a sql query. If the sql job has results then
22  * they are avaialable as properties of the row or rows properties of
23  * this job.
24  *
25  * @oddjob.example
26  *
27  *<pre>
28  * &lt;sequential&gt;
29  * &lt;sql id="query" connection="${vars.connection}"&gt;
30  * &lt;sql&gt;select text from greetings where style='nice'&lt;/sql&gt;
31  * &lt;/sql&gt;
32  * &lt;echo text="${query.row.greeting}"/&gt;
33  * &lt;sequential&gt;
34  *</pre>
35  *
36  * @oddjob.example
37  *
38  */

39 public class SqlJob
40 implements Runnable JavaDoc, Serializable JavaDoc {
41     private static final long serialVersionUID = 20051106;
42         
43     private static final Logger logger = Logger.getLogger(SqlJob.class);
44     
45     /**
46      * @oddjob.property
47      * @oddjob.description A name, can be any text.
48      * @oddjob.required No.
49      */

50     private String JavaDoc name;
51
52     /**
53      * @oddjob.property
54      * @oddjob.description The sql query or dml statement to run.
55      * @oddjob.required Yes.
56      */

57     private String JavaDoc sql;
58     
59     /**
60      * @oddjob.property
61      * @oddjob.description The {@link ConnectionType} to use.
62      * @oddjob.required Yes.
63      */

64     private transient Connection JavaDoc connection;
65
66     /**
67      * @oddjob.property
68      * @oddjob.description The results of the query.
69      * @oddjob.required Read only.
70      */

71     private List JavaDoc rows;
72
73     /**
74      * @oddjob.property
75      * @oddjob.description The update count of any insert/update/delete statement.
76      * @oddjob.required Read only.
77      */

78     private int updateCount;
79     
80     /**
81      * Get the name.
82      *
83      * @return The name.
84      */

85     public String JavaDoc getName() {
86         return name;
87     }
88     
89     /**
90      * Set the name
91      *
92      * @param name The name.
93      */

94     public void setName(String JavaDoc name) {
95         this.name = name;
96     }
97     
98     /**
99      * Set the connection.
100      *
101      * @param connection The connection.
102      */

103     public void setConnection(Connection JavaDoc connection) {
104         this.connection = connection;
105     }
106
107     /**
108      * Getter for connection.
109      *
110      * @return The connection.
111      */

112     public Connection JavaDoc getConnection() {
113         return connection;
114     }
115
116     /**
117      * Set the sql.
118      *
119      * @param sql The sql.
120      */

121     public void setSql(String JavaDoc sql) {
122         this.sql = sql;
123     }
124
125     /**
126      * Get the sql.
127      *
128      * @return The sql.
129      */

130     public String JavaDoc getSql() {
131         return sql;
132     }
133
134     /**
135      * Get the rows.
136      *
137      * @return The rows.
138      */

139     public Object JavaDoc[] getRows() {
140         if (rows == null) {
141             return null;
142         }
143         return rows.toArray();
144     }
145     
146     /**
147      * @oddjob.property row
148      * @oddjob.description The result of a query when only one result is expected.
149      * @oddjob.required Read only.
150      */

151     public Object JavaDoc getRow() {
152         if (rows == null) {
153             return null;
154         }
155         if (rows.size() != 1) {
156             return null;
157         }
158         return rows.get(0);
159     }
160
161     /**
162      * @oddjob.property
163      * @oddjob.description The number of rows returned by the query.
164      * @oddjob.required Read only.
165      */

166     public int getRowCount() {
167         if (rows == null) {
168             return -1;
169         }
170         return rows.size();
171     }
172     
173     /**
174      * Get the update count.
175      *
176      * @return The update count.
177      */

178     public int getUpdateCount() {
179         return updateCount;
180     }
181
182     /*
183      * (non-Javadoc)
184      * @see java.lang.Runnable#run()
185      */

186     public void run() {
187         if (connection == null) {
188             throw new NullPointerException JavaDoc("No Connection!");
189         }
190         if (sql == null) {
191             throw new NullPointerException JavaDoc("No Query!");
192         }
193         try {
194             executeWithConnection(connection);
195         }
196         catch (SQLException JavaDoc e) {
197             throw new RuntimeException JavaDoc(e);
198         }
199         finally {
200             try {
201                 connection.close();
202             } catch (SQLException JavaDoc e) {
203                 throw new RuntimeException JavaDoc(e);
204             }
205         }
206     }
207     
208     
209     /**
210      * Do the work of executing the query.
211      *
212      * @param connection The connection.
213      * @throws SQLException If something goes wrong.
214      */

215     protected void executeWithConnection(Connection JavaDoc connection) throws SQLException JavaDoc {
216         logger.debug("Executing query: " + sql);
217
218         Statement JavaDoc stmt = connection.createStatement();
219
220         RowSetDynaClass rsdc = null;
221         try {
222             boolean hasResults = stmt.execute(getSql());
223             updateCount = stmt.getUpdateCount();
224             if (hasResults) {
225                 ResultSet JavaDoc results = stmt.getResultSet();
226                 rsdc = new RowSetDynaClass(results);
227                 rows = rsdc.getRows();
228             }
229         }
230         finally {
231             stmt.close();
232         }
233     }
234     
235
236     
237     /**
238      * Custom serialsation.
239      */

240     private void writeObject(ObjectOutputStream JavaDoc s)
241     throws IOException JavaDoc {
242         s.defaultWriteObject();
243     }
244
245     /**
246      * Custom serialisation.
247      */

248     private void readObject(ObjectInputStream JavaDoc s)
249     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
250         s.defaultReadObject();
251     }
252     
253     /*
254      * (non-Javadoc)
255      * @see java.lang.Object#toString()
256      */

257     public String JavaDoc toString() {
258         if (name == null) {
259             return "Sql";
260         }
261         return name;
262     }
263 }
264
Popular Tags