| 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.message.DbException; |
| 12 |
import org.h2.table.ColumnResolver; |
| 13 |
import org.h2.table.TableFilter; |
| 14 |
import org.h2.value.Value; |
| 15 |
import org.h2.value.ValueBoolean; |
| 16 |
import org.h2.value.ValueNull; |
| 17 |
|
| 18 |
/** |
| 19 |
* A global condition or combination of local and global conditions. May be used |
| 20 |
* only as a top-level expression in a WHERE, HAVING, or QUALIFY clause of a |
| 21 |
* SELECT. |
| 22 |
*/ |
| 23 |
public class ConditionLocalAndGlobal extends Condition { |
| 24 |
|
| 25 |
private Expression local, global; |
| 26 |
|
| 27 |
public ConditionLocalAndGlobal(Expression local, Expression global) { |
| 28 |
if (global == null) { |
| 29 |
throw DbException.getInternalError(); |
| 30 |
} |
| 31 |
this.local = local; |
| 32 |
this.global = global; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public boolean needParentheses() { |
| 37 |
return local != null || global.needParentheses(); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 42 |
if (local == null) { |
| 43 |
return global.getUnenclosedSQL(builder, sqlFlags); |
| 44 |
} |
| 45 |
local.getSQL(builder, sqlFlags, AUTO_PARENTHESES); |
| 46 |
builder.append("\n _LOCAL_AND_GLOBAL_ "); |
| 47 |
return global.getSQL(builder, sqlFlags, AUTO_PARENTHESES); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 52 |
if (local != null) { |
| 53 |
local.createIndexConditions(session, filter); |
| 54 |
} |
| 55 |
global.createIndexConditions(session, filter); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public Value getValue(SessionLocal session) { |
| 60 |
if (local == null) { |
| 61 |
return global.getValue(session); |
| 62 |
} |
| 63 |
Value l = local.getValue(session), r; |
| 64 |
if (l.isFalse() || (r = global.getValue(session)).isFalse()) { |
| 65 |
return ValueBoolean.FALSE; |
| 66 |
} |
| 67 |
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) { |
| 68 |
return ValueNull.INSTANCE; |
| 69 |
} |
| 70 |
return ValueBoolean.TRUE; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Expression optimize(SessionLocal session) { |
| 75 |
global = global.optimize(session); |
| 76 |
if (local != null) { |
| 77 |
local = local.optimize(session); |
| 78 |
Expression e = ConditionAndOr.optimizeIfConstant(session, ConditionAndOr.AND, local, global); |
| 79 |
if (e != null) { |
| 80 |
return e; |
| 81 |
} |
| 82 |
} |
| 83 |
return this; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void addFilterConditions(TableFilter filter) { |
| 88 |
if (local != null) { |
| 89 |
local.addFilterConditions(filter); |
| 90 |
} |
| 91 |
global.addFilterConditions(filter); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 96 |
if (local != null) { |
| 97 |
local.mapColumns(resolver, level, state); |
| 98 |
} |
| 99 |
global.mapColumns(resolver, level, state); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 104 |
if (local != null) { |
| 105 |
local.setEvaluatable(tableFilter, b); |
| 106 |
} |
| 107 |
global.setEvaluatable(tableFilter, b); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void updateAggregate(SessionLocal session, int stage) { |
| 112 |
if (local != null) { |
| 113 |
local.updateAggregate(session, stage); |
| 114 |
} |
| 115 |
global.updateAggregate(session, stage); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 120 |
return (local == null || local.isEverything(visitor)) && global.isEverything(visitor); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public int getCost() { |
| 125 |
int cost = global.getCost(); |
| 126 |
if (local != null) { |
| 127 |
cost += local.getCost(); |
| 128 |
} |
| 129 |
return cost; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public int getSubexpressionCount() { |
| 134 |
return local == null ? 1 : 2; |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public Expression getSubexpression(int index) { |
| 139 |
switch (index) { |
| 140 |
case 0: |
| 141 |
return local != null ? local : global; |
| 142 |
case 1: |
| 143 |
if (local != null) { |
| 144 |
return global; |
| 145 |
} |
| 146 |
//$FALL-THROUGH$ |
| 147 |
default: |
| 148 |
throw new IndexOutOfBoundsException(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
} |