KickJava   Java API By Example, From Geeks To Geeks.

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


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.metadata.FieldMetaData;
15 import com.versant.core.metadata.parser.JdoField;
16 import com.versant.core.jdbc.sql.JdbcNameGenerator;
17 import com.versant.core.jdbc.sql.SqlDriver;
18 import com.versant.core.jdbc.sql.exp.ColumnExp;
19 import com.versant.core.jdbc.sql.exp.SelectExp;
20 import com.versant.core.jdbc.sql.exp.SqlExp;
21 import com.versant.core.jdbc.query.JdbcJDOQLCompiler;
22 import com.versant.core.jdbc.fetch.FetchOp;
23 import com.versant.core.jdbc.fetch.FetchSpec;
24 import com.versant.core.jdbc.fetch.FetchOptions;
25 import com.versant.core.server.PersistGraph;
26 import com.versant.core.util.CharBuf;
27 import com.versant.core.jdo.query.Node;
28 import com.versant.core.common.State;
29
30 import java.io.Serializable JavaDoc;
31 import java.io.PrintStream JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Connection JavaDoc;
37
38 import com.versant.core.common.*;
39
40 /**
41  * Extra meta data for a field stored in JDBC. There are different subclasses
42  * for different types of fields (e.g. simple, persistent class reference,
43  * mem etc.).
44  */

45 public abstract class JdbcField implements Serializable JavaDoc {
46
47     /**
48      * Do not join to resolve this field.
49      */

50     public static final int USE_JOIN_NO = 1;
51     /**
52      * Use an outer join to resolve this field.
53      */

54     public static final int USE_JOIN_OUTER = 2;
55     /**
56      * Use an inner join to resolve this field.
57      */

58     public static final int USE_JOIN_INNER = 3;
59
60     /**
61      * Our JDO field.
62      */

63     public FieldMetaData fmd;
64     /**
65      * Our state fieldNo.
66      */

67     public int stateFieldNo;
68     /**
69      * @keep Our main table columns.
70      */

71     public JdbcColumn[] mainTableCols;
72     /**
73      * Our main table columns that need to be updated and inserted.
74      */

75     public JdbcColumn[] mainTableColsForUpdate;
76     /**
77      * Our main table.
78      */

79     public JdbcTable mainTable;
80     /**
81      * Is this a fake field created to store some extra data (e.g. row version
82      * column values).
83      */

84     public boolean fake;
85     /**
86      * Should a join be done to pick up the fields for referenced classes
87      * when this field is read? This only makes sense for fields that
88      * reference other PC classes in some way.
89      */

90     public int useJoin;
91     /**
92      * Should this field be included in the where clause when using changed
93      * optimistic locking? This default to false for fields mapped to columns
94      * that have equalityTest false.
95      */

96     public boolean includeForChangedLocking;
97
98     /**
99      * Make sure all of this fields main table columns have names.
100      */

101     public void nameColumns(String JavaDoc tableName, JdbcNameGenerator nameGen) {
102     }
103
104     /**
105      * Add all of this fields main table constraints to cons.
106      */

107     public void addConstraints(ArrayList JavaDoc cons) {
108     }
109
110     /**
111      * Init the mainTableCols field to all our main table columns.
112      */

113     public void initMainTableCols() {
114     }
115
116     /**
117      * Init the mainTableColsForUpdate field to all our main table columns
118      * that are for update.
119      */

120     public void initMainTableColsForUpdate() {
121         if (mainTableCols == null) return;
122
123         // extract all the columns that should be updated and inserted into
124
// mainTableColsForUpdate
125
mainTableColsForUpdate = new JdbcColumn[mainTableCols.length];
126         int c = 0;
127         for (int i = 0; i < mainTableCols.length; i++) {
128             JdbcColumn col = mainTableCols[i];
129             if (col.isForUpdate()) mainTableColsForUpdate[c++] = col;
130         }
131         if (c == 0) {
132             mainTableColsForUpdate = null;
133         } else if (c < mainTableCols.length) {
134             JdbcColumn[] a = new JdbcColumn[c];
135             System.arraycopy(mainTableColsForUpdate, 0, a, 0, c);
136             mainTableColsForUpdate = a;
137         }
138     }
139
140     /**
141      * Flatten all of this fields main table columns to a.
142      */

143     public void addMainTableCols(ArrayList JavaDoc a) {
144     }
145
146     /**
147      * Set the table field on all our main table columns.
148      */

149     public void setMainTable(JdbcTable table) {
150         mainTable = table;
151     }
152
153     public String JavaDoc toString() {
154         String JavaDoc n = getClass().getName();
155         n = n.substring(n.lastIndexOf('.') + 1);
156         return n + " " + fmd.type.getName() + " " + fmd.classMetaData.getShortName() +
157                 "." + fmd.name + (fake ? " FAKE" : "");
158     }
159
160     /**
161      * Convert a useJoin field value to a String.
162      */

163     public static String JavaDoc toUseJoinString(int useJoin) {
164         switch (useJoin) {
165             case USE_JOIN_NO:
166                 return "NO";
167             case USE_JOIN_INNER:
168                 return "INNER";
169             case USE_JOIN_OUTER:
170                 return "OUTER";
171         }
172         return "unknown(" + useJoin + ")";
173     }
174
175     /**
176      * Get context information for this field from its .jdo meta data or
177      * the .jdo meta data of its class.
178      */

179     public String JavaDoc getContext() {
180         JdoField jf = fmd.jdoField;
181         if (jf != null) return jf.getContext();
182         return fmd.classMetaData.jdoClass.getContext();
183     }
184
185     /**
186      * Add all tables that belong to this field to the set.
187      */

188     public void getTables(HashSet JavaDoc tables) {
189         // nothing to do
190
}
191
192     /**
193      * Get the useKeyJoin value for this field. This is only valid for maps.
194      */

195     public int getUseKeyJoin() {
196         return 0;
197     }
198
199     public void dump() {
200         dump(Debug.OUT, "");
201     }
202
203     public void dump(PrintStream JavaDoc out, String JavaDoc indent) {
204         out.println(indent + this);
205         String JavaDoc is = indent + " ";
206         out.println(is + "useJoin " + toUseJoinString(useJoin));
207         out.println(is + "stateFieldNo " + stateFieldNo);
208         if (mainTableCols != null) {
209             out.println(is + mainTableCols.length + " mainTableCols(s)");
210             for (int i = 0; i < mainTableCols.length; i++) {
211                 out.println(is + "[" + i + "] " + mainTableCols[i]);
212             }
213         }
214         if (mainTableColsForUpdate != null) {
215             out.println(
216                     is + mainTableColsForUpdate.length + " mainTableColsForUpdate(s)");
217             for (int i = 0; i < mainTableColsForUpdate.length; i++) {
218                 out.println(is + "[" + i + "] " + mainTableColsForUpdate[i]);
219             }
220         }
221     }
222
223     /**
224      * Append part of an update statement for us to s (e.g col = ?). This
225      * must return true if a replacable parameter was <b>not</b> added (e.g.
226      * columns using Oracle LOBs which put in empty_clob() or whatever).
227      */

228     public boolean appendUpdate(CharBuf s, State state) {
229         return false;
230     }
231
232     /**
233      * Append part of a where clause for us to s (e.g cola = ? and colb = ?).
234      * This is used for generating the where clause for changed locking.
235      */

236     public void appendWhere(CharBuf s, SqlDriver sqlDriver) {
237     }
238
239     /**
240      * Append part of a is null where clause for us to s (e.g cola is null
241      * and colb is null).
242      * This is used for generating the where clause for changed locking.
243      */

244     public void appendWhereIsNull(CharBuf s, SqlDriver sqlDriver) {
245     }
246
247     /**
248      * Append part of the insert list for us to s (e.g. cola, colb)).
249      */

250     public void appendInsertColumnList(CharBuf s) {
251     }
252
253     /**
254      * Append part of the insert value list for us to s (e.g. ?, ?)). This
255      * must return true if a replacable parameter was <b>not</b> added (e.g.
256      * columns using Oracle LOBs which put in empty_clob() or whatever).
257      */

258     public boolean appendInsertValueList(CharBuf s, State state) {
259         return false;
260     }
261
262     /**
263      * Convert this field into a list of ColumnExp's or null if this is
264      * not possible.
265      */

266     public ColumnExp toColumnExp(SelectExp se, boolean joinToSuper) {
267         return null;
268     }
269
270     /**
271      * Convert this field into a list of ColumnExp's to be compared to
272      * a null literal. This should only include non-shared columns i.e.
273      * columns that are updated. If all columns are shared then all should
274      * be included.
275      */

276     public ColumnExp toColumnExpForNullLiteralCompare(SelectExp se) {
277         return toColumnExp(se, true);
278     }
279
280     /**
281      * Convert this field into an isEmpty expression.
282      */

283     public SqlExp toIsEmptySqlExp(JdbcJDOQLCompiler comp, SelectExp root) {
284         throw BindingSupportImpl.getInstance().runtime("isEmpty() may not be called on " +
285                 fmd.getQName());
286     }
287
288     /**
289      * Convert this field into an contains expression.
290      */

291     public SqlExp toContainsSqlExp(JdbcJDOQLCompiler comp, SelectExp root,
292             Node args) {
293         throw BindingSupportImpl.getInstance().runtime("contains(...) may not be called on " +
294                 fmd.getQName());
295     }
296
297     /**
298      * Convert this field into an containsKey expression.
299      */

300     public SqlExp toContainsKeySqlExp(JdbcJDOQLCompiler comp, SelectExp root,
301             Node args) {
302         throw BindingSupportImpl.getInstance().runtime("containsKey(...) may not be called on " +
303                 fmd.getQName());
304     }
305
306     /**
307      * Set this field on a PreparedStatement. This is used to set parameters
308      * for queries.
309      *
310      * @return Index of the parameter after the last one we set in ps
311      */

312     public int setQueryParam(PreparedStatement JavaDoc ps, int firstParam,
313             Object JavaDoc value)
314             throws SQLException JavaDoc {
315         throw BindingSupportImpl.getInstance().internal(
316                 "set called on " + this);
317     }
318
319     /**
320      * Persist pass 2 field for a block of graph entries all with
321      * the same class. The same ps'es can be used for all entries in the block.
322      */

323     public void persistPass2Block(PersistGraph graph, int blockStart,
324             int blockEnd, CharBuf s, Connection JavaDoc con, boolean batchInserts,
325             boolean batchUpdates)
326             throws SQLException JavaDoc {
327         throw BindingSupportImpl.getInstance().internal(
328                 "persistPass2Block called on " + this);
329     }
330
331     /**
332      * Delete a pass 2 field for a block of graph entries all with
333      * the same class. The same ps'es can be used for all entries in the block.
334      */

335     public void deletePass2Block(DeletePacket graph, int blockStart,
336             int blockEnd, CharBuf s, Connection JavaDoc con, boolean batch)
337             throws SQLException JavaDoc {
338         throw BindingSupportImpl.getInstance().internal(
339                 "deletePass2Block called on " + this);
340     }
341
342     /**
343      * Does this field require the sucky Oracle LOB support on insert/update?
344      */

345     public boolean isOracleStyleLOB() {
346         return false;
347     }
348
349     /**
350      * Make sure all the indexes on our link tables (if any) have names,
351      */

352     public void nameLinkTableIndexes(JdbcNameGenerator namegen) {
353     }
354
355     /**
356      * If there a columnName in our main table columns array then return it
357      * else return null.
358      */

359     public JdbcColumn findMainTableColumn(String JavaDoc columnName) {
360         if (mainTableCols == null) return null;
361         for (int i = mainTableCols.length - 1; i >= 0; i--) {
362             JdbcColumn c = mainTableCols[i];
363             if (c.name.equals(columnName)) return c;
364         }
365         return null;
366     }
367
368     /**
369      * Get the current SqlDriver.
370      */

371     public SqlDriver getSqlDriver() {
372         return ((JdbcClass)fmd.classMetaData.storeClass).sqlDriver;
373     }
374
375     /**
376      * Map an exception using the current SqlDriver.
377      */

378     public RuntimeException JavaDoc mapException(Throwable JavaDoc cause,
379             String JavaDoc message) {
380         return getSqlDriver().mapException(cause, message, true);
381     }
382
383     /**
384      * Create a list of ColumnExp's for this field or null if it has no
385      * columns stored in any of the tables for its owning class.
386      */

387     public ColumnExp createOwningTableColumnExpList(SelectExp se) {
388         return null;
389     }
390
391     /**
392      * Adjust spec so this field will be fetched.
393      */

394     public void prepareFetch(FetchSpec spec, FetchOptions options) {
395     }
396
397 }
398
399
400
Popular Tags