1 55 package org.lateralnz.c3d; 56 57 import java.io.Serializable ; 58 import java.sql.Connection ; 59 import java.sql.ResultSet ; 60 import java.sql.SQLException ; 61 import java.sql.SQLWarning ; 62 import java.sql.Statement ; 63 64 import org.apache.log4j.Logger; 65 66 import org.lateralnz.common.util.Constants; 67 import org.lateralnz.common.util.DAOUtils; 68 69 import org.lateralnz.c3d.util.ResultWrapper; 70 71 public class DCStatement implements Statement , Serializable , Constants { 72 private static final Logger log = Logger.getLogger(DCStatement.class.getName()); 73 74 protected static final int STATEMENT = 1; 75 protected static final int PREPARED = 2; 76 protected static final int CALLABLE = 3; 77 78 protected DCConnection conn; private String sql = EMPTY; Statement realStatement = null; protected int statementType; protected ResultSet rs; protected int updateCount = -1; protected boolean nocaching = false; 86 protected int[] insertKeyColumnIndexes = null; protected String [] insertKeyColumns = null; 88 protected int autoGeneratedKeys = Integer.MIN_VALUE; 89 private String cursorName = null; 90 private boolean escapeProcessing = true; 91 private int fetchDirection = Integer.MIN_VALUE; 92 private int fetchSize = Integer.MIN_VALUE; 93 private int maxFieldSize = Integer.MIN_VALUE; 94 private int maxRows = Integer.MIN_VALUE; 95 private int queryTimeout = Integer.MIN_VALUE; 96 protected int resultsetType = Integer.MIN_VALUE; 97 protected int resultsetConcurrency = Integer.MIN_VALUE; 98 protected int resultsetHoldability = Integer.MIN_VALUE; 99 100 105 DCStatement(DCConnection conn, int resultsetType, int resultsetConcurrency, int resultsetHoldability) { 106 this.conn = conn; 107 this.resultsetType = resultsetType; 108 this.resultsetConcurrency = resultsetConcurrency; 109 this.resultsetHoldability = resultsetHoldability; 110 this.statementType = STATEMENT; 111 } 112 113 protected Statement getRealStatement() throws SQLException { 114 if (realStatement == null) { 115 if (resultsetHoldability != Integer.MIN_VALUE && resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE) { 116 realStatement = conn.getRealConnection().createStatement(resultsetType, resultsetConcurrency, resultsetHoldability); 117 } 118 else if (resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE) { 119 realStatement = conn.getRealConnection().createStatement(resultsetType, resultsetConcurrency); 120 } 121 else { 122 realStatement = conn.getRealConnection().createStatement(); 123 } 124 if (cursorName != null) { 125 realStatement.setCursorName(cursorName); 126 } 127 128 realStatement.setEscapeProcessing(escapeProcessing); 129 130 if (fetchDirection != Integer.MIN_VALUE) { 131 realStatement.setFetchDirection(fetchDirection); 132 } 133 134 if (fetchSize != Integer.MIN_VALUE) { 135 realStatement.setFetchSize(fetchSize); 136 } 137 138 if (maxFieldSize != Integer.MIN_VALUE) { 139 realStatement.setMaxFieldSize(maxFieldSize); 140 } 141 142 if (maxRows != Integer.MIN_VALUE) { 143 realStatement.setMaxRows(maxRows); 144 } 145 146 if (queryTimeout != Integer.MIN_VALUE) { 147 realStatement.setQueryTimeout(queryTimeout); 148 } 149 } 150 return realStatement; 151 } 152 153 public void addBatch(String str) throws SQLException { 154 throw new SQLException ("batch not supported"); 155 } 156 157 public void cancel() throws SQLException { 158 if (realStatement != null) { 159 realStatement.cancel(); 160 } 161 } 162 163 public void clearBatch() throws SQLException { 164 throw new SQLException ("batch not supported"); 165 } 166 167 public void clearWarnings() throws SQLException { 168 if (realStatement != null) { 169 realStatement.clearWarnings(); 170 } 171 } 172 173 public void close() throws SQLException { 174 autoGeneratedKeys = Integer.MIN_VALUE; 175 insertKeyColumnIndexes = null; 176 insertKeyColumns = null; 177 178 DAOUtils.close(realStatement); 179 } 180 181 public boolean execute(String sql) throws SQLException { 182 setSQL(sql); 183 DatabaseEngine dbengine = conn.getDatabaseEngine(); 184 185 ResultWrapper rw = dbengine.execute(this); 186 if (rw != null) { 187 this.rs = rw.rs; 188 this.updateCount = rw.updateCount; 189 if (log.isDebugEnabled()) { 190 log.debug("execute returned rs " + rs + ", updatecount " + updateCount); 191 } 192 } 193 194 if (rs != null) { 195 return true; 196 } 197 else { 198 return false; 199 } 200 } 201 202 public boolean execute(String sql, String [] columnNames) throws SQLException { 203 this.insertKeyColumns = columnNames; 204 return execute(sql); 205 } 206 207 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 208 this.insertKeyColumnIndexes = columnIndexes; 209 return execute(sql); 210 } 211 212 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 213 this.autoGeneratedKeys = autoGeneratedKeys; 214 return execute(sql); 215 } 216 217 public int[] executeBatch() throws SQLException { 218 throw new SQLException ("batch not supported"); 219 } 220 221 public ResultSet executeQuery(String sql) throws SQLException { 222 if (execute(sql)) { 223 return getResultSet(); 224 } 225 else { 226 throw new SQLException ("invalid query"); 227 } 228 } 229 230 public int executeUpdate(String sql) throws SQLException { 231 execute(sql); 232 return getUpdateCount(); 233 } 234 235 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 236 execute(sql, columnNames); 237 return getUpdateCount(); 238 } 239 240 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 241 execute(sql, autoGeneratedKeys); 242 return getUpdateCount(); 243 } 244 245 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 246 execute(sql, columnIndexes); 247 return getUpdateCount(); 248 } 249 250 public Connection getConnection() throws SQLException { 251 return conn; 252 } 253 254 public int getFetchDirection() throws SQLException { 255 return ResultSet.FETCH_FORWARD; 256 } 257 258 public int getFetchSize() throws SQLException { 259 if (realStatement != null) { 260 return realStatement.getFetchSize(); 261 } 262 else { 263 return 10; 264 } 265 } 266 267 public ResultSet getGeneratedKeys() throws SQLException { 268 if (realStatement != null) { 269 return realStatement.getGeneratedKeys(); 270 } 271 else { 272 return new DCResultSet(null, null, null, null, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_FORWARD_ONLY); 273 } 274 } 275 276 public int getMaxFieldSize() throws SQLException { 277 return 0; 278 } 279 280 public int getMaxRows() throws SQLException { 281 return 0; 282 } 283 284 public boolean getMoreResults() throws SQLException { 285 return false; 286 } 287 288 public boolean getMoreResults(int param) throws SQLException { 289 return false; 290 } 291 292 public int getQueryTimeout() throws SQLException { 293 if (realStatement != null) { 294 return realStatement.getQueryTimeout(); 295 } 296 else { 297 return 0; 298 } 299 } 300 301 public ResultSet getResultSet() throws SQLException { 302 return rs; 303 } 304 305 public int getResultSetConcurrency() throws SQLException { 306 return resultsetConcurrency; 307 } 308 309 public int getResultSetHoldability() throws SQLException { 310 return resultsetHoldability; 311 } 312 313 public int getResultSetType() throws SQLException { 314 return resultsetType; 315 } 316 317 protected String getSQL() { 318 return sql; 319 } 320 321 public int getUpdateCount() throws SQLException { 322 return updateCount; 323 } 324 325 public SQLWarning getWarnings() throws SQLException { 326 if (realStatement != null) { 327 return realStatement.getWarnings(); 328 } 329 else { 330 return null; 331 } 332 } 333 334 public void setCursorName(String str) throws SQLException { 335 this.cursorName = str; 336 if (realStatement != null) { 337 realStatement.setCursorName(str); 338 } 339 } 340 341 public void setEscapeProcessing(boolean param) throws SQLException { 342 this.escapeProcessing = param; 343 if (realStatement != null) { 344 realStatement.setEscapeProcessing(param); 345 } 346 } 347 348 public void setFetchDirection(int param) throws SQLException { 349 this.fetchDirection = param; 350 if (realStatement != null) { 351 realStatement.setFetchDirection(param); 352 } 353 } 354 355 public void setFetchSize(int param) throws SQLException { 356 this.fetchSize = param; 357 if (realStatement != null) { 358 realStatement.setFetchSize(param); 359 } 360 } 361 362 public void setMaxFieldSize(int param) throws SQLException { 363 this.maxFieldSize = param; 364 if (realStatement != null) { 365 realStatement.setMaxFieldSize(maxFieldSize); 366 } 367 } 368 369 public void setMaxRows(int param) throws SQLException { 370 this.maxRows = param; 371 if (realStatement != null) { 372 realStatement.setMaxRows(param); 373 } 374 } 375 376 public void setQueryTimeout(int param) throws SQLException { 377 this.queryTimeout = param; 378 if (realStatement != null) { 379 realStatement.setQueryTimeout(param); 380 } 381 } 382 383 protected void setSQL(String sql) { 384 this.sql = sql; 385 } 386 } | Popular Tags |