Debian Codegraph @

src/main/org/h2/expression/Wildcard.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;
7
8 import java.util.ArrayList;
9 import java.util.HashMap;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.engine.SessionLocal;
13 import org.h2.message.DbException;
14 import org.h2.table.Column;
15 import org.h2.table.ColumnResolver;
16 import org.h2.table.TableFilter;
17 import org.h2.util.StringUtils;
18 import org.h2.value.TypeInfo;
19 import org.h2.value.Value;
20
21 /**
22 * A wildcard expression as in SELECT * FROM TEST.
23 * This object is only used temporarily during the parsing phase, and later
24 * replaced by column expressions.
25 */
26 public final class Wildcard extends Expression {
27
28 private final String schema;
29 private final String table;
30
31 private ArrayList<ExpressionColumn> exceptColumns;
32
33 public Wildcard(String schema, String table) {
34 this.schema = schema;
35 this.table = table;
36 }
37
38 public ArrayList<ExpressionColumn> getExceptColumns() {
39 return exceptColumns;
40 }
41
42 public void setExceptColumns(ArrayList<ExpressionColumn> exceptColumns) {
43 this.exceptColumns = exceptColumns;
44 }
45
46 /**
47 * Returns map of excluded table columns to expression columns and validates
48 * that all columns are resolved and not duplicated.
49 *
50 * @return map of excluded table columns to expression columns
51 */
52 public HashMap<Column, ExpressionColumn> mapExceptColumns() {
53 HashMap<Column, ExpressionColumn> exceptTableColumns = new HashMap<>();
54 for (ExpressionColumn ec : exceptColumns) {
55 Column column = ec.getColumn();
56 if (column == null) {
57 throw ec.getColumnException(ErrorCode.COLUMN_NOT_FOUND_1);
58 }
59 if (exceptTableColumns.putIfAbsent(column, ec) != null) {
60 throw ec.getColumnException(ErrorCode.DUPLICATE_COLUMN_NAME_1);
61 }
62 }
63 return exceptTableColumns;
64 }
65
66 @Override
67 public Value getValue(SessionLocal session) {
68 throw DbException.getInternalError(toString());
69 }
70
71 @Override
72 public TypeInfo getType() {
73 throw DbException.getInternalError(toString());
74 }
75
76 @Override
77 public void mapColumns(ColumnResolver resolver, int level, int state) {
78 if (exceptColumns != null) {
79 for (ExpressionColumn column : exceptColumns) {
80 column.mapColumns(resolver, level, state);
81 }
82 }
83 }
84
85 @Override
86 public Expression optimize(SessionLocal session) {
87 throw DbException.get(ErrorCode.SYNTAX_ERROR_1, table);
88 }
89
90 @Override
91 public void setEvaluatable(TableFilter tableFilter, boolean b) {
92 throw DbException.getInternalError(toString());
93 }
94
95 @Override
96 public String getTableAlias() {
97 return table;
98 }
99
100 @Override
101 public String getSchemaName() {
102 return schema;
103 }
104
105 @Override
106 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
107 if (table != null) {
108 StringUtils.quoteIdentifier(builder, table).append('.');
109 }
110 builder.append('*');
111 if (exceptColumns != null) {
112 writeExpressions(builder.append(" EXCEPT ("), exceptColumns, sqlFlags).append(')');
113 }
114 return builder;
115 }
116
117 @Override
118 public void updateAggregate(SessionLocal session, int stage) {
119 throw DbException.getInternalError(toString());
120 }
121
122 @Override
123 public boolean isEverything(ExpressionVisitor visitor) {
124 if (visitor.getType() == ExpressionVisitor.QUERY_COMPARABLE) {
125 return true;
126 }
127 throw DbException.getInternalError(Integer.toString(visitor.getType()));
128 }
129
130 @Override
131 public int getCost() {
132 throw DbException.getInternalError(toString());
133 }
134
135 }