KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > impl > SqlMapClientImpl


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.impl;
17
18 import com.ibatis.common.util.PaginatedList;
19 import com.ibatis.common.logging.Log;
20 import com.ibatis.common.logging.LogFactory;
21 import com.ibatis.sqlmap.client.SqlMapException;
22 import com.ibatis.sqlmap.client.SqlMapSession;
23 import com.ibatis.sqlmap.client.event.RowHandler;
24 import com.ibatis.sqlmap.engine.execution.SqlExecutor;
25 import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
26 import com.ibatis.sqlmap.engine.binding.MapperProxy;
27
28 import javax.sql.DataSource JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Implementation of ExtendedSqlMapClient
36  */

37 public class SqlMapClientImpl implements ExtendedSqlMapClient {
38
39   private static final Log log = LogFactory.getLog(SqlMapClientImpl.class);
40
41   /**
42    * Delegate for SQL execution
43    */

44   public SqlMapExecutorDelegate delegate;
45
46   private ThreadLocal JavaDoc localSqlMapSession = new ThreadLocal JavaDoc();
47
48   /**
49    * Constructor to supply a delegate
50    *
51    * @param delegate - the delegate
52    */

53   public SqlMapClientImpl(SqlMapExecutorDelegate delegate) {
54     this.delegate = delegate;
55   }
56
57   public Object JavaDoc insert(String JavaDoc id, Object JavaDoc param) throws SQLException JavaDoc {
58     return getLocalSqlMapSession().insert(id, param);
59   }
60
61   public int update(String JavaDoc id, Object JavaDoc param) throws SQLException JavaDoc {
62     return getLocalSqlMapSession().update(id, param);
63   }
64
65   public int delete(String JavaDoc id, Object JavaDoc param) throws SQLException JavaDoc {
66     return getLocalSqlMapSession().delete(id, param);
67   }
68
69   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc paramObject) throws SQLException JavaDoc {
70     return getLocalSqlMapSession().queryForObject(id, paramObject);
71   }
72
73   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc paramObject, Object JavaDoc resultObject) throws SQLException JavaDoc {
74     return getLocalSqlMapSession().queryForObject(id, paramObject, resultObject);
75   }
76
77   public List queryForList(String JavaDoc id, Object JavaDoc paramObject) throws SQLException JavaDoc {
78     return getLocalSqlMapSession().queryForList(id, paramObject);
79   }
80
81   public List queryForList(String JavaDoc id, Object JavaDoc paramObject, int skip, int max) throws SQLException JavaDoc {
82     return getLocalSqlMapSession().queryForList(id, paramObject, skip, max);
83   }
84
85   public PaginatedList queryForPaginatedList(String JavaDoc id, Object JavaDoc paramObject, int pageSize) throws SQLException JavaDoc {
86     return getLocalSqlMapSession().queryForPaginatedList(id, paramObject, pageSize);
87   }
88
89   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc paramObject, String JavaDoc keyProp) throws SQLException JavaDoc {
90     return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp);
91   }
92
93   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc paramObject, String JavaDoc keyProp, String JavaDoc valueProp) throws SQLException JavaDoc {
94     return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp, valueProp);
95   }
96
97   public void queryWithRowHandler(String JavaDoc id, Object JavaDoc paramObject, RowHandler rowHandler) throws SQLException JavaDoc {
98     getLocalSqlMapSession().queryWithRowHandler(id, paramObject, rowHandler);
99   }
100
101   public void startTransaction() throws SQLException JavaDoc {
102     getLocalSqlMapSession().startTransaction();
103   }
104
105   public void startTransaction(int transactionIsolation) throws SQLException JavaDoc {
106     getLocalSqlMapSession().startTransaction(transactionIsolation);
107   }
108
109   public void commitTransaction() throws SQLException JavaDoc {
110     getLocalSqlMapSession().commitTransaction();
111   }
112
113   public void endTransaction() throws SQLException JavaDoc {
114     try {
115       getLocalSqlMapSession().endTransaction();
116     } finally {
117       getLocalSqlMapSession().close();
118     }
119   }
120
121   public void startBatch() throws SQLException JavaDoc {
122     getLocalSqlMapSession().startBatch();
123   }
124
125   public int executeBatch() throws SQLException JavaDoc {
126     return getLocalSqlMapSession().executeBatch();
127   }
128
129   public void setUserConnection(Connection JavaDoc connection) throws SQLException JavaDoc {
130     getLocalSqlMapSession().setUserConnection(connection);
131   }
132
133   /**
134    * TODO Deprecated
135    *
136    * @return
137    * @throws SQLException
138    * @deprecated
139    */

140   public Connection JavaDoc getUserConnection() throws SQLException JavaDoc {
141     return getCurrentConnection();
142   }
143
144   public Connection JavaDoc getCurrentConnection() throws SQLException JavaDoc {
145     return getLocalSqlMapSession().getCurrentConnection();
146   }
147
148   public DataSource JavaDoc getDataSource() {
149     return getLocalSqlMapSession().getDataSource();
150   }
151
152   public MappedStatement getMappedStatement(String JavaDoc id) {
153     return delegate.getMappedStatement(id);
154   }
155
156   public boolean isLazyLoadingEnabled() {
157     return delegate.isLazyLoadingEnabled();
158   }
159
160   public boolean isEnhancementEnabled() {
161     return delegate.isEnhancementEnabled();
162   }
163
164   public SqlExecutor getSqlExecutor() {
165     return delegate.getSqlExecutor();
166   }
167
168   public SqlMapExecutorDelegate getDelegate() {
169     return delegate;
170   }
171
172   public SqlMapSession openSession() {
173     SqlMapSessionImpl sqlMapSession = getLocalSqlMapSession();
174     sqlMapSession.open();
175     return sqlMapSession;
176   }
177
178   public SqlMapSession openSession(Connection JavaDoc conn) {
179     try {
180       SqlMapSessionImpl sqlMapSession = getLocalSqlMapSession();
181       sqlMapSession.open();
182       sqlMapSession.setUserConnection(conn);
183       return sqlMapSession;
184     } catch (SQLException JavaDoc e) {
185       throw new SqlMapException("Error setting user provided connection. Cause: " + e, e);
186     }
187   }
188
189   /**
190    * TODO : DEPRECATED
191    *
192    * @deprecated Use openSession()
193    */

194   public SqlMapSession getSession() {
195     log.warn("Use of a deprecated API detected. SqlMapClient.getSession() is deprecated. Use SqlMapClient.openSession() instead.");
196     return openSession();
197   }
198
199   public void flushDataCache() {
200     delegate.flushDataCache();
201   }
202
203   public void flushDataCache(String JavaDoc cacheId) {
204     delegate.flushDataCache(cacheId);
205   }
206
207   public Object JavaDoc getMapper(Class JavaDoc c) {
208     return MapperProxy.newMapperProxy(this, c);
209   }
210
211   private SqlMapSessionImpl getLocalSqlMapSession() {
212     SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession.get();
213     if (sqlMapSession == null || sqlMapSession.isClosed()) {
214       sqlMapSession = new SqlMapSessionImpl(this);
215       localSqlMapSession.set(sqlMapSession);
216     }
217     return sqlMapSession;
218   }
219
220 }
221
Popular Tags