KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41
42 /**
43  * Represents an Amber select query
44  */

45 public class UpdateQuery extends AbstractQuery {
46   private ArrayList JavaDoc<AmberExpr> _fieldList;
47   private ArrayList JavaDoc<AmberExpr> _valueList;
48   private AmberExpr _where;
49
50   private String JavaDoc _sql;
51
52   UpdateQuery(String JavaDoc query)
53   {
54     super(query);
55   }
56
57   /**
58    * Sets the field list.
59    */

60   void setFieldList(ArrayList JavaDoc<AmberExpr> fieldList)
61   {
62     _fieldList = fieldList;
63   }
64
65   /**
66    * Returns the field list.
67    */

68   public ArrayList JavaDoc<AmberExpr> getFieldList()
69   {
70     return _fieldList;
71   }
72
73   /**
74    * Sets the field expr list.
75    */

76   void setValueList(ArrayList JavaDoc<AmberExpr> exprList)
77   {
78     _valueList = exprList;
79   }
80
81   /**
82    * Returns the field list.
83    */

84   public ArrayList JavaDoc<AmberExpr> getValueList()
85   {
86     return _valueList;
87   }
88
89   /**
90    * Sets the where expression
91    */

92   void setWhere(AmberExpr expr)
93   {
94     _where = expr;
95   }
96
97   /**
98    * Returns the id load sql
99    */

100   public String JavaDoc getSQL()
101   {
102     return _sql;
103   }
104
105   /**
106    * Initialize
107    */

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       // cb.append(_fieldList.get(i).generateSelect(null));
125

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   /**
144    * Generates update
145    */

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   /**
160    * Adds any completion info.
161    */

162   public void prepare(UserQuery userQuery, AmberConnection aConn)
163     throws SQLException JavaDoc
164   {
165     aConn.flushNoChecks();
166   }
167
168   /**
169    * Adds any completion info.
170    */

171   public void complete(UserQuery userQuery, AmberConnection aConn)
172     throws SQLException JavaDoc
173   {
174     aConn.expire();
175
176     FromItem item = _fromList.get(0);
177
178     aConn.addCompletion(new TableInvalidateCompletion(item.getEntityType().getTable().getName()));
179   }
180
181   /**
182    * Debug view.
183    */

184   public String JavaDoc toString()
185   {
186     return "UpdateQuery[" + getQueryString() + "]";
187   }
188 }
189
Popular Tags