001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.core.impl;
010:
011: import com.completex.objective.components.persistency.transact.Transaction;
012:
013: import java.sql.PreparedStatement;
014: import java.sql.SQLException;
015:
016: /**
017: * @author Gennady Krizhevsky
018: */
019: public class PreparedStatementWrapper {
020:
021: private PreparedStatement statement;
022: private boolean closed;
023: private boolean batchModify;
024: private String sql;
025: private StringBuffer bindBuffer;
026: private int lastRc;
027: private int[] rcs;
028:
029: private Transaction transaction;
030:
031: public PreparedStatementWrapper(PreparedStatement statement,
032: Transaction transaction) {
033:
034: this .statement = statement;
035: this .transaction = transaction;
036: }
037:
038: public int[] getRcs() {
039: return rcs;
040: }
041:
042: public int getLastRc() {
043: return lastRc;
044: }
045:
046: public String getSql() {
047: return sql;
048: }
049:
050: public void setSql(String sql) {
051: this .sql = sql;
052: }
053:
054: public StringBuffer getBindBuffer() {
055: return bindBuffer;
056: }
057:
058: public void setBindBuffer(StringBuffer bindBuffer) {
059: this .bindBuffer = bindBuffer;
060: }
061:
062: public boolean isBatchModify() {
063: return batchModify;
064: }
065:
066: public void setBatchModify(boolean batchModify) {
067: this .batchModify = batchModify;
068: }
069:
070: public boolean isClosed() {
071: return closed;
072: }
073:
074: public void addBatch() throws SQLException {
075: statement().addBatch();
076: }
077:
078: public void close() throws SQLException {
079: try {
080: if (statement != null) {
081: //
082: // Cycle through the rest of the results & close the result sets:
083: //
084: while (statement.getMoreResults()
085: && statement.getUpdateCount() == -1) {
086: }
087: transaction.releaseStatement(statement);
088: this .statement = null;
089: }
090: } finally {
091: this .closed = true;
092: }
093: }
094:
095: public void executeBatch() throws SQLException {
096: if (statement != null && batchModify) {
097: rcs = statement().executeBatch();
098: if (rcs != null && rcs.length > 0) {
099: lastRc = rcs[rcs.length - 1];
100: } else {
101: lastRc = 0;
102: }
103: }
104: }
105:
106: public PreparedStatement getStatement() {
107: return statement;
108: }
109:
110: public PreparedStatement statement() {
111: if (closed) {
112: throw new RuntimeException(
113: "Attempt to use closed prepared statement");
114: }
115: return statement;
116: }
117:
118: public int executeUpdate() throws SQLException {
119: lastRc = statement().executeUpdate();
120: return lastRc;
121: }
122:
123: public String toString() {
124: StringBuffer buffer = new StringBuffer(
125: "PreparedStatementWraper : {");
126: buffer.append("statement = ").append(statement);
127: buffer.append("; closed = ").append(closed);
128: buffer.append("; batchModify = ").append(batchModify);
129: buffer.append("; sql = ").append(sql);
130: buffer.append("; bindBuffer = ").append(bindBuffer);
131: buffer.append("}");
132: return super.toString() + buffer.toString();
133: }
134:
135: }
|