1 29 30 package com.caucho.amber.query; 31 32 import com.caucho.amber.expr.ArgExpr; 33 import com.caucho.amber.expr.EmbeddedExpr; 34 import com.caucho.amber.expr.JoinExpr; 35 import com.caucho.amber.expr.ManyToOneJoinExpr; 36 import com.caucho.amber.manager.AmberConnection; 37 import com.caucho.amber.table.LinkColumns; 38 import com.caucho.amber.table.Table; 39 40 import java.sql.SQLException ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 44 47 abstract public class AbstractQuery { 48 private String _sql; 49 50 protected ArrayList <FromItem> _fromList = new ArrayList <FromItem>(); 51 52 protected HashMap <String , EmbeddedExpr> _embeddedAliases 57 = new HashMap <String , EmbeddedExpr>(); 58 59 private ArgExpr []_argList; 60 61 private ArrayList <String > _preparedMapping = new ArrayList <String >(); 67 68 69 AbstractQuery(String sql) 70 { 71 _sql = sql; 72 } 73 74 77 public String getQueryString() 78 { 79 return _sql; 80 } 81 82 85 public void addEmbeddedAlias(String alias, 86 EmbeddedExpr expr) 87 { 88 _embeddedAliases.put(alias, expr); 89 } 90 91 94 public HashMap <String , EmbeddedExpr> getEmbeddedAliases() 95 { 96 return _embeddedAliases; 97 } 98 99 102 public FromItem createFromItem(Table table, String name) 103 { 104 FromItem item = new FromItem(table, name, _fromList.size()); 105 106 item.setQuery(this); 107 108 _fromList.add(item); 109 110 return item; 111 } 112 113 116 public FromItem createDependentFromItem(FromItem parent, 117 LinkColumns link, 118 String name) 119 { 120 for (int i = 0; i < _fromList.size(); i++) { 121 JoinExpr join = _fromList.get(i).getJoinExpr(); 122 123 if (join != null && join.isDependent(parent, link)) 124 return _fromList.get(i); 125 } 126 127 FromItem item = createFromItem(link.getSourceTable(), name); 128 129 JoinExpr join = new ManyToOneJoinExpr(link, item, parent); 130 131 item.setJoinExpr(join); 132 133 return item; 134 } 135 136 139 public ArrayList <FromItem> getFromList() 140 { 141 return _fromList; 142 } 143 144 147 public AbstractQuery getParentQuery() 148 { 149 return null; 150 } 151 152 155 public ArrayList <String > getPreparedMapping() 156 { 157 return _preparedMapping; 158 } 159 160 163 public abstract String getSQL(); 164 165 168 boolean setArgList(ArgExpr []argList) 169 { 170 _argList = argList; 171 172 int n = argList.length; 173 174 if (n > 0) { 175 176 if (argList[0].getName() != null) { 177 178 for (int i=0; i < n; i++) { 179 180 String name = argList[i].getName(); 181 182 if (name == null) { 183 _preparedMapping = null; 184 return false; 185 } 186 187 _preparedMapping.add(name); 188 } 189 } 190 } 191 192 return true; 193 } 194 195 198 ArgExpr []getArgList() 199 { 200 return _argList; 201 } 202 203 206 abstract void registerUpdates(CachedQuery query); 207 208 211 public long getCacheMaxAge() 212 { 213 return -1; 214 } 215 216 219 public void prepare(UserQuery userQuery, AmberConnection aConn) 220 throws SQLException 221 { 222 } 223 224 227 public void complete(UserQuery userQuery, AmberConnection aConn) 228 throws SQLException 229 { 230 } 231 } 232 | Popular Tags |