| 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; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.util.ParserUtil; |
| 10 |
import org.h2.value.TypeInfo; |
| 11 |
import org.h2.value.Value; |
| 12 |
|
| 13 |
/** |
| 14 |
* A user-defined variable, for example: @ID. |
| 15 |
*/ |
| 16 |
public final class Variable extends Operation0 { |
| 17 |
|
| 18 |
private final String name; |
| 19 |
private Value lastValue; |
| 20 |
|
| 21 |
public Variable(SessionLocal session, String name) { |
| 22 |
this.name = name; |
| 23 |
lastValue = session.getVariable(name); |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public int getCost() { |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 33 |
return ParserUtil.quoteIdentifier(builder.append('@'), name, sqlFlags); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public TypeInfo getType() { |
| 38 |
return lastValue.getType(); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public Value getValue(SessionLocal session) { |
| 43 |
lastValue = session.getVariable(name); |
| 44 |
return lastValue; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 49 |
switch (visitor.getType()) { |
| 50 |
case ExpressionVisitor.DETERMINISTIC: |
| 51 |
return false; |
| 52 |
default: |
| 53 |
return true; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public String getName() { |
| 58 |
return name; |
| 59 |
} |
| 60 |
|
| 61 |
} |