001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.expression;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.constant.SysProperties;
011: import org.h2.engine.Session;
012: import org.h2.table.ColumnResolver;
013: import org.h2.table.TableFilter;
014: import org.h2.value.Value;
015: import org.h2.value.ValueNull;
016:
017: /**
018: * A NOT condition.
019: */
020: public class ConditionNot extends Condition {
021:
022: private Expression condition;
023:
024: public ConditionNot(Expression condition) {
025: this .condition = condition;
026: }
027:
028: public Expression getNotIfPossible(Session session) {
029: return condition;
030: }
031:
032: public Value getValue(Session session) throws SQLException {
033: Value v = condition.getValue(session);
034: if (v == ValueNull.INSTANCE) {
035: return v;
036: }
037: return v.convertTo(Value.BOOLEAN).negate();
038: }
039:
040: public void mapColumns(ColumnResolver resolver, int level)
041: throws SQLException {
042: condition.mapColumns(resolver, level);
043: }
044:
045: public Expression optimize(Session session) throws SQLException {
046: if (!SysProperties.OPTIMIZE_NOT) {
047: condition = condition.optimize(session);
048: return this ;
049: }
050: Expression e2 = condition.getNotIfPossible(session);
051: if (e2 != null) {
052: return e2.optimize(session);
053: }
054: Expression expr = condition.optimize(session);
055: if (expr.isConstant()) {
056: Value v = expr.getValue(session);
057: if (v == ValueNull.INSTANCE) {
058: return ValueExpression.NULL;
059: }
060: return ValueExpression.get(v.convertTo(Value.BOOLEAN)
061: .negate());
062: }
063: condition = expr;
064: return this ;
065: }
066:
067: public void setEvaluatable(TableFilter tableFilter, boolean b) {
068: condition.setEvaluatable(tableFilter, b);
069: }
070:
071: public String getSQL() {
072: return "(NOT " + condition.getSQL() + ")";
073: }
074:
075: public void updateAggregate(Session session) throws SQLException {
076: condition.updateAggregate(session);
077: }
078:
079: public void addFilterConditions(TableFilter filter,
080: boolean outerJoin) {
081: if (outerJoin) {
082: // can not optimize:
083: // select * from test t1 left join test t2 on t1.id = t2.id where
084: // not t2.id is not null
085: // to
086: // select * from test t1 left join test t2 on t1.id = t2.id and
087: // t2.id is not null
088: return;
089: }
090: super .addFilterConditions(filter, outerJoin);
091: }
092:
093: public boolean isEverything(ExpressionVisitor visitor) {
094: return condition.isEverything(visitor);
095: }
096:
097: public int getCost() {
098: return condition.getCost();
099: }
100:
101: }
|