/
help.go
237 lines (216 loc) · 7.11 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package parser
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"github.com/ruiaylin/pgparser/pgwire/pgcode"
"github.com/ruiaylin/pgparser/pgwire/pgerror"
"github.com/ruiaylin/pgparser/ast"
"github.com/ruiaylin/pgparser/sessiondata"
"github.com/cockroachdb/errors"
)
// HelpMessage describes a contextual help message.
type HelpMessage struct {
// Command is set if the message is about a statement.
Command string
// Function is set if the message is about a built-in function.
Function string
// HelpMessageBody contains the details of the message.
HelpMessageBody
}
// String implements the fmt.String interface.
func (h *HelpMessage) String() string {
var buf bytes.Buffer
buf.WriteString("help:\n")
h.Format(&buf)
return buf.String()
}
// Format prints out details about the message onto the specified output stream.
func (h *HelpMessage) Format(w io.Writer) {
if h.Command != "" {
fmt.Fprintf(w, "Command: %s\n", h.Command)
}
if h.Function != "" {
fmt.Fprintf(w, "Function: %s\n", h.Function)
}
if h.ShortDescription != "" {
fmt.Fprintf(w, "Description: %s\n", h.ShortDescription)
}
if h.Category != "" {
fmt.Fprintf(w, "Category: %s\n", h.Category)
}
if h.Command != "" {
fmt.Fprintln(w, "Syntax:")
}
fmt.Fprintln(w, strings.TrimSpace(h.Text))
if h.SeeAlso != "" {
fmt.Fprintf(w, "\nSee also:\n %s\n", h.SeeAlso)
}
}
// helpWith is to be used in parser actions to mark the parser "in
// error", with the error set to a contextual help message about the
// current statement.
func helpWith(sqllex sqlLexer, helpText string) int {
scan := sqllex.(*lexer)
if helpText == "" {
scan.lastError = pgerror.WithCandidateCode(errors.New("help upon syntax error"), pgcode.Syntax)
scan.populateHelpMsg("help:\n" + AllHelp)
return 1
}
msg := HelpMessage{Command: helpText, HelpMessageBody: HelpMessages[helpText]}
scan.SetHelp(msg)
// We return non-zero to indicate to the caller of Parse() that the
// parse was unsuccessful.
return 1
}
// helpWithFunction is to be used in parser actions to mark the parser
// "in error", with the error set to a contextual help message about
// the current built-in function.
func helpWithFunction(sqllex sqlLexer, f ast.ResolvableFunctionReference) int {
d, err := f.Resolve(sessiondata.SearchPath{})
if err != nil {
return 1
}
msg := HelpMessage{
Function: f.String(),
HelpMessageBody: HelpMessageBody{
Category: d.Category,
SeeAlso: "base.DocsURL(\"functions-and-operators.html\")",
},
}
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)
// Each function definition contains one or more overloads. We need
// to extract them all; moreover each overload may have a different
// documentation, so we need to also combine the descriptions
// together.
lastInfo := ""
for i, overload := range d.Definition {
b := overload.(*ast.Overload)
if b.Info != "" && b.Info != lastInfo {
if i > 0 {
fmt.Fprintln(w, "---")
}
fmt.Fprintf(w, "\n%s\n\n", b.Info)
fmt.Fprintln(w, "Signature")
}
lastInfo = b.Info
simplifyRet := d.Class == ast.GeneratorClass
fmt.Fprintf(w, "%s%s\n", d.Name, b.Signature(simplifyRet))
}
_ = w.Flush()
msg.Text = buf.String()
sqllex.(*lexer).SetHelp(msg)
return 1
}
func helpWithFunctionByName(sqllex sqlLexer, s string) int {
un := &ast.UnresolvedName{NumParts: 1, Parts: ast.NameParts{s}}
return helpWithFunction(sqllex, ast.ResolvableFunctionReference{FunctionReference: un})
}
const (
hGroup = ""
hDDL = "schema manipulation"
hDML = "data manipulation"
hTxn = "transaction control"
hPriv = "privileges and security"
hMisc = "miscellaneous"
hCfg = "configuration"
hExperimental = "experimental"
hCCL = "enterprise features"
)
// HelpMessageBody defines the body of a help text. The messages are
// structured to facilitate future help navigation functionality.
type HelpMessageBody struct {
Category string
ShortDescription string
Text string
SeeAlso string
}
// HelpMessages is the registry of all help messages, keyed by the
// top-level statement that they document. The key is intended for use
// via the \h client-side command.
var HelpMessages = func(h map[string]HelpMessageBody) map[string]HelpMessageBody {
appendSeeAlso := func(newItem, prevItems string) string {
// "See also" items start with no indentation, and then use two
// space indentation from the 2nd item onward.
if prevItems != "" {
return newItem + "\n " + prevItems
}
return newItem
}
reformatSeeAlso := func(seeAlso string) string {
return strings.Replace(
strings.Replace(seeAlso, ", ", "\n ", -1),
"WEBDOCS", "base.DocsURLBase", -1)
}
srcMsg := h["<SOURCE>"]
srcMsg.SeeAlso = reformatSeeAlso(strings.TrimSpace(srcMsg.SeeAlso))
selectMsg := h["<SELECTCLAUSE>"]
selectMsg.SeeAlso = reformatSeeAlso(strings.TrimSpace(selectMsg.SeeAlso))
for k, m := range h {
m = h[k]
m.ShortDescription = strings.TrimSpace(m.ShortDescription)
m.Text = strings.TrimSpace(m.Text)
m.SeeAlso = strings.TrimSpace(m.SeeAlso)
// If the description contains <source>, append the <source> help.
if strings.Contains(m.Text, "<source>") && k != "<SOURCE>" {
m.Text = strings.TrimSpace(m.Text) + "\n\n" + strings.TrimSpace(srcMsg.Text)
m.SeeAlso = appendSeeAlso(srcMsg.SeeAlso, m.SeeAlso)
}
// Ditto for <selectclause>.
if strings.Contains(m.Text, "<selectclause>") && k != "<SELECTCLAUSE>" {
m.Text = strings.TrimSpace(m.Text) + "\n\n" + strings.TrimSpace(selectMsg.Text)
m.SeeAlso = appendSeeAlso(selectMsg.SeeAlso, m.SeeAlso)
}
// If the description contains <tablename>, mention SHOW TABLES in "See Also".
if strings.Contains(m.Text, "<tablename>") {
m.SeeAlso = appendSeeAlso("SHOW TABLES", m.SeeAlso)
}
m.SeeAlso = reformatSeeAlso(m.SeeAlso)
h[k] = m
}
return h
}(helpMessages)
// AllHelp contains an overview of all statements with help messages.
// For example, displayed in the CLI shell with \h without additional parameters.
var AllHelp = func(h map[string]HelpMessageBody) string {
// Aggregate the help items.
cmds := make(map[string][]string)
for c, details := range h {
if details.Category == "" {
continue
}
cmds[details.Category] = append(cmds[details.Category], c)
}
// Ensure the result is deterministic.
var categories []string
for c, l := range cmds {
categories = append(categories, c)
sort.Strings(l)
}
sort.Strings(categories)
// Compile the final help index.
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)
for _, cat := range categories {
fmt.Fprintf(w, "%s:\n", strings.Title(cat))
for _, item := range cmds[cat] {
fmt.Fprintf(w, "\t\t%s\t%s\n", item, h[item].ShortDescription)
}
fmt.Fprintln(w)
}
_ = w.Flush()
return buf.String()
}(helpMessages)