1 package com.quadcap.sql.meta; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Iterator ; 44 45 import java.sql.DatabaseMetaData ; 46 import java.sql.ResultSetMetaData ; 47 import java.sql.SQLException ; 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 74 public class MetaCrossReference extends MetaCursor { 75 static Column[] cols = { 76 new Column("PKTABLE_CAT", typeString), new Column("PKTABLE_SCHEM", typeString), new Column("PKTABLE_NAME", typeString), new Column("PKCOLUMN_NAME", typeString), new Column("FKTABLE_CAT", typeString), new Column("FKTABLE_SCHEM", typeString), new Column("FKTABLE_NAME", typeString), new Column("FKCOLUMN_NAME", typeString), new Column("KEY_SEQ", typeShort), new Column("UPDATE_RULE", typeShort), new Column("DELETE_RULE", typeShort), new Column("FK_NAME", typeString), new Column("PK_NAME", typeString), new Column("DEFERRABILITY", typeShort) }; 91 92 static int[] sortColumns = { 1, 2, 3, 9 }; 93 94 public MetaCrossReference(Session session, Expression predicate) 95 throws SQLException 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 iter = db.getRelationNameIterator(); 104 while (iter.hasNext()) { 105 String name = (String )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 te = new SQLException (e.toString(), "Q000L"); 123 te.setNextException(e); 124 throw te; 125 } catch (IOException e) { 126 Debug.print(e); 127 throw new SQLException (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 , IOException 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 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 |