KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.query;
30
31 import com.caucho.amber.entity.AmberEntityHome;
32 import com.caucho.amber.entity.TableInvalidateCompletion;
33 import com.caucho.amber.expr.AmberExpr;
34 import com.caucho.amber.manager.AmberConnection;
35 import com.caucho.util.CharBuffer;
36
37 import java.sql.SQLException JavaDoc;
38
39
40 /**
41  * Represents an Amber delete query
42  */

43 public class DeleteQuery extends AbstractQuery {
44   private AmberExpr _where;
45
46   private String JavaDoc _sql;
47
48   DeleteQuery(String JavaDoc query)
49   {
50     super(query);
51   }
52
53   /**
54    * Sets the where expression
55    */

56   void setWhere(AmberExpr expr)
57   {
58     _where = expr;
59   }
60
61   /**
62    * Returns the id load sql
63    */

64   public String JavaDoc getSQL()
65   {
66     return _sql;
67   }
68
69   /**
70    * Initialize
71    */

72   void init()
73   {
74     CharBuffer cb = CharBuffer.allocate();
75
76     cb.append("DELETE FROM ");
77
78     FromItem item = _fromList.get(0);
79
80     cb.append(item.getTable().getName());
81
82     if (_where != null) {
83       cb.append(" WHERE ");
84       _where.generateUpdateWhere(cb);
85     }
86
87     _sql = cb.close();
88   }
89
90   /**
91    * Generates update
92    */

93   void registerUpdates(CachedQuery query)
94   {
95     for (int i = 0; i < _fromList.size(); i++) {
96       FromItem item = _fromList.get(i);
97
98       AmberEntityHome home = item.getEntityHome();
99
100       CacheUpdate update = new TableCacheUpdate(query);
101
102       home.addUpdate(update);
103     }
104   }
105
106   /**
107    * Adds any completion info.
108    */

109   public void prepare(UserQuery userQuery, AmberConnection aConn)
110     throws SQLException JavaDoc
111   {
112     aConn.flushNoChecks();
113   }
114
115   /**
116    * Adds any completion info.
117    */

118   public void complete(UserQuery userQuery, AmberConnection aConn)
119     throws SQLException JavaDoc
120   {
121     aConn.expire();
122
123     FromItem item = _fromList.get(0);
124
125     aConn.addCompletion(new TableInvalidateCompletion(item.getEntityType().getTable().getName()));
126   }
127
128   /**
129    * Debug view.
130    */

131   public String JavaDoc toString()
132   {
133     return "DeleteQuery[" + getQueryString() + "]";
134   }
135 }
136
Popular Tags