001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.mapping.statement;
017:
018: import com.ibatis.sqlmap.client.event.RowHandler;
019: import com.ibatis.sqlmap.engine.cache.CacheKey;
020: import com.ibatis.sqlmap.engine.cache.CacheModel;
021: import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
022: import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
023: import com.ibatis.sqlmap.engine.mapping.sql.Sql;
024: import com.ibatis.sqlmap.engine.scope.RequestScope;
025: import com.ibatis.sqlmap.engine.transaction.Transaction;
026:
027: import java.sql.SQLException;
028: import java.util.List;
029:
030: public class CachingStatement implements MappedStatement {
031:
032: private MappedStatement statement;
033: private CacheModel cacheModel;
034:
035: public CachingStatement(MappedStatement statement,
036: CacheModel cacheModel) {
037: this .statement = statement;
038: this .cacheModel = cacheModel;
039: }
040:
041: public String getId() {
042: return statement.getId();
043: }
044:
045: public StatementType getStatementType() {
046: return statement.getStatementType();
047: }
048:
049: public Integer getResultSetType() {
050: return statement.getResultSetType();
051: }
052:
053: public Integer getFetchSize() {
054: return statement.getFetchSize();
055: }
056:
057: public ParameterMap getParameterMap() {
058: return statement.getParameterMap();
059: }
060:
061: public ResultMap getResultMap() {
062: return statement.getResultMap();
063: }
064:
065: public int executeUpdate(RequestScope request, Transaction trans,
066: Object parameterObject) throws SQLException {
067: int n = statement
068: .executeUpdate(request, trans, parameterObject);
069: return n;
070: }
071:
072: public Object executeQueryForObject(RequestScope request,
073: Transaction trans, Object parameterObject,
074: Object resultObject) throws SQLException {
075: CacheKey cacheKey = getCacheKey(request, parameterObject);
076: cacheKey.update("executeQueryForObject");
077: Object object = cacheModel.getObject(cacheKey);
078: if (object == CacheModel.NULL_OBJECT) {
079: // This was cached, but null
080: object = null;
081: } else if (object == null) {
082: object = statement.executeQueryForObject(request, trans,
083: parameterObject, resultObject);
084: cacheModel.putObject(cacheKey, object);
085: }
086: return object;
087: }
088:
089: public List executeQueryForList(RequestScope request,
090: Transaction trans, Object parameterObject, int skipResults,
091: int maxResults) throws SQLException {
092: CacheKey cacheKey = getCacheKey(request, parameterObject);
093: cacheKey.update("executeQueryForList");
094: cacheKey.update(skipResults);
095: cacheKey.update(maxResults);
096: Object listAsObject = cacheModel.getObject(cacheKey);
097: List list;
098: if (listAsObject == CacheModel.NULL_OBJECT) {
099: // The cached object was null
100: list = null;
101: } else if (listAsObject == null) {
102: list = statement.executeQueryForList(request, trans,
103: parameterObject, skipResults, maxResults);
104: cacheModel.putObject(cacheKey, list);
105: } else {
106: list = (List) listAsObject;
107: }
108: return list;
109: }
110:
111: public void executeQueryWithRowHandler(RequestScope request,
112: Transaction trans, Object parameterObject,
113: RowHandler rowHandler) throws SQLException {
114: statement.executeQueryWithRowHandler(request, trans,
115: parameterObject, rowHandler);
116: }
117:
118: public CacheKey getCacheKey(RequestScope request,
119: Object parameterObject) {
120: CacheKey key = statement.getCacheKey(request, parameterObject);
121: if (!cacheModel.isReadOnly() && !cacheModel.isSerialize()) {
122: key.update(request.getSession());
123: }
124: return key;
125: }
126:
127: public void setBaseCacheKey(int base) {
128: statement.setBaseCacheKey(base);
129: }
130:
131: public void addExecuteListener(ExecuteListener listener) {
132: statement.addExecuteListener(listener);
133: }
134:
135: public void notifyListeners() {
136: statement.notifyListeners();
137: }
138:
139: public void initRequest(RequestScope request) {
140: statement.initRequest(request);
141: }
142:
143: public Sql getSql() {
144: return statement.getSql();
145: }
146:
147: public Class getParameterClass() {
148: return statement.getParameterClass();
149: }
150:
151: public Integer getTimeout() {
152: return statement.getTimeout();
153: }
154:
155: public boolean hasMultipleResultMaps() {
156: return statement.hasMultipleResultMaps();
157: }
158:
159: public ResultMap[] getAdditionalResultMaps() {
160: return statement.getAdditionalResultMaps();
161: }
162:
163: }
|