| 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.sql.SQLException ; 47 48 import com.quadcap.sql.io.Extern; 49 50 import com.quadcap.util.Debug; 51 52 57 public class DeleteConstraint extends LogStep implements Externalizable { 58 transient Table table; 59 60 Constraint constraint; 61 String tableName = null; 62 boolean global = false; 63 64 public DeleteConstraint() {} 65 66 public DeleteConstraint(Session session, 67 Table table, Constraint constraint) { 68 this(session, table, constraint, false); 69 } 70 71 public DeleteConstraint(Session session, Table table, 72 Constraint constraint, boolean global) { 73 super(session); 74 this.table = table; 75 this.constraint = constraint; 76 this.global = global; 77 this.tableName = table.getName(); 78 } 79 80 Table getTable(Database db) throws IOException , SQLException { 81 if (table == null) { 82 table = (Table)db.getRelation(tableName); 83 if (table == null) { 84 throw new SQLException ("No table: " + tableName); 85 } 86 } 87 return table; 88 } 89 90 public void redo(Session session) throws IOException , SQLException { 91 Database db = session.getDatabase(); 92 getTable(db); 93 table.deleteConstraint(constraint.getName()); 94 if (constraint.table == null) { 95 constraint.setTable(table); 96 } 97 constraint.delete(session); 98 db.updateRelation(table); 99 if (global || constraint.isGlobal()) { 100 db.deleteIndex(constraint.getName()); 101 } 102 } 103 104 public void undo(Session session) throws IOException , SQLException { 105 Database db = session.getDatabase(); 106 getTable(db); 107 table.addConstraint(constraint); 108 constraint.undoDelete(session); 109 db.updateRelation(table); 110 if (global || constraint.isGlobal()) { 111 db.addIndex(constraint.getName(), table.getName()); 112 } 113 } 114 115 public void prepare(Session session) throws IOException , SQLException { 116 } 117 118 public static void deleteForeign(Session session, Constraint constraint) 119 throws SQLException , IOException  120 { 121 Database db = session.getDatabase(); 122 if (constraint instanceof ImportedKeyConstraint) { 123 ImportedKeyConstraint ic = (ImportedKeyConstraint)constraint; 124 Table f = ic.getFTable(db); 125 ExportedKeyConstraint ec = ic.findExportedKeyConstraint(db); 126 if (ec != null) { 127 session.doStep(new DeleteConstraint(session, f, ec)); 128 } 129 } else if (constraint instanceof ExportedKeyConstraint) { 130 ExportedKeyConstraint ec = (ExportedKeyConstraint)constraint; 131 Table f = ec.getFTable(db); 132 ImportedKeyConstraint ic = ec.getImportedKeyConstraint(db); 133 if (ic != null) { 134 session.doStep(new DeleteConstraint(session, f, ic)); 135 } 136 } 137 138 } 139 140 public void readExternal(ObjectInput in) 141 throws IOException , ClassNotFoundException  142 { 143 super.readExternal(in); 144 this.constraint = (Constraint)in.readObject(); 145 this.tableName = (String )in.readObject(); 146 this.global = in.read() == 1; 147 } 148 149 public void writeExternal(ObjectOutput out) throws IOException { 150 super.writeExternal(out); 151 out.writeObject(constraint); 152 out.writeObject(tableName); 153 out.write(global ? 1 : 0); 154 } 155 156 public String toString() { 158 StringBuffer sb = new StringBuffer (super.toString()); 159 sb.append(" DeleteConstraint("); 160 sb.append(tableName); 161 sb.append(','); 162 sb.append(constraint.toString()); 163 return sb.toString(); 164 } 165 167 static Extern extern; 168 public void setExtern(Extern extern) { DeleteConstraint.extern = extern; } 169 public Extern getExtern() { return extern; } 170 } 171 | Popular Tags |