| 1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.Externalizable ; 43 import java.io.IOException ; 44 import java.io.ObjectInput ; 45 import java.io.ObjectOutput ; 46 47 import java.util.Vector ; 48 49 import java.sql.ResultSet ; 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.io.ObjectInputStream; 53 import com.quadcap.sql.io.ObjectOutputStream; 54 55 import com.quadcap.sql.index.Btree; 56 import com.quadcap.sql.index.Comparator; 57 58 63 public abstract class ForeignKeyConstraint 64 extends Constraint 65 implements Externalizable  66 { 67 String fTableName; 68 Vector fColNames; 69 Table fTable; 70 UniqueConstraint fConstraint; 71 int[] fCols; 72 transient Comparator compare; 73 74 77 public ForeignKeyConstraint() {} 78 79 public ForeignKeyConstraint(String name, String fTableName) { 80 super(name); 81 this.fTableName = fTableName; 82 } 83 84 public ForeignKeyConstraint(String name, Vector colNames, 85 String fTableName, Vector fColNames) { 86 super(name, colNames); 87 this.fTableName = fTableName; 88 this.fColNames = fColNames; 89 } 90 91 94 public void resetColumns() throws SQLException { 95 super.resetColumns(); 96 fCols = null; 97 } 98 99 public void checkInsert(Session session, Row row) 100 throws SQLException , IOException  101 { 102 } 103 104 public void applyInsert(Session session, Row row, long rowId, 105 Constraint activeIndex) 106 throws SQLException , IOException  107 { 108 } 109 110 public void checkUpdate(Session session, Row row, long rowId) 111 throws SQLException , IOException  112 { 113 } 114 115 public void applyUpdate(Session session, byte[] oldKey, Row row, 116 Row oldRow, long rowId, Constraint activeIndex) 117 throws SQLException , IOException  118 { 119 } 120 121 public void checkDelete(Session session, Row row, long rowId) 122 throws SQLException , IOException  123 { 124 } 125 126 public void applyDelete(Session session, Row row, long rowId, 127 Constraint activeIndex) 128 throws SQLException , IOException  129 { 130 } 131 132 135 public final boolean isDeferred() { 136 return (spec & INIT_DEFERRED) != 0; 137 } 138 139 final Comparator getComparator() throws SQLException { 140 if (compare == null) compare = new Key(getColumns().length); 141 return compare; 142 } 143 144 147 public byte[] makeKey(Session session, Row row) 148 throws SQLException  149 { 150 return makeKey(row, getColumns()); 151 } 152 153 156 public byte[] makeFKey(Session session, Row row) 157 throws IOException , SQLException  158 { 159 return makeKey(row, getFCols(session.getDatabase())); 160 } 161 162 private final byte[] makeKey(Row row, int[] k) throws SQLException { 163 return Key.makeKey(null, row, k, 0, false); 164 } 165 166 public void setForeignColumn(String name) { 167 fColNames = new Vector (); 168 fColNames.addElement(name); 169 } 170 171 public void readExternal(ObjectInput in) 172 throws IOException , ClassNotFoundException  173 { 174 super.readExternal(in); 175 fTableName = (String )in.readObject(); 176 fColNames = (Vector )in.readObject(); 177 } 178 179 public void writeExternal(ObjectOutput out) throws IOException { 180 super.writeExternal(out); 181 out.writeObject(fTableName); 182 out.writeObject(fColNames); 183 } 184 185 public String getFTableName() { 186 return fTableName; 187 } 188 189 public void setFTableName(String s) { 190 fTableName = s; 191 } 192 193 public Table getFTable(Database db) throws SQLException , IOException { 194 if (fTable == null) { 195 Relation t = db.getRelation(fTableName); 196 if (t == null) { 197 throw new SQLException ("No such table: " + fTableName, 198 "42000"); 199 } 200 try { 201 fTable = (Table)t; 202 } catch (ClassCastException e) { 203 throw new SQLException ("Not a base table: " + fTableName, 204 "42000"); 205 } 206 } 207 return fTable; 208 } 209 210 213 public void add(Session session) throws SQLException , IOException {} 214 215 abstract public void delete(Session session) 216 throws SQLException , IOException  217 ; 218 219 222 public int[] getFCols(Database db) throws SQLException , IOException { 223 if (fCols == null) { 224 if (fColNames == null) { 225 UniqueConstraint pk = getFTable(db).getPrimaryKey(); 226 if (pk == null) { 227 throw new SQLException ("No primary key for table " + 228 fTable.getName()); 229 } 230 fConstraint = pk; 231 fCols = pk.getColumns(); 232 } else { 233 fCols = getFTable(db).mapColumns(fColNames); 234 fConstraint = fTable.getIndexForColumns(fCols); 235 } 236 } 237 return fCols; 238 } 239 240 boolean isSelfReferencing(Database db) throws IOException , SQLException { 241 boolean ret = table == getFTable(db); 242 return ret; 243 } 244 245 public String toString() { 246 StringBuffer sb = new StringBuffer (super.toString()); 247 sb.append(" references " + fTableName + "("); 248 if (fColNames != null) { 249 for (int i = 0; i < fColNames.size(); i++) { 250 if (i > 0) sb.append(","); 251 sb.append(fColNames.elementAt(i).toString()); 252 } 253 } 254 sb.append(')'); 255 return sb.toString(); 256 } 257 } 258 | Popular Tags |