001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.Enumeration;
044:
045: import java.sql.SQLException;
046:
047: import com.quadcap.sql.index.BCursor;
048: import com.quadcap.sql.index.Btree;
049:
050: import com.quadcap.util.Debug;
051: import com.quadcap.util.Util;
052:
053: /**
054: * A statement context which keeps track of the set of changes to the
055: * keys in a foreign-key constraint during the statement execution.
056: * At statement end, we check the entire set to make sure referential
057: * integrity has been maintained.
058: *
059: * @author Stan Bailes
060: */
061: public class ExportedKeys implements StatementContext {
062: Session session;
063: ExportedKeyConstraint ec;
064: Btree index;
065: BCursor bc;
066: byte[] buf = new byte[1];
067:
068: /**
069: * fkey -> key
070: *
071: * 'k' means "insert k"
072: * '~k' means "delete k"
073: *
074: * First case, key deleted which is not referenced:
075: * lsb Encoding of 'ok, ~nk, ~of, ~nf' -> 1
076: *
077: * Second case, key deleted where foreign key is also deleted:
078: * lsb Encoding of 'ok, ~nk, of, ~nf' -> 5
079: *
080: * Encoding of 1, 5: 0x0022
081: */
082: static final int valid = 0x22;
083:
084: public ExportedKeys(Session session, ExportedKeyConstraint ec)
085: throws IOException {
086: this .session = session;
087: this .ec = ec;
088: this .index = session.makeTempTree();
089: this .bc = index.getCursor(false);
090: }
091:
092: void doKey(byte[] key, int bit) throws IOException {
093: if (bc.seek(key) && bc.getVal(buf) == 1) {
094: buf[0] |= bit;
095: bc.replace(buf);
096: } else {
097: buf[0] = (byte) bit;
098: bc.insert(key, buf);
099: }
100: }
101:
102: public void addDeleteSelfRef(byte[] key, byte[] fkey)
103: throws IOException, SQLException {
104: try {
105: doKey(key, 1);
106: doKey(fkey, 4);
107: } finally {
108: bc.close();
109: }
110: }
111:
112: public void addEntry(byte[] oldkey, byte[] newkey)
113: throws IOException, SQLException {
114: try {
115: doKey(oldkey, 1);
116: doKey(newkey, 2);
117: } finally {
118: bc.close();
119: }
120: }
121:
122: public void addSelfRefEntry(byte[] oldkey, byte[] newkey,
123: byte[] oldfkey, byte[] newfkey) throws IOException {
124: try {
125: doKey(oldkey, 1);
126: doKey(newkey, 2);
127: doKey(oldfkey, 4);
128: doKey(newfkey, 8);
129: } finally {
130: bc.close();
131: }
132: }
133:
134: public void finish(boolean abort) throws SQLException, IOException {
135: try {
136: if (!abort) {
137: bc.beforeFirst();
138: while (bc.next()) {
139: if (bc.getVal(buf) == 1) {
140: if ((valid & (1 << buf[0])) != 0) {
141: ec.checkKeyRemoval(session, bc.getKey());
142: }
143: }
144: }
145: }
146: } finally {
147: try {
148: if (bc != null)
149: bc.release();
150: } finally {
151: bc = null;
152: try {
153: if (index != null)
154: index.free();
155: } finally {
156: session.getDatabase().releaseTempFile();
157: index = null;
158: session = null;
159: }
160: }
161: }
162: }
163:
164: // must be after UpdateIndex to properly handle self ref foreign keys
165: public int priority() {
166: return 2;
167: }
168:
169: /**
170: * removal with cascade. NYI. XXX
171: */
172: public void removeKey(int refSpec, byte[] key) throws SQLException {
173: if (refSpec == Constraint.CASCADE) {
174: }
175: }
176: }
|