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 remove a table constraint.
054: *
055: * @author Stan Bailes
056: */
057: public class DeleteConstraint extends LogStep implements Externalizable {
058: transient Table table;
059:
060: Constraint constraint;
061: String tableName = null;
062: boolean global = false;
063:
064: public DeleteConstraint() {
065: }
066:
067: public DeleteConstraint(Session session, Table table,
068: Constraint constraint) {
069: this (session, table, constraint, false);
070: }
071:
072: public DeleteConstraint(Session session, Table table,
073: Constraint constraint, boolean global) {
074: super (session);
075: this .table = table;
076: this .constraint = constraint;
077: this .global = global;
078: this .tableName = table.getName();
079: }
080:
081: Table getTable(Database db) throws IOException, SQLException {
082: if (table == null) {
083: table = (Table) db.getRelation(tableName);
084: if (table == null) {
085: throw new SQLException("No table: " + tableName);
086: }
087: }
088: return table;
089: }
090:
091: public void redo(Session session) throws IOException, SQLException {
092: Database db = session.getDatabase();
093: getTable(db);
094: table.deleteConstraint(constraint.getName());
095: if (constraint.table == null) {
096: constraint.setTable(table);
097: }
098: constraint.delete(session);
099: db.updateRelation(table);
100: if (global || constraint.isGlobal()) {
101: db.deleteIndex(constraint.getName());
102: }
103: }
104:
105: public void undo(Session session) throws IOException, SQLException {
106: Database db = session.getDatabase();
107: getTable(db);
108: table.addConstraint(constraint);
109: constraint.undoDelete(session);
110: db.updateRelation(table);
111: if (global || constraint.isGlobal()) {
112: db.addIndex(constraint.getName(), table.getName());
113: }
114: }
115:
116: public void prepare(Session session) throws IOException,
117: SQLException {
118: }
119:
120: public static void deleteForeign(Session session,
121: Constraint constraint) throws SQLException, IOException {
122: Database db = session.getDatabase();
123: if (constraint instanceof ImportedKeyConstraint) {
124: ImportedKeyConstraint ic = (ImportedKeyConstraint) constraint;
125: Table f = ic.getFTable(db);
126: ExportedKeyConstraint ec = ic.findExportedKeyConstraint(db);
127: if (ec != null) {
128: session.doStep(new DeleteConstraint(session, f, ec));
129: }
130: } else if (constraint instanceof ExportedKeyConstraint) {
131: ExportedKeyConstraint ec = (ExportedKeyConstraint) constraint;
132: Table f = ec.getFTable(db);
133: ImportedKeyConstraint ic = ec.getImportedKeyConstraint(db);
134: if (ic != null) {
135: session.doStep(new DeleteConstraint(session, f, ic));
136: }
137: }
138:
139: }
140:
141: public void readExternal(ObjectInput in) throws IOException,
142: ClassNotFoundException {
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: //#ifdef DEBUG
157: 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:
166: //#endif
167:
168: static Extern extern;
169:
170: public void setExtern(Extern extern) {
171: DeleteConstraint.extern = extern;
172: }
173:
174: public Extern getExtern() {
175: return extern;
176: }
177: }
|