| 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 AlterColumn extends LogStep implements Externalizable { 58 transient Table table; 59 transient Column column; 60 61 String columnName = null; 62 String tableName = null; 63 Expression oldDefault = null; 64 Expression newDefault = null; 65 66 69 public AlterColumn() {} 70 71 74 public AlterColumn(Session session, Table table, 75 String columnName, Expression newDefault) { 76 super(session); 77 this.table = table; 78 this.tableName = table.getName(); 79 this.columnName = columnName; 80 this.newDefault = newDefault; 81 } 82 83 Table getTable(Database db) throws IOException { 84 if (table == null) { 85 table = (Table)db.getRelation(tableName); 86 } 87 return table; 88 } 89 90 public void undo(Session session) throws IOException , SQLException { 91 Database db = session.getDatabase(); 92 getTable(db); 93 column = table.getColumn(columnName); 94 column.setDefault(oldDefault); 95 db.updateRelation(table); 96 } 97 98 public void redo(Session session) throws IOException , SQLException { 99 Database db = session.getDatabase(); 100 getTable(db); 101 column.setDefault(newDefault); 102 db.updateRelation(table); 103 } 104 105 public void prepare(Session session) throws IOException , SQLException { 106 column = table.getColumn(columnName); 107 if (oldDefault == null) { 108 oldDefault = column.getDefault(); 109 } 110 } 111 112 public void readExternal(ObjectInput in) 113 throws IOException , ClassNotFoundException  114 { 115 super.readExternal(in); 116 this.columnName = (String )in.readObject(); 117 this.tableName = (String )in.readObject(); 118 this.oldDefault = (Expression)in.readObject(); 119 this.newDefault = (Expression)in.readObject(); 120 } 121 122 public void writeExternal(ObjectOutput out) throws IOException { 123 super.writeExternal(out); 124 out.writeObject(columnName); 125 out.writeObject(tableName); 126 out.writeObject(oldDefault); 127 out.writeObject(newDefault); 128 } 129 130 public String toString() { 132 StringBuffer sb = new StringBuffer (super.toString()); 133 sb.append(" AlterColumn("); 134 sb.append(tableName); 135 sb.append(','); 136 sb.append(columnName); 137 sb.append(" set default " + newDefault); 138 sb.append(")"); 139 return sb.toString(); 140 } 141 143 static Extern extern = null; 144 public Extern getExtern() { return extern; } 145 public void setExtern(Extern extern) { AlterColumn.extern = extern; } 146 } 147 | Popular Tags |