Debian Codegraph @

src/main/org/h2/mode/Regclass.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.mode;
7
8 import org.h2.api.ErrorCode;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.Operation1;
12 import org.h2.expression.ValueExpression;
13 import org.h2.index.Index;
14 import org.h2.message.DbException;
15 import org.h2.schema.Schema;
16 import org.h2.table.Table;
17 import org.h2.value.TypeInfo;
18 import org.h2.value.Value;
19 import org.h2.value.ValueInteger;
20 import org.h2.value.ValueNull;
21
22 /**
23 * A ::regclass expression.
24 */
25 public final class Regclass extends Operation1 {
26
27 public Regclass(Expression arg) {
28 super(arg);
29 }
30
31 @Override
32 public Value getValue(SessionLocal session) {
33 Value value = arg.getValue(session);
34 if (value == ValueNull.INSTANCE) {
35 return ValueNull.INSTANCE;
36 }
37 int valueType = value.getValueType();
38 if (valueType >= Value.TINYINT && valueType <= Value.INTEGER) {
39 return value.convertToInt(null);
40 }
41 if (valueType == Value.BIGINT) {
42 return ValueInteger.get((int) value.getLong());
43 }
44 String name = value.getString();
45 for (Schema schema : session.getDatabase().getAllSchemas()) {
46 Table table = schema.findTableOrView(session, name);
47 if (table != null && !table.isHidden()) {
48 return ValueInteger.get(table.getId());
49 }
50 Index index = schema.findIndex(session, name);
51 if (index != null && index.getCreateSQL() != null) {
52 return ValueInteger.get(index.getId());
53 }
54 }
55 throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, name);
56 }
57
58 @Override
59 public TypeInfo getType() {
60 return TypeInfo.TYPE_INTEGER;
61 }
62
63 @Override
64 public Expression optimize(SessionLocal session) {
65 arg = arg.optimize(session);
66 if (arg.isConstant()) {
67 return ValueExpression.get(getValue(session));
68 }
69 return this;
70 }
71
72 @Override
73 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
74 return arg.getSQL(builder, sqlFlags, AUTO_PARENTHESES).append("::REGCLASS");
75 }
76
77 @Override
78 public int getCost() {
79 return arg.getCost() + 100;
80 }
81
82 }