1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.impl;
17
18 import com.ibatis.common.logging.Log;
19 import com.ibatis.common.logging.LogFactory;
20 import com.ibatis.common.util.PaginatedList;
21 import com.ibatis.sqlmap.client.SqlMapClient;
22 import com.ibatis.sqlmap.client.SqlMapException;
23 import com.ibatis.sqlmap.client.SqlMapSession;
24 import com.ibatis.sqlmap.client.event.RowHandler;
25 import com.ibatis.sqlmap.engine.execution.BatchException;
26 import com.ibatis.sqlmap.engine.execution.SqlExecutor;
27 import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
28 import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
29
30 import java.sql.Connection;
31 import java.sql.SQLException;
32 import java.util.List;
33 import java.util.Map;
34
35 import javax.sql.DataSource;
36
37
38
39
40 public class SqlMapClientImpl implements SqlMapClient, ExtendedSqlMapClient {
41
42
43 private static final Log log = LogFactory.getLog(SqlMapClientImpl.class);
44
45
46 public SqlMapExecutorDelegate delegate;
47
48
49 protected ThreadLocal<SqlMapSessionImpl> localSqlMapSession = new ThreadLocal<SqlMapSessionImpl>();
50
51
52
53
54
55
56
57 public SqlMapClientImpl(SqlMapExecutorDelegate delegate) {
58 this.delegate = delegate;
59 }
60
61 public Object insert(String id, Object param) throws SQLException {
62 return getLocalSqlMapSession().insert(id, param);
63 }
64
65 public Object insert(String id) throws SQLException {
66 return getLocalSqlMapSession().insert(id);
67 }
68
69 public int update(String id, Object param) throws SQLException {
70 return getLocalSqlMapSession().update(id, param);
71 }
72
73 public int update(String id) throws SQLException {
74 return getLocalSqlMapSession().update(id);
75 }
76
77 public int delete(String id, Object param) throws SQLException {
78 return getLocalSqlMapSession().delete(id, param);
79 }
80
81 public int delete(String id) throws SQLException {
82 return getLocalSqlMapSession().delete(id);
83 }
84
85 public Object queryForObject(String id, Object paramObject) throws SQLException {
86 return getLocalSqlMapSession().queryForObject(id, paramObject);
87 }
88
89 public Object queryForObject(String id) throws SQLException {
90 return getLocalSqlMapSession().queryForObject(id);
91 }
92
93 public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
94 return getLocalSqlMapSession().queryForObject(id, paramObject, resultObject);
95 }
96
97 public List queryForList(String id, Object paramObject) throws SQLException {
98 return getLocalSqlMapSession().queryForList(id, paramObject);
99 }
100
101 public List queryForList(String id) throws SQLException {
102 return getLocalSqlMapSession().queryForList(id);
103 }
104
105 public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
106 return getLocalSqlMapSession().queryForList(id, paramObject, skip, max);
107 }
108
109 public List queryForList(String id, int skip, int max) throws SQLException {
110 return getLocalSqlMapSession().queryForList(id, skip, max);
111 }
112
113
114
115
116 public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
117 return getLocalSqlMapSession().queryForPaginatedList(id, paramObject, pageSize);
118 }
119
120
121
122
123 public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException {
124 return getLocalSqlMapSession().queryForPaginatedList(id, pageSize);
125 }
126
127 public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
128 return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp);
129 }
130
131 public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
132 return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp, valueProp);
133 }
134
135 public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
136 getLocalSqlMapSession().queryWithRowHandler(id, paramObject, rowHandler);
137 }
138
139 public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException {
140 getLocalSqlMapSession().queryWithRowHandler(id, rowHandler);
141 }
142
143 public void startTransaction() throws SQLException {
144 getLocalSqlMapSession().startTransaction();
145 }
146
147 public void startTransaction(int transactionIsolation) throws SQLException {
148 getLocalSqlMapSession().startTransaction(transactionIsolation);
149 }
150
151 public void commitTransaction() throws SQLException {
152 getLocalSqlMapSession().commitTransaction();
153 }
154
155 public void endTransaction() throws SQLException {
156 try {
157 getLocalSqlMapSession().endTransaction();
158 } finally {
159 getLocalSqlMapSession().close();
160 localSqlMapSession.remove();
161 }
162 }
163
164 public void startBatch() throws SQLException {
165 getLocalSqlMapSession().startBatch();
166 }
167
168 public int executeBatch() throws SQLException {
169 return getLocalSqlMapSession().executeBatch();
170 }
171
172 public List executeBatchDetailed() throws SQLException, BatchException {
173 return getLocalSqlMapSession().executeBatchDetailed();
174 }
175
176 public void setUserConnection(Connection connection) throws SQLException {
177 try {
178 getLocalSqlMapSession().setUserConnection(connection);
179 } finally {
180 if (connection == null) {
181 getLocalSqlMapSession().close();
182 }
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196 public Connection getUserConnection() throws SQLException {
197 return getCurrentConnection();
198 }
199
200 public Connection getCurrentConnection() throws SQLException {
201 return getLocalSqlMapSession().getCurrentConnection();
202 }
203
204 public DataSource getDataSource() {
205 return delegate.getDataSource();
206 }
207
208 public MappedStatement getMappedStatement(String id) {
209 return delegate.getMappedStatement(id);
210 }
211
212 public boolean isLazyLoadingEnabled() {
213 return delegate.isLazyLoadingEnabled();
214 }
215
216 public boolean isEnhancementEnabled() {
217 return delegate.isEnhancementEnabled();
218 }
219
220 public SqlExecutor getSqlExecutor() {
221 return delegate.getSqlExecutor();
222 }
223
224 public SqlMapExecutorDelegate getDelegate() {
225 return delegate;
226 }
227
228 public SqlMapSession openSession() {
229 SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
230 sqlMapSession.open();
231 return sqlMapSession;
232 }
233
234 public SqlMapSession openSession(Connection conn) {
235 try {
236 SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
237 sqlMapSession.open();
238 sqlMapSession.setUserConnection(conn);
239 return sqlMapSession;
240 } catch (SQLException e) {
241 throw new SqlMapException("Error setting user provided connection. Cause: " + e, e);
242 }
243 }
244
245
246
247
248
249
250 public SqlMapSession getSession() {
251 log.warn(
252 "Use of a deprecated API detected. SqlMapClient.getSession() is deprecated. Use SqlMapClient.openSession() instead.");
253 return openSession();
254 }
255
256 public void flushDataCache() {
257 delegate.flushDataCache();
258 }
259
260 public void flushDataCache(String cacheId) {
261 delegate.flushDataCache(cacheId);
262 }
263
264
265
266
267
268
269 protected SqlMapSessionImpl getLocalSqlMapSession() {
270 SqlMapSessionImpl sqlMapSession = localSqlMapSession.get();
271 if (sqlMapSession == null || sqlMapSession.isClosed()) {
272 sqlMapSession = new SqlMapSessionImpl(this);
273 localSqlMapSession.set(sqlMapSession);
274 }
275 return sqlMapSession;
276 }
277
278 public ResultObjectFactory getResultObjectFactory() {
279 return delegate.getResultObjectFactory();
280 }
281 }