001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029: package com.caucho.db.jdbc;
030:
031: import com.caucho.db.Database;
032: import com.caucho.db.sql.Query;
033: import com.caucho.db.sql.QueryContext;
034: import com.caucho.db.sql.SelectQuery;
035: import com.caucho.db.store.Transaction;
036: import com.caucho.util.L10N;
037:
038: import java.sql.SQLException;
039: import java.sql.SQLWarning;
040:
041: /**
042: * The JDBC statement implementation.
043: */
044: public class StatementImpl implements java.sql.Statement {
045: private final static L10N L = new L10N(StatementImpl.class);
046:
047: protected Database _db;
048: protected final ConnectionImpl _conn;
049:
050: protected ResultSetImpl _rs;
051:
052: protected QueryContext _queryContext;
053:
054: private boolean _isClosed;
055:
056: StatementImpl(ConnectionImpl conn) {
057: _conn = conn;
058: _db = conn.getDatabase();
059:
060: if (_db == null)
061: throw new NullPointerException();
062:
063: init();
064: }
065:
066: protected void init() {
067: if (_queryContext == null)
068: _queryContext = QueryContext.allocate();
069: }
070:
071: protected QueryContext getQueryContext() {
072: if (_queryContext == null)
073: _queryContext = QueryContext.allocate();
074:
075: return _queryContext;
076: }
077:
078: public void addBatch(String sql) {
079: }
080:
081: public void cancel() {
082: }
083:
084: public void clearBatch() {
085: }
086:
087: public void clearWarnings() {
088: }
089:
090: public java.sql.ResultSet executeQuery(String sql)
091: throws SQLException {
092: if (_db == null)
093: throw new SQLException(L.l("statement is closed"));
094:
095: Query query = _db.parseQuery(sql);
096:
097: return executeQuery(query);
098: }
099:
100: private java.sql.ResultSet executeQuery(Query query)
101: throws SQLException {
102: Transaction xa = _conn.getTransaction();
103:
104: boolean isOkay = false;
105: try {
106: query.execute(_queryContext, xa);
107: isOkay = true;
108: } finally {
109: if (!xa.isAutoCommit()) {
110: } else if (isOkay)
111: xa.commit();
112: else
113: xa.rollback();
114: }
115:
116: _rs = new ResultSetImpl(this , _queryContext.getResult());
117:
118: return _rs;
119: }
120:
121: /**
122: * Executes an update statement with the given SQL.
123: *
124: * @param sql the SQL to execute.
125: *
126: * @return the number of rows modified.
127: */
128: public int executeUpdate(String sql) throws SQLException {
129: Query query = _db.parseQuery(sql);
130:
131: return executeUpdate(query);
132: }
133:
134: public int executeUpdate(String sql, int autoGeneratedKeys)
135: throws SQLException {
136: Query query = _db.parseQuery(sql);
137:
138: _queryContext.setReturnGeneratedKeys(true);
139:
140: return executeUpdate(query);
141: }
142:
143: public int executeUpdate(String sql, int[] columnIndexes)
144: throws SQLException {
145: Query query = _db.parseQuery(sql);
146:
147: _queryContext.setReturnGeneratedKeys(true);
148:
149: return executeUpdate(query);
150: }
151:
152: public int executeUpdate(String sql, String[] columnNames)
153: throws SQLException {
154: Query query = _db.parseQuery(sql);
155:
156: _queryContext.setReturnGeneratedKeys(true);
157:
158: return executeUpdate(query);
159: }
160:
161: /**
162: * Executes the update for the given query.
163: *
164: * @return the number of rows modified
165: */
166: private int executeUpdate(Query query) throws SQLException {
167: Transaction xa = _conn.getTransaction();
168: boolean isOkay = false;
169: _queryContext.setTransaction(xa);
170:
171: try {
172: query.execute(_queryContext, xa);
173: isOkay = true;
174: } finally {
175: if (!xa.isAutoCommit()) {
176: } else if (isOkay)
177: xa.commit();
178: else
179: xa.rollback();
180: }
181:
182: return _queryContext.getRowUpdateCount();
183: }
184:
185: public java.sql.ResultSet getGeneratedKeys() {
186: java.sql.ResultSet rs = _queryContext
187: .getGeneratedKeysResultSet();
188:
189: if (rs != null)
190: return rs;
191: else
192: return new NullResultSet();
193: }
194:
195: public boolean execute(String sql) throws SQLException {
196: Query query = _db.parseQuery(sql);
197:
198: if (query instanceof SelectQuery) {
199: executeQuery(query);
200:
201: return true;
202: } else {
203: executeUpdate(query);
204:
205: return false;
206: }
207: }
208:
209: public int[] executeBatch() throws SQLException {
210: return null;
211: }
212:
213: public java.sql.ResultSet getResultSet() {
214: return _rs;
215: }
216:
217: public int getUpdateCount() {
218: return _queryContext.getRowUpdateCount();
219: }
220:
221: public java.sql.Connection getConnection() {
222: return _conn;
223: }
224:
225: public int getFetchDirection() {
226: return 0;
227: }
228:
229: public int getFetchSize() {
230: return _queryContext.getLimit();
231: }
232:
233: public int getMaxFieldSize() {
234: return 0;
235: }
236:
237: public int getMaxRows() {
238: return 0;
239: }
240:
241: public void setMaxRows(int max) {
242: }
243:
244: public boolean getMoreResults() {
245: return false;
246: }
247:
248: public int getQueryTimeout() {
249: return 0;
250: }
251:
252: public int getResultSetConcurrency() {
253: return 0;
254: }
255:
256: public int getResultSetType() {
257: return 0;
258: }
259:
260: public SQLWarning getWarnings() {
261: return null;
262: }
263:
264: public void setCursorName(String name) {
265: }
266:
267: public void setEscapeProcessing(boolean enable) {
268: }
269:
270: public void setFetchDirection(int direction) {
271: }
272:
273: public void setFetchSize(int rows) {
274: _queryContext.setLimit(rows);
275: }
276:
277: public void setMaxFieldSize(int max) {
278: }
279:
280: public void setQueryTimeout(int seconds) {
281: }
282:
283: // jdk 1.4
284: public boolean getMoreResults(int count) {
285: throw new UnsupportedOperationException();
286: }
287:
288: public boolean execute(String query, int foo) {
289: throw new UnsupportedOperationException();
290: }
291:
292: public boolean execute(String query, int[] foo) {
293: throw new UnsupportedOperationException();
294: }
295:
296: public boolean execute(String query, String[] foo) {
297: throw new UnsupportedOperationException();
298: }
299:
300: public int getResultSetHoldability() {
301: throw new UnsupportedOperationException();
302: }
303:
304: public void close() throws SQLException {
305: _db = null;
306:
307: ResultSetImpl rs = _rs;
308: _rs = null;
309:
310: QueryContext queryContext = _queryContext;
311: _queryContext = null;
312:
313: if (rs != null)
314: rs.close();
315:
316: _conn.closeStatement(this );
317:
318: if (queryContext != null)
319: QueryContext.free(queryContext);
320: }
321:
322: public boolean isClosed() throws SQLException {
323: throw new UnsupportedOperationException("Not supported yet.");
324: }
325:
326: public void setPoolable(boolean poolable) throws SQLException {
327: throw new UnsupportedOperationException("Not supported yet.");
328: }
329:
330: public boolean isPoolable() throws SQLException {
331: throw new UnsupportedOperationException("Not supported yet.");
332: }
333:
334: public <T> T unwrap(Class<T> iface) throws SQLException {
335: throw new UnsupportedOperationException("Not supported yet.");
336: }
337:
338: public boolean isWrapperFor(Class<?> iface) throws SQLException {
339: throw new UnsupportedOperationException("Not supported yet.");
340: }
341: }
|