Debian Codegraph @

src/main/org/h2/expression/function/SessionControlFunction.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 org.h2.command.Command;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.ExpressionVisitor;
12 import org.h2.message.DbException;
13 import org.h2.value.TypeInfo;
14 import org.h2.value.Value;
15 import org.h2.value.ValueBoolean;
16 import org.h2.value.ValueNull;
17
18 /**
19 * An ABORT_SESSION() or CANCEL_SESSION() function.
20 */
21 public final class SessionControlFunction extends Function1 {
22
23 /**
24 * ABORT_SESSION().
25 */
26 public static final int ABORT_SESSION = 0;
27
28 /**
29 * CANCEL_SESSION().
30 */
31 public static final int CANCEL_SESSION = ABORT_SESSION + 1;
32
33 private static final String[] NAMES = { //
34 "ABORT_SESSION", "CANCEL_SESSION" //
35 };
36
37 private final int function;
38
39 public SessionControlFunction(Expression arg, int function) {
40 super(arg);
41 this.function = function;
42 }
43
44 @Override
45 public Value getValue(SessionLocal session) {
46 Value v = arg.getValue(session);
47 if (v == ValueNull.INSTANCE) {
48 return ValueNull.INSTANCE;
49 }
50 int targetSessionId = v.getInt();
51 session.getUser().checkAdmin();
52 loop: for (SessionLocal s : session.getDatabase().getSessions(false)) {
53 if (s.getId() == targetSessionId) {
54 Command c = s.getCurrentCommand();
55 switch (function) {
56 case ABORT_SESSION:
57 if (c != null) {
58 c.cancel();
59 }
60 s.close();
61 return ValueBoolean.TRUE;
62 case CANCEL_SESSION:
63 if (c != null) {
64 c.cancel();
65 return ValueBoolean.TRUE;
66 }
67 break loop;
68 default:
69 throw DbException.getInternalError("function=" + function);
70 }
71 }
72 }
73 return ValueBoolean.FALSE;
74 }
75
76 @Override
77 public Expression optimize(SessionLocal session) {
78 arg = arg.optimize(session);
79 type = TypeInfo.TYPE_BOOLEAN;
80 return this;
81 }
82
83 @Override
84 public boolean isEverything(ExpressionVisitor visitor) {
85 switch (visitor.getType()) {
86 case ExpressionVisitor.DETERMINISTIC:
87 case ExpressionVisitor.READONLY:
88 case ExpressionVisitor.QUERY_COMPARABLE:
89 return false;
90 }
91 return super.isEverything(visitor);
92 }
93
94 @Override
95 public String getName() {
96 return NAMES[function];
97 }
98
99 }