View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
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    *    https://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.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   * Implementation of ExtendedSqlMapClient.
39   */
40  public class SqlMapClientImpl implements SqlMapClient, ExtendedSqlMapClient {
41  
42    /** The Constant log. */
43    private static final Log log = LogFactory.getLog(SqlMapClientImpl.class);
44  
45    /** Delegate for SQL execution. */
46    public SqlMapExecutorDelegate delegate;
47  
48    /** The local sql map session. */
49    protected ThreadLocal<SqlMapSessionImpl> localSqlMapSession = new ThreadLocal<>();
50  
51    /**
52     * Constructor to supply a delegate.
53     *
54     * @param delegate
55     *          - the delegate
56     */
57    public SqlMapClientImpl(SqlMapExecutorDelegate delegate) {
58      this.delegate = delegate;
59    }
60  
61    @Override
62    public Object insert(String id, Object param) throws SQLException {
63      return getLocalSqlMapSession().insert(id, param);
64    }
65  
66    @Override
67    public Object insert(String id) throws SQLException {
68      return getLocalSqlMapSession().insert(id);
69    }
70  
71    @Override
72    public int update(String id, Object param) throws SQLException {
73      return getLocalSqlMapSession().update(id, param);
74    }
75  
76    @Override
77    public int update(String id) throws SQLException {
78      return getLocalSqlMapSession().update(id);
79    }
80  
81    @Override
82    public int delete(String id, Object param) throws SQLException {
83      return getLocalSqlMapSession().delete(id, param);
84    }
85  
86    @Override
87    public int delete(String id) throws SQLException {
88      return getLocalSqlMapSession().delete(id);
89    }
90  
91    @Override
92    public Object queryForObject(String id, Object paramObject) throws SQLException {
93      return getLocalSqlMapSession().queryForObject(id, paramObject);
94    }
95  
96    @Override
97    public Object queryForObject(String id) throws SQLException {
98      return getLocalSqlMapSession().queryForObject(id);
99    }
100 
101   @Override
102   public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
103     return getLocalSqlMapSession().queryForObject(id, paramObject, resultObject);
104   }
105 
106   @Override
107   public List queryForList(String id, Object paramObject) throws SQLException {
108     return getLocalSqlMapSession().queryForList(id, paramObject);
109   }
110 
111   @Override
112   public List queryForList(String id) throws SQLException {
113     return getLocalSqlMapSession().queryForList(id);
114   }
115 
116   @Override
117   public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
118     return getLocalSqlMapSession().queryForList(id, paramObject, skip, max);
119   }
120 
121   @Override
122   public List queryForList(String id, int skip, int max) throws SQLException {
123     return getLocalSqlMapSession().queryForList(id, skip, max);
124   }
125 
126   /**
127    * @deprecated All paginated list features have been deprecated
128    */
129   @Override
130   @Deprecated
131   public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
132     return getLocalSqlMapSession().queryForPaginatedList(id, paramObject, pageSize);
133   }
134 
135   /**
136    * @deprecated All paginated list features have been deprecated
137    */
138   @Override
139   @Deprecated
140   public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException {
141     return getLocalSqlMapSession().queryForPaginatedList(id, pageSize);
142   }
143 
144   @Override
145   public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
146     return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp);
147   }
148 
149   @Override
150   public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
151     return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp, valueProp);
152   }
153 
154   @Override
155   public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
156     getLocalSqlMapSession().queryWithRowHandler(id, paramObject, rowHandler);
157   }
158 
159   @Override
160   public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException {
161     getLocalSqlMapSession().queryWithRowHandler(id, rowHandler);
162   }
163 
164   @Override
165   public void startTransaction() throws SQLException {
166     getLocalSqlMapSession().startTransaction();
167   }
168 
169   @Override
170   public void startTransaction(int transactionIsolation) throws SQLException {
171     getLocalSqlMapSession().startTransaction(transactionIsolation);
172   }
173 
174   @Override
175   public void commitTransaction() throws SQLException {
176     getLocalSqlMapSession().commitTransaction();
177   }
178 
179   @Override
180   public void endTransaction() throws SQLException {
181     try {
182       getLocalSqlMapSession().endTransaction();
183     } finally {
184       getLocalSqlMapSession().close();
185       localSqlMapSession.remove();
186     }
187   }
188 
189   @Override
190   public void startBatch() throws SQLException {
191     getLocalSqlMapSession().startBatch();
192   }
193 
194   @Override
195   public int executeBatch() throws SQLException {
196     return getLocalSqlMapSession().executeBatch();
197   }
198 
199   @Override
200   public List executeBatchDetailed() throws SQLException, BatchException {
201     return getLocalSqlMapSession().executeBatchDetailed();
202   }
203 
204   @Override
205   public void setUserConnection(Connection connection) throws SQLException {
206     try {
207       getLocalSqlMapSession().setUserConnection(connection);
208     } finally {
209       if (connection == null) {
210         getLocalSqlMapSession().close();
211       }
212     }
213   }
214 
215   /**
216    * Get user connection.
217    *
218    * @return Current connection
219    *
220    * @throws SQLException
221    *           the SQL exception
222    *
223    * @deprecated Use getCurrentconnection() instead.
224    */
225   @Override
226   @Deprecated
227   public Connection getUserConnection() throws SQLException {
228     return getCurrentConnection();
229   }
230 
231   @Override
232   public Connection getCurrentConnection() throws SQLException {
233     return getLocalSqlMapSession().getCurrentConnection();
234   }
235 
236   @Override
237   public DataSource getDataSource() {
238     return delegate.getDataSource();
239   }
240 
241   @Override
242   public MappedStatement getMappedStatement(String id) {
243     return delegate.getMappedStatement(id);
244   }
245 
246   @Override
247   public boolean isLazyLoadingEnabled() {
248     return delegate.isLazyLoadingEnabled();
249   }
250 
251   @Override
252   public boolean isEnhancementEnabled() {
253     return delegate.isEnhancementEnabled();
254   }
255 
256   @Override
257   public SqlExecutor getSqlExecutor() {
258     return delegate.getSqlExecutor();
259   }
260 
261   @Override
262   public SqlMapExecutorDelegate getDelegate() {
263     return delegate;
264   }
265 
266   @Override
267   public SqlMapSession openSession() {
268     SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
269     sqlMapSession.open();
270     return sqlMapSession;
271   }
272 
273   @Override
274   public SqlMapSession openSession(Connection conn) {
275     try {
276       SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
277       sqlMapSession.open();
278       sqlMapSession.setUserConnection(conn);
279       return sqlMapSession;
280     } catch (SQLException e) {
281       throw new SqlMapException("Error setting user provided connection.  Cause: " + e, e);
282     }
283   }
284 
285   /**
286    * GetSession.
287    *
288    * @deprecated Use openSession()
289    */
290   @Override
291   @Deprecated
292   public SqlMapSession getSession() {
293     log.warn(
294         "Use of a deprecated API detected.  SqlMapClient.getSession() is deprecated.  Use SqlMapClient.openSession() instead.");
295     return openSession();
296   }
297 
298   @Override
299   public void flushDataCache() {
300     delegate.flushDataCache();
301   }
302 
303   @Override
304   public void flushDataCache(String cacheId) {
305     delegate.flushDataCache(cacheId);
306   }
307 
308   /**
309    * Gets the local sql map session.
310    *
311    * @return the local sql map session
312    */
313   protected SqlMapSessionImpl getLocalSqlMapSession() {
314     SqlMapSessionImpl sqlMapSession = localSqlMapSession.get();
315     if (sqlMapSession == null || sqlMapSession.isClosed()) {
316       sqlMapSession = new SqlMapSessionImpl(this);
317       localSqlMapSession.set(sqlMapSession);
318     }
319     return sqlMapSession;
320   }
321 
322   @Override
323   public ResultObjectFactory getResultObjectFactory() {
324     return delegate.getResultObjectFactory();
325   }
326 }