01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.dml;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.command.Prepared;
11: import org.h2.engine.Session;
12: import org.h2.expression.ExpressionColumn;
13: import org.h2.result.LocalResult;
14: import org.h2.table.Column;
15: import org.h2.util.ObjectArray;
16: import org.h2.value.Value;
17: import org.h2.value.ValueString;
18:
19: /**
20: * This class represents the statement
21: * EXPLAIN
22: */
23: public class ExplainPlan extends Prepared {
24:
25: private Prepared command;
26: private LocalResult result;
27:
28: public ExplainPlan(Session session) {
29: super (session);
30: }
31:
32: public void setCommand(Prepared command) {
33: this .command = command;
34: }
35:
36: public void prepare() throws SQLException {
37: command.prepare();
38: }
39:
40: public LocalResult queryMeta() throws SQLException {
41: return query(-1);
42: }
43:
44: public LocalResult query(int maxrows) throws SQLException {
45: // TODO rights: are rights required for explain?
46: ObjectArray expressions = new ObjectArray();
47: Column column = new Column("PLAN", Value.STRING);
48: ExpressionColumn expr = new ExpressionColumn(session
49: .getDatabase(), column);
50: expressions.add(expr);
51: result = new LocalResult(session, expressions, 1);
52: if (maxrows >= 0) {
53: String plan = command.getPlanSQL();
54: add(plan);
55: }
56: result.done();
57: return result;
58: }
59:
60: private void add(String text) throws SQLException {
61: Value[] row = new Value[1];
62: Value value = ValueString.get(text);
63: row[0] = value;
64: result.addRow(row);
65: }
66:
67: public boolean isQuery() {
68: return true;
69: }
70:
71: public boolean isTransactional() {
72: return true;
73: }
74:
75: public boolean isReadOnly() {
76: return true;
77: }
78: }
|