KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > FgDs


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.common.Debug;
15 import com.versant.core.metadata.FetchGroup;
16 import com.versant.core.metadata.FetchGroupField;
17 import com.versant.core.metadata.FieldMetaData;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import com.versant.core.common.BindingSupportImpl;
23 import com.versant.core.jdbc.metadata.JdbcClass;
24 import com.versant.core.jdbc.metadata.JdbcFetchGroup;
25
26 /**
27  * This is a tree like data structure that is created with sql generation.
28  * The aim of this structure is to help with the reading rs that is obtained from
29  * the sql.
30  */

31 public class FgDs {
32     
33     public static String JavaDoc INCLUDING_SUBCLASSES = "INCLUDING_SUBCLASSES";
34     public static String JavaDoc INCLUDING_SUBCLASSES_OUTER = "INCLUDING_SUBCLASSES_OUTER";
35     public static String JavaDoc EXCLUDING_SUBCLASSES = "EXCLUDING_SUBCLASSES";
36     public static String JavaDoc EXCLUDING_SUBCLASSES_OUTER = "EXCLUDING_SUBCLASSES_OUTER";
37
38     public JoinStructure valueJs;
39     public JoinStructure keyJs;
40     /**
41      * If this is the root then the owner will be null.
42      */

43     public FgDs owner;
44     /**
45      * This is the fetchGroup that this ds represents
46      */

47     public FetchGroup fg;
48     /**
49      * This is a array that is created for performance. It is created in updateCounts.
50      */

51     private FgDs[] sameTableFgDsArray;
52     /**
53      * This is the fetchGroups that were added as result of fg.
54      */

55     private List JavaDoc sameTableFgDsCol;
56     /**
57      * This is a list of fetch groups as added by reference navigation.
58      */

59     private List JavaDoc refFgDsCol;
60     /**
61      * This is the amount to skip in the rs. This encapsulates all of its children
62      * as well.
63      */

64     public int columnSkipCount;
65     private boolean clsIdAdded;
66
67     public String JavaDoc name;
68
69     private boolean finished;
70
71     private JoinStructure rootValueJoinStruct;
72     public FieldMetaData refFmd;
73
74     /**
75      * Cache sql strings to retrieve the fg for a getState call.
76      */

77     private String JavaDoc sql;
78     private String JavaDoc sqlForUpdate;
79     private boolean refIdFieldsAdded;
80
81     private int opts;
82
83     public FgDs(FgDs owner, FetchGroup fg, String JavaDoc name, int opts) {
84         this.name = name;
85         this.owner = owner;
86         this.fg = fg;
87         columnSkipCount = fg.jdbcTotalCols;
88
89         sameTableFgDsCol = new ArrayList JavaDoc();
90         refFgDsCol = new ArrayList JavaDoc();
91         this.opts = opts;
92     }
93
94     public int getOpts() {
95         return opts;
96     }
97
98     public String JavaDoc getSql(boolean forUpdate) {
99         if (forUpdate) return sqlForUpdate;
100         return sql;
101     }
102
103     public void setSql(String JavaDoc sql, boolean forUpdate) {
104         if (forUpdate) {
105             sqlForUpdate = sql;
106         } else {
107             this.sql = sql;
108         }
109     }
110
111     public String JavaDoc toString() {
112         return this.getClass().getName() + "@" + System.identityHashCode(this) + " opts: " + opts;
113     }
114
115     public boolean isJoinOk() {
116         if (owner == null) {
117             return (opts & JdbcFetchGroup.OPT_START_OUTER) == 0;
118         } else {
119             return owner.isJoinOk();
120         }
121     }
122
123     public boolean isOuter() {
124         return (opts & JdbcFetchGroup.OPT_START_OUTER) != 0;
125     }
126
127     public boolean isIncludingSubs() {
128         return (opts & JdbcFetchGroup.OPT_INC_SUBS) != 0;
129     }
130
131     public JoinStructure getJoinStruct() {
132         if (Debug.DEBUG) {
133             if (owner != null) {
134                 throw BindingSupportImpl.getInstance().internal(
135                         "This may only be called on the root");
136             }
137         }
138         if (rootValueJoinStruct == null) {
139             rootValueJoinStruct = new JoinStructure(fg);
140         }
141         return rootValueJoinStruct;
142     }
143
144     public void addRefIdFields() {
145         if (finished) {
146             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
147         }
148         if (refIdFieldsAdded) {
149             throw BindingSupportImpl.getInstance().internal("The refIdFields is already added");
150         }
151         refIdFieldsAdded = true;
152         columnSkipCount += ((JdbcClass)fg.classMetaData.storeClass).table.pk.length;
153     }
154
155     public boolean isRefIdFieldsAdded() {
156         return refIdFieldsAdded;
157     }
158
159     /**
160      * Notify that a classId col was added.
161      */

162     public void addClsIdCount() {
163         if (finished) {
164             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
165         }
166         if (clsIdAdded) {
167             throw BindingSupportImpl.getInstance().internal("ClsId already added");
168         }
169         clsIdAdded = true;
170         columnSkipCount++;
171     }
172
173     public void addClsIdCount(int amount) {
174         if (finished) {
175             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
176         }
177         if (clsIdAdded) {
178             throw BindingSupportImpl.getInstance().internal("ClsId already added");
179         }
180         clsIdAdded = true;
181         columnSkipCount += amount;
182     }
183
184     public void addSameTable(FgDs sameTableFgDs) {
185         if (finished) {
186             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
187         }
188         sameTableFgDsCol.add(sameTableFgDs);
189     }
190
191     /**
192      * This is called to finish the FgDs. After this the structure is immutable.
193      */

194     public void updateCounts() {
195         if (finished) {
196             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
197         }
198         finished = true;
199         for (int i = 0; i < sameTableFgDsCol.size(); i++) {
200             FgDs fgDs = (FgDs)sameTableFgDsCol.get(i);
201             fgDs.updateCounts();
202             columnSkipCount += fgDs.columnSkipCount;
203         }
204
205         for (int i = 0; i < refFgDsCol.size(); i++) {
206             FgDs fgDs = (FgDs)refFgDsCol.get(i);
207             fgDs.updateCounts();
208             columnSkipCount += fgDs.columnSkipCount;
209         }
210
211         sameTableFgDsArray = new FgDs[sameTableFgDsCol.size()];
212         sameTableFgDsCol.toArray(sameTableFgDsArray);
213         sameTableFgDsCol = null;
214         if (owner == null) {
215             ((JdbcFetchGroup)fg.storeFetchGroup).setFgDs(this);
216         }
217         if (rootValueJoinStruct != null) rootValueJoinStruct.finish();
218     }
219
220     public void addReference(FgDs refFgDs, FieldMetaData aFmd) {
221         if (finished) {
222             throw BindingSupportImpl.getInstance().internal("The FgDs is already finished");
223         }
224         refFgDsCol.add(refFgDs);
225         refFgDs.refFmd = aFmd;
226     }
227
228     public boolean isEmpty() {
229         return refFgDsCol.isEmpty();
230     }
231
232     /**
233      * This returns Ref fg's
234      *
235      * @param index
236      * @return
237      */

238     public FgDs get(int index) {
239         if (index >= refFgDsCol.size()) {
240             return null;
241         }
242         return (FgDs)refFgDsCol.get(index);
243     }
244
245     public void dump(String JavaDoc indent) {
246         if (!Debug.DEBUG) return;
247         System.out.println(
248                 indent + "fg name = " + fg + "@" + System.identityHashCode(
249                         this) + " class " + fg.classMetaData.cls.getName() + " finished = " + finished);
250         System.out.println(indent + "name = " + name);
251         System.out.println(
252                 indent + "TotcolCount " + columnSkipCount + " ownColCount " + fg.jdbcTotalCols);
253         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
254         for (int i = 0; i < fg.fields.length; i++) {
255             FetchGroupField field = fg.fields[i];
256             sb.append(field.fmd.name + ", ");
257         }
258         System.out.println(indent + "fg fields = " + sb.toString());
259         System.out.println(indent + "refIdFieldsAdded = " + refIdFieldsAdded);
260         System.out.println(indent + "listing children-----");
261         if (sameTableFgDsCol != null) {
262             System.out.println(
263                     indent + sameTableFgDsCol.size() + " same table fgs ----");
264             for (int i = 0; i < sameTableFgDsCol.size(); i++) {
265                 FgDs ds = (FgDs)sameTableFgDsCol.get(i);
266                 ds.dump(indent + " ");
267             }
268         } else {
269             System.out.println(
270                     indent + sameTableFgDsArray.length + " same table fgs ----");
271             for (int i = 0; i < sameTableFgDsArray.length; i++) {
272                 FgDs fgDs = sameTableFgDsArray[i];
273                 fgDs.dump(indent + " ");
274             }
275         }
276         System.out.println(indent + refFgDsCol.size() + " ref fgs ----");
277         for (int i = 0; i < refFgDsCol.size(); i++) {
278             com.versant.core.jdbc.FgDs fgDs = (com.versant.core.jdbc.FgDs)refFgDsCol.get(
279                     i);
280             fgDs.dump(indent + " ");
281         }
282         System.out.println(indent + "---------------------------------");
283     }
284
285     public int getChildrenColumnCount() {
286         return columnSkipCount - fg.jdbcTotalCols - (clsIdAdded ? 1 : 0);
287     }
288
289     public FgDs[] getSameTableFgDss() {
290         if (!finished) {
291             throw BindingSupportImpl.getInstance().internal("This fgds was never finised");
292         }
293         return sameTableFgDsArray;
294     }
295
296     public int getAmountOfRefFgDs() {
297         return refFgDsCol.size();
298     }
299
300     public FgDs getFirtsFor(FetchGroup afg) {
301         if (!finished) {
302             throw BindingSupportImpl.getInstance().internal("This fgds was never finised");
303         }
304         for (int i = 0; i < sameTableFgDsCol.size(); i++) {
305             FgDs fgDs = (FgDs)sameTableFgDsCol.get(i);
306             if (fgDs.fg == afg) return fgDs;
307         }
308         return null;
309     }
310
311     public boolean isFinished() {
312         return finished;
313     }
314
315     /**
316      * This will return a List of pass2 FetchGroupFields.
317      */

318     public List JavaDoc getPass2FetchGroupFields() {
319         ArrayList JavaDoc result = new ArrayList JavaDoc();
320         addPass2Fields(result, fg);
321
322         if (sameTableFgDsArray != null) {
323             for (int i = 0; i < sameTableFgDsArray.length; i++) {
324                 addPass2Fields(result, sameTableFgDsArray[i].fg);
325             }
326         }
327         return result;
328     }
329
330     private static void addPass2Fields(ArrayList JavaDoc result, FetchGroup aFg) {
331         FetchGroupField[] fields = aFg.fields;
332         for (int i = 0; i < fields.length; i++) {
333             FetchGroupField field = fields[i];
334             if (field.fmd.secondaryField) {
335                 result.add(field);
336             }
337         }
338     }
339
340     public List JavaDoc getRefFgs() {
341         return refFgDsCol;
342     }
343 }
344
Popular Tags