| 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.function; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.expression.ExpressionVisitor; |
| 12 |
import org.h2.expression.Variable; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.value.Value; |
| 15 |
|
| 16 |
/** |
| 17 |
* A SET function. |
| 18 |
*/ |
| 19 |
public final class SetFunction extends Function2 { |
| 20 |
|
| 21 |
public SetFunction(Expression arg1, Expression arg2) { |
| 22 |
super(arg1, arg2); |
| 23 |
} |
| 24 |
|
| 25 |
@Override |
| 26 |
public Value getValue(SessionLocal session) { |
| 27 |
Variable var = (Variable) left; |
| 28 |
Value v = right.getValue(session); |
| 29 |
session.setVariable(var.getName(), v); |
| 30 |
return v; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public Expression optimize(SessionLocal session) { |
| 35 |
left = left.optimize(session); |
| 36 |
right = right.optimize(session); |
| 37 |
type = right.getType(); |
| 38 |
if (!(left instanceof Variable)) { |
| 39 |
throw DbException.get(ErrorCode.CAN_ONLY_ASSIGN_TO_VARIABLE_1, left.getTraceSQL()); |
| 40 |
} |
| 41 |
return this; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public String getName() { |
| 46 |
return "SET"; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 51 |
if (!super.isEverything(visitor)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
switch (visitor.getType()) { |
| 55 |
case ExpressionVisitor.DETERMINISTIC: |
| 56 |
case ExpressionVisitor.QUERY_COMPARABLE: |
| 57 |
case ExpressionVisitor.READONLY: |
| 58 |
return false; |
| 59 |
default: |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
} |