Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ibatis/sqlmap/engine/impl/SqlMapClientImpl.java


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.sqlmap.client.SqlMapException;
20  import com.ibatis.sqlmap.client.SqlMapSession;
21  import com.ibatis.sqlmap.client.event.RowHandler;
22  import com.ibatis.sqlmap.engine.execution.SqlExecutor;
23  import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import javax.sql.DataSource;
28  import java.sql.Connection;
29  import java.sql.SQLException;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Implementation of ExtendedSqlMapClient
35   */
36  public class SqlMapClientImpl implements ExtendedSqlMapClient {
37  
38    private static final Log log = LogFactory.getLog(SqlMapClientImpl.class);
39  
40    /**
41     * Delegate for SQL execution
42     */
43    public SqlMapExecutorDelegate delegate;
44  
45    private ThreadLocal localSqlMapSession = new ThreadLocal();
46  
47    /**
48     * Constructor to supply a delegate
49     *
50     * @param delegate - the delegate
51     */
52    public SqlMapClientImpl(SqlMapExecutorDelegate delegate) {
53      this.delegate = delegate;
54    }
55  
56    public Object insert(String id, Object param) throws SQLException {
57      return getLocalSqlMapSession().insert(id, param);
58    }
59  
60    public int update(String id, Object param) throws SQLException {
61      return getLocalSqlMapSession().update(id, param);
62    }
63  
64    public int delete(String id, Object param) throws SQLException {
65      return getLocalSqlMapSession().delete(id, param);
66    }
67  
68    public Object queryForObject(String id, Object paramObject) throws SQLException {
69      return getLocalSqlMapSession().queryForObject(id, paramObject);
70    }
71  
72    public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
73      return getLocalSqlMapSession().queryForObject(id, paramObject, resultObject);
74    }
75  
76    public List queryForList(String id, Object paramObject) throws SQLException {
77      return getLocalSqlMapSession().queryForList(id, paramObject);
78    }
79  
80    public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
81      return getLocalSqlMapSession().queryForList(id, paramObject, skip, max);
82    }
83  
84    public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
85      return getLocalSqlMapSession().queryForPaginatedList(id, paramObject, pageSize);
86    }
87  
88    public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
89      return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp);
90    }
91  
92    public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
93      return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp, valueProp);
94    }
95  
96    public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
97      getLocalSqlMapSession().queryWithRowHandler(id, paramObject, rowHandler);
98    }
99  
100   public void startTransaction() throws SQLException {
101     getLocalSqlMapSession().startTransaction();
102   }
103 
104   public void startTransaction(int transactionIsolation) throws SQLException {
105     getLocalSqlMapSession().startTransaction(transactionIsolation);
106   }
107 
108   public void commitTransaction() throws SQLException {
109     getLocalSqlMapSession().commitTransaction();
110   }
111 
112   public void endTransaction() throws SQLException {
113     try {
114       getLocalSqlMapSession().endTransaction();
115     } finally {
116       getLocalSqlMapSession().close();
117     }
118   }
119 
120   public void startBatch() throws SQLException {
121     getLocalSqlMapSession().startBatch();
122   }
123 
124   public int executeBatch() throws SQLException {
125     return getLocalSqlMapSession().executeBatch();
126   }
127 
128   public void setUserConnection(Connection connection) throws SQLException {
129     getLocalSqlMapSession().setUserConnection(connection);
130   }
131 
132   /**
133    * TODO Deprecated
134    *
135    * @return
136    * @throws SQLException
137    * @deprecated
138    */
139   public Connection getUserConnection() throws SQLException {
140     return getCurrentConnection();
141   }
142 
143   public Connection getCurrentConnection() throws SQLException {
144     return getLocalSqlMapSession().getCurrentConnection();
145   }
146 
147   public DataSource getDataSource() {
148     return getLocalSqlMapSession().getDataSource();
149   }
150 
151   public MappedStatement getMappedStatement(String id) {
152     return delegate.getMappedStatement(id);
153   }
154 
155   public boolean isLazyLoadingEnabled() {
156     return delegate.isLazyLoadingEnabled();
157   }
158 
159   public boolean isEnhancementEnabled() {
160     return delegate.isEnhancementEnabled();
161   }
162 
163   public SqlExecutor getSqlExecutor() {
164     return delegate.getSqlExecutor();
165   }
166 
167   public SqlMapExecutorDelegate getDelegate() {
168     return delegate;
169   }
170 
171   public SqlMapSession openSession() {
172     SqlMapSessionImpl sqlMapSession = getLocalSqlMapSession();
173     sqlMapSession.open();
174     return sqlMapSession;
175   }
176 
177   public SqlMapSession openSession(Connection conn) {
178     try {
179       SqlMapSessionImpl sqlMapSession = getLocalSqlMapSession();
180       sqlMapSession.open();
181       sqlMapSession.setUserConnection(conn);
182       return sqlMapSession;
183     } catch (SQLException e) {
184       throw new SqlMapException("Error setting user provided connection.  Cause: " + e, e);
185     }
186   }
187 
188   /**
189    * TODO : DEPRECATED
190    *
191    * @deprecated Use openSession()
192    */
193   public SqlMapSession getSession() {
194     log.warn("Use of a deprecated API detected.  SqlMapClient.getSession() is deprecated.  Use SqlMapClient.openSession() instead.");
195     return openSession();
196   }
197 
198   public void flushDataCache() {
199     delegate.flushDataCache();
200   }
201 
202   public void flushDataCache(String cacheId) {
203     delegate.flushDataCache(cacheId);
204   }
205 
206   private SqlMapSessionImpl getLocalSqlMapSession() {
207     SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession.get();
208     if (sqlMapSession == null || sqlMapSession.isClosed()) {
209       sqlMapSession = new SqlMapSessionImpl(this);
210       localSqlMapSession.set(sqlMapSession);
211     }
212     return sqlMapSession;
213   }
214 
215 }