Quadcap Embeddable Database

com/quadcap/sql/CheckConstraint.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * Constraint class for SQL <b>CHECK</b> constraints. 00054 * 00055 * @author Stan Bailes 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 }