-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clisqlshell: new infrastructure for describe commands
Release note: None
- Loading branch information
Showing
6 changed files
with
170 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 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 clisqlshell | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
type dkey struct { | ||
prefix string | ||
nargs int | ||
} | ||
|
||
var dcmds = map[dkey]func(bool, bool) string{ | ||
{`l`, 0}: func(p, s bool) string { return `SHOW DATABASES` }, | ||
{`d`, 0}: func(p, s bool) string { return `SHOW TABLES` }, | ||
{`d`, 1}: func(p, s bool) string { return `SHOW COLUMNS FROM $1` }, | ||
{`dT`, 0}: func(p, s bool) string { return `SHOW TYPES` }, | ||
{`dt`, 0}: func(p, s bool) string { return `SHOW TABLES` }, | ||
{`du`, 0}: func(p, s bool) string { return `SHOW USERS` }, | ||
{`du`, 1}: func(p, s bool) string { return `SELECT * FROM [SHOW USERS] WHERE username = $1` }, | ||
{`dd`, 1}: func(p, s bool) string { return `SHOW CONSTRAINTS FROM $1 WITH COMMENT` }, | ||
} | ||
|
||
func (c *cliState) pgInspect(args []string) (sql string, qargs []interface{}, err error) { | ||
origCmd := args[0] | ||
// Strip the leading `\`. | ||
cmd := origCmd[1:] | ||
args = args[1:] | ||
|
||
plus := strings.Contains(cmd, "+") | ||
inclSystem := strings.Contains(cmd, "S") | ||
// Remove the characters "S" and "+" from the describe command. | ||
cmd = strings.TrimRight(cmd, "S+") | ||
|
||
key := dkey{cmd, len(args)} | ||
fn := dcmds[key] | ||
if fn == nil { | ||
return "", nil, errors.WithHint( | ||
errors.Newf("unsupported command: %s with %d arguments", origCmd, len(args)), | ||
"Use the SQL SHOW statement to inspect your schema.") | ||
} | ||
|
||
for _, a := range args { | ||
qargs = append(qargs, a) | ||
} | ||
return fn(plus, inclSystem), qargs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 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 clisqlshell_test | ||
|
||
import "github.com/cockroachdb/cockroach/pkg/cli" | ||
|
||
func Example_describe_unknown() { | ||
c := cli.NewCLITest(cli.TestCLIParams{}) | ||
defer c.Cleanup() | ||
|
||
c.RunWithArgs([]string{`sql`, `-e`, `\set echo`, `-e`, `\dz`}) | ||
|
||
// Output: | ||
// sql -e \set echo -e \dz | ||
// ERROR: unsupported command: \dz with 0 arguments | ||
// HINT: Use the SQL SHOW statement to inspect your schema. | ||
// ERROR: -e: unsupported command: \dz with 0 arguments | ||
// HINT: Use the SQL SHOW statement to inspect your schema. | ||
} | ||
|
||
var describeCmdPrefix = []string{ | ||
`sql`, `-e`, `\set display_format csv`, `-e`, `\set echo`, `-e`, | ||
} | ||
|
||
func Example_describe_l() { | ||
c := cli.NewCLITest(cli.TestCLIParams{}) | ||
defer c.Cleanup() | ||
|
||
c.RunWithArgs([]string{`sql`, `-e`, `create database mydb`}) | ||
c.RunWithArgs(append(describeCmdPrefix, `\l`)) | ||
|
||
// Output: | ||
// sql -e create database mydb | ||
// CREATE DATABASE | ||
// sql -e \set display_format csv -e \set echo -e \l | ||
// > SHOW DATABASES | ||
// database_name,owner,primary_region,secondary_region,regions,survival_goal | ||
// defaultdb,root,NULL,NULL,{},NULL | ||
// mydb,root,NULL,NULL,{},NULL | ||
// postgres,root,NULL,NULL,{},NULL | ||
// system,node,NULL,NULL,{},NULL | ||
} | ||
|
||
func Example_describe_du() { | ||
c := cli.NewCLITest(cli.TestCLIParams{}) | ||
defer c.Cleanup() | ||
|
||
c.RunWithArgs([]string{`sql`, `-e`, `CREATE USER my_user WITH CREATEDB; GRANT admin TO my_user;`}) | ||
c.RunWithArgs(append(describeCmdPrefix, `\du`)) | ||
// Output: | ||
// sql -e CREATE USER my_user WITH CREATEDB; GRANT admin TO my_user; | ||
// CREATE ROLE | ||
// GRANT | ||
// sql -e \set display_format csv -e \set echo -e \du | ||
// > SHOW USERS | ||
// username,options,member_of | ||
// admin,,{} | ||
// my_user,CREATEDB,{admin} | ||
// root,,{admin} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters