1 5 package com.teamkonzept.db; 6 7 import java.io.*; 8 import java.sql.*; 9 10 import com.teamkonzept.lib.*; 11 import com.teamkonzept.webman.mainint.DatabaseDefaults; 12 import org.apache.log4j.Category; 13 14 21 public abstract class TKQuery implements QueryConstants 22 { 23 24 protected static final Category CAT = Category.getInstance(TKQuery.class); 25 26 public static final int DBFORM_IDENT = 69; 27 public static final int DBSTRUC_IDENT = 66; 28 public static final int DBFRAGMENT_IDENT = 67; 29 30 31 33 protected Connection conn; 34 35 37 protected TKDBConnection aTKDBConnection; 38 39 41 protected Statement stmt; 42 43 48 protected String sqlString; 49 50 57 protected TKHashtable paramTypes; 58 59 64 protected boolean sensitiveQuery = false; 65 66 68 protected int deregisterIndex; 69 70 71 72 75 protected TKHashtable queryParams; 76 protected boolean[] setRelevants; 77 78 protected int currPos; 79 protected boolean currIsResultSet; 80 81 91 public abstract void initQuery( final Connection conn ); 92 93 106 public abstract void initQuery( final TKSQLTypeConverter conv, final TKDBConnection tkdbConn, Object queryID); 107 108 109 116 public void initQuery( final Connection conn, 117 final Object [][] paramTypes, 118 final boolean[] setRelevants ) 119 { 120 this.conn = conn; 121 this.setRelevants = setRelevants; 122 this.currPos = 0; 123 124 if( this instanceof SensitiveQuery ){ 125 sensitiveQuery = true; 126 } 127 if ( paramTypes != null ) { 128 this.paramTypes = new TKHashtable(paramTypes); 129 } else { 130 this.paramTypes = null; 131 } 132 queryParams = new TKHashtable(); 133 } 134 135 136 144 public abstract boolean execute() throws SQLException; 145 146 147 150 public void executeAsTran() throws SQLException{ 151 aTKDBConnection.beginTransaction(); 152 try{ 153 this.execute(); 154 this.close(); 155 } 156 catch(SQLException t){ 157 try{ 158 aTKDBConnection.rollbackTransaction(); 159 } 160 catch( SQLException e ){ 161 throw e; 162 } 163 throw new TKSQLError(t.getMessage(), t); 164 } 165 } 166 167 168 170 public TKQuery() { } 171 172 173 181 protected int registerQuery(){ 182 int anInt; 183 if ( sensitiveQuery == false){ 184 anInt = aTKDBConnection.registerNonSensitiveQuery(this); 185 186 } 187 else{ 188 aTKDBConnection.registerSensitiveQuery(); 189 anInt = -1; 190 } 191 return anInt; 192 } 193 194 196 protected void deregisterQuery(){ 197 TKQuery query; 198 if( sensitiveQuery){ 199 aTKDBConnection.deregisterSensitiveQuery(); 200 } 201 else{ 202 query = aTKDBConnection.deregisterNonSensitiveQuery( deregisterIndex); 203 if( query != this) CAT.warn(" Wrong Query deregistered "); 204 } 205 } 206 207 214 public ResultSet fetchResultSet() 215 { 216 if(TKDBManager.getDBVendor()== ORACLE || TKDBManager.getDBVendor()==POSTGRESQL){ 217 try{ 218 return stmt.getResultSet(); 219 } catch (SQLException sqle) { 220 printSqlException(sqle, "fetchResultSet"); 221 } 222 } 223 224 if(setRelevants== null) return null; 225 226 ResultSet rs = null; 227 228 FETCH: 229 while( currPos < setRelevants.length ) { 230 try { 231 if ((currPos != 0) || (!currIsResultSet) ) { 232 while ( 233 (!(currIsResultSet = stmt.getMoreResults())) && 234 (stmt.getUpdateCount() != -1)) { 235 CAT.debug("POS " + currPos + " NO RS OR UPDATECOUNT<BR>"); 236 } 237 } 238 239 if (currIsResultSet) { 240 rs = stmt.getResultSet(); 241 CAT.debug("POS " + currPos + " RS FOUND <BR>"); 242 } else { 243 CAT.debug("POS " + currPos + " END <BR>"); 244 currPos = setRelevants.length; 245 break FETCH; 246 } 247 248 if (setRelevants[currPos++]) { 249 CAT.debug("POS " + (currPos-1) + " RS RELEVANT <BR>"); 250 break FETCH; 251 } else { 252 CAT.debug("POS " + (currPos-1) + " NOT RELEVANT, RS SET NULL<BR>"); 253 rs = null; 254 } 255 256 } catch (SQLException sqle) { 257 printSqlException(sqle, "fetchResultSet"); 258 } 259 } 260 261 return rs; 262 } 263 264 265 266 270 271 public void close()throws SQLException{ 272 try{ 273 274 specClose(); 275 276 } 277 catch(SQLException e){ 278 try{ 279 if(!aTKDBConnection.isTransactionOpen()){ 280 TKDBManager.closeConnection(); 281 } 282 } 283 catch(SQLException s){ 284 CAT.debug(s.getMessage()); 285 } 286 printSqlException(e, " close PrepQuery failed "); 287 } 288 } 289 290 291 292 296 public abstract void specClose() throws SQLException; 297 298 299 300 308 public void throwawayResults() throws SQLException{ 309 310 if( TKDBManager.getDBVendor() == POSTGRESQL) return; 311 if( TKDBManager.getDBVendor() == ORACLE) return; 312 if( TKDBManager.getDBVendor() == DONTKNOW) return; 313 314 if (stmt != null){ 315 while( stmt.getMoreResults() || (stmt.getUpdateCount() != -1 ) ) { 316 } 317 } 318 } 319 320 321 326 public void setQueryParams( String param, Object val) 327 { 328 if (val == null) { 329 queryParams.put(param, TKNull.NULL); 330 } else { 331 queryParams.put(param, val); 332 } 333 } 334 335 344 public void printSqlException(SQLException sqle, String s) 345 { 346 throw new TKSQLError( 347 " Query: " + this.getClass().getName() + 348 " STMT: " + sqlString + 349 " Action: " + s + 350 " Message: " + sqle.getMessage(), sqle); 351 } 352 } 353 354 355 | Popular Tags |