| 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 AddConstraint extends LogStep implements Externalizable { 58 transient Table table; 59 60 Constraint constraint; 61 String tableName = null; 62 boolean global; 63 64 67 public AddConstraint() {} 68 69 72 public AddConstraint(Session session, Table table, Constraint constraint, 73 boolean global) { 74 super(session); 75 this.table = table; 76 this.constraint = constraint; 77 table.nameConstraint(constraint); 78 this.global = global; 79 this.tableName = table.getName(); 80 } 81 82 85 public AddConstraint(Session session, Table table, Constraint constraint) { 86 this(session, table, constraint, false); 87 } 88 89 92 Table getTable(Database db) throws IOException { 93 if (table == null) { 94 table = (Table)db.getRelation(tableName); 95 } 96 return table; 97 } 98 99 103 public void undo(Session session) throws IOException , SQLException { 104 Database db = session.getDatabase(); 105 getTable(db); 106 constraint = table.getConstraint(constraint.getName()); 107 if (constraint != null) { 111 table.deleteConstraint(constraint.getName()); 112 constraint.undoAdd(session); 113 db.updateRelation(table); 114 db.deleteIndex(constraint.getName()); 115 } 116 } 117 118 122 public void redo(Session session) throws IOException , SQLException { 123 Database db = session.getDatabase(); 124 getTable(db); 125 table.addConstraint(constraint); 126 constraint.add(session); 127 db.updateRelation(table); 128 db.addIndex(constraint.getName(), tableName); 129 } 130 131 134 public void prepare(Session session) throws IOException , SQLException { 135 constraint.setTable(getTable(session.getDatabase())); 136 } 137 138 141 public void readExternal(ObjectInput in) 142 throws IOException , ClassNotFoundException  143 { 144 super.readExternal(in); 145 this.constraint = (Constraint)in.readObject(); 146 this.tableName = (String )in.readObject(); 147 this.global = in.read() == 1; 148 } 149 150 153 public void writeExternal(ObjectOutput out) throws IOException { 154 super.writeExternal(out); 155 out.writeObject(constraint); 156 out.writeObject(tableName); 157 out.write(global ? 1 : 0); 158 } 159 160 163 static Extern extern; 164 public void setExtern(Extern extern) { AddConstraint.extern = extern; } 165 public Extern getExtern() { return extern; } 166 167 public String toString() { 169 StringBuffer sb = new StringBuffer (super.toString()); 170 sb.append(" AddConstraint("); 171 sb.append(tableName); 172 sb.append(','); 173 sb.append(constraint == null ? "null" : constraint.toString()); 174 return sb.toString(); 175 } 176 178 } 179 | Popular Tags |