Debian Codegraph @

src/main/org/h2/expression/function/SignalFunction.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.function;
7
8 import java.util.regex.Pattern;
9
10 import org.h2.engine.SessionLocal;
11 import org.h2.expression.Expression;
12 import org.h2.message.DbException;
13 import org.h2.value.TypeInfo;
14 import org.h2.value.Value;
15
16 /**
17 * A SIGNAL function.
18 */
19 public final class SignalFunction extends Function2 {
20
21 private static final Pattern SIGNAL_PATTERN = Pattern.compile("[0-9A-Z]{5}");
22
23 public SignalFunction(Expression arg1, Expression arg2) {
24 super(arg1, arg2);
25 }
26
27 @Override
28 public Value getValue(SessionLocal session, Value v1, Value v2) {
29 String sqlState = v1.getString();
30 if (sqlState.startsWith("00") || !SIGNAL_PATTERN.matcher(sqlState).matches()) {
31 throw DbException.getInvalidValueException("SQLSTATE", sqlState);
32 }
33 throw DbException.fromUser(sqlState, v2.getString());
34 }
35
36 @Override
37 public Expression optimize(SessionLocal session) {
38 left = left.optimize(session);
39 right = right.optimize(session);
40 type = TypeInfo.TYPE_NULL;
41 return this;
42 }
43
44 @Override
45 public String getName() {
46 return "SIGNAL";
47 }
48
49 }