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.IOException;
00042
00043
import java.util.Enumeration;
00044
00045
import java.sql.SQLException;
00046
00047
import com.quadcap.sql.index.BCursor;
00048
import com.quadcap.sql.index.Btree;
00049
00050
import com.quadcap.util.Debug;
00051
import com.quadcap.util.Util;
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 public class ExportedKeys implements StatementContext {
00062 Session session;
00063 ExportedKeyConstraint ec;
00064 Btree
index;
00065 BCursor
bc;
00066 byte[]
buf =
new byte[1];
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 static final int valid = 0x22;
00083
00084 public ExportedKeys(
Session session,
ExportedKeyConstraint ec)
00085
throws IOException
00086 {
00087
this.session =
session;
00088
this.ec =
ec;
00089
this.index =
session.
makeTempTree();
00090
this.bc =
index.getCursor(
false);
00091 }
00092
00093 void doKey(byte[] key,
int bit)
throws IOException {
00094
if (
bc.seek(key) &&
bc.getVal(
buf) == 1) {
00095
buf[0] |= bit;
00096
bc.replace(
buf);
00097 }
else {
00098
buf[0] = (byte)bit;
00099
bc.insert(key,
buf);
00100 }
00101 }
00102
00103 public void addDeleteSelfRef(byte[] key, byte[] fkey)
00104
throws IOException, SQLException
00105 {
00106
try {
00107 doKey(key, 1);
00108 doKey(fkey, 4);
00109 } finally {
00110
bc.close();
00111 }
00112 }
00113
00114 public void addEntry(byte[] oldkey, byte[] newkey)
00115
throws IOException, SQLException
00116 {
00117
try {
00118 doKey(oldkey, 1);
00119 doKey(newkey, 2);
00120 } finally {
00121
bc.close();
00122 }
00123 }
00124
00125 public void addSelfRefEntry(byte[] oldkey, byte[] newkey,
00126 byte[] oldfkey, byte[] newfkey)
00127
throws IOException
00128 {
00129
try {
00130 doKey(oldkey, 1);
00131 doKey(newkey, 2);
00132 doKey(oldfkey, 4);
00133 doKey(newfkey, 8);
00134 } finally {
00135
bc.close();
00136 }
00137 }
00138
00139 public void finish(
boolean abort)
throws SQLException, IOException {
00140
try {
00141
if (!abort) {
00142
bc.beforeFirst();
00143
while (
bc.next()) {
00144
if (
bc.getVal(
buf) == 1) {
00145
if ((
valid & (1 <<
buf[0])) != 0) {
00146
ec.
checkKeyRemoval(
session,
bc.getKey());
00147 }
00148 }
00149 }
00150 }
00151 } finally {
00152
try {
00153
if (
bc != null)
bc.release();
00154 } finally {
00155
bc = null;
00156
try {
00157
if (
index != null)
index.free();
00158 } finally {
00159
session.
getDatabase().releaseTempFile();
00160
index = null;
00161
session = null;
00162 }
00163 }
00164 }
00165 }
00166
00167
00168 public int priority() {
return 2; }
00169
00170
00171
00172
00173 public void removeKey(
int refSpec, byte[] key)
throws SQLException {
00174
if (refSpec ==
Constraint.CASCADE) {
00175 }
00176 }
00177 }