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.sql.SQLException;
00047
00048
import com.quadcap.sql.types.Value;
00049
import com.quadcap.sql.types.ValueBoolean;
00050
import com.quadcap.sql.types.ValueUnknown;
00051
00052
00053
00054
00055
00056
00057 public class CheckConstraint extends Constraint implements
Externalizable {
00058 Expression expression;
00059
00060 public CheckConstraint() {}
00061
00062 public CheckConstraint(String name,
Expression expression) {
00063 super(name);
00064
this.expression = expression;
00065 }
00066
00067 public void add(
Session session)
throws SQLException {
00068
if (!table.
isUnderConstruction()) {
00069
Cursor c = table.
getCursor(session, null, null);
00070
if (c != null) {
00071
try {
00072
while (c.
next()) {
00073
checkValue(
expression.
getValue(session, c));
00074 }
00075 } finally {
00076 c.
close();
00077 }
00078 }
00079 }
00080 }
00081
00082 public void delete(
Session session) {}
00083 public void undoDelete(
Session session) {}
00084
00085 public void applyInsert(
Session session,
Row row,
long rowId,
00086
Constraint activeIndex)
00087
throws SQLException, IOException
00088 {
00089 }
00090
00091 public void applyDelete(
Session session,
Row row,
long rowId,
00092
Constraint activeIndex)
00093
throws SQLException, IOException
00094 {
00095 }
00096
00097 public final void checkValue(
Value val)
throws SQLException {
00098
if (!(val instanceof
ValueBoolean)) {
00099
if (val instanceof
ValueUnknown)
return;
00100
throw new SQLException(
"Bad check expression: " +
expression +
00101
", value = " + val,
"42000");
00102 }
00103
if (!((
ValueBoolean)val).isTrue()) {
00104
throw new SQLException(
"Check constraint violated: " +
00105
expression,
"23000");
00106 }
00107 }
00108
00109 public void checkInsert(
Session session,
Row row)
00110
throws SQLException, IOException
00111 {
00112
StaticCursor cursor =
new StaticCursor(session, table, row);
00113 cursor.
next();
00114 checkValue(
expression.
getValue(session, cursor));
00115 }
00116
00117 public void checkUpdate(
Session session, byte[] oldKey,
Row row,
00118
Row oldRow,
long rowId,
Constraint activeIndex)
00119
throws SQLException, IOException
00120 {
00121 checkInsert(session, row);
00122 }
00123
00124 public void checkDelete(
Session session,
Row row,
long rowId)
00125
throws SQLException, IOException
00126 {
00127 }
00128
00129 public void applyUpdate(
Session session, byte[] oldKey,
Row row,
00130
Row oldRow,
long rowId,
Constraint activeIndex)
00131
throws SQLException, IOException
00132 {
00133 }
00134
00135 public void readExternal(ObjectInput in)
00136
throws IOException, ClassNotFoundException
00137 {
00138 super.readExternal(in);
00139
expression = (
Expression)in.readObject();
00140 }
00141
00142 public void writeExternal(ObjectOutput out)
throws IOException {
00143 super.writeExternal(out);
00144 out.writeObject(
expression);
00145 }
00146 }