001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.impl;
017:
018: import com.ibatis.common.jdbc.exception.NestedSQLException;
019: import com.ibatis.common.util.PaginatedList;
020: import com.ibatis.sqlmap.client.SqlMapSession;
021: import com.ibatis.sqlmap.client.event.RowHandler;
022: import com.ibatis.sqlmap.engine.execution.BatchException;
023: import com.ibatis.sqlmap.engine.execution.SqlExecutor;
024: import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
025: import com.ibatis.sqlmap.engine.scope.SessionScope;
026: import com.ibatis.sqlmap.engine.transaction.Transaction;
027: import com.ibatis.sqlmap.engine.transaction.TransactionException;
028:
029: import javax.sql.DataSource;
030: import java.sql.Connection;
031: import java.sql.SQLException;
032: import java.util.List;
033: import java.util.Map;
034:
035: /**
036: * Implementation of SqlMapSession
037: */
038: public class SqlMapSessionImpl implements SqlMapSession {
039:
040: protected SqlMapExecutorDelegate delegate;
041: protected SessionScope session;
042: protected boolean closed;
043:
044: /**
045: * Constructor
046: *
047: * @param client - the client that will use the session
048: */
049: public SqlMapSessionImpl(ExtendedSqlMapClient client) {
050: this .delegate = client.getDelegate();
051: this .session = this .delegate.popSession();
052: this .session.setSqlMapClient(client);
053: this .session.setSqlMapExecutor(client);
054: this .session.setSqlMapTxMgr(client);
055: this .closed = false;
056: }
057:
058: /**
059: * Start the session
060: */
061: public void open() {
062: session.setSqlMapTxMgr(this );
063: }
064:
065: /**
066: * Getter to tell if the session is still open
067: *
068: * @return - the status of the session
069: */
070: public boolean isClosed() {
071: return closed;
072: }
073:
074: public void close() {
075: if (delegate != null && session != null)
076: delegate.pushSession(session);
077: if (session != null)
078: session = null;
079: if (delegate != null)
080: delegate = null;
081: if (!closed)
082: closed = true;
083: }
084:
085: public Object insert(String id, Object param) throws SQLException {
086: return delegate.insert(session, id, param);
087: }
088:
089: public Object insert(String id) throws SQLException {
090: return insert(id, null);
091: }
092:
093: public int update(String id, Object param) throws SQLException {
094: return delegate.update(session, id, param);
095: }
096:
097: public int update(String id) throws SQLException {
098: return update(id, null);
099: }
100:
101: public int delete(String id, Object param) throws SQLException {
102: return delegate.delete(session, id, param);
103: }
104:
105: public int delete(String id) throws SQLException {
106: return delete(id, null);
107: }
108:
109: public Object queryForObject(String id, Object paramObject)
110: throws SQLException {
111: return delegate.queryForObject(session, id, paramObject);
112: }
113:
114: public Object queryForObject(String id) throws SQLException {
115: return queryForObject(id, null);
116: }
117:
118: public Object queryForObject(String id, Object paramObject,
119: Object resultObject) throws SQLException {
120: return delegate.queryForObject(session, id, paramObject,
121: resultObject);
122: }
123:
124: public List queryForList(String id, Object paramObject)
125: throws SQLException {
126: return delegate.queryForList(session, id, paramObject);
127: }
128:
129: public List queryForList(String id) throws SQLException {
130: return queryForList(id, null);
131: }
132:
133: public List queryForList(String id, Object paramObject, int skip,
134: int max) throws SQLException {
135: return delegate.queryForList(session, id, paramObject, skip,
136: max);
137: }
138:
139: public List queryForList(String id, int skip, int max)
140: throws SQLException {
141: return queryForList(id, null, skip, max);
142: }
143:
144: /**
145: * @deprecated All paginated list features have been deprecated
146: */
147: public PaginatedList queryForPaginatedList(String id,
148: Object paramObject, int pageSize) throws SQLException {
149: return delegate.queryForPaginatedList(session, id, paramObject,
150: pageSize);
151: }
152:
153: /**
154: * @deprecated All paginated list features have been deprecated
155: */
156: public PaginatedList queryForPaginatedList(String id, int pageSize)
157: throws SQLException {
158: return queryForPaginatedList(id, null, pageSize);
159: }
160:
161: public Map queryForMap(String id, Object paramObject, String keyProp)
162: throws SQLException {
163: return delegate.queryForMap(session, id, paramObject, keyProp);
164: }
165:
166: public Map queryForMap(String id, Object paramObject,
167: String keyProp, String valueProp) throws SQLException {
168: return delegate.queryForMap(session, id, paramObject, keyProp,
169: valueProp);
170: }
171:
172: public void queryWithRowHandler(String id, Object paramObject,
173: RowHandler rowHandler) throws SQLException {
174: delegate.queryWithRowHandler(session, id, paramObject,
175: rowHandler);
176: }
177:
178: public void queryWithRowHandler(String id, RowHandler rowHandler)
179: throws SQLException {
180: queryWithRowHandler(id, null, rowHandler);
181: }
182:
183: public void startTransaction() throws SQLException {
184: delegate.startTransaction(session);
185: }
186:
187: public void startTransaction(int transactionIsolation)
188: throws SQLException {
189: delegate.startTransaction(session, transactionIsolation);
190: }
191:
192: public void commitTransaction() throws SQLException {
193: delegate.commitTransaction(session);
194: }
195:
196: public void endTransaction() throws SQLException {
197: delegate.endTransaction(session);
198: }
199:
200: public void startBatch() throws SQLException {
201: delegate.startBatch(session);
202: }
203:
204: public int executeBatch() throws SQLException {
205: return delegate.executeBatch(session);
206: }
207:
208: public List executeBatchDetailed() throws SQLException,
209: BatchException {
210: return delegate.executeBatchDetailed(session);
211: }
212:
213: public void setUserConnection(Connection connection)
214: throws SQLException {
215: delegate.setUserProvidedTransaction(session, connection);
216: }
217:
218: /**
219: * TODO Deprecated
220: *
221: * @return Current connection
222: * @throws SQLException
223: * @deprecated
224: */
225: public Connection getUserConnection() throws SQLException {
226: return getCurrentConnection();
227: }
228:
229: public Connection getCurrentConnection() throws SQLException {
230: try {
231: Connection conn = null;
232: Transaction trans = delegate.getTransaction(session);
233: if (trans != null) {
234: conn = trans.getConnection();
235: }
236: return conn;
237: } catch (TransactionException e) {
238: throw new NestedSQLException(
239: "Error getting Connection from Transaction. Cause: "
240: + e, e);
241: }
242: }
243:
244: public DataSource getDataSource() {
245: return delegate.getDataSource();
246: }
247:
248: /**
249: * Gets a mapped statement by ID
250: *
251: * @param id - the ID
252: * @return - the mapped statement
253: */
254: public MappedStatement getMappedStatement(String id) {
255: return delegate.getMappedStatement(id);
256: }
257:
258: /**
259: * Get the status of lazy loading
260: *
261: * @return - the status
262: */
263: public boolean isLazyLoadingEnabled() {
264: return delegate.isLazyLoadingEnabled();
265: }
266:
267: /**
268: * Get the status of CGLib enhancements
269: *
270: * @return - the status
271: */
272: public boolean isEnhancementEnabled() {
273: return delegate.isEnhancementEnabled();
274: }
275:
276: /**
277: * Get the SQL executor
278: *
279: * @return - the executor
280: */
281: public SqlExecutor getSqlExecutor() {
282: return delegate.getSqlExecutor();
283: }
284:
285: /**
286: * Get the delegate
287: *
288: * @return - the delegate
289: */
290: public SqlMapExecutorDelegate getDelegate() {
291: return delegate;
292: }
293:
294: }
|