github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/delegate/show_syntax.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package delegate 12 13 import ( 14 "bytes" 15 "context" 16 "fmt" 17 18 "github.com/cockroachdb/cockroach/pkg/sql/lex" 19 "github.com/cockroachdb/cockroach/pkg/sql/parser" 20 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 21 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 22 ) 23 24 // delegateShowSyntax implements SHOW SYNTAX. This statement is usually handled 25 // as a special case in Executor, but for FROM [SHOW SYNTAX ...] we will arrive 26 // here too. 27 func (d *delegator) delegateShowSyntax(n *tree.ShowSyntax) (tree.Statement, error) { 28 // Construct an equivalent SELECT query that produces the results: 29 // 30 // SELECT @1 AS field, @2 AS message 31 // FROM (VALUES 32 // ('file', 'foo.go'), 33 // ('line', '123'), 34 // ('function', 'blix()'), 35 // ('detail', 'some details'), 36 // ('hint', 'some hints')) 37 // 38 var query bytes.Buffer 39 fmt.Fprintf( 40 &query, "SELECT @1 AS %s, @2 AS %s FROM (VALUES ", 41 sqlbase.ShowSyntaxColumns[0].Name, sqlbase.ShowSyntaxColumns[1].Name, 42 ) 43 44 comma := "" 45 // TODO(knz): in the call below, reportErr is nil although we might 46 // want to be able to capture (and report) these errors as well. 47 // 48 // However, this code path is only used when SHOW SYNTAX is used as 49 // a data source, i.e. a client actively uses a query of the form 50 // SELECT ... FROM [SHOW SYNTAX ' ... '] WHERE .... This is not 51 // what `cockroach sql` does: the SQL shell issues a straight `SHOW 52 // SYNTAX` that goes through the "statement observer" code 53 // path. Since we care mainly about what users do in the SQL shell, 54 // it's OK if we only deal with that case well for now and, for the 55 // time being, forget/ignore errors when SHOW SYNTAX is used as data 56 // source. This can be added later if deemed useful or necessary. 57 parser.RunShowSyntax( 58 d.ctx, n.Statement, 59 func(ctx context.Context, field, msg string) { 60 fmt.Fprintf(&query, "%s('%s', ", comma, field) 61 lex.EncodeSQLString(&query, msg) 62 query.WriteByte(')') 63 comma = ", " 64 }, 65 nil, /* reportErr */ 66 ) 67 query.WriteByte(')') 68 return parse(query.String()) 69 }