KickJava   Java API By Example, From Geeks To Geeks.

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


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.OID;
15 import com.versant.core.metadata.FetchGroupField;
16 import com.versant.core.jdbc.metadata.JdbcCollectionField;
17 import com.versant.core.jdbc.metadata.JdbcField;
18 import com.versant.core.jdbc.metadata.JdbcRefField;
19 import com.versant.core.jdbc.metadata.JdbcClass;
20 import com.versant.core.jdbc.sql.exp.SelectExp;
21
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 /**
27  * This is a data structure to hold the ResultSet and the PreparedStatement
28  * for a Collection field that is being iterated over.
29  * <p/>
30  * This struct also has a ref to a parent. This creates a structrure whereby
31  * the child knows about the parent but the parent not about the child.
32  * To create the exp to query for this collection we follow the parent links all
33  * the way to the top and start from there.
34  */

35 public class ColFieldHolder {
36
37     /**
38      * The following fields are used when the query has been executed to maintain
39      * the state.
40      */

41     public ResultSet JavaDoc rs;
42     public PreparedStatement JavaDoc ps;
43     public OID[] lastOIDs = new OID[2];
44     public int returnState;
45
46     /**
47      * The parent.
48      */

49     public ColFieldHolder parent;
50     /**
51      *
52      */

53     public JoinStructure js;
54
55     public JoinStructure valueJs;
56
57     public JoinStructure keyJs;
58     /**
59      * If this collection is from the parents key of value field.
60      * This can only be false if the parent is a Map with a pc key.
61      * This field will not change once set.
62      */

63     public final boolean valueJoinToParent;
64     public boolean crossJoinedField;
65
66     public ColFieldHolder(ColFieldHolder parent,
67             boolean valueJoinToParent, JoinStructure js) {
68         this.parent = parent;
69         this.valueJoinToParent = valueJoinToParent;
70         this.js = js;
71     }
72
73     public void close() {
74         if (rs != null) {
75             try {
76                 rs.close();
77             } catch (SQLException JavaDoc e) {
78                 //ignore
79
}
80         }
81
82         if (ps != null) {
83             try {
84                 ps.close();
85             } catch (SQLException JavaDoc e) {
86                 //ignore
87
}
88         }
89     }
90
91     private SelectExp createSelectExpImp(JdbcStorageManager sm,
92             FetchGroupField fgField, SelectExp inSe,
93             boolean valueJoin, boolean justJoin,
94             SelectExp root, boolean addRootJoin) {
95         final JdbcField jdbcField = (JdbcField)fgField.fmd.storeField;
96         if (jdbcField instanceof JdbcRefField) {
97             JdbcRefField refField = (JdbcRefField)jdbcField;
98
99             SelectExp se = new SelectExp();
100             se.table = ((JdbcClass)refField.targetClass.storeClass).table;
101             se.jdbcField = refField;
102             inSe.addJoin(refField.cols, se.table.pk, se);
103
104             inSe = se;
105         } else {
106             if (justJoin) {
107                 inSe = ((JdbcCollectionField)jdbcField).getSelectFilterJoinExp(
108                         valueJoin,
109                         inSe, root, addRootJoin);
110             } else {
111                 final JdbcCollectionField jdbcCollectionField =
112                         (JdbcCollectionField)fgField.fmd.storeField;
113                 SelectExp se = jdbcCollectionField.getSelectFilterExp(sm, fgField,
114                         this);
115                 inSe.addJoin(inSe.table.pk, jdbcCollectionField.ourPkColumns,
116                         se);
117                 return se;
118             }
119         }
120         return inSe;
121     }
122
123     /**
124      * Must pass up to the root parent and start creating the exp from there.
125      *
126      * @param sm
127      * @param joinToExp The current most right side of the exp tree that we must
128      * join to.
129      * @param rootExp The exp where the query originated from. This is left
130      * unchanged and passed up the chain.
131      * @param valueJoin This is passed from our child to specify how to join to
132      */

133     private SelectExp createSEImp(JdbcStorageManager sm, SelectExp joinToExp,
134             SelectExp rootExp, boolean valueJoin, boolean addRootJoin) {
135         //if there is a parent then let it do its joins first
136
if (parent != null) {
137             joinToExp = parent.createSEImp(sm, joinToExp, rootExp,
138                     valueJoinToParent, false);
139         }
140
141         //add our joins
142
return appendJoins(sm, joinToExp, valueJoin, true, rootExp, addRootJoin);
143     }
144
145     /**
146      * Join to the supplied selectExp. Join ourselve to the right of the exp.
147      *
148      * @return return the exp tree with the newly create exp on the right.
149      */

150     private SelectExp appendJoins(JdbcStorageManager sm, SelectExp joinToExp,
151             boolean valueJoin, boolean justJoin, SelectExp root,
152             boolean addRootJoin) {
153         FetchGroupField[] fgFields = js.fgFields;
154         for (int i = 0; i < fgFields.length; i++) {
155             joinToExp = createSelectExpImp(sm, fgFields[i], joinToExp, valueJoin,
156                     justJoin, root, addRootJoin);
157         }
158         return joinToExp;
159     }
160
161     /**
162      * Create a SelectExp for this instance using the supplied root exp.
163      */

164     public SelectExp createSE(JdbcStorageManager sm, SelectExp root) {
165         SelectExp rhSe = root;
166         if (parent != null) {
167             rhSe = parent.createSEImp(sm, rhSe, root, valueJoinToParent, true);
168         }
169
170         return appendJoins(sm, rhSe, true, false, root, false);
171     }
172
173     public String JavaDoc toString() {
174         return this.getClass().getName() + "@"
175                 + System.identityHashCode(this)
176                 + "field " + js.fgField.fmd.name
177                 + " --> " + parent;
178
179     }
180 }
181
Popular Tags