00001 package edu.virtualschool.jwaa.dbms;
00002
00003 import java.io.InputStream;
00004 import java.io.Reader;
00005 import java.sql.Array;
00006 import java.sql.Blob;
00007 import java.sql.Clob;
00008 import java.sql.Date;
00009 import java.sql.Ref;
00010 import java.sql.ResultSet;
00011 import java.sql.ResultSetMetaData;
00012 import java.sql.SQLException;
00013 import java.sql.SQLWarning;
00014 import java.sql.Statement;
00015 import java.sql.Time;
00016 import java.sql.Timestamp;
00017 import java.util.Calendar;
00018 import java.util.Map;
00019
00020 import org.apache.log4j.Logger;
00021
00022 import edu.virtualschool.jwaa.RuntimeFault;
00023
00024
00043 public class DBQuery
00044 {
00045 protected final String sql;
00046 protected final ResultSet results;
00047 protected final DB dbms;
00048
00049 private final static Logger logger = Logger.getLogger(DBQuery.class.getName());
00050
00058 protected DBQuery(DB dbms, String sql) throws DB.Fault
00059 {
00060 this.sql = sql;
00061 this.dbms = dbms;
00062 try
00063 {
00064 this.results = dbms.connection.createStatement().executeQuery(sql);
00065 }
00066 catch (SQLException e)
00067 {
00068 throw new DB.Fault(e, e);
00069 }
00070 }
00071 public boolean hasNext() throws DB.Fault
00072 {
00073 try { return results.next(); }
00074 catch (SQLException e) { throw new DB.Fault(e, e); }
00075 }
00076 public void close() throws DB.Fault
00077 {
00078 try { results.close(); }
00079 catch (SQLException e) { throw new DB.Fault(e, e); }
00080 }
00081 public void finalize() throws DB.Fault
00082 {
00083 try { if (results != null) results.close(); }
00084 catch (SQLException e) { logger.error(e, e); }
00085 }
00086 public final Array getArray(String n) throws DB.Fault
00087 {
00088 try { return results.getArray(n); }
00089 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 public final Blob getBlob(String n) throws DB.Fault
00104 {
00105 return getBlob(n);
00106 }
00107 public final boolean getBoolean(String n) throws DB.Fault
00108 {
00109 try { return results.getBoolean(n); }
00110 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00111 }
00112 public final byte[] getBytes(String n) throws DB.Fault
00113 {
00114 try { return results.getBytes(n); }
00115 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00116 }
00117 public final byte getByte(String n) throws DB.Fault
00118 {
00119 try { return results.getByte(n); }
00120 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00121 }
00122 public final Clob getClob(int i) throws DB.Fault
00123 {
00124 try { return results.getClob(i); }
00125 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00126 }
00127 public final Clob getClob(String n) throws DB.Fault
00128 {
00129 try { return results.getClob(n); }
00130 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00131 }
00132 public final double getDouble(String n) throws DB.Fault
00133 {
00134 try { return results.getDouble(n); }
00135 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00136 }
00137 public final double getDouble(int i) throws DB.Fault
00138 {
00139 try { return results.getDouble(i); }
00140 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00141 }
00142 public final float getFloat(String n) throws DB.Fault
00143 {
00144 try { return results.getFloat(n); }
00145 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00146 }
00147 public final int getInt(String n) throws DB.Fault
00148 {
00149 try { return results.getInt(n); }
00150 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00151 }
00152 public final int getInt(int i) throws DB.Fault
00153 {
00154 try { return results.getInt(i); }
00155 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00156 }
00157 public final InputStream getAsciiStream(String n) throws DB.Fault
00158 {
00159 try { return results.getAsciiStream(n); }
00160 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00161 }
00162 public final InputStream getBinaryStream(String n) throws DB.Fault
00163 {
00164 try { return results.getBinaryStream(n); }
00165 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00166 }
00167
00168
00169
00170
00171
00172 public final Reader getCharacterStream(String n) throws DB.Fault
00173 {
00174 try { return results.getCharacterStream(n); }
00175 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00176 }
00177 public final Date getDate(String n, Calendar cal) throws DB.Fault
00178 {
00179 try { return results.getDate(n, cal); }
00180 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00181 }
00182 public final Date getDate(String n) throws DB.Fault
00183 {
00184 try { return results.getDate(n); }
00185 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00186 }
00187 public final Time getTime(String n, Calendar cal) throws DB.Fault
00188 {
00189 try { return results.getTime(n, cal); }
00190 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00191 }
00192 public final Time getTime(String n) throws DB.Fault
00193 {
00194 try { return results.getTime(n); }
00195 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00196 }
00197 public final Timestamp getTimestamp(String n, Calendar cal) throws DB.Fault
00198 {
00199 try { return results.getTimestamp(n, cal); }
00200 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00201 }
00202 public final Timestamp getTimestamp(String n) throws DB.Fault
00203 {
00204 try { return results.getTimestamp(n); }
00205 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00206 }
00207 public final long getLong(String n) throws DB.Fault
00208 {
00209 try { return results.getLong(n); }
00210 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00211 }
00212 public final long getLong(int i) throws DB.Fault
00213 {
00214 try { return results.getLong(i); }
00215 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00216 }
00217 public final Object getObject(int i, java.util.Map map) throws DB.Fault
00218 {
00219 try { return results.getObject(i, map); }
00220 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00221 }
00222 public final Object getObject(String n, Map map) throws DB.Fault
00223 {
00224 try { return results.getObject(n, map); }
00225 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00226 }
00227 public final Object getObject(String n) throws DB.Fault
00228 {
00229 try { return results.getObject(n); }
00230 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00231 }
00232 public final Ref getRef(int i) throws DB.Fault
00233 {
00234 try { return results.getRef(i); }
00235 catch (SQLException e) { throw new DB.Fault(i+":"+e.getMessage(), e); }
00236 }
00237 public final Ref getRef(String n) throws DB.Fault
00238 {
00239 try { return results.getRef(n); }
00240 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00241 }
00242 public final ResultSetMetaData getMetaData() throws DB.Fault
00243 {
00244 try { return results.getMetaData(); }
00245 catch (SQLException e) { throw new DB.Fault(e, e); }
00246 }
00247 public final short getShort(String n) throws DB.Fault
00248 {
00249 try { return results.getShort(n); }
00250 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00251 }
00252 public final SQLWarning getWarnings() throws DB.Fault
00253 {
00254 try { return results.getWarnings(); }
00255 catch (SQLException e) { throw new DB.Fault(e, e); }
00256 }
00257 public final Statement getStatement() throws DB.Fault
00258 {
00259 try { return results.getStatement(); }
00260 catch (SQLException e) { throw new DB.Fault(e, e); }
00261 }
00262 public final String getCursorName() throws DB.Fault
00263 {
00264 try { return results.getCursorName(); }
00265 catch (SQLException e) { throw new DB.Fault(e, e); }
00266 }
00267 public final String getString(String n) throws DB.Fault
00268 {
00269 try { return results.getString(n); }
00270 catch (SQLException e) { throw new DB.Fault(n+": "+e.getMessage(), e); }
00271 }
00272 public final String getString(int i) throws DB.Fault
00273 {
00274 try { return results.getString(i); }
00275 catch (SQLException e) { throw new DB.Fault(i+": "+e.getMessage(), e); }
00276 }
00277 public String toString()
00278 {
00279 return sql;
00280 }
00281 public String inspect()
00282 {
00283 StringBuffer buf = new StringBuffer("Query: "+sql+"\n");
00284 try
00285 {
00286 ResultSetMetaData md = getMetaData();
00287 for (int i = 1; i <= md.getColumnCount(); i++)
00288 buf.append(md.getColumnName(i) + ":" + results.getString(i)+" ");
00289 }
00290 catch (Exception e) { logger.error(e, e); buf.append(e.toString()); }
00291 return buf.toString();
00292 }
00293 public static abstract class Iterator implements java.util.Iterator
00294 {
00295 final protected DBQuery query;
00296
00297 public Iterator(final DB dbms, final String sql)
00298 throws DB.Fault
00299 {
00300 try
00301 {
00302 this.query = new DBQuery(dbms, sql);
00303 }
00304 catch (DB.Fault e)
00305 {
00306 throw new RuntimeFault(sql, e);
00307 }
00308 }
00309 public boolean hasNext()
00310 {
00311 try
00312 {
00313 return this.query.hasNext();
00314 }
00315 catch (DB.Fault e)
00316 {
00317 throw new RuntimeFault(query.sql, e);
00318 }
00319 }
00320 public void finalize()
00321 {
00322 try
00323 {
00324 query.close();
00325 }
00326 catch (DB.Fault e)
00327 {
00328 throw new RuntimeFault(query.sql, e);
00329 }
00330 }
00331 public void remove()
00332 {
00333 throw new UnsupportedOperationException("not implemented");
00334 }
00335 public final String toString()
00336 {
00337 return query.sql;
00338 }
00339 }
00340 }