Debian Codegraph @

src/main/org/h2/command/dml/Explain.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.command.dml;
7
8 import java.util.HashSet;
9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.TreeMap;
12 import org.h2.command.CommandInterface;
13 import org.h2.command.Prepared;
14 import org.h2.engine.Database;
15 import org.h2.engine.DbObject;
16 import org.h2.engine.SessionLocal;
17 import org.h2.expression.Expression;
18 import org.h2.expression.ExpressionColumn;
19 import org.h2.mvstore.db.Store;
20 import org.h2.result.LocalResult;
21 import org.h2.result.ResultInterface;
22 import org.h2.table.Column;
23 import org.h2.util.HasSQL;
24 import org.h2.value.TypeInfo;
25 import org.h2.value.ValueVarchar;
26
27 /**
28 * This class represents the statement
29 * EXPLAIN
30 */
31 public class Explain extends Prepared {
32
33 private Prepared command;
34 private LocalResult result;
35 private boolean executeCommand;
36
37 public Explain(SessionLocal session) {
38 super(session);
39 }
40
41 public void setCommand(Prepared command) {
42 this.command = command;
43 }
44
45 public Prepared getCommand() {
46 return command;
47 }
48
49 @Override
50 public void prepare() {
51 command.prepare();
52 }
53
54 public void setExecuteCommand(boolean executeCommand) {
55 this.executeCommand = executeCommand;
56 }
57
58 @Override
59 public ResultInterface queryMeta() {
60 return query(-1);
61 }
62
63 @Override
64 protected void checkParameters() {
65 // Check params only in case of EXPLAIN ANALYZE
66 if (executeCommand) {
67 super.checkParameters();
68 }
69 }
70
71 @Override
72 public ResultInterface query(long maxrows) {
73 Database db = getDatabase();
74 Expression[] expressions = { new ExpressionColumn(db, new Column("PLAN", TypeInfo.TYPE_VARCHAR)) };
75 result = new LocalResult(session, expressions, 1, 1);
76 int sqlFlags = HasSQL.ADD_PLAN_INFORMATION;
77 if (maxrows >= 0) {
78 String plan;
79 if (executeCommand) {
80 Store store = null;
81 if (db.isPersistent()) {
82 store = db.getStore();
83 store.statisticsStart();
84 }
85 if (command.isQuery()) {
86 command.query(maxrows);
87 } else {
88 command.update();
89 }
90 plan = command.getPlanSQL(sqlFlags);
91 Map<String, Integer> statistics = null;
92 if (store != null) {
93 statistics = store.statisticsEnd();
94 }
95 if (statistics != null) {
96 int total = 0;
97 for (Integer value : statistics.values()) {
98 total += value;
99 }
100 if (total > 0) {
101 statistics = new TreeMap<>(statistics);
102 StringBuilder buff = new StringBuilder();
103 if (statistics.size() > 1) {
104 buff.append("total: ").append(total).append('\n');
105 }
106 for (Entry<String, Integer> e : statistics.entrySet()) {
107 int value = e.getValue();
108 int percent = (int) (100L * value / total);
109 buff.append(e.getKey()).append(": ").append(value);
110 if (statistics.size() > 1) {
111 buff.append(" (").append(percent).append("%)");
112 }
113 buff.append('\n');
114 }
115 plan += "\n/*\n" + buff + "*/";
116 }
117 }
118 } else {
119 plan = command.getPlanSQL(sqlFlags);
120 }
121 add(plan);
122 }
123 result.done();
124 return result;
125 }
126
127 private void add(String text) {
128 result.addRow(ValueVarchar.get(text));
129 }
130
131 @Override
132 public boolean isQuery() {
133 return true;
134 }
135
136 @Override
137 public boolean isTransactional() {
138 return true;
139 }
140
141 @Override
142 public boolean isReadOnly() {
143 return command.isReadOnly();
144 }
145
146 @Override
147 public int getType() {
148 return executeCommand ? CommandInterface.EXPLAIN_ANALYZE : CommandInterface.EXPLAIN;
149 }
150
151 @Override
152 public void collectDependencies(HashSet<DbObject> dependencies) {
153 command.collectDependencies(dependencies);
154 }
155
156 }