KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > statement > CachingStatement


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.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.RequestScope;
25 import com.ibatis.sqlmap.engine.transaction.Transaction;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.List JavaDoc;
29
30 public class CachingStatement implements MappedStatement {
31
32   private MappedStatement statement;
33   private CacheModel cacheModel;
34
35   public CachingStatement(MappedStatement statement, CacheModel cacheModel) {
36     this.statement = statement;
37     this.cacheModel = cacheModel;
38   }
39
40   public String JavaDoc getId() {
41     return statement.getId();
42   }
43
44   public StatementType getStatementType() {
45     return statement.getStatementType();
46   }
47
48   public Integer JavaDoc getResultSetType() {
49     return statement.getResultSetType();
50   }
51
52   public Integer JavaDoc getFetchSize() {
53     return statement.getFetchSize();
54   }
55
56   public ParameterMap getParameterMap() {
57     return statement.getParameterMap();
58   }
59
60   public ResultMap getResultMap() {
61     return statement.getResultMap();
62   }
63
64   public int executeUpdate(RequestScope request, Transaction trans, Object JavaDoc parameterObject)
65       throws SQLException JavaDoc {
66     int n = statement.executeUpdate(request, trans, parameterObject);
67     return n;
68   }
69
70   public Object JavaDoc executeQueryForObject(RequestScope request, Transaction trans, Object JavaDoc parameterObject, Object JavaDoc resultObject)
71       throws SQLException JavaDoc {
72     CacheKey cacheKey = getCacheKey(request, parameterObject);
73     cacheKey.update("executeQueryForObject");
74     Object JavaDoc object = cacheModel.getObject(cacheKey);
75     if (object == CacheModel.NULL_OBJECT){
76         // This was cached, but null
77
object = null;
78     }else if (object == null) {
79       synchronized (cacheModel.getLock(cacheKey)) {
80         object = statement.executeQueryForObject(request, trans, parameterObject, resultObject);
81         cacheModel.putObject(cacheKey, object);
82       }
83     }
84     return object;
85   }
86
87   public List JavaDoc executeQueryForList(RequestScope request, Transaction trans, Object JavaDoc parameterObject, int skipResults, int maxResults)
88       throws SQLException JavaDoc {
89     CacheKey cacheKey = getCacheKey(request, parameterObject);
90     cacheKey.update("executeQueryForList");
91     cacheKey.update(skipResults);
92     cacheKey.update(maxResults);
93     Object JavaDoc listAsObject = cacheModel.getObject(cacheKey);
94     List JavaDoc list;
95     if(listAsObject == CacheModel.NULL_OBJECT){
96       // The cached object was null
97
list = null;
98     }else if (listAsObject == null) {
99       synchronized (cacheModel.getLock(cacheKey)) {
100         list = statement.executeQueryForList(request, trans, parameterObject, skipResults, maxResults);
101         cacheModel.putObject(cacheKey, list);
102       }
103     }else{
104       list = (List JavaDoc) listAsObject;
105     }
106     return list;
107   }
108
109   public void executeQueryWithRowHandler(RequestScope request, Transaction trans, Object JavaDoc parameterObject, RowHandler rowHandler)
110       throws SQLException JavaDoc {
111     statement.executeQueryWithRowHandler(request, trans, parameterObject, rowHandler);
112   }
113
114   public CacheKey getCacheKey(RequestScope request, Object JavaDoc parameterObject) {
115     CacheKey key = statement.getCacheKey(request, parameterObject);
116     if (!cacheModel.isReadOnly() && !cacheModel.isSerialize()) {
117       key.update(request.getSession());
118     }
119     return key;
120   }
121
122   public void setBaseCacheKey(int base) {
123     statement.setBaseCacheKey(base);
124   }
125
126   public void addExecuteListener(ExecuteListener listener) {
127     statement.addExecuteListener(listener);
128   }
129
130   public void notifyListeners() {
131     statement.notifyListeners();
132   }
133
134   public void initRequest(RequestScope request) {
135     statement.initRequest(request);
136   }
137
138   public Sql getSql() {
139     return statement.getSql();
140   }
141
142   public Class JavaDoc getParameterClass() {
143     return statement.getParameterClass();
144   }
145
146 }
147
Popular Tags