Debian Codegraph @

src/main/org/h2/command/dml/Help.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.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.Reader;
12 import java.nio.charset.StandardCharsets;
13 import java.sql.ResultSet;
14
15 import org.h2.command.CommandInterface;
16 import org.h2.command.Prepared;
17 import org.h2.engine.Database;
18 import org.h2.engine.SessionLocal;
19 import org.h2.expression.Expression;
20 import org.h2.expression.ExpressionColumn;
21 import org.h2.message.DbException;
22 import org.h2.result.LocalResult;
23 import org.h2.result.ResultInterface;
24 import org.h2.table.Column;
25 import org.h2.tools.Csv;
26 import org.h2.util.Utils;
27 import org.h2.value.TypeInfo;
28 import org.h2.value.ValueVarchar;
29
30 /**
31 * This class represents the statement CALL.
32 */
33 public class Help extends Prepared {
34
35 private final String[] conditions;
36
37 private final Expression[] expressions;
38
39 public Help(SessionLocal session, String[] conditions) {
40 super(session);
41 this.conditions = conditions;
42 Database db = getDatabase();
43 expressions = new Expression[] { //
44 new ExpressionColumn(db, new Column("SECTION", TypeInfo.TYPE_VARCHAR)), //
45 new ExpressionColumn(db, new Column("TOPIC", TypeInfo.TYPE_VARCHAR)), //
46 new ExpressionColumn(db, new Column("SYNTAX", TypeInfo.TYPE_VARCHAR)), //
47 new ExpressionColumn(db, new Column("TEXT", TypeInfo.TYPE_VARCHAR)), //
48 };
49 }
50
51 @Override
52 public ResultInterface queryMeta() {
53 LocalResult result = new LocalResult(session, expressions, 4, 4);
54 result.done();
55 return result;
56 }
57
58 @Override
59 public ResultInterface query(long maxrows) {
60 LocalResult result = new LocalResult(session, expressions, 4, 4);
61 try {
62 ResultSet rs = getTable();
63 loop: while (rs.next()) {
64 String topic = rs.getString(2).trim();
65 for (String condition : conditions) {
66 if (!topic.contains(condition)) {
67 continue loop;
68 }
69 }
70 result.addRow(
71 // SECTION
72 ValueVarchar.get(rs.getString(1).trim(), session),
73 // TOPIC
74 ValueVarchar.get(topic, session),
75 // SYNTAX
76 ValueVarchar.get(stripAnnotationsFromSyntax(rs.getString(3)), session),
77 // TEXT
78 ValueVarchar.get(processHelpText(rs.getString(4)), session));
79 }
80 } catch (Exception e) {
81 throw DbException.convert(e);
82 }
83 result.done();
84 return result;
85 }
86
87 /**
88 * Strip out the special annotations we use to help build the railroad/BNF diagrams
89 * @param s to process
90 * @return cleaned text
91 */
92 public static String stripAnnotationsFromSyntax(String s) {
93 // SYNTAX column - Strip out the special annotations we use to
94 // help build the railroad/BNF diagrams.
95 return s.replaceAll("@c@ ", "").replaceAll("@h2@ ", "")
96 .replaceAll("@c@", "").replaceAll("@h2@", "").trim();
97 }
98
99 /**
100 * Sanitize value read from csv file (i.e. help.csv)
101 * @param s text to process
102 * @return text without wrapping quotes and trimmed
103 */
104 public static String processHelpText(String s) {
105 int len = s.length();
106 int end = 0;
107 for (; end < len; end++) {
108 char ch = s.charAt(end);
109 if (ch == '.') {
110 end++;
111 break;
112 }
113 if (ch == '"') {
114 do {
115 end++;
116 } while (end < len && s.charAt(end) != '"');
117 }
118 }
119 s = s.substring(0, end);
120 return s.trim();
121 }
122
123 /**
124 * Returns HELP table.
125 *
126 * @return HELP table with columns SECTION,TOPIC,SYNTAX,TEXT
127 * @throws IOException
128 * on I/O exception
129 */
130 public static ResultSet getTable() throws IOException {
131 Reader reader = new InputStreamReader(new ByteArrayInputStream(Utils.getResource("/org/h2/res/help.csv")),
132 StandardCharsets.UTF_8);
133 Csv csv = new Csv();
134 csv.setLineCommentCharacter('#');
135 return csv.read(reader, null);
136 }
137
138 @Override
139 public boolean isQuery() {
140 return true;
141 }
142
143 @Override
144 public boolean isTransactional() {
145 return true;
146 }
147
148 @Override
149 public boolean isReadOnly() {
150 return true;
151 }
152
153 @Override
154 public int getType() {
155 return CommandInterface.CALL;
156 }
157
158 @Override
159 public boolean isCacheable() {
160 return true;
161 }
162
163 }