KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > DeleteConstraint


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.sql.SQLException JavaDoc;
47
48 import com.quadcap.sql.io.Extern;
49
50 import com.quadcap.util.Debug;
51
52 /**
53  * Log step to remove a table constraint.
54  *
55  * @author Stan Bailes
56  */

57 public class DeleteConstraint extends LogStep implements Externalizable JavaDoc {
58     transient Table table;
59
60     Constraint constraint;
61     String JavaDoc tableName = null;
62     boolean global = false;
63
64     public DeleteConstraint() {}
65     
66     public DeleteConstraint(Session session,
67                             Table table, Constraint constraint) {
68     this(session, table, constraint, false);
69     }
70
71     public DeleteConstraint(Session session, Table table,
72                             Constraint constraint, boolean global) {
73         super(session);
74     this.table = table;
75     this.constraint = constraint;
76     this.global = global;
77     this.tableName = table.getName();
78     }
79
80     Table getTable(Database db) throws IOException JavaDoc, SQLException JavaDoc {
81     if (table == null) {
82         table = (Table)db.getRelation(tableName);
83         if (table == null) {
84         throw new SQLException JavaDoc("No table: " + tableName);
85         }
86     }
87     return table;
88     }
89     
90     public void redo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
91     Database db = session.getDatabase();
92     getTable(db);
93     table.deleteConstraint(constraint.getName());
94         if (constraint.table == null) {
95             constraint.setTable(table);
96         }
97     constraint.delete(session);
98     db.updateRelation(table);
99     if (global || constraint.isGlobal()) {
100             db.deleteIndex(constraint.getName());
101         }
102     }
103
104     public void undo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
105     Database db = session.getDatabase();
106     getTable(db);
107     table.addConstraint(constraint);
108         constraint.undoDelete(session);
109     db.updateRelation(table);
110     if (global || constraint.isGlobal()) {
111             db.addIndex(constraint.getName(), table.getName());
112         }
113     }
114
115     public void prepare(Session session) throws IOException JavaDoc, SQLException JavaDoc {
116     }
117
118     public static void deleteForeign(Session session, Constraint constraint)
119     throws SQLException JavaDoc, IOException JavaDoc
120     {
121     Database db = session.getDatabase();
122     if (constraint instanceof ImportedKeyConstraint) {
123         ImportedKeyConstraint ic = (ImportedKeyConstraint)constraint;
124         Table f = ic.getFTable(db);
125         ExportedKeyConstraint ec = ic.findExportedKeyConstraint(db);
126             if (ec != null) {
127                 session.doStep(new DeleteConstraint(session, f, ec));
128             }
129     } else if (constraint instanceof ExportedKeyConstraint) {
130         ExportedKeyConstraint ec = (ExportedKeyConstraint)constraint;
131         Table f = ec.getFTable(db);
132         ImportedKeyConstraint ic = ec.getImportedKeyConstraint(db);
133             if (ic != null) {
134                 session.doStep(new DeleteConstraint(session, f, ic));
135             }
136     }
137     
138     }
139     
140     public void readExternal(ObjectInput JavaDoc in)
141     throws IOException JavaDoc, ClassNotFoundException JavaDoc
142     {
143     super.readExternal(in);
144     this.constraint = (Constraint)in.readObject();
145     this.tableName = (String JavaDoc)in.readObject();
146     this.global = in.read() == 1;
147     }
148     
149     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
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 JavaDoc toString() {
158     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
159         sb.append(" DeleteConstraint(");
160     sb.append(tableName);
161     sb.append(',');
162     sb.append(constraint.toString());
163     return sb.toString();
164     }
165     //#endif
166

167     static Extern extern;
168     public void setExtern(Extern extern) { DeleteConstraint.extern = extern; }
169     public Extern getExtern() { return extern; }
170 }
171
Popular Tags