KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > meta > MetaCrossReference


1 package com.quadcap.sql.meta;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.util.Iterator JavaDoc;
44
45 import java.sql.DatabaseMetaData JavaDoc;
46 import java.sql.ResultSetMetaData JavaDoc;
47 import java.sql.SQLException JavaDoc;
48
49 import com.quadcap.sql.Column;
50 import com.quadcap.sql.Constraint;
51 import com.quadcap.sql.Database;
52 import com.quadcap.sql.Expression;
53 import com.quadcap.sql.ImportedKeyConstraint;
54 import com.quadcap.sql.Relation;
55 import com.quadcap.sql.Row;
56 import com.quadcap.sql.Session;
57 import com.quadcap.sql.StaticCursor;
58 import com.quadcap.sql.Table;
59
60 import com.quadcap.sql.index.BCursor;
61 import com.quadcap.sql.index.Btree;
62
63 import com.quadcap.sql.types.*;
64
65 import com.quadcap.util.Debug;
66
67 /**
68  * A Cursor supporting the <code>getCrossReference</code>,
69  * <code>getExportedKeys</code>, <code>getImportedKeys</code>
70  * functions.
71  *
72  * @author Stan Bailes
73  */

74 public class MetaCrossReference extends MetaCursor {
75     static Column[] cols = {
76     new Column("PKTABLE_CAT", typeString), // 1
77
new Column("PKTABLE_SCHEM", typeString), // 2
78
new Column("PKTABLE_NAME", typeString), // 3
79
new Column("PKCOLUMN_NAME", typeString), // 4
80
new Column("FKTABLE_CAT", typeString), // 5
81
new Column("FKTABLE_SCHEM", typeString), // 6
82
new Column("FKTABLE_NAME", typeString), // 7
83
new Column("FKCOLUMN_NAME", typeString), // 8
84
new Column("KEY_SEQ", typeShort), // 9
85
new Column("UPDATE_RULE", typeShort), // 10
86
new Column("DELETE_RULE", typeShort), // 11
87
new Column("FK_NAME", typeString), // 12
88
new Column("PK_NAME", typeString), // 13
89
new Column("DEFERRABILITY", typeShort) // 14
90
};
91
92     static int[] sortColumns = { 1, 2, 3, 9 };
93
94     public MetaCrossReference(Session session, Expression predicate)
95     throws SQLException JavaDoc
96     {
97     super(session, predicate);
98     try {
99         addColumns(cols);
100         Database db = session.getDatabase();
101             session.getTableWriteLock("#Schema");
102             synchronized (db.getFile().getLock()) {
103                 Iterator JavaDoc iter = db.getRelationNameIterator();
104                 while (iter.hasNext()) {
105                     String JavaDoc name = (String JavaDoc)iter.next();
106                     Relation r = db.getRelation(name);
107                     if (r instanceof Table) {
108                         Table t = (Table)r;
109                         int num = t.getNumConstraints();
110                         for (int i = 0; i < num; i++) {
111                             Constraint c = t.getConstraint(i);
112                             if (c instanceof ImportedKeyConstraint) {
113                                 doConstraint(t, (ImportedKeyConstraint)c);
114                             }
115                         }
116                     }
117                 }
118             }
119         sort();
120     } catch (ValueException e) {
121         Debug.print(e);
122         SQLException JavaDoc te = new SQLException JavaDoc(e.toString(), "Q000L");
123         te.setNextException(e);
124         throw te;
125     } catch (IOException JavaDoc e) {
126         Debug.print(e);
127         throw new SQLException JavaDoc(e.toString(), "Q000M");
128     }
129     }
130
131     public int[] getSortColumns() {
132     return sortColumns;
133     }
134
135     void doConstraint(Table t, ImportedKeyConstraint c)
136     throws SQLException JavaDoc, IOException JavaDoc
137     {
138     int[] colIndices = c.getColumns();
139     int[] fCols = c.getFCols(session.getDatabase());
140     Table f = c.getFTable(session.getDatabase());
141     for (int i = 0; i < colIndices.length; i++) {
142         Column col = t.getColumn(colIndices[i]);
143         Column fcol = f.getColumn(fCols[i]);
144         Row row = doColumn(t, f, c, i, col, fcol);
145         if (rowMatch(row)) {
146         addRow(row);
147         }
148     }
149     }
150
151     Row doColumn(Table t, Table f, ImportedKeyConstraint c, int pos, Column col,
152          Column fcol)
153     throws SQLException JavaDoc
154     {
155     Row row = new Row(14);
156     row.set(1, ValueNull.valueNull);
157     doTableName(2, row, f.getName());
158     row.set(4, new ValueString(fcol.getShortName()));
159     
160     row.set(5, ValueNull.valueNull);
161     doTableName(6, row, t.getName());
162     row.set(8, new ValueString(col.getShortName()));
163     
164     row.set(9, new ValueInteger(pos+1));
165
166     int spec = c.getSpec();
167     int ud = DatabaseMetaData.importedKeyRestrict;
168     if ((spec & Constraint.CASCADE) != 0) {
169         ud = DatabaseMetaData.importedKeyCascade;
170     }
171     ValueInteger v = new ValueInteger(ud);
172     row.set(10, v);
173     row.set(11, v);
174
175     row.set(12, new ValueString(c.getName()));
176     row.set(13, ValueNull.valueNull);
177
178     ud = DatabaseMetaData.importedKeyNotDeferrable;
179     if ((spec & Constraint.DEFERRABLE) != 0) {
180         if ((spec & Constraint.INIT_DEFERRED) != 0) {
181         ud = DatabaseMetaData.importedKeyInitiallyDeferred;
182         } else {
183         ud = DatabaseMetaData.importedKeyInitiallyImmediate;
184         }
185     }
186     row.set(14, new ValueInteger(ud));
187     return row;
188     }
189 }
190
Popular Tags