View Javadoc
1   /*
2    * Copyright 2004-2022 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      int n = statement.executeUpdate(statementScope, trans, parameterObject);
88      return n;
89    }
90  
91    @Override
92    public Object executeQueryForObject(StatementScope statementScope, Transaction trans, Object parameterObject,
93        Object resultObject) throws SQLException {
94      CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
95      cacheKey.update("executeQueryForObject");
96      Object object = cacheModel.getObject(cacheKey);
97      if (object == CacheModel.NULL_OBJECT) {
98        // This was cached, but null
99        object = null;
100     } else if (object == null) {
101       object = statement.executeQueryForObject(statementScope, trans, parameterObject, resultObject);
102       cacheModel.putObject(cacheKey, object);
103     }
104     return object;
105   }
106 
107   @Override
108   public List executeQueryForList(StatementScope statementScope, Transaction trans, Object parameterObject,
109       int skipResults, int maxResults) throws SQLException {
110     CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
111     cacheKey.update("executeQueryForList");
112     cacheKey.update(skipResults);
113     cacheKey.update(maxResults);
114     Object listAsObject = cacheModel.getObject(cacheKey);
115     List list;
116     if (listAsObject == CacheModel.NULL_OBJECT) {
117       // The cached object was null
118       list = null;
119     } else if (listAsObject == null) {
120       list = statement.executeQueryForList(statementScope, trans, parameterObject, skipResults, maxResults);
121       cacheModel.putObject(cacheKey, list);
122     } else {
123       list = (List) listAsObject;
124     }
125     return list;
126   }
127 
128   @Override
129   public void executeQueryWithRowHandler(StatementScope statementScope, Transaction trans, Object parameterObject,
130       RowHandler rowHandler) throws SQLException {
131     statement.executeQueryWithRowHandler(statementScope, trans, parameterObject, rowHandler);
132   }
133 
134   @Override
135   public CacheKey getCacheKey(StatementScope statementScope, Object parameterObject) {
136     CacheKey key = statement.getCacheKey(statementScope, parameterObject);
137     if (!cacheModel.isReadOnly() && !cacheModel.isSerialize()) {
138       key.update(statementScope.getSession());
139     }
140     return key;
141   }
142 
143   @Override
144   public void setBaseCacheKey(int base) {
145     statement.setBaseCacheKey(base);
146   }
147 
148   @Override
149   public void addExecuteListener(ExecuteListener listener) {
150     statement.addExecuteListener(listener);
151   }
152 
153   @Override
154   public void notifyListeners() {
155     statement.notifyListeners();
156   }
157 
158   @Override
159   public void initRequest(StatementScope statementScope) {
160     statement.initRequest(statementScope);
161   }
162 
163   @Override
164   public Sql getSql() {
165     return statement.getSql();
166   }
167 
168   @Override
169   public Class getParameterClass() {
170     return statement.getParameterClass();
171   }
172 
173   @Override
174   public Integer getTimeout() {
175     return statement.getTimeout();
176   }
177 
178   @Override
179   public boolean hasMultipleResultMaps() {
180     return statement.hasMultipleResultMaps();
181   }
182 
183   @Override
184   public ResultMap[] getAdditionalResultMaps() {
185     return statement.getAdditionalResultMaps();
186   }
187 
188 }