1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.Externalizable ; 43 import java.io.IOException ; 44 import java.io.ObjectInput ; 45 import java.io.ObjectOutput ; 46 47 import java.util.Enumeration ; 48 import java.util.Vector ; 49 50 import java.sql.ResultSet ; 51 import java.sql.SQLException ; 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 71 public class UniqueConstraint extends IndexConstraint 72 implements Externalizable 73 { 74 Vector exportConstraints = null; 75 76 79 public UniqueConstraint() {} 80 81 84 public UniqueConstraint(String name) { 85 super(name); 86 } 87 88 91 public UniqueConstraint(String name, Vector names) { 92 super(name, names); 93 } 94 95 102 public void checkInsert(Session session, Row row) 103 throws SQLException , IOException 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 (constraintType() + 110 " constraint violated: " + this, 111 "23000"); 112 } 113 } 114 115 118 public int getIndexColumnCount() { 119 return getColumnCount() + 1; 120 } 121 122 public byte[] makeKey(Session session, Row row, long rowId) 123 throws SQLException 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 constraintType() { return "unique"; } 137 138 public final void addExportConstraint(ExportedKeyConstraint ec) { 139 if (exportConstraints == null) { 140 exportConstraints = new Vector (); 141 } 142 exportConstraints.addElement(ec.getName()); 143 } 144 145 public final Enumeration getExportConstraints() { 146 if (exportConstraints == null) { 147 return new Vector ().elements(); 148 } else { 149 return exportConstraints.elements(); 150 } 151 } 152 153 public final void removeExportConstraint(String name) 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).toString())) { 158 pos = i; 159 } 160 } 161 } 162 if (pos == -1) { 163 throw new SQLException ("removeExportConstraint(" + name + 164 "), not found"); 165 } 166 exportConstraints.removeElementAt(pos); 167 } 168 169 public void readExternal(ObjectInput in) 170 throws IOException , ClassNotFoundException 171 { 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 } 182 | Popular Tags |