KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > sql > generator > QueryPlan


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * QueryPlan.java
26  *
27  * Created on March 3, 2000
28  *
29  */

30
31 package com.sun.jdo.spi.persistence.support.sqlstore.sql.generator;
32
33 import org.netbeans.modules.dbschema.TableElement;
34 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
35 import com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc;
36 import com.sun.jdo.spi.persistence.support.sqlstore.SQLStoreManager;
37 import com.sun.jdo.spi.persistence.support.sqlstore.model.ClassDesc;
38 import com.sun.jdo.spi.persistence.support.sqlstore.model.ReferenceKeyDesc;
39 import com.sun.jdo.spi.persistence.support.sqlstore.model.TableDesc;
40 import com.sun.jdo.spi.persistence.utility.I18NHelper;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44
45
46 /**
47  * This class is used to generate SQL statements.
48  */

49 public abstract class QueryPlan {
50     public static final int ACT_UPDATE = 1;
51
52     public static final int ACT_INSERT = 2;
53
54     public static final int ACT_DELETE = 3;
55
56     public static final int ACT_SELECT = 4;
57
58     public static final int ACT_NOOP = 5;
59
60     protected static final int ST_BUILT = 0x1;
61
62     /** Array of Statement. */
63     public ArrayList JavaDoc statements;
64
65     protected ClassDesc config;
66
67     /**
68      * Bitmask containing one of {@link QueryPlan#ST_BUILT},
69      * {@link SelectQueryPlan#ST_C_BUILT}, {@link SelectQueryPlan#ST_OC_BUILT},
70      * {@link SelectQueryPlan#ST_JOINED}
71      */

72     protected int status;
73
74     protected int action;
75
76     protected SQLStoreManager store;
77
78     /** Array of QueryTable. */
79     protected ArrayList JavaDoc tables;
80
81     /** I18N message handler. */
82     protected final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
83             "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
84
QueryPlan.class.getClassLoader());
85
86     public QueryPlan(ActionDesc desc, SQLStoreManager store) {
87         this.tables = new ArrayList JavaDoc();
88         this.statements = new ArrayList JavaDoc();
89         this.store = store;
90         this.config = (ClassDesc) store.getPersistenceConfig(desc.getPersistenceCapableClass());
91     }
92
93     public QueryTable addQueryTable(TableDesc tableDesc) {
94         QueryTable table = new QueryTable(tableDesc);
95         tables.add(table);
96         table.setTableIndex(new TableIndex(tables.size() - 1));
97         return table;
98     }
99
100     /**
101      * Identifies a database table which will become part of this
102      * query plan. We will build a QueryTable object describing its
103      * use and return it.
104      *
105      * Note: No join is constructed at this point for the table added.
106      *
107      * @param tableElement Identifies which table is being added.
108      * @param persistenceConfig
109      * If we are adding a foreign table the persistenceConfig parameter
110      * holds the PersistenceConfig for the foreign Persistence Class.
111      */

112     public QueryTable addQueryTable(TableElement tableElement,
113                                     ClassDesc persistenceConfig) {
114
115         ClassDesc _config = (persistenceConfig == null) ? this.config : persistenceConfig;
116         TableDesc tableDesc = _config.findTableDesc(tableElement);
117
118         if (tableDesc == null) {
119
120             if (tableElement != null) {
121                throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
122                         "core.configuration.classnotmappedtotable", // NOI18N
123
_config.getPersistenceCapableClass().getName(),
124                         tableElement.getName().getName()));
125             } else {
126                 throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
127                          "core.configuration.classnotmapped", // NOI18N
128
_config.getPersistenceCapableClass().getName()));
129
130             }
131         }
132
133         return addQueryTable(tableDesc);
134     }
135
136     /**
137      * Add all the tables from <code>queryTables</code> to the current
138      * query plan. Useful when transfering tables when joining query plans
139      * together.
140      *
141      * @param queryTables Query tables from the foreign plan to be added.
142      */

143     public void addQueryTables(ArrayList JavaDoc queryTables) {
144
145         for (int i = 0; i < queryTables.size(); i++) {
146             QueryTable t = (QueryTable) queryTables.get(i);
147
148             if (tables.indexOf(t) == -1) {
149                 tables.add(t);
150                 t.getTableIndex().setValue(tables.size() - 1);
151             }
152         }
153     }
154
155     /**
156      * Finds the QueryTable object that this query plan is using
157      * to describe the TableElement indicated by the tableElement parameter.
158      */

159     public QueryTable findQueryTable(TableElement tableElement) {
160         for (int i = 0; i < tables.size(); i++) {
161             QueryTable t = (QueryTable) tables.get(i);
162
163             if (t.getTableDesc().getTableElement() == tableElement) {
164             //if (t.getTableDesc().getTableElement().equals(tableElement)) {
165
return t;
166             }
167         }
168
169         return null;
170     }
171
172     /**
173      * Finds the QueryTable object that this query plan is using
174      * to describe the TableDesc indicated by the tableDesc parameter.
175      */

176     public QueryTable findQueryTable(TableDesc tableDesc) {
177         for (int i = 0; i < tables.size(); i++) {
178             QueryTable t = (QueryTable) tables.get(i);
179
180             if (t.getTableDesc() == tableDesc) return t;
181         }
182
183         return null;
184     }
185
186     public ArrayList JavaDoc getStatements() {
187         for (int i = 0; i < statements.size(); i++) {
188             Statement s = (Statement) statements.get(i);
189             //Initialize sql text of the statement
190
s.getText();
191         }
192
193         return statements;
194     }
195
196     protected Statement addStatement(QueryTable t) {
197         Statement s = createStatement(t);
198         statements.add(s);
199
200         return s;
201     }
202
203     protected abstract Statement newStatement();
204
205     protected Statement createStatement(QueryTable t) {
206         Statement statement = newStatement();
207
208         statement.action = action;
209         statement.addQueryTable(t);
210
211         return statement;
212     }
213
214     protected Statement getStatement(QueryTable t) {
215         if (t == null) return null;
216
217         for (int i = 0; i < statements.size(); i++) {
218             Statement s = (Statement) statements.get(i);
219
220             if (s.tableList.indexOf(t) != -1)
221                 return s;
222         }
223
224         return null;
225     }
226
227     public abstract void build();
228
229     /**
230      * This method goes through the statement list and tries to set up relationship
231      * between statements based on secondary table keys.
232      */

233     protected void processStatements() {
234         for (int i = 0; i < statements.size(); i++) {
235             Statement s = (Statement) statements.get(i);
236
237             QueryTable qt = (QueryTable) s.getQueryTables().get(0);
238
239             ArrayList JavaDoc secondaryTableKeys = qt.getTableDesc().getSecondaryTableKeys();
240
241             if (secondaryTableKeys != null) {
242                 for (int j = 0; j < secondaryTableKeys.size(); j++) {
243                     ReferenceKeyDesc secondaryTableKey = (ReferenceKeyDesc) secondaryTableKeys.get(j);
244                     s.addSecondaryTableStatement(getStatement(findQueryTable(secondaryTableKey.getTableDesc())));
245                 }
246             }
247         }
248     }
249
250     public int getAction() {
251         return action;
252     }
253
254     public ClassDesc getConfig() {
255         return config;
256     }
257
258 }
259
260
261
262
263
264
Popular Tags