| 1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Vector ; 47 48 import java.sql.SQLException ; 49 50 import com.quadcap.sql.index.Btree; 51 52 import com.quadcap.util.Debug; 53 54 61 public abstract class Constraint implements Externalizable { 62 transient Table table; 63 transient int[] columns; 64 Vector colNames = new Vector (); 65 String name = null; 66 int spec = 0; 67 68 public static final int FULL = (1 << 0); 70 public static final int PARTIAL = (1 << 1); 71 72 public static final int NOACTION = 0; 74 public static final int CASCADE = 1; 75 public static final int SETNULL = 2; 76 public static final int SETDEFAULT = 3; 77 78 public static final int UPDATE = 2; 80 public static final int DELETE = 4; 82 83 public static final int DEFERRABLE = (1 << 6); 85 public static final int INIT_DEFERRED = (1 << 7); 87 public static final int GLOBAL = (1 << 8); 89 90 93 public Constraint() {} 94 95 98 public Constraint(String name) { 99 this.name = name; 100 } 101 102 105 public Constraint(String name, Vector colNames) { 106 this.name = name; 107 this.colNames = colNames; 108 } 109 110 113 public String getName() { 114 return name; 115 } 116 117 120 public void setName(String name) { 121 this.name = name; 122 } 123 124 127 public int getRefAction(int opType) { 128 return (spec >> opType) & 0x3; 129 } 130 131 134 static String [] refActions = { "NO ACTION", "CASCADE", "SET NULL", 135 "SET DEFAULT" }; 136 137 140 public String getRefActionString(int opType) { 141 return refActions[getRefAction(opType)]; 142 } 143 144 147 public void setGlobal(boolean g) { 148 if (g) { 149 spec |= GLOBAL; 150 } else { 151 spec &= ~GLOBAL; 152 } 153 } 154 155 158 public boolean isGlobal() { return (spec & GLOBAL) != 0; } 159 160 164 abstract public void checkInsert(Session session, Row row) 165 throws SQLException , IOException  166 ; 167 168 174 abstract public void applyInsert(Session session, Row row, long rowId, 175 Constraint activeIndex) 176 throws SQLException , IOException  177 ; 178 179 183 abstract public void checkDelete(Session session, Row row, long rowId) 184 throws SQLException , IOException  185 ; 186 187 192 abstract public void applyDelete(Session session, Row row, long rowId, 193 Constraint activeIndex) 194 throws SQLException , IOException  195 ; 196 197 207 abstract public void checkUpdate(Session session, byte[] oldKey, Row row, 208 Row oldRow, long rowId, 209 Constraint activeIndex) 210 throws SQLException , IOException  211 ; 212 213 219 abstract public void applyUpdate(Session session, byte[] oldKey, Row row, 220 Row oldRow, long rowId, 221 Constraint activeIndex) 222 throws SQLException , IOException  223 ; 224 225 230 abstract public void delete(Session session) 231 throws SQLException , IOException  232 ; 233 234 239 abstract public void add(Session session) 240 throws SQLException , IOException ; 241 242 245 public void undoAdd(Session session) 246 throws SQLException , IOException { 247 delete(session); 248 } 249 250 251 254 public void undoDelete(Session session) 255 throws SQLException , IOException  256 { 257 add(session); 258 } 259 260 261 264 public void setDeferrable(int def) { 265 this.spec |= def; 266 } 267 268 271 public void setRefSpec(int ref) { 272 this.spec |= ref; 273 } 274 275 278 public void setTable(Table table) throws SQLException { 279 this.table = table; 280 this.columns = null; 281 getColumns(); 282 } 283 284 287 public Table getTable() { return this.table; } 288 289 292 public int getSpec() { 293 return spec; 294 } 295 296 299 public void setColumn(Column column) { 300 if (colNames.size() != 0) { 301 colNames = new Vector (); 302 } 303 colNames.add(column.getName()); 304 this.columns = null; 305 } 306 307 312 public Column getColumn() throws SQLException { 313 getColumns(); 314 if (columns.length < 1) throw new SQLException ("No column", "Q0000"); 315 if (columns.length > 1) 316 throw new SQLException ("Not a single column constraint: " + this, 317 "Q0000"); 318 return table.getColumn(columns[0]); 319 } 320 321 324 public int getColumnCount() { 325 return colNames.size(); 326 } 327 328 331 public Column getColumn(int c) throws SQLException { 332 return table.getColumn(columns[c]); 333 } 334 335 338 public void readExternal(ObjectInput in) 339 throws IOException , ClassNotFoundException  340 { 341 spec = in.readInt(); 342 name = (String )in.readObject(); 343 colNames = (Vector )in.readObject(); 344 columns = null; 345 } 346 347 350 public void writeExternal(ObjectOutput out) throws IOException { 351 out.writeInt(spec); 352 out.writeObject(name); 353 out.writeObject(colNames); 354 } 355 356 362 public int[] getColumns() throws SQLException { 363 if (columns == null) { 364 columns = table.mapColumns(colNames); 365 } 366 return columns; 367 } 368 369 372 public void resetColumns() throws SQLException { 373 columns = null; 374 } 375 376 379 public Vector getColumnNames() throws SQLException { 380 return colNames; 381 } 382 383 387 public Btree getIndex(Database db) throws IOException { 388 return null; 389 } 390 391 394 public int getPriority() { return 3; } 395 396 399 public boolean isDeferred() { 400 return false; } 402 403 406 public String toString() { 407 StringBuffer sb = new StringBuffer ("Constraint "); 408 sb.append(name); 409 sb.append(": "); 410 if (table == null) { 411 sb.append("<null table>"); 412 } else { 413 sb.append(table.getName()); 414 } 415 sb.append("("); 416 if (colNames != null) { 417 for (int i = 0; i < colNames.size(); i++) { 418 if (i > 0) sb.append(','); 419 sb.append(colNames.elementAt(i).toString()); 420 } 421 } 422 sb.append(")"); 423 if ((spec & FULL) != 0) sb.append(" FULL"); 424 if ((spec & PARTIAL) != 0) sb.append(" PARTIAL"); 425 426 if (getRefAction(UPDATE) != NOACTION) { 427 sb.append(" ON UPDATE "); 428 sb.append(getRefActionString(UPDATE)); 429 } 430 if (getRefAction(DELETE) != NOACTION) { 431 sb.append(" ON DELETE "); 432 sb.append(getRefActionString(DELETE)); 433 } 434 if ((spec & DEFERRABLE) != 0) sb.append(" DEFERRABLE"); 435 if ((spec & INIT_DEFERRED) != 0) sb.append(" INITIALLY DEFERRED"); 436 437 return sb.toString(); 438 } 439 } 440 | Popular Tags |