| 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.value.TypeInfo; |
| 10 |
import org.h2.value.Value; |
| 11 |
import org.h2.value.ValueNull; |
| 12 |
|
| 13 |
/** |
| 14 |
* Unary operation. Only negation operation is currently supported. |
| 15 |
*/ |
| 16 |
public class UnaryOperation extends Operation1 { |
| 17 |
|
| 18 |
public UnaryOperation(Expression arg) { |
| 19 |
super(arg); |
| 20 |
} |
| 21 |
|
| 22 |
@Override |
| 23 |
public boolean needParentheses() { |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
@Override |
| 28 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 29 |
// don't remove the space, otherwise it might end up some thing like |
| 30 |
// --1 which is a line remark |
| 31 |
return arg.getSQL(builder.append("- "), sqlFlags, AUTO_PARENTHESES); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public Value getValue(SessionLocal session) { |
| 36 |
Value a = arg.getValue(session).convertTo(type, session); |
| 37 |
return a == ValueNull.INSTANCE ? a : a.negate(); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public Expression optimize(SessionLocal session) { |
| 42 |
arg = arg.optimize(session); |
| 43 |
type = arg.getType(); |
| 44 |
if (type.getValueType() == Value.UNKNOWN) { |
| 45 |
type = TypeInfo.TYPE_NUMERIC_FLOATING_POINT; |
| 46 |
} else if (type.getValueType() == Value.ENUM) { |
| 47 |
type = TypeInfo.TYPE_INTEGER; |
| 48 |
} |
| 49 |
if (arg.isConstant()) { |
| 50 |
return ValueExpression.get(getValue(session)); |
| 51 |
} |
| 52 |
return this; |
| 53 |
} |
| 54 |
|
| 55 |
} |