1 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 ; 29 import java.sql.Connection ; 30 import java.sql.SQLException ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 37 public class SqlMapClientImpl implements ExtendedSqlMapClient { 38 39 private static final Log log = LogFactory.getLog(SqlMapClientImpl.class); 40 41 44 public SqlMapExecutorDelegate delegate; 45 46 private ThreadLocal localSqlMapSession = new ThreadLocal (); 47 48 53 public SqlMapClientImpl(SqlMapExecutorDelegate delegate) { 54 this.delegate = delegate; 55 } 56 57 public Object insert(String id, Object param) throws SQLException { 58 return getLocalSqlMapSession().insert(id, param); 59 } 60 61 public int update(String id, Object param) throws SQLException { 62 return getLocalSqlMapSession().update(id, param); 63 } 64 65 public int delete(String id, Object param) throws SQLException { 66 return getLocalSqlMapSession().delete(id, param); 67 } 68 69 public Object queryForObject(String id, Object paramObject) throws SQLException { 70 return getLocalSqlMapSession().queryForObject(id, paramObject); 71 } 72 73 public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException { 74 return getLocalSqlMapSession().queryForObject(id, paramObject, resultObject); 75 } 76 77 public List queryForList(String id, Object paramObject) throws SQLException { 78 return getLocalSqlMapSession().queryForList(id, paramObject); 79 } 80 81 public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException { 82 return getLocalSqlMapSession().queryForList(id, paramObject, skip, max); 83 } 84 85 public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException { 86 return getLocalSqlMapSession().queryForPaginatedList(id, paramObject, pageSize); 87 } 88 89 public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException { 90 return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp); 91 } 92 93 public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException { 94 return getLocalSqlMapSession().queryForMap(id, paramObject, keyProp, valueProp); 95 } 96 97 public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException { 98 getLocalSqlMapSession().queryWithRowHandler(id, paramObject, rowHandler); 99 } 100 101 public void startTransaction() throws SQLException { 102 getLocalSqlMapSession().startTransaction(); 103 } 104 105 public void startTransaction(int transactionIsolation) throws SQLException { 106 getLocalSqlMapSession().startTransaction(transactionIsolation); 107 } 108 109 public void commitTransaction() throws SQLException { 110 getLocalSqlMapSession().commitTransaction(); 111 } 112 113 public void endTransaction() throws SQLException { 114 try { 115 getLocalSqlMapSession().endTransaction(); 116 } finally { 117 getLocalSqlMapSession().close(); 118 } 119 } 120 121 public void startBatch() throws SQLException { 122 getLocalSqlMapSession().startBatch(); 123 } 124 125 public int executeBatch() throws SQLException { 126 return getLocalSqlMapSession().executeBatch(); 127 } 128 129 public void setUserConnection(Connection connection) throws SQLException { 130 getLocalSqlMapSession().setUserConnection(connection); 131 } 132 133 140 public Connection getUserConnection() throws SQLException { 141 return getCurrentConnection(); 142 } 143 144 public Connection getCurrentConnection() throws SQLException { 145 return getLocalSqlMapSession().getCurrentConnection(); 146 } 147 148 public DataSource getDataSource() { 149 return getLocalSqlMapSession().getDataSource(); 150 } 151 152 public MappedStatement getMappedStatement(String 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 conn) { 179 try { 180 SqlMapSessionImpl sqlMapSession = getLocalSqlMapSession(); 181 sqlMapSession.open(); 182 sqlMapSession.setUserConnection(conn); 183 return sqlMapSession; 184 } catch (SQLException e) { 185 throw new SqlMapException("Error setting user provided connection. Cause: " + e, e); 186 } 187 } 188 189 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 cacheId) { 204 delegate.flushDataCache(cacheId); 205 } 206 207 public Object getMapper(Class 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 |