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.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.types.Value;
049: import com.quadcap.sql.types.ValueBoolean;
050: import com.quadcap.sql.types.ValueUnknown;
051:
052: /**
053: * Constraint class for SQL <b>CHECK</b> constraints.
054: *
055: * @author Stan Bailes
056: */
057: public class CheckConstraint extends Constraint implements
058: Externalizable {
059: Expression expression;
060:
061: public CheckConstraint() {
062: }
063:
064: public CheckConstraint(String name, Expression expression) {
065: super (name);
066: this .expression = expression;
067: }
068:
069: public void add(Session session) throws SQLException {
070: if (!table.isUnderConstruction()) {
071: Cursor c = table.getCursor(session, null, null);
072: if (c != null) {
073: try {
074: while (c.next()) {
075: checkValue(expression.getValue(session, c));
076: }
077: } finally {
078: c.close();
079: }
080: }
081: }
082: }
083:
084: public void delete(Session session) {
085: }
086:
087: public void undoDelete(Session session) {
088: }
089:
090: public void applyInsert(Session session, Row row, long rowId,
091: Constraint activeIndex) throws SQLException, IOException {
092: }
093:
094: public void applyDelete(Session session, Row row, long rowId,
095: Constraint activeIndex) throws SQLException, IOException {
096: }
097:
098: public final void checkValue(Value val) throws SQLException {
099: if (!(val instanceof ValueBoolean)) {
100: if (val instanceof ValueUnknown)
101: return;
102: throw new SQLException("Bad check expression: "
103: + expression + ", value = " + val, "42000");
104: }
105: if (!((ValueBoolean) val).isTrue()) {
106: throw new SQLException("Check constraint violated: "
107: + expression, "23000");
108: }
109: }
110:
111: public void checkInsert(Session session, Row row)
112: throws SQLException, IOException {
113: StaticCursor cursor = new StaticCursor(session, table, row);
114: cursor.next();
115: checkValue(expression.getValue(session, cursor));
116: }
117:
118: public void checkUpdate(Session session, byte[] oldKey, Row row,
119: Row oldRow, long rowId, Constraint activeIndex)
120: throws SQLException, IOException {
121: checkInsert(session, row);
122: }
123:
124: public void checkDelete(Session session, Row row, long rowId)
125: throws SQLException, IOException {
126: }
127:
128: public void applyUpdate(Session session, byte[] oldKey, Row row,
129: Row oldRow, long rowId, Constraint activeIndex)
130: throws SQLException, IOException {
131: }
132:
133: public void readExternal(ObjectInput in) throws IOException,
134: ClassNotFoundException {
135: super .readExternal(in);
136: expression = (Expression) in.readObject();
137: }
138:
139: public void writeExternal(ObjectOutput out) throws IOException {
140: super.writeExternal(out);
141: out.writeObject(expression);
142: }
143: }
|