KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ObjectInputStream;
53 import com.quadcap.sql.io.ObjectOutputStream;
54
55 import com.quadcap.sql.index.Btree;
56 import com.quadcap.sql.index.Comparator;
57
58 /**
59  * Abstract base class for imported and exported key constraints.
60  *
61  * @author Stan Bailes
62  */

63 public abstract class ForeignKeyConstraint
64     extends Constraint
65     implements Externalizable JavaDoc
66 {
67     String JavaDoc fTableName;
68     Vector JavaDoc fColNames;
69     Table fTable;
70     UniqueConstraint fConstraint;
71     int[] fCols;
72     transient Comparator compare;
73
74     /**
75      * Default constructor
76      */

77     public ForeignKeyConstraint() {}
78
79     public ForeignKeyConstraint(String JavaDoc name, String JavaDoc fTableName) {
80     super(name);
81     this.fTableName = fTableName;
82     }
83
84     public ForeignKeyConstraint(String JavaDoc name, Vector JavaDoc colNames,
85                 String JavaDoc fTableName, Vector JavaDoc fColNames) {
86     super(name, colNames);
87     this.fTableName = fTableName;
88     this.fColNames = fColNames;
89     }
90     
91     /**
92      * Reset any mapped columns (e.g. in case a column is added or deleted)
93      */

94     public void resetColumns() throws SQLException JavaDoc {
95         super.resetColumns();
96         fCols = null;
97     }
98
99     public void checkInsert(Session session, Row row)
100     throws SQLException JavaDoc, IOException JavaDoc
101     {
102     }
103     
104     public void applyInsert(Session session, Row row, long rowId,
105                 Constraint activeIndex)
106     throws SQLException JavaDoc, IOException JavaDoc
107     {
108     }
109     
110     public void checkUpdate(Session session, Row row, long rowId)
111     throws SQLException JavaDoc, IOException JavaDoc
112     {
113     }
114     
115     public void applyUpdate(Session session, byte[] oldKey, Row row,
116                 Row oldRow, long rowId, Constraint activeIndex)
117     throws SQLException JavaDoc, IOException JavaDoc
118     {
119     }
120
121     public void checkDelete(Session session, Row row, long rowId)
122     throws SQLException JavaDoc, IOException JavaDoc
123     {
124     }
125     
126     public void applyDelete(Session session, Row row, long rowId,
127                             Constraint activeIndex)
128     throws SQLException JavaDoc, IOException JavaDoc
129     {
130     }
131     
132     /**
133      * Is this constraint 'deferred'?
134      */

135     public final boolean isDeferred() {
136         return (spec & INIT_DEFERRED) != 0;
137     }
138
139     final Comparator getComparator() throws SQLException JavaDoc {
140     if (compare == null) compare = new Key(getColumns().length);
141     return compare;
142     }
143
144     /**
145      * Create a candidate key for a row from this table
146      */

147     public byte[] makeKey(Session session, Row row)
148     throws SQLException JavaDoc
149     {
150     return makeKey(row, getColumns());
151     }
152
153     /**
154      * Create a candidate key for a row from the foreign table
155      */

156     public byte[] makeFKey(Session session, Row row)
157     throws IOException JavaDoc, SQLException JavaDoc
158     {
159     return makeKey(row, getFCols(session.getDatabase()));
160     }
161
162     private final byte[] makeKey(Row row, int[] k) throws SQLException JavaDoc {
163         return Key.makeKey(null, row, k, 0, false);
164     }
165
166     public void setForeignColumn(String JavaDoc name) {
167     fColNames = new Vector JavaDoc();
168     fColNames.addElement(name);
169     }
170
171     public void readExternal(ObjectInput JavaDoc in)
172     throws IOException JavaDoc, ClassNotFoundException JavaDoc
173     {
174     super.readExternal(in);
175     fTableName = (String JavaDoc)in.readObject();
176     fColNames = (Vector JavaDoc)in.readObject();
177     }
178     
179     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
180     super.writeExternal(out);
181     out.writeObject(fTableName);
182     out.writeObject(fColNames);
183     }
184
185     public String JavaDoc getFTableName() {
186     return fTableName;
187     }
188
189     public void setFTableName(String JavaDoc s) {
190         fTableName = s;
191     }
192
193     public Table getFTable(Database db) throws SQLException JavaDoc, IOException JavaDoc {
194     if (fTable == null) {
195         Relation t = db.getRelation(fTableName);
196         if (t == null) {
197         throw new SQLException JavaDoc("No such table: " + fTableName,
198                        "42000");
199         }
200         try {
201         fTable = (Table)t;
202         } catch (ClassCastException JavaDoc e) {
203         throw new SQLException JavaDoc("Not a base table: " + fTableName,
204                        "42000");
205         }
206     }
207     return fTable;
208     }
209
210     /**
211      * If you care, remember to 'extend' and embrace.
212      */

213     public void add(Session session) throws SQLException JavaDoc, IOException JavaDoc {}
214     
215     abstract public void delete(Session session)
216     throws SQLException JavaDoc, IOException JavaDoc
217     ;
218
219     /**
220      *
221      */

222     public int[] getFCols(Database db) throws SQLException JavaDoc, IOException JavaDoc {
223     if (fCols == null) {
224         if (fColNames == null) {
225         UniqueConstraint pk = getFTable(db).getPrimaryKey();
226         if (pk == null) {
227             throw new SQLException JavaDoc("No primary key for table " +
228                        fTable.getName());
229         }
230                 fConstraint = pk;
231             fCols = pk.getColumns();
232         } else {
233         fCols = getFTable(db).mapColumns(fColNames);
234                 fConstraint = fTable.getIndexForColumns(fCols);
235         }
236     }
237     return fCols;
238     }
239
240     boolean isSelfReferencing(Database db) throws IOException JavaDoc, SQLException JavaDoc {
241     boolean ret = table == getFTable(db);
242         return ret;
243     }
244
245     public String JavaDoc toString() {
246     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
247     sb.append(" references " + fTableName + "(");
248     if (fColNames != null) {
249         for (int i = 0; i < fColNames.size(); i++) {
250         if (i > 0) sb.append(",");
251         sb.append(fColNames.elementAt(i).toString());
252         }
253     }
254     sb.append(')');
255     return sb.toString();
256     }
257 }
258
Popular Tags