Quadcap Embeddable Database

com/quadcap/sql/UniqueConstraint.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.ByteArrayOutputStream; 00042 import java.io.Externalizable; 00043 import java.io.IOException; 00044 import java.io.ObjectInput; 00045 import java.io.ObjectOutput; 00046 00047 import java.util.Enumeration; 00048 import java.util.Vector; 00049 00050 import java.sql.ResultSet; 00051 import java.sql.SQLException; 00052 00053 import com.quadcap.sql.types.Value; 00054 import com.quadcap.sql.types.ValueInteger; 00055 00056 import com.quadcap.sql.io.ObjectInputStream; 00057 import com.quadcap.sql.io.ObjectOutputStream; 00058 00059 import com.quadcap.sql.index.Btree; 00060 00061 import com.quadcap.sql.file.BlockFile; 00062 00063 import com.quadcap.util.Debug; 00064 import com.quadcap.util.Util; 00065 00066 /** 00067 * Index constraint for indexes in which keys must be unique. 00068 * 00069 * @author Stan Bailes 00070 */ 00071 public class UniqueConstraint extends IndexConstraint 00072 implements Externalizable 00073 { 00074 Vector exportConstraints = null; 00075 00076 /** 00077 * Default constraint (required for Externalizable) 00078 */ 00079 public UniqueConstraint() {} 00080 00081 /** 00082 * Explicit constructor for named constraint 00083 */ 00084 public UniqueConstraint(String name) { 00085 super(name); 00086 } 00087 00088 /** 00089 * Explicit constructor with name and columns 00090 */ 00091 public UniqueConstraint(String name, Vector names) { 00092 super(name, names); 00093 } 00094 00095 /** 00096 * Constraint.checkInsert() We make sure the key isn't already in 00097 * the index. 00098 * 00099 * XXX This implementation could be optimized if we could roll this up 00100 * XXX in the apply step 00101 */ 00102 public void checkInsert(Session session, Row row) 00103 throws SQLException, IOException 00104 { 00105 Database db = session.getDatabase(); 00106 byte[] key = makeKey(session, row, 0); 00107 getIndex(db); 00108 if (index.get(key) != null) { 00109 throw new SQLException(constraintType() + 00110 " constraint violated: " + this, 00111 "23000"); 00112 } 00113 } 00114 00115 /** 00116 * IndexConstraint.Return the number of columns in this index 00117 */ 00118 public int getIndexColumnCount() { 00119 return getColumnCount() + 1; 00120 } 00121 00122 public byte[] makeKey(Session session, Row row, long rowId) 00123 throws SQLException 00124 { 00125 long discrim = 0; 00126 int[] k = getColumns(); 00127 for (int i = 0; i < k.length; i++) { 00128 if (Value.isNull(row.item(k[i]))) { 00129 discrim = rowId; 00130 break; 00131 } 00132 } 00133 return Key.makeKey(table, row, k, discrim, true); 00134 } 00135 00136 public String constraintType() { return "unique"; } 00137 00138 public final void addExportConstraint(ExportedKeyConstraint ec) { 00139 if (exportConstraints == null) { 00140 exportConstraints = new Vector(); 00141 } 00142 exportConstraints.addElement(ec.getName()); 00143 } 00144 00145 public final Enumeration getExportConstraints() { 00146 if (exportConstraints == null) { 00147 return new Vector().elements(); 00148 } else { 00149 return exportConstraints.elements(); 00150 } 00151 } 00152 00153 public final void removeExportConstraint(String name) throws SQLException { 00154 int pos = -1; 00155 if (exportConstraints != null) { 00156 for (int i = 0; i < exportConstraints.size() && pos < 0; i++) { 00157 if (name.equals(exportConstraints.elementAt(i).toString())) { 00158 pos = i; 00159 } 00160 } 00161 } 00162 if (pos == -1) { 00163 throw new SQLException("removeExportConstraint(" + name + 00164 "), not found"); 00165 } 00166 exportConstraints.removeElementAt(pos); 00167 } 00168 00169 public void readExternal(ObjectInput in) 00170 throws IOException, ClassNotFoundException 00171 { 00172 super.readExternal(in); 00173 exportConstraints = (Vector)in.readObject(); 00174 } 00175 00176 public void writeExternal(ObjectOutput out) throws IOException { 00177 super.writeExternal(out); 00178 out.writeObject(exportConstraints); 00179 } 00180 00181 }