Quadcap Embeddable Database

com/quadcap/sql/meta/MetaCrossReference.java

Go to the documentation of this file.
00001 package com.quadcap.sql.meta; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.util.Iterator; 00044 00045 import java.sql.DatabaseMetaData; 00046 import java.sql.ResultSetMetaData; 00047 import java.sql.SQLException; 00048 00049 import com.quadcap.sql.Column; 00050 import com.quadcap.sql.Constraint; 00051 import com.quadcap.sql.Database; 00052 import com.quadcap.sql.Expression; 00053 import com.quadcap.sql.ImportedKeyConstraint; 00054 import com.quadcap.sql.Relation; 00055 import com.quadcap.sql.Row; 00056 import com.quadcap.sql.Session; 00057 import com.quadcap.sql.StaticCursor; 00058 import com.quadcap.sql.Table; 00059 00060 import com.quadcap.sql.index.BCursor; 00061 import com.quadcap.sql.index.Btree; 00062 00063 import com.quadcap.sql.types.*; 00064 00065 import com.quadcap.util.Debug; 00066 00067 /** 00068 * A Cursor supporting the <code>getCrossReference</code>, 00069 * <code>getExportedKeys</code>, <code>getImportedKeys</code> 00070 * functions. 00071 * 00072 * @author Stan Bailes 00073 */ 00074 public class MetaCrossReference extends MetaCursor { 00075 static Column[] cols = { 00076 new Column("PKTABLE_CAT", typeString), // 1 00077 new Column("PKTABLE_SCHEM", typeString), // 2 00078 new Column("PKTABLE_NAME", typeString), // 3 00079 new Column("PKCOLUMN_NAME", typeString), // 4 00080 new Column("FKTABLE_CAT", typeString), // 5 00081 new Column("FKTABLE_SCHEM", typeString), // 6 00082 new Column("FKTABLE_NAME", typeString), // 7 00083 new Column("FKCOLUMN_NAME", typeString), // 8 00084 new Column("KEY_SEQ", typeShort), // 9 00085 new Column("UPDATE_RULE", typeShort), // 10 00086 new Column("DELETE_RULE", typeShort), // 11 00087 new Column("FK_NAME", typeString), // 12 00088 new Column("PK_NAME", typeString), // 13 00089 new Column("DEFERRABILITY", typeShort) // 14 00090 }; 00091 00092 static int[] sortColumns = { 1, 2, 3, 9 }; 00093 00094 public MetaCrossReference(Session session, Expression predicate) 00095 throws SQLException 00096 { 00097 super(session, predicate); 00098 try { 00099 addColumns(cols); 00100 Database db = session.getDatabase(); 00101 session.getTableWriteLock("#Schema"); 00102 synchronized (db.getFile().getLock()) { 00103 Iterator iter = db.getRelationNameIterator(); 00104 while (iter.hasNext()) { 00105 String name = (String)iter.next(); 00106 Relation r = db.getRelation(name); 00107 if (r instanceof Table) { 00108 Table t = (Table)r; 00109 int num = t.getNumConstraints(); 00110 for (int i = 0; i < num; i++) { 00111 Constraint c = t.getConstraint(i); 00112 if (c instanceof ImportedKeyConstraint) { 00113 doConstraint(t, (ImportedKeyConstraint)c); 00114 } 00115 } 00116 } 00117 } 00118 } 00119 sort(); 00120 } catch (ValueException e) { 00121 Debug.print(e); 00122 SQLException te = new SQLException(e.toString(), "Q000L"); 00123 te.setNextException(e); 00124 throw te; 00125 } catch (IOException e) { 00126 Debug.print(e); 00127 throw new SQLException(e.toString(), "Q000M"); 00128 } 00129 } 00130 00131 public int[] getSortColumns() { 00132 return sortColumns; 00133 } 00134 00135 void doConstraint(Table t, ImportedKeyConstraint c) 00136 throws SQLException, IOException 00137 { 00138 int[] colIndices = c.getColumns(); 00139 int[] fCols = c.getFCols(session.getDatabase()); 00140 Table f = c.getFTable(session.getDatabase()); 00141 for (int i = 0; i < colIndices.length; i++) { 00142 Column col = t.getColumn(colIndices[i]); 00143 Column fcol = f.getColumn(fCols[i]); 00144 Row row = doColumn(t, f, c, i, col, fcol); 00145 if (rowMatch(row)) { 00146 addRow(row); 00147 } 00148 } 00149 } 00150 00151 Row doColumn(Table t, Table f, ImportedKeyConstraint c, int pos, Column col, 00152 Column fcol) 00153 throws SQLException 00154 { 00155 Row row = new Row(14); 00156 row.set(1, ValueNull.valueNull); 00157 doTableName(2, row, f.getName()); 00158 row.set(4, new ValueString(fcol.getShortName())); 00159 00160 row.set(5, ValueNull.valueNull); 00161 doTableName(6, row, t.getName()); 00162 row.set(8, new ValueString(col.getShortName())); 00163 00164 row.set(9, new ValueInteger(pos+1)); 00165 00166 int spec = c.getSpec(); 00167 int ud = DatabaseMetaData.importedKeyRestrict; 00168 if ((spec & Constraint.CASCADE) != 0) { 00169 ud = DatabaseMetaData.importedKeyCascade; 00170 } 00171 ValueInteger v = new ValueInteger(ud); 00172 row.set(10, v); 00173 row.set(11, v); 00174 00175 row.set(12, new ValueString(c.getName())); 00176 row.set(13, ValueNull.valueNull); 00177 00178 ud = DatabaseMetaData.importedKeyNotDeferrable; 00179 if ((spec & Constraint.DEFERRABLE) != 0) { 00180 if ((spec & Constraint.INIT_DEFERRED) != 0) { 00181 ud = DatabaseMetaData.importedKeyInitiallyDeferred; 00182 } else { 00183 ud = DatabaseMetaData.importedKeyInitiallyImmediate; 00184 } 00185 } 00186 row.set(14, new ValueInteger(ud)); 00187 return row; 00188 } 00189 }