File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\Explain.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import java.util.Map;
10 import java.util.TreeMap;
11 import java.util.Map.Entry;
12 import org.h2.command.CommandInterface;
13 import org.h2.command.Prepared;
14 import org.h2.engine.Database;
15 import org.h2.engine.Session;
16 import org.h2.expression.Expression;
17 import org.h2.expression.ExpressionColumn;
18 import org.h2.result.LocalResult;
19 import org.h2.result.ResultInterface;
20 import org.h2.store.PageStore;
21 import org.h2.table.Column;
22 import org.h2.value.Value;
23 import org.h2.value.ValueString;
24
25 /**
26 * This class represents the statement
27 * EXPLAIN
28 */
29 public class Explain extends Prepared {
30
31 private Prepared command;
32 private LocalResult result;
33 private boolean executeCommand;
34
35 public Explain(Session session) {
36 super(session);
37 }
38
39 public void setCommand(Prepared command) {
40 this.command = command;
41 }
42
43 public void prepare() {
44 command.prepare();
45 }
46
47 public void setExecuteCommand(boolean executeCommand) {
48 this.executeCommand = executeCommand;
49 }
50
51 public ResultInterface queryMeta() {
52 return query(-1);
53 }
54
55 public ResultInterface query(int maxrows) {
56 Column column = new Column("PLAN", Value.STRING);
57 Database db = session.getDatabase();
58 ExpressionColumn expr = new ExpressionColumn(db, column);
59 Expression[] expressions = { expr };
60 result = new LocalResult(session, expressions, 1);
61 if (maxrows >= 0) {
62 String plan;
63 if (executeCommand) {
64 PageStore store = db.isPersistent() ? db.getPageStore() : null;
65 if (store != null) {
66 store.statisticsStart();
67 }
68 if (command.isQuery()) {
69 command.query(maxrows);
70 } else {
71 command.update();
72 }
73 plan = command.getPlanSQL();
74 Map<String, Integer> statistics = store == null ? null : store.statisticsEnd();
75 if (statistics != null) {
76 int total = 0;
77 for (Entry<String, Integer> e : statistics.entrySet()) {
78 total += e.getValue();
79 }
80 if (total > 0) {
81 statistics = new TreeMap<String, Integer>(statistics);
82 StringBuilder buff = new StringBuilder();
83 buff.append("total: ").append(total).append('\n');
84 for (Entry<String, Integer> e : statistics.entrySet()) {
85 int value = e.getValue();
86 int percent = (int) (100L * value / total);
87 buff.append(e.getKey()).append(": ").append(value).append(" (").append(percent).append("%)\n");
88 }
89 plan += "\n/*\n" + buff.toString() + "*/";
90 }
91 }
92 } else {
93 plan = command.getPlanSQL();
94 }
95 add(plan);
96 }
97 result.done();
98 return result;
99 }
100
101 private void add(String text) {
102 Value[] row = { ValueString.get(text) };
103 result.addRow(row);
104 }
105
106 public boolean isQuery() {
107 return true;
108 }
109
110 public boolean isTransactional() {
111 return true;
112 }
113
114 public boolean isReadOnly() {
115 return command.isReadOnly();
116 }
117
118 public int getType() {
119 return CommandInterface.EXPLAIN;
120 }
121 }
122