00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.sql.SQLException;
00047
00048
import com.quadcap.sql.io.Extern;
00049
00050
import com.quadcap.util.Debug;
00051
00052
00053
00054
00055
00056
00057 public class AddConstraint extends LogStep implements
Externalizable {
00058 transient Table table;
00059
00060 Constraint constraint;
00061 String
tableName = null;
00062 boolean global;
00063
00064
00065
00066
00067 public AddConstraint() {}
00068
00069
00070
00071
00072 public AddConstraint(
Session session,
Table table,
Constraint constraint,
00073
boolean global) {
00074 super(session);
00075
this.table = table;
00076
this.constraint = constraint;
00077 table.
nameConstraint(constraint);
00078
this.global = global;
00079
this.tableName = table.
getName();
00080 }
00081
00082
00083
00084
00085 public AddConstraint(
Session session,
Table table,
Constraint constraint) {
00086
this(session, table, constraint,
false);
00087 }
00088
00089
00090
00091
00092 Table getTable(
Database db)
throws IOException {
00093
if (
table == null) {
00094
table = (
Table)db.getRelation(
tableName);
00095 }
00096
return table;
00097 }
00098
00099
00100
00101
00102
00103 public void undo(
Session session)
throws IOException, SQLException {
00104
Database db = session.getDatabase();
00105 getTable(db);
00106
constraint =
table.
getConstraint(
constraint.
getName());
00107
00108
00109
00110
if (
constraint != null) {
00111
table.
deleteConstraint(
constraint.
getName());
00112
constraint.
undoAdd(session);
00113 db.
updateRelation(
table);
00114 db.
deleteIndex(
constraint.
getName());
00115 }
00116 }
00117
00118
00119
00120
00121
00122 public void redo(
Session session)
throws IOException, SQLException {
00123
Database db = session.getDatabase();
00124 getTable(db);
00125
table.
addConstraint(
constraint);
00126
constraint.
add(session);
00127 db.
updateRelation(
table);
00128 db.
addIndex(
constraint.
getName(),
tableName);
00129 }
00130
00131
00132
00133
00134 public void prepare(
Session session)
throws IOException, SQLException {
00135
constraint.
setTable(getTable(session.getDatabase()));
00136 }
00137
00138
00139
00140
00141 public void readExternal(ObjectInput in)
00142
throws IOException, ClassNotFoundException
00143 {
00144 super.readExternal(in);
00145
this.constraint = (
Constraint)in.readObject();
00146
this.tableName = (String)in.readObject();
00147
this.global = in.read() == 1;
00148 }
00149
00150
00151
00152
00153 public void writeExternal(ObjectOutput out)
throws IOException {
00154 super.writeExternal(out);
00155 out.writeObject(
constraint);
00156 out.writeObject(
tableName);
00157 out.write(
global ? 1 : 0);
00158 }
00159
00160
00161
00162
00163 static Extern
extern;
00164 public void setExtern(Extern
extern) {
AddConstraint.extern =
extern; }
00165 public Extern
getExtern() {
return extern; }
00166
00167
00168 public String
toString() {
00169 StringBuffer sb =
new StringBuffer(super.toString());
00170 sb.append(
" AddConstraint(");
00171 sb.append(
tableName);
00172 sb.append(
',');
00173 sb.append(
constraint == null ?
"null" :
constraint.
toString());
00174
return sb.toString();
00175 }
00176
00177
00178 }