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.util.PaginatedList;
019: import com.ibatis.common.logging.Log;
020: import com.ibatis.common.logging.LogFactory;
021: import com.ibatis.sqlmap.client.SqlMapException;
022: import com.ibatis.sqlmap.client.SqlMapSession;
023: import com.ibatis.sqlmap.client.event.RowHandler;
024: import com.ibatis.sqlmap.engine.execution.BatchException;
025: import com.ibatis.sqlmap.engine.execution.SqlExecutor;
026: import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
027: import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
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 ExtendedSqlMapClient
037: */
038: public class SqlMapClientImpl implements ExtendedSqlMapClient {
039:
040: private static final Log log = LogFactory
041: .getLog(SqlMapClientImpl.class);
042:
043: /**
044: * Delegate for SQL execution
045: */
046: public SqlMapExecutorDelegate delegate;
047:
048: protected ThreadLocal localSqlMapSession = new ThreadLocal();
049:
050: /**
051: * Constructor to supply a delegate
052: *
053: * @param delegate - the delegate
054: */
055: public SqlMapClientImpl(SqlMapExecutorDelegate delegate) {
056: this .delegate = delegate;
057: }
058:
059: public Object insert(String id, Object param) throws SQLException {
060: return getLocalSqlMapSession().insert(id, param);
061: }
062:
063: public Object insert(String id) throws SQLException {
064: return getLocalSqlMapSession().insert(id);
065: }
066:
067: public int update(String id, Object param) throws SQLException {
068: return getLocalSqlMapSession().update(id, param);
069: }
070:
071: public int update(String id) throws SQLException {
072: return getLocalSqlMapSession().update(id);
073: }
074:
075: public int delete(String id, Object param) throws SQLException {
076: return getLocalSqlMapSession().delete(id, param);
077: }
078:
079: public int delete(String id) throws SQLException {
080: return getLocalSqlMapSession().delete(id);
081: }
082:
083: public Object queryForObject(String id, Object paramObject)
084: throws SQLException {
085: return getLocalSqlMapSession().queryForObject(id, paramObject);
086: }
087:
088: public Object queryForObject(String id) throws SQLException {
089: return getLocalSqlMapSession().queryForObject(id);
090: }
091:
092: public Object queryForObject(String id, Object paramObject,
093: Object resultObject) throws SQLException {
094: return getLocalSqlMapSession().queryForObject(id, paramObject,
095: resultObject);
096: }
097:
098: public List queryForList(String id, Object paramObject)
099: throws SQLException {
100: return getLocalSqlMapSession().queryForList(id, paramObject);
101: }
102:
103: public List queryForList(String id) throws SQLException {
104: return getLocalSqlMapSession().queryForList(id);
105: }
106:
107: public List queryForList(String id, Object paramObject, int skip,
108: int max) throws SQLException {
109: return getLocalSqlMapSession().queryForList(id, paramObject,
110: skip, max);
111: }
112:
113: public List queryForList(String id, int skip, int max)
114: throws SQLException {
115: return getLocalSqlMapSession().queryForList(id, skip, max);
116: }
117:
118: /**
119: * @deprecated All paginated list features have been deprecated
120: */
121: public PaginatedList queryForPaginatedList(String id,
122: Object paramObject, int pageSize) throws SQLException {
123: return getLocalSqlMapSession().queryForPaginatedList(id,
124: paramObject, pageSize);
125: }
126:
127: /**
128: * @deprecated All paginated list features have been deprecated
129: */
130: public PaginatedList queryForPaginatedList(String id, int pageSize)
131: throws SQLException {
132: return getLocalSqlMapSession().queryForPaginatedList(id,
133: pageSize);
134: }
135:
136: public Map queryForMap(String id, Object paramObject, String keyProp)
137: throws SQLException {
138: return getLocalSqlMapSession().queryForMap(id, paramObject,
139: keyProp);
140: }
141:
142: public Map queryForMap(String id, Object paramObject,
143: String keyProp, String valueProp) throws SQLException {
144: return getLocalSqlMapSession().queryForMap(id, paramObject,
145: keyProp, valueProp);
146: }
147:
148: public void queryWithRowHandler(String id, Object paramObject,
149: RowHandler rowHandler) throws SQLException {
150: getLocalSqlMapSession().queryWithRowHandler(id, paramObject,
151: rowHandler);
152: }
153:
154: public void queryWithRowHandler(String id, RowHandler rowHandler)
155: throws SQLException {
156: getLocalSqlMapSession().queryWithRowHandler(id, rowHandler);
157: }
158:
159: public void startTransaction() throws SQLException {
160: getLocalSqlMapSession().startTransaction();
161: }
162:
163: public void startTransaction(int transactionIsolation)
164: throws SQLException {
165: getLocalSqlMapSession().startTransaction(transactionIsolation);
166: }
167:
168: public void commitTransaction() throws SQLException {
169: getLocalSqlMapSession().commitTransaction();
170: }
171:
172: public void endTransaction() throws SQLException {
173: try {
174: getLocalSqlMapSession().endTransaction();
175: } finally {
176: getLocalSqlMapSession().close();
177: }
178: }
179:
180: public void startBatch() throws SQLException {
181: getLocalSqlMapSession().startBatch();
182: }
183:
184: public int executeBatch() throws SQLException {
185: return getLocalSqlMapSession().executeBatch();
186: }
187:
188: public List executeBatchDetailed() throws SQLException,
189: BatchException {
190: return getLocalSqlMapSession().executeBatchDetailed();
191: }
192:
193: public void setUserConnection(Connection connection)
194: throws SQLException {
195: try {
196: getLocalSqlMapSession().setUserConnection(connection);
197: } finally {
198: if (connection == null) {
199: getLocalSqlMapSession().close();
200: }
201: }
202: }
203:
204: /**
205: * TODO Deprecated
206: *
207: * @return Current connection
208: * @throws SQLException
209: * @deprecated
210: */
211: public Connection getUserConnection() throws SQLException {
212: return getCurrentConnection();
213: }
214:
215: public Connection getCurrentConnection() throws SQLException {
216: return getLocalSqlMapSession().getCurrentConnection();
217: }
218:
219: public DataSource getDataSource() {
220: return delegate.getDataSource();
221: }
222:
223: public MappedStatement getMappedStatement(String id) {
224: return delegate.getMappedStatement(id);
225: }
226:
227: public boolean isLazyLoadingEnabled() {
228: return delegate.isLazyLoadingEnabled();
229: }
230:
231: public boolean isEnhancementEnabled() {
232: return delegate.isEnhancementEnabled();
233: }
234:
235: public SqlExecutor getSqlExecutor() {
236: return delegate.getSqlExecutor();
237: }
238:
239: public SqlMapExecutorDelegate getDelegate() {
240: return delegate;
241: }
242:
243: public SqlMapSession openSession() {
244: SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this );
245: sqlMapSession.open();
246: return sqlMapSession;
247: }
248:
249: public SqlMapSession openSession(Connection conn) {
250: try {
251: SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(
252: this );
253: sqlMapSession.open();
254: sqlMapSession.setUserConnection(conn);
255: return sqlMapSession;
256: } catch (SQLException e) {
257: throw new SqlMapException(
258: "Error setting user provided connection. Cause: "
259: + e, e);
260: }
261: }
262:
263: /**
264: * TODO : DEPRECATED
265: *
266: * @deprecated Use openSession()
267: */
268: public SqlMapSession getSession() {
269: log
270: .warn("Use of a deprecated API detected. SqlMapClient.getSession() is deprecated. Use SqlMapClient.openSession() instead.");
271: return openSession();
272: }
273:
274: public void flushDataCache() {
275: delegate.flushDataCache();
276: }
277:
278: public void flushDataCache(String cacheId) {
279: delegate.flushDataCache(cacheId);
280: }
281:
282: protected SqlMapSessionImpl getLocalSqlMapSession() {
283: SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession
284: .get();
285: if (sqlMapSession == null || sqlMapSession.isClosed()) {
286: sqlMapSession = new SqlMapSessionImpl(this );
287: localSqlMapSession.set(sqlMapSession);
288: }
289: return sqlMapSession;
290: }
291:
292: public ResultObjectFactory getResultObjectFactory() {
293: return delegate.getResultObjectFactory();
294: }
295: }
|