Debian Codegraph @

src/main/org/h2/table/MaterializedView.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.table;
7
8 import java.util.ArrayList;
9 import java.util.HashSet;
10
11 import org.h2.command.query.AllColumnsForPlan;
12 import org.h2.command.query.Query;
13 import org.h2.engine.DbObject;
14 import org.h2.engine.SessionLocal;
15 import org.h2.index.Index;
16 import org.h2.index.IndexType;
17 import org.h2.message.DbException;
18 import org.h2.result.Row;
19 import org.h2.result.SortOrder;
20 import org.h2.schema.Schema;
21 import org.h2.util.StringUtils;
22
23 /**
24 * A materialized view.
25 */
26 public class MaterializedView extends Table {
27
28 private Table table;
29 private String querySQL;
30 private Query query;
31
32 public MaterializedView(Schema schema, int id, String name, Table table, Query query, String querySQL) {
33 super(schema, id, name, false, true);
34 this.table = table;
35 this.query = query;
36 this.querySQL = querySQL;
37 }
38
39 public void replace(Table table, Query query, String querySQL) {
40 this.table = table;
41 this.query = query;
42 this.querySQL = querySQL;
43 }
44
45 public Table getUnderlyingTable() {
46 return table;
47 }
48
49 public Query getSelect() {
50 return query;
51 }
52
53 @Override
54 public final void close(SessionLocal session) {
55 table.close(session);
56 }
57
58 @Override
59 public final Index addIndex(SessionLocal session, String indexName, int indexId, IndexColumn[] cols,
60 int uniqueColumnCount, IndexType indexType, boolean create, String indexComment) {
61 return table.addIndex(session, indexName, indexId, cols, uniqueColumnCount, indexType, create, indexComment);
62 }
63
64 @Override
65 public final boolean isView() {
66 return true;
67 }
68
69 @Override
70 public final PlanItem getBestPlanItem(SessionLocal session, int[] masks, TableFilter[] filters, int filter,
71 SortOrder sortOrder, AllColumnsForPlan allColumnsSet) {
72 return table.getBestPlanItem(session, masks, filters, filter, sortOrder, allColumnsSet);
73 }
74
75 @Override
76 public boolean isQueryComparable() {
77 return table.isQueryComparable();
78 }
79
80 @Override
81 public final boolean isInsertable() {
82 return false;
83 }
84
85 @Override
86 public final void removeRow(SessionLocal session, Row row) {
87 throw DbException.getUnsupportedException(getClass().getSimpleName() + ".removeRow");
88 }
89
90 @Override
91 public final void addRow(SessionLocal session, Row row) {
92 throw DbException.getUnsupportedException(getClass().getSimpleName() + ".addRow");
93 }
94
95 @Override
96 public final void checkSupportAlter() {
97 throw DbException.getUnsupportedException(getClass().getSimpleName() + ".checkSupportAlter");
98 }
99
100 @Override
101 public final long truncate(SessionLocal session) {
102 throw DbException.getUnsupportedException(getClass().getSimpleName() + ".truncate");
103 }
104
105 @Override
106 public final long getRowCount(SessionLocal session) {
107 return table.getRowCount(session);
108 }
109
110 @Override
111 public final boolean canGetRowCount(SessionLocal session) {
112 return table.canGetRowCount(session);
113 }
114
115 @Override
116 public final long getRowCountApproximation(SessionLocal session) {
117 return table.getRowCountApproximation(session);
118 }
119
120 @Override
121 public final boolean canReference() {
122 return false;
123 }
124
125 @Override
126 public final ArrayList<Index> getIndexes() {
127 return table.getIndexes();
128 }
129
130 @Override
131 public final Index getScanIndex(SessionLocal session) {
132 return getBestPlanItem(session, null, null, -1, null, null).getIndex();
133 }
134
135 @Override
136 public Index getScanIndex(SessionLocal session, int[] masks, TableFilter[] filters, int filter, //
137 SortOrder sortOrder, AllColumnsForPlan allColumnsSet) {
138 return getBestPlanItem(session, masks, filters, filter, sortOrder, allColumnsSet).getIndex();
139 }
140
141 @Override
142 public boolean isDeterministic() {
143 return table.isDeterministic();
144 }
145
146 @Override
147 public final void addDependencies(HashSet<DbObject> dependencies) {
148 table.addDependencies(dependencies);
149 }
150
151 @Override
152 public String getDropSQL() {
153 return getSQL(new StringBuilder("DROP MATERIALIZED VIEW IF EXISTS "), DEFAULT_SQL_FLAGS).toString();
154 }
155
156 @Override
157 public String getCreateSQLForCopy(Table table, String quotedName) {
158 return getCreateSQL(false, true, quotedName);
159 }
160
161 @Override
162 public String getCreateSQL() {
163 return getCreateSQL(false, true);
164 }
165
166 /**
167 * Generate "CREATE" SQL statement for the materialized view.
168 *
169 * @param orReplace
170 * if true, then include the OR REPLACE clause
171 * @param force
172 * if true, then include the FORCE clause
173 * @return the SQL statement
174 */
175 public String getCreateSQL(boolean orReplace, boolean force) {
176 return getCreateSQL(orReplace, force, getSQL(DEFAULT_SQL_FLAGS));
177 }
178
179 private String getCreateSQL(boolean orReplace, boolean force, String quotedName) {
180 StringBuilder builder = new StringBuilder("CREATE ");
181 if (orReplace) {
182 builder.append("OR REPLACE ");
183 }
184 if (force) {
185 builder.append("FORCE ");
186 }
187 builder.append("MATERIALIZED VIEW ");
188 builder.append(quotedName);
189 if (comment != null) {
190 builder.append(" COMMENT ");
191 StringUtils.quoteStringSQL(builder, comment);
192 }
193 return builder.append(" AS\n").append(querySQL).toString();
194 }
195
196 @Override
197 public boolean canDrop() {
198 return true;
199 }
200
201 @Override
202 public TableType getTableType() {
203 return TableType.MATERIALIZED_VIEW;
204 }
205
206 @Override
207 public void removeChildrenAndResources(SessionLocal session) {
208 table.removeChildrenAndResources(session);
209 database.removeMeta(session, getId());
210 querySQL = null;
211 invalidate();
212 }
213
214 @Override
215 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
216 if (isTemporary() && querySQL != null) {
217 builder.append("(\n");
218 return StringUtils.indent(builder, querySQL, 4, true).append(')');
219 }
220 return super.getSQL(builder, sqlFlags);
221 }
222
223 public String getQuerySQL() {
224 return querySQL;
225 }
226
227 @Override
228 public long getMaxDataModificationId() {
229 return table.getMaxDataModificationId();
230 }
231
232 }