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.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.util.Vector;
00047
00048
import java.sql.SQLException;
00049
00050
import com.quadcap.sql.index.Btree;
00051
00052
import com.quadcap.util.Debug;
00053
00054
00055
00056
00057
00058
00059
00060
00061 public abstract class Constraint implements Externalizable {
00062 transient Table table;
00063 transient int[]
columns;
00064 Vector
colNames =
new Vector();
00065 String
name = null;
00066 int spec = 0;
00067
00068
00069 public static final int FULL = (1 << 0);
00070 public static final int PARTIAL = (1 << 1);
00071
00072
00073 public static final int NOACTION = 0;
00074 public static final int CASCADE = 1;
00075 public static final int SETNULL = 2;
00076 public static final int SETDEFAULT = 3;
00077
00078
00079 public static final int UPDATE = 2;
00080
00081 public static final int DELETE = 4;
00082
00083
00084 public static final int DEFERRABLE = (1 << 6);
00085
00086 public static final int INIT_DEFERRED = (1 << 7);
00087
00088 public static final int GLOBAL = (1 << 8);
00089
00090
00091
00092
00093 public Constraint() {}
00094
00095
00096
00097
00098 public Constraint(String name) {
00099
this.name = name;
00100 }
00101
00102
00103
00104
00105 public Constraint(String name, Vector colNames) {
00106
this.name = name;
00107
this.colNames = colNames;
00108 }
00109
00110
00111
00112
00113 public String
getName() {
00114
return name;
00115 }
00116
00117
00118
00119
00120 public void setName(String name) {
00121
this.name = name;
00122 }
00123
00124
00125
00126
00127 public int getRefAction(
int opType) {
00128
return (
spec >> opType) & 0x3;
00129 }
00130
00131
00132
00133
00134 static String[]
refActions = {
"NO ACTION",
"CASCADE",
"SET NULL",
00135
"SET DEFAULT" };
00136
00137
00138
00139
00140 public String
getRefActionString(
int opType) {
00141
return refActions[getRefAction(opType)];
00142 }
00143
00144
00145
00146
00147 public void setGlobal(
boolean g) {
00148
if (g) {
00149
spec |=
GLOBAL;
00150 }
else {
00151
spec &= ~
GLOBAL;
00152 }
00153 }
00154
00155
00156
00157
00158 public boolean isGlobal() {
return (
spec &
GLOBAL) != 0; }
00159
00160
00161
00162
00163
00164
abstract public void checkInsert(
Session session,
Row row)
00165
throws SQLException, IOException
00166 ;
00167
00168
00169
00170
00171
00172
00173
00174
abstract public void applyInsert(
Session session,
Row row,
long rowId,
00175
Constraint activeIndex)
00176
throws SQLException, IOException
00177 ;
00178
00179
00180
00181
00182
00183
abstract public void checkDelete(
Session session,
Row row,
long rowId)
00184
throws SQLException, IOException
00185 ;
00186
00187
00188
00189
00190
00191
00192
abstract public void applyDelete(
Session session,
Row row,
long rowId,
00193
Constraint activeIndex)
00194
throws SQLException, IOException
00195 ;
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
abstract public void checkUpdate(
Session session, byte[] oldKey,
Row row,
00208
Row oldRow,
long rowId,
00209
Constraint activeIndex)
00210
throws SQLException, IOException
00211 ;
00212
00213
00214
00215
00216
00217
00218
00219
abstract public void applyUpdate(
Session session, byte[] oldKey,
Row row,
00220
Row oldRow,
long rowId,
00221
Constraint activeIndex)
00222
throws SQLException, IOException
00223 ;
00224
00225
00226
00227
00228
00229
00230
abstract public void delete(
Session session)
00231
throws SQLException, IOException
00232 ;
00233
00234
00235
00236
00237
00238
00239
abstract public void add(
Session session)
00240
throws SQLException, IOException;
00241
00242
00243
00244
00245 public void undoAdd(
Session session)
00246
throws SQLException, IOException {
00247
delete(session);
00248 }
00249
00250
00251
00252
00253
00254 public void undoDelete(
Session session)
00255
throws SQLException, IOException
00256 {
00257 add(session);
00258 }
00259
00260
00261
00262
00263
00264 public void setDeferrable(
int def) {
00265
this.spec |= def;
00266 }
00267
00268
00269
00270
00271 public void setRefSpec(
int ref) {
00272
this.spec |= ref;
00273 }
00274
00275
00276
00277
00278 public void setTable(
Table table)
throws SQLException {
00279
this.table =
table;
00280
this.
columns = null;
00281
getColumns();
00282 }
00283
00284
00285
00286
00287 public Table getTable() {
return this.table; }
00288
00289
00290
00291
00292 public int getSpec() {
00293
return spec;
00294 }
00295
00296
00297
00298
00299 public void setColumn(
Column column) {
00300
if (
colNames.size() != 0) {
00301
colNames =
new Vector();
00302 }
00303
colNames.add(column.
getName());
00304
this.columns = null;
00305 }
00306
00307
00308
00309
00310
00311
00312 public Column getColumn() throws SQLException {
00313
getColumns();
00314
if (
columns.length < 1)
throw new SQLException(
"No column",
"Q0000");
00315
if (
columns.length > 1)
00316
throw new SQLException(
"Not a single column constraint: " +
this,
00317
"Q0000");
00318
return table.
getColumn(
columns[0]);
00319 }
00320
00321
00322
00323
00324 public int getColumnCount() {
00325
return colNames.size();
00326 }
00327
00328
00329
00330
00331 public Column getColumn(
int c)
throws SQLException {
00332
return table.
getColumn(
columns[c]);
00333 }
00334
00335
00336
00337
00338 public void readExternal(ObjectInput in)
00339
throws IOException, ClassNotFoundException
00340 {
00341
spec = in.readInt();
00342
name = (String)in.readObject();
00343
colNames = (Vector)in.readObject();
00344
columns = null;
00345 }
00346
00347
00348
00349
00350 public void writeExternal(ObjectOutput out)
throws IOException {
00351 out.writeInt(
spec);
00352 out.writeObject(
name);
00353 out.writeObject(
colNames);
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 public int[]
getColumns() throws SQLException {
00363
if (
columns == null) {
00364
columns =
table.
mapColumns(
colNames);
00365 }
00366
return columns;
00367 }
00368
00369
00370
00371
00372 public void resetColumns() throws SQLException {
00373
columns = null;
00374 }
00375
00376
00377
00378
00379 public Vector
getColumnNames() throws SQLException {
00380
return colNames;
00381 }
00382
00383
00384
00385
00386
00387 public Btree getIndex(
Database db)
throws IOException {
00388
return null;
00389 }
00390
00391
00392
00393
00394 public int getPriority() {
return 3; }
00395
00396
00397
00398
00399 public boolean isDeferred() {
00400
return false;
00401 }
00402
00403
00404
00405
00406 public String
toString() {
00407 StringBuffer sb =
new StringBuffer(
"Constraint ");
00408 sb.append(
name);
00409 sb.append(
": ");
00410
if (
table == null) {
00411 sb.append(
"<null table>");
00412 }
else {
00413 sb.append(
table.
getName());
00414 }
00415 sb.append(
"(");
00416
if (
colNames != null) {
00417
for (
int i = 0; i <
colNames.size(); i++) {
00418
if (i > 0) sb.append(
',');
00419 sb.append(
colNames.elementAt(i).toString());
00420 }
00421 }
00422 sb.append(
")");
00423
if ((
spec &
FULL) != 0) sb.append(
" FULL");
00424
if ((
spec &
PARTIAL) != 0) sb.append(
" PARTIAL");
00425
00426
if (getRefAction(
UPDATE) !=
NOACTION) {
00427 sb.append(
" ON UPDATE ");
00428 sb.append(getRefActionString(
UPDATE));
00429 }
00430
if (getRefAction(
DELETE) !=
NOACTION) {
00431 sb.append(
" ON DELETE ");
00432 sb.append(getRefActionString(
DELETE));
00433 }
00434
if ((
spec &
DEFERRABLE) != 0) sb.append(
" DEFERRABLE");
00435
if ((
spec &
INIT_DEFERRED) != 0) sb.append(
" INITIALLY DEFERRED");
00436
00437
return sb.toString();
00438 }
00439 }