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.mapping.statement;
17  
18  import com.ibatis.sqlmap.client.event.RowHandler;
19  import com.ibatis.sqlmap.engine.cache.CacheKey;
20  import com.ibatis.sqlmap.engine.cache.CacheModel;
21  import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
22  import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
23  import com.ibatis.sqlmap.engine.mapping.sql.Sql;
24  import com.ibatis.sqlmap.engine.scope.StatementScope;
25  import com.ibatis.sqlmap.engine.transaction.Transaction;
26  
27  import java.sql.SQLException;
28  import java.util.List;
29  
30  /**
31   * The Class CachingStatement.
32   */
33  public class CachingStatement extends MappedStatement {
34  
35    /** The statement. */
36    private MappedStatement statement;
37  
38    /** The cache model. */
39    private CacheModel cacheModel;
40  
41    /**
42     * Instantiates a new caching statement.
43     *
44     * @param statement
45     *          the statement
46     * @param cacheModel
47     *          the cache model
48     */
49    public CachingStatement(MappedStatement statement, CacheModel cacheModel) {
50      this.statement = statement;
51      this.cacheModel = cacheModel;
52    }
53  
54    @Override
55    public String getId() {
56      return statement.getId();
57    }
58  
59    @Override
60    public StatementType getStatementType() {
61      return statement.getStatementType();
62    }
63  
64    @Override
65    public Integer getResultSetType() {
66      return statement.getResultSetType();
67    }
68  
69    @Override
70    public Integer getFetchSize() {
71      return statement.getFetchSize();
72    }
73  
74    @Override
75    public ParameterMap getParameterMap() {
76      return statement.getParameterMap();
77    }
78  
79    @Override
80    public ResultMap getResultMap() {
81      return statement.getResultMap();
82    }
83  
84    @Override
85    public int executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject)
86        throws SQLException {
87      return statement.executeUpdate(statementScope, trans, parameterObject);
88    }
89  
90    @Override
91    public Object executeQueryForObject(StatementScope statementScope, Transaction trans, Object parameterObject,
92        Object resultObject) throws SQLException {
93      CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
94      cacheKey.update("executeQueryForObject");
95      Object object = cacheModel.getObject(cacheKey);
96      if (object == CacheModel.NULL_OBJECT) {
97        // This was cached, but null
98        object = null;
99      } else if (object == null) {
100       object = statement.executeQueryForObject(statementScope, trans, parameterObject, resultObject);
101       cacheModel.putObject(cacheKey, object);
102     }
103     return object;
104   }
105 
106   @Override
107   public List executeQueryForList(StatementScope statementScope, Transaction trans, Object parameterObject,
108       int skipResults, int maxResults) throws SQLException {
109     CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
110     cacheKey.update("executeQueryForList");
111     cacheKey.update(skipResults);
112     cacheKey.update(maxResults);
113     Object listAsObject = cacheModel.getObject(cacheKey);
114     List list;
115     if (listAsObject == CacheModel.NULL_OBJECT) {
116       // The cached object was null
117       list = null;
118     } else if (listAsObject == null) {
119       list = statement.executeQueryForList(statementScope, trans, parameterObject, skipResults, maxResults);
120       cacheModel.putObject(cacheKey, list);
121     } else {
122       list = (List) listAsObject;
123     }
124     return list;
125   }
126 
127   @Override
128   public void executeQueryWithRowHandler(StatementScope statementScope, Transaction trans, Object parameterObject,
129       RowHandler rowHandler) throws SQLException {
130     statement.executeQueryWithRowHandler(statementScope, trans, parameterObject, rowHandler);
131   }
132 
133   @Override
134   public CacheKey getCacheKey(StatementScope statementScope, Object parameterObject) {
135     CacheKey key = statement.getCacheKey(statementScope, parameterObject);
136     if (!cacheModel.isReadOnly() && !cacheModel.isSerialize()) {
137       key.update(statementScope.getSession());
138     }
139     return key;
140   }
141 
142   @Override
143   public void setBaseCacheKey(int base) {
144     statement.setBaseCacheKey(base);
145   }
146 
147   @Override
148   public void addExecuteListener(ExecuteListener listener) {
149     statement.addExecuteListener(listener);
150   }
151 
152   @Override
153   public void notifyListeners() {
154     statement.notifyListeners();
155   }
156 
157   @Override
158   public void initRequest(StatementScope statementScope) {
159     statement.initRequest(statementScope);
160   }
161 
162   @Override
163   public Sql getSql() {
164     return statement.getSql();
165   }
166 
167   @Override
168   public Class getParameterClass() {
169     return statement.getParameterClass();
170   }
171 
172   @Override
173   public Integer getTimeout() {
174     return statement.getTimeout();
175   }
176 
177   @Override
178   public boolean hasMultipleResultMaps() {
179     return statement.hasMultipleResultMaps();
180   }
181 
182   @Override
183   public ResultMap[] getAdditionalResultMaps() {
184     return statement.getAdditionalResultMaps();
185   }
186 
187 }