KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > metadata > JdbcCollectionField


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.metadata;
13
14 import com.versant.core.common.State;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.metadata.FetchGroupField;
17 import com.versant.core.metadata.parser.JdoElement;
18 import com.versant.core.metadata.parser.JdoExtension;
19 import com.versant.core.common.OID;
20 import com.versant.core.common.*;
21 import com.versant.core.server.StateContainer;
22 import com.versant.core.jdbc.*;
23 import com.versant.core.jdbc.sql.exp.SelectExp;
24 import com.versant.core.util.CharBuf;
25
26 import java.io.PrintStream JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 import com.versant.core.common.BindingSupportImpl;
33
34 /**
35  * Base class for field that are Collections, Maps or arrays.
36  */

37 public abstract class JdbcCollectionField extends JdbcField {
38
39     public static int STATUS_CLOSED = 1;
40     public static int STATUS_VALID_ROWS = 2;
41     public static int STATUS_DATA_ADDED = 4;
42
43     public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
44     public static final OID[] EMPTY_OID_ARRAY = new OID[0];
45     public static final Object JavaDoc[] PRE_GEN_EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
46
47     /**
48      * The column(s) holding primary key of the main table for our class.
49      * These could be in the link table or in the value PC class table.
50      */

51     public JdbcColumn[] ourPkColumns;
52     /**
53      * The column holding the sequence number for each value (null if the
54      * collection is not ordered).
55      */

56     public JdbcColumn sequenceColumn;
57
58     public void dump(PrintStream JavaDoc out, String JavaDoc indent) {
59         super.dump(out, indent);
60         String JavaDoc is = indent + " ";
61         if (ourPkColumns == null) {
62             out.println(is + "ourPkColumns null");
63         } else {
64             for (int i = 0; i < ourPkColumns.length; i++) {
65                 out.println(is + "ourPkColumns[" + i + "] " + ourPkColumns[i]);
66             }
67         }
68         out.println(is + "sequenceColumn " + sequenceColumn);
69     }
70
71     /**
72      * Complete the meta data for this collection. This must use info
73      * already supplied in the .jdo file and add anything else needed.
74      */

75     public void processMetaData(JdoElement context, JdbcMetaDataBuilder mdb,
76             boolean quiet) {
77         fmd.primaryField = false;
78         fmd.secondaryField = true;
79
80         // make sure there are no jdbc-XXX extensions on the field element
81
// itself - they should be nested in the collection, map or array
82
// element
83
JdoExtension[] a = fmd.jdoField == null ? null : fmd.jdoField.extensions;
84         if (a != null) {
85             for (int i = 0; i < a.length; i++) {
86                 if (a[i].isJdbc()) {
87                     fmd.addError(BindingSupportImpl.getInstance().runtime("Unexpected extension: " + a[i] + "\n" +
88                             context.getContext()), quiet);
89                 }
90             }
91         }
92     }
93
94     public void deletePass2Block(DeletePacket graph, int blockStart,
95             int blockEnd, CharBuf s, Connection JavaDoc con, boolean batch)
96             throws SQLException JavaDoc {
97         //ignore
98
}
99
100     /**
101      * This is called for the first logical row of a crossjoin resultset.
102      */

103     protected boolean updateForFirstRow(FetchInfo fetchInfo,
104                                             boolean mustBreak, ResultSet JavaDoc rs,
105                                             int colIndex, OID oid) throws SQLException JavaDoc {
106         fetchInfo.onNextRow = false;
107         //if this keyOid was already checked in the previous round then
108
//it must not be read again
109
if (fetchInfo.breakStatus == FetchInfo.BREAK_STATUS_NULL) {
110             //the oid was read in previous round and was null
111
mustBreak = true;
112             fetchInfo.breakStatus = FetchInfo.BREAK_STATUS_DEFAULT;
113         } else if (fetchInfo.breakStatus == FetchInfo.BREAK_STATUS_DEFAULT) {
114             //the oid was not read in previous round so read it now
115
mustBreak = checkKeyOid(rs, colIndex, fetchInfo, mustBreak, oid);
116         }
117         return mustBreak;
118     }
119
120     protected boolean checkKeyOid(ResultSet JavaDoc rs, int colIndex,
121                                       FetchInfo fetchInfo, boolean mustBreak,
122                                       OID oid) throws SQLException JavaDoc {
123         OID keyOid;
124         keyOid = fmd.classMetaData.createOID(false);
125
126         if (!((JdbcOID)keyOid).copyKeyFields(rs, colIndex)) {
127             fetchInfo.breakStatus = FetchInfo.BREAK_STATUS_NULL;
128             fetchInfo.nextOid = null;
129             mustBreak = true;
130         }
131
132         if (!oid.equals(keyOid)) {
133             fetchInfo.breakStatus = FetchInfo.BREAK_STATUS_VALID;
134             fetchInfo.nextOid = keyOid;
135             fetchInfo.onNextRow = true;
136             mustBreak = true;
137         }
138         return mustBreak;
139     }
140
141     /**
142      * Create a empty collection data structure in the state. This is used for parallel
143      * query processing.
144      */

145     public abstract void fillStateWithEmpty(FetchGroupField field, State state);
146
147     /**
148      * Fetch the values for this field.
149      */

150     public abstract int fetch(JdbcStorageManager sm, OID oid, State state,
151             FetchGroupField field, boolean forUpdate,
152             StateContainer container, boolean fetchPass2Fields,
153             ColFieldHolder colFHolder)
154             throws SQLException JavaDoc;
155
156     public abstract int fetchFrom(ResultSet JavaDoc rs, OID oid,
157             State state,
158             FetchGroupField field, boolean forUpdate,
159             StateContainer container, boolean fetchPass2Fields, int colIndex,
160             FetchInfo fetchInfo, JdbcStorageManager sm)
161             throws SQLException JavaDoc;
162
163     /**
164      * Fetch the values for this field using parallel query processing.
165      */

166     public abstract int fetchWithFilter(JdbcStorageManager sm,
167             StateContainer oidStates,
168             FetchGroupField field, ResultSet JavaDoc rs, boolean forUpdate,
169             OID oidToCheckOn,
170             OID[] lastReadStateOID, ClassMetaData cmd,
171             ColFieldHolder colFHolder)
172             throws SQLException JavaDoc;
173
174     /**
175      * Create the select exp for this collection.
176      */

177     public abstract SelectExp getSelectFilterExp(JdbcStorageManager sm,
178             FetchGroupField field, ColFieldHolder colFHolder);
179
180     public abstract SelectExp getSelectExpFrom(JdbcStorageManager sm,
181             SelectExp joinToExp, FetchGroupField field, FgDs owningFgDs);
182
183     /**
184      * Create a join select exp through this.
185      */

186     public abstract SelectExp getSelectFilterJoinExp(boolean value,
187             SelectExp lhSe,
188             SelectExp rootSe,
189             boolean addRootJoin);
190
191     protected void cleanup(Statement JavaDoc s) {
192         if (s != null) {
193             try {
194                 s.close();
195             } catch (SQLException JavaDoc x) {
196                 // ignore
197
}
198         }
199     }
200
201     protected void cleanup(ResultSet JavaDoc rs) {
202         if (rs != null) {
203             try {
204                 rs.close();
205             } catch (SQLException JavaDoc x) {
206                 // ignore
207
}
208         }
209     }
210 }
211
212
Popular Tags