KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
42 import java.io.Externalizable JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.ObjectOutput JavaDoc;
46
47 import java.util.Vector JavaDoc;
48
49 import java.sql.ResultSet JavaDoc;
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.index.Btree;
53 import com.quadcap.sql.index.BCursor;
54
55 import com.quadcap.util.Debug;
56 import com.quadcap.util.Util;
57
58 /**
59  * A hidden 'ExportedKeyConstraint' is created for tables that
60  * are referenced as foreign keys by other tables.
61  *
62  * @author Stan Bailes
63  */

64 public class ExportedKeyConstraint extends ForeignKeyConstraint
65 implements Externalizable JavaDoc
66 {
67     transient ImportedKeyConstraint iConstraint;
68     transient String JavaDoc iConstraintName;
69
70     String JavaDoc uConstraintName;
71     
72
73     /**
74      * Default constructor
75      */

76     public ExportedKeyConstraint() {}
77
78     /**
79      * Explicit constructor for matching ImportedKeyConstraint.
80      */

81     public ExportedKeyConstraint(String JavaDoc name, Vector JavaDoc colNames,
82                  String JavaDoc fTableName, Vector JavaDoc fColNames,
83                  ImportedKeyConstraint iConstraint,
84                  UniqueConstraint uConstraint)
85         throws SQLException JavaDoc
86     {
87     super(name, colNames, fTableName, fColNames);
88     this.iConstraint = iConstraint;
89     this.iConstraintName = iConstraint.getName();
90     this.uConstraintName = uConstraint.getName();
91         uConstraint.addExportConstraint(this);
92         if (this.colNames == null) {
93             this.colNames = uConstraint.getColumnNames();
94         }
95     }
96
97     public void delete(Session session) throws SQLException JavaDoc, IOException JavaDoc {
98     UniqueConstraint uc =
99         (UniqueConstraint)table.getConstraint(uConstraintName);
100         if (uc != null) {
101             uc.removeExportConstraint(name);
102         }
103     }
104
105     void setForeignKeyCols(int[] fCols) {
106     this.fCols = fCols;
107     }
108     
109     public void checkUpdate(Session session, byte[] oldKey, Row row,
110                             Row oldRow, long rowId, Constraint activeIndex)
111     throws SQLException JavaDoc, IOException JavaDoc
112     {
113     getComparator();
114     Database db = session.getDatabase();
115     byte[] newKey = makeKey(session, row);
116     if (activeIndex != this) oldKey = makeKey(session, oldRow);
117     if (compare.compare(newKey, oldKey) != 0) {
118         ExportedKeys ek = getExportedKeys(session);
119         if (isSelfReferencing(db)) {
120         byte[] oldfKey = makeFKey(session, oldRow);
121         byte[] newfKey = makeFKey(session, row);
122         ek.addSelfRefEntry(oldKey, newKey, oldfKey, newfKey);
123         } else {
124         ek.addEntry(oldKey, newKey);
125         }
126     }
127     }
128     
129     public void checkDelete(Session session, Row row, long rowId)
130     throws SQLException JavaDoc, IOException JavaDoc
131     {
132         byte[] fkey = getImportedKeyConstraint(session.getDatabase()).makeFKey(session, row);
133     if (isSelfReferencing(session.getDatabase())) {
134         ExportedKeys ek = getExportedKeys(session);
135             byte[] key = makeKey(session, row);
136         ek.addDeleteSelfRef(key, fkey);
137     } else {
138         checkKeyRemoval(session, fkey);
139     }
140     }
141
142     public void checkKeyRemoval(Session session, byte[] oldkey)
143     throws SQLException JavaDoc, IOException JavaDoc
144     {
145     getComparator();
146     Database db = session.getDatabase();
147     Btree fTree = getForeignIndex(db);
148     int count = 0;
149         boolean match = false;
150     BCursor cursor = fTree.getCursor(false);
151         try {
152             boolean kvalid = cursor.seek(oldkey);
153             while (!match && (kvalid || cursor.next())) {
154                 byte[] key = cursor.getKey();
155                 int cmp = compare.compare(key, oldkey);
156                 match = cmp == 0;
157                 kvalid = false;
158             }
159         } finally {
160             cursor.release();
161         }
162     if (match) {
163         int refSpec = iConstraint.getRefAction(DELETE);
164         switch (refSpec) {
165         case NOACTION:
166         throw new SQLException JavaDoc(
167             "Foreign key constraint violation: children exist",
168             "23000");
169
170         case CASCADE:
171         ExportedKeys ek = getExportedKeys(session);
172         ek.removeKey(refSpec, oldkey);
173         break;
174         case SETNULL:
175         case SETDEFAULT:
176         // XXX TODO: set null, set default
177
throw new SQLException JavaDoc(
178             "Foreign key constraint violation and SET " +
179             "{NULL|DEFAULT} not implemented", "23000");
180         }
181     }
182     }
183     
184     /**
185      * Utility function to get / lazy-create the session context used
186      * to keep track of key additions and removals generated by the current
187      * statement.
188      */

189     ExportedKeys getExportedKeys(Session session) throws IOException JavaDoc {
190     ExportedKeys ek =
191             (ExportedKeys)session.getContext(this, isDeferred());
192     if (ek == null) {
193         ek = new ExportedKeys(session, this);
194         session.putContext(this, isDeferred(), ek);
195     }
196     return ek;
197     }
198     
199     ImportedKeyConstraint getImportedKeyConstraint(Database db)
200     throws SQLException JavaDoc, IOException JavaDoc
201     {
202     if (iConstraint == null) {
203         getFTable(db);
204         iConstraint =
205         (ImportedKeyConstraint)fTable.getConstraint(iConstraintName);
206     }
207     return iConstraint;
208     }
209
210     Btree getForeignIndex(Database db) throws SQLException JavaDoc, IOException JavaDoc {
211     return getImportedKeyConstraint(db).getIndex(db);
212     }
213
214     public void readExternal(ObjectInput JavaDoc in)
215     throws IOException JavaDoc, ClassNotFoundException JavaDoc
216     {
217     super.readExternal(in);
218     iConstraintName = (String JavaDoc)in.readObject();
219     uConstraintName = (String JavaDoc)in.readObject();
220     }
221     
222     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
223     super.writeExternal(out);
224     out.writeObject(iConstraintName);
225     out.writeObject(uConstraintName);
226     }
227 }
228
Popular Tags