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.ByteArrayOutputStream;
042: import java.io.Externalizable;
043: import java.io.IOException;
044: import java.io.ObjectInput;
045: import java.io.ObjectOutput;
046:
047: import java.util.Enumeration;
048: import java.util.Vector;
049:
050: import java.sql.ResultSet;
051: import java.sql.SQLException;
052:
053: import com.quadcap.sql.types.Value;
054: import com.quadcap.sql.types.ValueInteger;
055:
056: import com.quadcap.sql.io.ObjectInputStream;
057: import com.quadcap.sql.io.ObjectOutputStream;
058:
059: import com.quadcap.sql.index.Btree;
060:
061: import com.quadcap.sql.file.BlockFile;
062:
063: import com.quadcap.util.Debug;
064: import com.quadcap.util.Util;
065:
066: /**
067: * Index constraint for indexes in which keys must be unique.
068: *
069: * @author Stan Bailes
070: */
071: public class UniqueConstraint extends IndexConstraint implements
072: Externalizable {
073: Vector exportConstraints = null;
074:
075: /**
076: * Default constraint (required for Externalizable)
077: */
078: public UniqueConstraint() {
079: }
080:
081: /**
082: * Explicit constructor for named constraint
083: */
084: public UniqueConstraint(String name) {
085: super (name);
086: }
087:
088: /**
089: * Explicit constructor with name and columns
090: */
091: public UniqueConstraint(String name, Vector names) {
092: super (name, names);
093: }
094:
095: /**
096: * Constraint.checkInsert() We make sure the key isn't already in
097: * the index.
098: *
099: * 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, IOException {
104: Database db = session.getDatabase();
105: byte[] key = makeKey(session, row, 0);
106: getIndex(db);
107: if (index.get(key) != null) {
108: throw new SQLException(constraintType()
109: + " constraint violated: " + this , "23000");
110: }
111: }
112:
113: /**
114: * IndexConstraint.Return the number of columns in this index
115: */
116: public int getIndexColumnCount() {
117: return getColumnCount() + 1;
118: }
119:
120: public byte[] makeKey(Session session, Row row, long rowId)
121: throws SQLException {
122: long discrim = 0;
123: int[] k = getColumns();
124: for (int i = 0; i < k.length; i++) {
125: if (Value.isNull(row.item(k[i]))) {
126: discrim = rowId;
127: break;
128: }
129: }
130: return Key.makeKey(table, row, k, discrim, true);
131: }
132:
133: public String constraintType() {
134: return "unique";
135: }
136:
137: public final void addExportConstraint(ExportedKeyConstraint ec) {
138: if (exportConstraints == null) {
139: exportConstraints = new Vector();
140: }
141: exportConstraints.addElement(ec.getName());
142: }
143:
144: public final Enumeration getExportConstraints() {
145: if (exportConstraints == null) {
146: return new Vector().elements();
147: } else {
148: return exportConstraints.elements();
149: }
150: }
151:
152: public final void removeExportConstraint(String name)
153: throws SQLException {
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)
158: .toString())) {
159: pos = i;
160: }
161: }
162: }
163: if (pos == -1) {
164: throw new SQLException("removeExportConstraint(" + name
165: + "), not found");
166: }
167: exportConstraints.removeElementAt(pos);
168: }
169:
170: public void readExternal(ObjectInput in) throws IOException,
171: ClassNotFoundException {
172: super .readExternal(in);
173: exportConstraints = (Vector) in.readObject();
174: }
175:
176: public void writeExternal(ObjectOutput out) throws IOException {
177: super.writeExternal(out);
178: out.writeObject(exportConstraints);
179: }
180:
181: }
|