001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.io.Extern;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * Log step to add a constraint to a table.
054: *
055: * @author Stan Bailes
056: */
057: public class AddConstraint extends LogStep implements Externalizable {
058: transient Table table;
059:
060: Constraint constraint;
061: String tableName = null;
062: boolean global;
063:
064: /**
065: * Default constructor
066: */
067: public AddConstraint() {
068: }
069:
070: /**
071: * Explicit constructor: {table, constraint, global flag}
072: */
073: public AddConstraint(Session session, Table table,
074: Constraint constraint, boolean global) {
075: super (session);
076: this .table = table;
077: this .constraint = constraint;
078: table.nameConstraint(constraint);
079: this .global = global;
080: this .tableName = table.getName();
081: }
082:
083: /**
084: * Constructor for a non-global constraint
085: */
086: public AddConstraint(Session session, Table table,
087: Constraint constraint) {
088: this (session, table, constraint, false);
089: }
090:
091: /**
092: * Lazy table accessor
093: */
094: Table getTable(Database db) throws IOException {
095: if (table == null) {
096: table = (Table) db.getRelation(tableName);
097: }
098: return table;
099: }
100:
101: /**
102: * LogStep.undo(): delete the constraint; perform any constraint-specified
103: * cleanup, and remove the constraint from the table.
104: */
105: public void undo(Session session) throws IOException, SQLException {
106: Database db = session.getDatabase();
107: getTable(db);
108: constraint = table.getConstraint(constraint.getName());
109: // Generally, we will have a constraint here. But if things have
110: // already gone badly, the constraint might not have actually been
111: // added. So don't freak out if you can't find it.
112: if (constraint != null) {
113: table.deleteConstraint(constraint.getName());
114: constraint.undoAdd(session);
115: db.updateRelation(table);
116: db.deleteIndex(constraint.getName());
117: }
118: }
119:
120: /**
121: * LogStep.redo(): add the constrtaint, perform any constraint-specific
122: * initialization, and update the table with the new constraint
123: */
124: public void redo(Session session) throws IOException, SQLException {
125: Database db = session.getDatabase();
126: getTable(db);
127: table.addConstraint(constraint);
128: constraint.add(session);
129: db.updateRelation(table);
130: db.addIndex(constraint.getName(), tableName);
131: }
132:
133: /**
134: * LogStep.prepare(): A good constraint prepares his table, anyway
135: */
136: public void prepare(Session session) throws IOException,
137: SQLException {
138: constraint.setTable(getTable(session.getDatabase()));
139: }
140:
141: /**
142: * Read me from a stream
143: */
144: public void readExternal(ObjectInput in) throws IOException,
145: ClassNotFoundException {
146: super .readExternal(in);
147: this .constraint = (Constraint) in.readObject();
148: this .tableName = (String) in.readObject();
149: this .global = in.read() == 1;
150: }
151:
152: /**
153: * Write me to a stream
154: */
155: public void writeExternal(ObjectOutput out) throws IOException {
156: super .writeExternal(out);
157: out.writeObject(constraint);
158: out.writeObject(tableName);
159: out.write(global ? 1 : 0);
160: }
161:
162: /**
163: * My class's Extern object
164: */
165: static Extern extern;
166:
167: public void setExtern(Extern extern) {
168: AddConstraint.extern = extern;
169: }
170:
171: public Extern getExtern() {
172: return extern;
173: }
174:
175: //#ifdef DEBUG
176: public String toString() {
177: StringBuffer sb = new StringBuffer(super .toString());
178: sb.append(" AddConstraint(");
179: sb.append(tableName);
180: sb.append(',');
181: sb.append(constraint == null ? "null" : constraint.toString());
182: return sb.toString();
183: }
184: //#endif
185:
186: }
|