Debian Codegraph @

src/main/org/h2/expression/condition/BooleanTest.java

raw
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 java.util.ArrayList;
9
10 import org.h2.engine.SessionLocal;
11 import org.h2.expression.Expression;
12 import org.h2.expression.ExpressionColumn;
13 import org.h2.expression.TypedValueExpression;
14 import org.h2.expression.ValueExpression;
15 import org.h2.index.IndexCondition;
16 import org.h2.table.TableFilter;
17 import org.h2.value.Value;
18 import org.h2.value.ValueBoolean;
19 import org.h2.value.ValueNull;
20
21 /**
22 * Boolean test (IS [NOT] { TRUE | FALSE | UNKNOWN }).
23 */
24 public final class BooleanTest extends SimplePredicate {
25
26 private final Boolean right;
27
28 public BooleanTest(Expression left, boolean not, boolean whenOperand, Boolean right) {
29 super(left, not, whenOperand);
30 this.right = right;
31 }
32
33 @Override
34 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
35 return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags);
36 }
37
38 @Override
39 public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) {
40 return builder.append(not ? " IS NOT " : " IS ").append(right == null ? "UNKNOWN" : right ? "TRUE" : "FALSE");
41 }
42
43 @Override
44 public Value getValue(SessionLocal session) {
45 return ValueBoolean.get(getValue(left.getValue(session)));
46 }
47
48 @Override
49 public boolean getWhenValue(SessionLocal session, Value left) {
50 if (!whenOperand) {
51 return super.getWhenValue(session, left);
52 }
53 return getValue(left);
54 }
55
56 private boolean getValue(Value left) {
57 return (left == ValueNull.INSTANCE ? right == null : right != null && right == left.getBoolean()) ^ not;
58 }
59
60 @Override
61 public Expression getNotIfPossible(SessionLocal session) {
62 if (whenOperand) {
63 return null;
64 }
65 return new BooleanTest(left, !not, false, right);
66 }
67
68 @Override
69 public void createIndexConditions(SessionLocal session, TableFilter filter) {
70 if (whenOperand || !filter.getTable().isQueryComparable()) {
71 return;
72 }
73 if (left instanceof ExpressionColumn) {
74 ExpressionColumn c = (ExpressionColumn) left;
75 if (c.getType().getValueType() == Value.BOOLEAN && filter == c.getTableFilter()) {
76 if (not) {
77 if (right == null && c.getColumn().isNullable()) {
78 ArrayList<Expression> list = new ArrayList<>(2);
79 list.add(ValueExpression.FALSE);
80 list.add(ValueExpression.TRUE);
81 filter.addIndexCondition(IndexCondition.getInList(c, list));
82 }
83 } else {
84 filter.addIndexCondition(IndexCondition.get(Comparison.EQUAL_NULL_SAFE, c,
85 right == null ? TypedValueExpression.UNKNOWN : ValueExpression.getBoolean(right)));
86 }
87 }
88 }
89 }
90
91 }