1 29 30 package com.caucho.amber.query; 31 32 import com.caucho.amber.entity.AmberEntityHome; 33 import com.caucho.amber.entity.TableInvalidateCompletion; 34 import com.caucho.amber.expr.AmberExpr; 35 import com.caucho.amber.manager.AmberConnection; 36 import com.caucho.util.CharBuffer; 37 38 import java.sql.SQLException ; 39 import java.util.ArrayList ; 40 41 42 45 public class UpdateQuery extends AbstractQuery { 46 private ArrayList <AmberExpr> _fieldList; 47 private ArrayList <AmberExpr> _valueList; 48 private AmberExpr _where; 49 50 private String _sql; 51 52 UpdateQuery(String query) 53 { 54 super(query); 55 } 56 57 60 void setFieldList(ArrayList <AmberExpr> fieldList) 61 { 62 _fieldList = fieldList; 63 } 64 65 68 public ArrayList <AmberExpr> getFieldList() 69 { 70 return _fieldList; 71 } 72 73 76 void setValueList(ArrayList <AmberExpr> exprList) 77 { 78 _valueList = exprList; 79 } 80 81 84 public ArrayList <AmberExpr> getValueList() 85 { 86 return _valueList; 87 } 88 89 92 void setWhere(AmberExpr expr) 93 { 94 _where = expr; 95 } 96 97 100 public String getSQL() 101 { 102 return _sql; 103 } 104 105 108 void init() 109 { 110 CharBuffer cb = new CharBuffer(); 111 112 cb.append("update "); 113 114 FromItem item = _fromList.get(0); 115 116 cb.append(item.getTable().getName()); 117 118 cb.append(" set "); 119 120 for (int i = 0; i < _fieldList.size(); i++) { 121 if (i != 0) 122 cb.append(", "); 123 124 126 AmberExpr expr = _fieldList.get(i); 127 128 expr.generateUpdateWhere(cb); 129 130 cb.append("="); 131 132 _valueList.get(i).generateUpdateWhere(cb); 133 } 134 135 if (_where != null) { 136 cb.append(" where "); 137 _where.generateUpdateWhere(cb); 138 } 139 140 _sql = cb.close(); 141 } 142 143 146 void registerUpdates(CachedQuery query) 147 { 148 for (int i = 0; i < _fromList.size(); i++) { 149 FromItem item = _fromList.get(i); 150 151 AmberEntityHome home = item.getEntityHome(); 152 153 CacheUpdate update = new TableCacheUpdate(query); 154 155 home.addUpdate(update); 156 } 157 } 158 159 162 public void prepare(UserQuery userQuery, AmberConnection aConn) 163 throws SQLException 164 { 165 aConn.flushNoChecks(); 166 } 167 168 171 public void complete(UserQuery userQuery, AmberConnection aConn) 172 throws SQLException 173 { 174 aConn.expire(); 175 176 FromItem item = _fromList.get(0); 177 178 aConn.addCompletion(new TableInvalidateCompletion(item.getEntityType().getTable().getName())); 179 } 180 181 184 public String toString() 185 { 186 return "UpdateQuery[" + getQueryString() + "]"; 187 } 188 } 189 | Popular Tags |