KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
48 import java.util.Vector JavaDoc;
49
50 import java.sql.ResultSet JavaDoc;
51 import java.sql.SQLException JavaDoc;
52
53 import com.quadcap.sql.types.Value;
54 import com.quadcap.sql.types.ValueInteger;
55
56 import com.quadcap.sql.io.ObjectInputStream;
57 import com.quadcap.sql.io.ObjectOutputStream;
58
59 import com.quadcap.sql.index.Btree;
60
61 import com.quadcap.sql.file.BlockFile;
62
63 import com.quadcap.util.Debug;
64 import com.quadcap.util.Util;
65
66 /**
67  * Index constraint for indexes in which keys must be unique.
68  *
69  * @author Stan Bailes
70  */

71 public class UniqueConstraint extends IndexConstraint
72                               implements Externalizable JavaDoc
73 {
74     Vector JavaDoc exportConstraints = null;
75
76     /**
77      * Default constraint (required for Externalizable)
78      */

79     public UniqueConstraint() {}
80
81     /**
82      * Explicit constructor for named constraint
83      */

84     public UniqueConstraint(String JavaDoc name) {
85     super(name);
86     }
87
88     /**
89      * Explicit constructor with name and columns
90      */

91     public UniqueConstraint(String JavaDoc name, Vector JavaDoc names) {
92     super(name, names);
93     }
94
95     /**
96      * Constraint.checkInsert() We make sure the key isn't already in
97      * the index.
98      *
99      * XXX This implementation could be optimized if we could roll this up
100      * XXX in the apply step
101      */

102     public void checkInsert(Session session, Row row)
103     throws SQLException JavaDoc, IOException JavaDoc
104     {
105     Database db = session.getDatabase();
106     byte[] key = makeKey(session, row, 0);
107     getIndex(db);
108     if (index.get(key) != null) {
109         throw new SQLException JavaDoc(constraintType() +
110                                    " constraint violated: " + this,
111                    "23000");
112     }
113     }
114
115     /**
116      * IndexConstraint.Return the number of columns in this index
117      */

118     public int getIndexColumnCount() {
119     return getColumnCount() + 1;
120     }
121
122     public byte[] makeKey(Session session, Row row, long rowId)
123     throws SQLException JavaDoc
124     {
125     long discrim = 0;
126     int[] k = getColumns();
127     for (int i = 0; i < k.length; i++) {
128         if (Value.isNull(row.item(k[i]))) {
129         discrim = rowId;
130         break;
131         }
132     }
133     return Key.makeKey(table, row, k, discrim, true);
134     }
135
136     public String JavaDoc constraintType() { return "unique"; }
137
138     public final void addExportConstraint(ExportedKeyConstraint ec) {
139     if (exportConstraints == null) {
140         exportConstraints = new Vector JavaDoc();
141     }
142     exportConstraints.addElement(ec.getName());
143     }
144
145     public final Enumeration JavaDoc getExportConstraints() {
146     if (exportConstraints == null) {
147         return new Vector JavaDoc().elements();
148     } else {
149         return exportConstraints.elements();
150     }
151     }
152
153     public final void removeExportConstraint(String JavaDoc name) throws SQLException JavaDoc {
154     int pos = -1;
155     if (exportConstraints != null) {
156         for (int i = 0; i < exportConstraints.size() && pos < 0; i++) {
157         if (name.equals(exportConstraints.elementAt(i).toString())) {
158             pos = i;
159         }
160         }
161     }
162     if (pos == -1) {
163         throw new SQLException JavaDoc("removeExportConstraint(" + name +
164                    "), not found");
165     }
166     exportConstraints.removeElementAt(pos);
167     }
168
169     public void readExternal(ObjectInput JavaDoc in)
170     throws IOException JavaDoc, ClassNotFoundException JavaDoc
171     {
172     super.readExternal(in);
173     exportConstraints = (Vector JavaDoc)in.readObject();
174     }
175     
176     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
177     super.writeExternal(out);
178     out.writeObject(exportConstraints);
179     }
180
181 }
182
Popular Tags