1 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 ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.util.ArrayList ; 42 import java.util.List ; 43 44 47 public class CachedQuery { 48 private SelectQuery _query; 49 private CachedQueryKey _key; 50 private ResultSetImpl _rs; 51 52 private Type []_argTypes; 53 private Object []_argValues; 54 private int _argLength = 0; 55 56 private long _loadTime; 57 58 private ArrayList <Object > _values = new ArrayList <Object >(); 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 []argValues = query.getArgValues(); 69 70 _argLength = query.getArgLength(); 71 72 if (_argLength > 0) { 73 _argTypes = new Type[_argLength]; 74 _argValues = new Object [_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 91 public CachedQueryKey getKey() 92 { 93 return _key; 94 } 95 96 99 public void update() 100 { 101 synchronized (this) { 102 _loadTime = 0; 103 _isValidLoad = false; 104 } 105 } 106 107 110 public void list(List <Object > list, AmberConnection aConn, long maxAge) 111 throws SQLException 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 key = _values.get(i); 125 126 list.add(aConn.loadLazy(cl.getName(), entityType.getName(), (java.io.Serializable ) key)); 127 } 128 return; 129 } 130 131 _isLoading = true; 132 _isValidLoad = true; 133 } 134 135 try { 136 ArrayList <Object > values = new ArrayList <Object >(); 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 163 private ResultSetImpl executeQuery(AmberConnection aConn) 164 throws SQLException 165 { 166 if (_rs == null) 167 _rs = new ResultSetImpl(); 168 169 PreparedStatement 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 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 toString() 190 { 191 return "UserQuery[" + _query.getQueryString() + "]"; 192 } 193 } 194 | Popular Tags |