KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Represents an amber query
46  */

47 abstract public class AbstractQuery {
48   private String JavaDoc _sql;
49
50   protected ArrayList JavaDoc<FromItem> _fromList = new ArrayList JavaDoc<FromItem>();
51
52   // jpa/0w22
53
// SELECT p.startMonth FROM TestBean o JOIN o.period p
54
// p is an alias to o.period (o.period is @Embedded)
55
// "p" -> "o.period"
56
protected HashMap JavaDoc<String JavaDoc, EmbeddedExpr> _embeddedAliases
57     = new HashMap JavaDoc<String JavaDoc, EmbeddedExpr>();
58
59   private ArgExpr []_argList;
60
61   // Map named parameters to JDBC ?,?,?.
62
// Ex: INSERT INTO test VALUES(:testId, :testName) is mapped as [0]->"testId", [1]->"testName"
63
// INSERT INTO test VALUES(:testName, :testName) is mapped as [0]->"testName", [1]->"testName"
64
// XXX: HashMap<String, ArrayList<Long>> would probably be an overkill.
65
//
66
private ArrayList JavaDoc<String JavaDoc> _preparedMapping = new ArrayList JavaDoc<String JavaDoc>();
67
68
69   AbstractQuery(String JavaDoc sql)
70   {
71     _sql = sql;
72   }
73
74   /**
75    * Returns the query string.
76    */

77   public String JavaDoc getQueryString()
78   {
79     return _sql;
80   }
81
82   /**
83    * Adds an embedded alias.
84    */

85   public void addEmbeddedAlias(String JavaDoc alias,
86                                EmbeddedExpr expr)
87   {
88     _embeddedAliases.put(alias, expr);
89   }
90
91   /**
92    * Gets the embedded aliases.
93    */

94   public HashMap JavaDoc<String JavaDoc, EmbeddedExpr> getEmbeddedAliases()
95   {
96     return _embeddedAliases;
97   }
98
99   /**
100    * Sets the from list.
101    */

102   public FromItem createFromItem(Table table, String JavaDoc 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   /**
114    * Creates a dependent from item
115    */

116   public FromItem createDependentFromItem(FromItem parent,
117                                           LinkColumns link,
118                                           String JavaDoc 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   /**
137    * Returns the from list.
138    */

139   public ArrayList JavaDoc<FromItem> getFromList()
140   {
141     return _fromList;
142   }
143
144   /**
145    * Gets the parent query.
146    */

147   public AbstractQuery getParentQuery()
148   {
149     return null;
150   }
151
152   /**
153    * Returns the prepared mapping.
154    */

155   public ArrayList JavaDoc<String JavaDoc> getPreparedMapping()
156   {
157     return _preparedMapping;
158   }
159
160   /**
161    * Returns the SQL.
162    */

163   public abstract String JavaDoc getSQL();
164
165   /**
166    * Sets the arg list.
167    */

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 JavaDoc 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   /**
196    * Returns the arg list.
197    */

198   ArgExpr []getArgList()
199   {
200     return _argList;
201   }
202
203   /**
204    * Generates update
205    */

206   abstract void registerUpdates(CachedQuery query);
207
208   /**
209    * Returns the expire time.
210    */

211   public long getCacheMaxAge()
212   {
213     return -1;
214   }
215
216   /**
217    * Prepares before any update.
218    */

219   public void prepare(UserQuery userQuery, AmberConnection aConn)
220     throws SQLException JavaDoc
221   {
222   }
223
224   /**
225    * Any post-sql completion
226    */

227   public void complete(UserQuery userQuery, AmberConnection aConn)
228     throws SQLException JavaDoc
229   {
230   }
231 }
232
Popular Tags