Debian Codegraph @

src/main/org/h2/expression/function/SysInfoFunction.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.engine.Constants;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.ExpressionVisitor;
11 import org.h2.expression.Operation0;
12 import org.h2.message.DbException;
13 import org.h2.util.Utils;
14 import org.h2.value.TypeInfo;
15 import org.h2.value.Value;
16 import org.h2.value.ValueBigint;
17 import org.h2.value.ValueBoolean;
18 import org.h2.value.ValueInteger;
19 import org.h2.value.ValueNull;
20 import org.h2.value.ValueVarchar;
21
22 /**
23 * Database or session information function.
24 */
25 public final class SysInfoFunction extends Operation0 implements NamedExpression {
26
27 /**
28 * AUTOCOMMIT().
29 */
30 public static final int AUTOCOMMIT = 0;
31
32 /**
33 * DATABASE_PATH().
34 */
35 public static final int DATABASE_PATH = AUTOCOMMIT + 1;
36
37 /**
38 * H2VERSION().
39 */
40 public static final int H2VERSION = DATABASE_PATH + 1;
41
42 /**
43 * LOCK_MODE().
44 */
45 public static final int LOCK_MODE = H2VERSION + 1;
46
47 /**
48 * LOCK_TIMEOUT().
49 */
50 public static final int LOCK_TIMEOUT = LOCK_MODE + 1;
51
52 /**
53 * MEMORY_FREE().
54 */
55 public static final int MEMORY_FREE = LOCK_TIMEOUT + 1;
56
57 /**
58 * MEMORY_USED().
59 */
60 public static final int MEMORY_USED = MEMORY_FREE + 1;
61
62 /**
63 * READONLY().
64 */
65 public static final int READONLY = MEMORY_USED + 1;
66
67 /**
68 * SESSION_ID().
69 */
70 public static final int SESSION_ID = READONLY + 1;
71
72 /**
73 * TRANSACTION_ID().
74 */
75 public static final int TRANSACTION_ID = SESSION_ID + 1;
76
77 private static final int[] TYPES = { Value.BOOLEAN, Value.VARCHAR, Value.VARCHAR, Value.INTEGER, Value.INTEGER,
78 Value.BIGINT, Value.BIGINT, Value.BOOLEAN, Value.INTEGER, Value.VARCHAR };
79
80 private static final String[] NAMES = { "AUTOCOMMIT", "DATABASE_PATH", "H2VERSION", "LOCK_MODE", "LOCK_TIMEOUT",
81 "MEMORY_FREE", "MEMORY_USED", "READONLY", "SESSION_ID", "TRANSACTION_ID" };
82
83 /**
84 * Get the name for this function id.
85 *
86 * @param function
87 * the function id
88 * @return the name
89 */
90 public static String getName(int function) {
91 return NAMES[function];
92 }
93
94 private final int function;
95
96 private final TypeInfo type;
97
98 public SysInfoFunction(int function) {
99 this.function = function;
100 type = TypeInfo.getTypeInfo(TYPES[function]);
101 }
102
103 @Override
104 public Value getValue(SessionLocal session) {
105 Value result;
106 switch (function) {
107 case AUTOCOMMIT:
108 result = ValueBoolean.get(session.getAutoCommit());
109 break;
110 case DATABASE_PATH: {
111 String path = session.getDatabase().getDatabasePath();
112 result = path != null ? ValueVarchar.get(path, session) : ValueNull.INSTANCE;
113 break;
114 }
115 case H2VERSION:
116 result = ValueVarchar.get(Constants.VERSION, session);
117 break;
118 case LOCK_MODE:
119 result = ValueInteger.get(session.getDatabase().getLockMode());
120 break;
121 case LOCK_TIMEOUT:
122 result = ValueInteger.get(session.getLockTimeout());
123 break;
124 case MEMORY_FREE:
125 session.getUser().checkAdmin();
126 result = ValueBigint.get(Utils.getMemoryFree());
127 break;
128 case MEMORY_USED:
129 session.getUser().checkAdmin();
130 result = ValueBigint.get(Utils.getMemoryUsed());
131 break;
132 case READONLY:
133 result = ValueBoolean.get(session.getDatabase().isReadOnly());
134 break;
135 case SESSION_ID:
136 result = ValueInteger.get(session.getId());
137 break;
138 case TRANSACTION_ID:
139 result = session.getTransactionId();
140 break;
141 default:
142 throw DbException.getInternalError("function=" + function);
143 }
144 return result;
145 }
146
147 @Override
148 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
149 return builder.append(getName()).append("()");
150 }
151
152 @Override
153 public boolean isEverything(ExpressionVisitor visitor) {
154 switch (visitor.getType()) {
155 case ExpressionVisitor.DETERMINISTIC:
156 return false;
157 }
158 return true;
159 }
160
161 @Override
162 public TypeInfo getType() {
163 return type;
164 }
165
166 @Override
167 public int getCost() {
168 return 1;
169 }
170
171 @Override
172 public String getName() {
173 return NAMES[function];
174 }
175
176 }