1 package com.teamkonzept.db; 2 3 import java.sql.*; 4 import java.io.StringReader ; 5 import com.teamkonzept.lib.*; 6 7 15 public abstract class TKPrepQuery extends TKQuery implements QueryConstants { 16 17 24 public static boolean REGISTER_QUERY = false; 25 26 29 public static boolean REUSE_QUERY = false; 30 31 33 protected String [] paramOrder; 34 35 36 38 private Object queryID; 39 40 41 52 public void initQuery( final TKSQLTypeConverter conv, final TKDBConnection tkConn, Object queryID) 53 { 54 this.queryID = queryID; 55 this.aTKDBConnection = tkConn; 56 initQuery( tkConn.getConnection() ); 57 } 58 59 72 public void initQuery( final Connection conn, 73 final boolean isPrepared, 74 final String [] paramOrder, 75 final Object [][] paramTypes, 76 final boolean[] setRelevants, 77 final String sqlString ) 78 { 79 super.initQuery( conn, paramTypes, setRelevants ); 80 81 this.paramOrder = paramOrder; 82 this.sqlString = sqlString; 83 84 if ( sqlString == null ) { 85 return; 86 } 87 88 try { 89 stmt = conn.prepareStatement(sqlString); 90 } catch (SQLException sqle) { 91 printSqlException(sqle, "Create Statement"); 92 } 93 } 94 95 103 104 105 108 public boolean execute() throws SQLException 109 { 110 if(REGISTER_QUERY)deregisterIndex = registerQuery(); 111 final PreparedStatement pstmt = (PreparedStatement) stmt; 112 boolean isAuto = aTKDBConnection.isAutoCommit(); 114 if (CAT.isDebugEnabled() ) { 115 CAT.debug("isaAuto Value during execute: "+ isAuto ); 116 CAT.debug("executing " + this ); 117 CAT.debug("<BR>SQL:" + sqlString + "<BR>"); 118 CAT.debug("PARAMS:" + queryParams + "<BR>"); 119 CAT.debug("paramOrder" + paramOrder); 120 } 121 122 try { 123 pstmt.clearParameters(); 124 } catch (SQLException sqle) { 125 if( isAuto) TKDBManager.closeConnection(); 126 printSqlException(sqle, "clear Parameters"); 127 128 } 129 130 try { 131 if (paramOrder != null) { 132 for(int i=0; i < paramOrder.length; i++) { 133 final Object h = queryParams.get(paramOrder[i]); 134 if ((h==null) || (h instanceof TKNull)) { 135 int ht; 136 Object paramType = null; 137 if( paramTypes != null ){ 138 paramType = paramTypes.get(paramOrder[i]); 139 } 140 if ( paramType != null ){ 141 ht = ((Integer )paramType).intValue(); 142 } else { 143 ht = Types.INTEGER; 144 } 145 pstmt.setNull(i+1, ht); 146 } else { 147 if (paramTypes !=null) 148 { 149 Integer type = (Integer )paramTypes.get(paramOrder[i]); 150 if (type != null && type.intValue() == Types.CLOB && TKDBManager.getDBVendor() == QueryConstants.ORACLE) 151 { 152 pstmt.setObject(i+1," "); 156 } 157 else 158 pstmt.setObject(i+1,h); 159 } 160 else 161 pstmt.setObject(i+1,h); 162 } 163 } 164 } 165 } catch(SQLException sqle) { 166 if (isAuto) TKDBManager.closeConnection(); 167 printSqlException(sqle, "setObject"); 168 169 } 170 171 try { 172 currIsResultSet = pstmt.execute(); 173 } catch (SQLException sqle) { 174 if (isAuto) TKDBManager.closeConnection(); 175 printSqlException(sqle, "Execute Prepared Statement"); 176 } 177 queryParams.clear(); 178 currPos = 0; 179 180 181 return currIsResultSet; 182 } 183 184 185 186 187 188 196 public void specClose() throws SQLException{ 197 if(REGISTER_QUERY)deregisterQuery(); 198 199 if ( stmt != null ) { 200 throwawayResults(); 201 } 202 if(REUSE_QUERY) 203 { 204 aTKDBConnection.storePrepQuery(this, queryID); 205 } 206 else { 208 if (stmt != null) 209 { 210 stmt.close(); 211 } 212 } 213 } 214 215 218 public Statement getStatement(){ 219 return stmt; 220 } 221 222 240 public static void enableCleanup(){ 241 REGISTER_QUERY = true; 242 REUSE_QUERY = true; 243 244 } 245 246 247 250 public static void enableCleanup(boolean yes){ 251 if(yes){ 252 enableCleanup(); 253 } 254 else{ 255 disableCleanup(); 256 } 257 } 258 259 266 public static void disableCleanup(){ 267 REGISTER_QUERY = false; 268 REUSE_QUERY = true; 269 270 } 271 272 } 273 274
| Popular Tags
|