001: package com.quadcap.sql.meta;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.Iterator;
044:
045: import java.sql.DatabaseMetaData;
046: import java.sql.ResultSetMetaData;
047: import java.sql.SQLException;
048:
049: import com.quadcap.sql.Column;
050: import com.quadcap.sql.Constraint;
051: import com.quadcap.sql.Database;
052: import com.quadcap.sql.Expression;
053: import com.quadcap.sql.ImportedKeyConstraint;
054: import com.quadcap.sql.Relation;
055: import com.quadcap.sql.Row;
056: import com.quadcap.sql.Session;
057: import com.quadcap.sql.StaticCursor;
058: import com.quadcap.sql.Table;
059:
060: import com.quadcap.sql.index.BCursor;
061: import com.quadcap.sql.index.Btree;
062:
063: import com.quadcap.sql.types.*;
064:
065: import com.quadcap.util.Debug;
066:
067: /**
068: * A Cursor supporting the <code>getCrossReference</code>,
069: * <code>getExportedKeys</code>, <code>getImportedKeys</code>
070: * functions.
071: *
072: * @author Stan Bailes
073: */
074: public class MetaCrossReference extends MetaCursor {
075: static Column[] cols = { new Column("PKTABLE_CAT", typeString), // 1
076: new Column("PKTABLE_SCHEM", typeString), // 2
077: new Column("PKTABLE_NAME", typeString), // 3
078: new Column("PKCOLUMN_NAME", typeString), // 4
079: new Column("FKTABLE_CAT", typeString), // 5
080: new Column("FKTABLE_SCHEM", typeString), // 6
081: new Column("FKTABLE_NAME", typeString), // 7
082: new Column("FKCOLUMN_NAME", typeString), // 8
083: new Column("KEY_SEQ", typeShort), // 9
084: new Column("UPDATE_RULE", typeShort), // 10
085: new Column("DELETE_RULE", typeShort), // 11
086: new Column("FK_NAME", typeString), // 12
087: new Column("PK_NAME", typeString), // 13
088: new Column("DEFERRABILITY", typeShort) // 14
089: };
090:
091: static int[] sortColumns = { 1, 2, 3, 9 };
092:
093: public MetaCrossReference(Session session, Expression predicate)
094: throws SQLException {
095: super (session, predicate);
096: try {
097: addColumns(cols);
098: Database db = session.getDatabase();
099: session.getTableWriteLock("#Schema");
100: synchronized (db.getFile().getLock()) {
101: Iterator iter = db.getRelationNameIterator();
102: while (iter.hasNext()) {
103: String name = (String) iter.next();
104: Relation r = db.getRelation(name);
105: if (r instanceof Table) {
106: Table t = (Table) r;
107: int num = t.getNumConstraints();
108: for (int i = 0; i < num; i++) {
109: Constraint c = t.getConstraint(i);
110: if (c instanceof ImportedKeyConstraint) {
111: doConstraint(t,
112: (ImportedKeyConstraint) c);
113: }
114: }
115: }
116: }
117: }
118: sort();
119: } catch (ValueException e) {
120: Debug.print(e);
121: SQLException te = new SQLException(e.toString(), "Q000L");
122: te.setNextException(e);
123: throw te;
124: } catch (IOException e) {
125: Debug.print(e);
126: throw new SQLException(e.toString(), "Q000M");
127: }
128: }
129:
130: public int[] getSortColumns() {
131: return sortColumns;
132: }
133:
134: void doConstraint(Table t, ImportedKeyConstraint c)
135: throws SQLException, IOException {
136: int[] colIndices = c.getColumns();
137: int[] fCols = c.getFCols(session.getDatabase());
138: Table f = c.getFTable(session.getDatabase());
139: for (int i = 0; i < colIndices.length; i++) {
140: Column col = t.getColumn(colIndices[i]);
141: Column fcol = f.getColumn(fCols[i]);
142: Row row = doColumn(t, f, c, i, col, fcol);
143: if (rowMatch(row)) {
144: addRow(row);
145: }
146: }
147: }
148:
149: Row doColumn(Table t, Table f, ImportedKeyConstraint c, int pos,
150: Column col, Column fcol) throws SQLException {
151: Row row = new Row(14);
152: row.set(1, ValueNull.valueNull);
153: doTableName(2, row, f.getName());
154: row.set(4, new ValueString(fcol.getShortName()));
155:
156: row.set(5, ValueNull.valueNull);
157: doTableName(6, row, t.getName());
158: row.set(8, new ValueString(col.getShortName()));
159:
160: row.set(9, new ValueInteger(pos + 1));
161:
162: int spec = c.getSpec();
163: int ud = DatabaseMetaData.importedKeyRestrict;
164: if ((spec & Constraint.CASCADE) != 0) {
165: ud = DatabaseMetaData.importedKeyCascade;
166: }
167: ValueInteger v = new ValueInteger(ud);
168: row.set(10, v);
169: row.set(11, v);
170:
171: row.set(12, new ValueString(c.getName()));
172: row.set(13, ValueNull.valueNull);
173:
174: ud = DatabaseMetaData.importedKeyNotDeferrable;
175: if ((spec & Constraint.DEFERRABLE) != 0) {
176: if ((spec & Constraint.INIT_DEFERRED) != 0) {
177: ud = DatabaseMetaData.importedKeyInitiallyDeferred;
178: } else {
179: ud = DatabaseMetaData.importedKeyInitiallyImmediate;
180: }
181: }
182: row.set(14, new ValueInteger(ud));
183: return row;
184: }
185: }
|