KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > CachedQuery


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.query;
31
32 import com.caucho.amber.manager.AmberConnection;
33 import com.caucho.amber.type.EntityType;
34 import com.caucho.amber.type.Type;
35 import com.caucho.bytecode.JClass;
36 import com.caucho.util.Alarm;
37
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  * Represents the application's view of the query.
46  */

47 public class CachedQuery {
48   private SelectQuery _query;
49   private CachedQueryKey _key;
50   private ResultSetImpl _rs;
51
52   private Type []_argTypes;
53   private Object JavaDoc []_argValues;
54   private int _argLength = 0;
55
56   private long _loadTime;
57
58   private ArrayList JavaDoc<Object JavaDoc> _values = new ArrayList JavaDoc<Object JavaDoc>();
59
60   private volatile boolean _isLoading;
61   private volatile boolean _isValidLoad;
62
63   CachedQuery(UserQuery query)
64   {
65     _query = (SelectQuery) query.getQuery();
66
67     Type []argTypes = query.getArgTypes();
68     Object JavaDoc []argValues = query.getArgValues();
69     
70     _argLength = query.getArgLength();
71
72     if (_argLength > 0) {
73       _argTypes = new Type[_argLength];
74       _argValues = new Object JavaDoc[_argLength];
75
76       for (int i = _argLength - 1; i >= 0; i--) {
77     _argTypes[i] = argTypes[i];
78     _argValues[i] = argValues[i];
79       }
80     }
81
82     _key = new CachedQueryKey();
83     _key.init(_query.getQueryString(), _argValues, _argLength);
84
85     _query.registerUpdates(this);
86   }
87
88   /**
89    * returns the key.
90    */

91   public CachedQueryKey getKey()
92   {
93     return _key;
94   }
95
96   /**
97    * Updates the query.
98    */

99   public void update()
100   {
101     synchronized (this) {
102       _loadTime = 0;
103       _isValidLoad = false;
104     }
105   }
106
107   /**
108    * Executes the query, filling the list.
109    */

110   public void list(List JavaDoc<Object JavaDoc> list, AmberConnection aConn, long maxAge)
111     throws SQLException JavaDoc
112   {
113     Type type = _query.getResultType(0);
114     EntityType entityType = (EntityType) type;
115     JClass cl = entityType.getBeanClass();
116     
117     synchronized (this) {
118       long now = Alarm.getCurrentTime();
119
120       if (now < _loadTime + maxAge || _isLoading && _loadTime > 0) {
121     int length = _values.size();
122     
123     for (int i = 0; i < length; i++) {
124       Object JavaDoc key = _values.get(i);
125
126       list.add(aConn.loadLazy(cl.getName(), entityType.getName(), (java.io.Serializable JavaDoc) key));
127     }
128     return;
129       }
130
131       _isLoading = true;
132       _isValidLoad = true;
133     }
134
135     try {
136       ArrayList JavaDoc<Object JavaDoc> values = new ArrayList JavaDoc<Object JavaDoc>();
137
138       ResultSetImpl rs = executeQuery(aConn);
139
140       while (rs.next()) {
141     values.add(rs.getKey(1));
142     
143     list.add(rs.getObject(1));
144       }
145
146       rs.close();
147       
148       synchronized (this) {
149     if (_isValidLoad) {
150       _values = values;
151
152       _loadTime = Alarm.getCurrentTime();
153     }
154       }
155     } finally {
156       _isLoading = false;
157     }
158   }
159
160   /**
161    * Executes the query returning a result set.
162    */

163   private ResultSetImpl executeQuery(AmberConnection aConn)
164     throws SQLException JavaDoc
165   {
166     if (_rs == null)
167       _rs = new ResultSetImpl();
168
169     PreparedStatement JavaDoc pstmt;
170     pstmt = aConn.prepareStatement(_query.getSQL());
171
172     pstmt.clearParameters();
173     
174     for (int i = 0; i < _argLength; i++) {
175       if (_argValues[i] != null)
176     _argTypes[i].setParameter(pstmt, i + 1, _argValues[i]);
177     }
178
179     ResultSet JavaDoc rs = pstmt.executeQuery();
180
181     _rs.setResultSet(rs);
182     _rs.setQuery((SelectQuery) _query);
183     _rs.setSession(aConn);
184     _rs.init();
185     
186     return _rs;
187   }
188
189   public String JavaDoc toString()
190   {
191     return "UserQuery[" + _query.getQueryString() + "]";
192   }
193 }
194
Popular Tags