001: /*
002: * $Id: CheckConstraint.java,v 1.17 2005/12/20 18:32:46 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.constraints;
042:
043: import org.axiondb.AxionException;
044: import org.axiondb.Database;
045: import org.axiondb.Row;
046: import org.axiondb.RowDecorator;
047: import org.axiondb.RowIterator;
048: import org.axiondb.Selectable;
049: import org.axiondb.Table;
050: import org.axiondb.TableIdentifier;
051: import org.axiondb.engine.visitors.ResolveSelectableVisitor;
052: import org.axiondb.event.RowEvent;
053:
054: /**
055: * A CHECK constraint, which is violated whenever the given
056: * {@link #setCondition condition}is violated.
057: *
058: * @version $Revision: 1.17 $ $Date: 2005/12/20 18:32:46 $
059: * @author Rodney Waldhoff
060: * @author Amrish Lal
061: * @author Jonathan Giron
062: * @author Ahimanikya Satapathy
063: */
064: public class CheckConstraint extends BaseConstraint {
065: public CheckConstraint(String name) {
066: super (name, "CHECK");
067: }
068:
069: public void setCondition(Selectable where) {
070: _condition = where;
071: }
072:
073: public Selectable getCondition() {
074: return _condition;
075: }
076:
077: public void resolve(Database db, TableIdentifier table)
078: throws AxionException {
079: ResolveSelectableVisitor resolveSel = new ResolveSelectableVisitor(
080: db);
081: _condition = resolveSel.visit(_condition, null, toArray(table));
082: }
083:
084: public boolean evaluate(RowEvent event) throws AxionException {
085: return evaluate(event, event.getTable().makeRowDecorator());
086: }
087:
088: public boolean evaluate(RowEvent event, RowDecorator dec)
089: throws AxionException {
090: Row row = event.getNewRow();
091: return ((null == row) ? true : check(row, dec));
092: }
093:
094: public boolean evaluate(RowIterator oldRows, RowIterator newRows,
095: Table table) throws AxionException {
096: if (null == newRows || newRows.isEmpty()) {
097: return true;
098: }
099:
100: RowDecorator dec = table.makeRowDecorator();
101: while (newRows.hasNext()) {
102: if (!check(newRows.next(), dec)) {
103: return false;
104: }
105: }
106: return true;
107: }
108:
109: private boolean check(Row row, RowDecorator dec)
110: throws AxionException {
111: dec.setRow(row);
112:
113: // ISO/IEC 9075-2:2003, Section 4.17 - a constraint is satisfied if and only if the
114: // specified search condition is not FALSE (that is, evaluates to TRUE or null).
115: Boolean result = (Boolean) _condition.evaluate(dec);
116: return (result == null) ? true : result.booleanValue();
117: }
118:
119: private Selectable _condition;
120: private static final long serialVersionUID = 6322188199298311548L;
121: }
|