00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00068
00069
00070
00071 public class UniqueConstraint extends IndexConstraint
00072 implements
Externalizable
00073 {
00074 Vector
exportConstraints = null;
00075
00076
00077
00078
00079 public UniqueConstraint() {}
00080
00081
00082
00083
00084 public UniqueConstraint(String name) {
00085 super(name);
00086 }
00087
00088
00089
00090
00091 public UniqueConstraint(String name, Vector names) {
00092 super(name, names);
00093 }
00094
00095
00096
00097
00098
00099
00100
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
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 }