| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.condition; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.expression.ExpressionVisitor; |
| 11 |
import org.h2.expression.TypedValueExpression; |
| 12 |
import org.h2.expression.ValueExpression; |
| 13 |
import org.h2.table.ColumnResolver; |
| 14 |
import org.h2.table.TableFilter; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueNull; |
| 17 |
|
| 18 |
/** |
| 19 |
* A NOT condition. |
| 20 |
*/ |
| 21 |
public class ConditionNot extends Condition { |
| 22 |
|
| 23 |
private Expression condition; |
| 24 |
|
| 25 |
public ConditionNot(Expression condition) { |
| 26 |
this.condition = condition; |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public Expression getNotIfPossible(SessionLocal session) { |
| 31 |
return castToBoolean(session, condition.optimize(session)); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public Value getValue(SessionLocal session) { |
| 36 |
Value v = condition.getValue(session); |
| 37 |
if (v == ValueNull.INSTANCE) { |
| 38 |
return v; |
| 39 |
} |
| 40 |
return v.convertToBoolean().negate(); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 45 |
condition.mapColumns(resolver, level, state); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Expression optimize(SessionLocal session) { |
| 50 |
Expression e2 = condition.getNotIfPossible(session); |
| 51 |
if (e2 != null) { |
| 52 |
return e2.optimize(session); |
| 53 |
} |
| 54 |
Expression expr = condition.optimize(session); |
| 55 |
if (expr.isConstant()) { |
| 56 |
Value v = expr.getValue(session); |
| 57 |
if (v == ValueNull.INSTANCE) { |
| 58 |
return TypedValueExpression.UNKNOWN; |
| 59 |
} |
| 60 |
return ValueExpression.getBoolean(!v.getBoolean()); |
| 61 |
} |
| 62 |
condition = expr; |
| 63 |
return this; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 68 |
condition.setEvaluatable(tableFilter, b); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean needParentheses() { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 78 |
return condition.getSQL(builder.append("NOT "), sqlFlags, AUTO_PARENTHESES); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void updateAggregate(SessionLocal session, int stage) { |
| 83 |
condition.updateAggregate(session, stage); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 88 |
return condition.isEverything(visitor); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public int getCost() { |
| 93 |
return condition.getCost(); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public int getSubexpressionCount() { |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Expression getSubexpression(int index) { |
| 103 |
if (index == 0) { |
| 104 |
return condition; |
| 105 |
} |
| 106 |
throw new IndexOutOfBoundsException(); |
| 107 |
} |
| 108 |
|
| 109 |
} |