-
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
7 changed files
with
301 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,59 @@ | ||
// 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/cockroach/pkg/sql/lexbase" | ||
"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]s` }, | ||
{`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 = %[2]s` }, | ||
{`dd`, 1}: func(p, s bool) string { return `SHOW CONSTRAINTS FROM %[1]s WITH COMMENT` }, | ||
} | ||
|
||
func 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, lexbase.EscapeSQLString(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,75 @@ | ||
// 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 ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cli" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/datadriven" | ||
) | ||
|
||
// Example_describe_unknown checks an error path. | ||
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. | ||
} | ||
|
||
func TestDescribe(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
c := cli.NewCLITest(cli.TestCLIParams{T: t}) | ||
defer c.Cleanup() | ||
|
||
db := serverutils.OpenDBConn( | ||
t, c.TestServer.ServingSQLAddr(), "defaultdb", false /* insecure */, c.TestServer.Stopper()) | ||
|
||
var commonArgs []string | ||
|
||
datadriven.RunTest(t, "testdata/describe", func(t *testing.T, td *datadriven.TestData) string { | ||
switch td.Cmd { | ||
case "sql": | ||
_, err := db.Exec(td.Input) | ||
if err != nil { | ||
t.Fatalf("%s: sql error: %v", td.Pos, err) | ||
} | ||
return "ok" | ||
|
||
case "common": | ||
commonArgs = strings.Split(td.Input, "\n") | ||
return "ok" | ||
|
||
case "cli": | ||
args := strings.Split(td.Input, "\n") | ||
out, err := c.RunWithCaptureArgs(append(commonArgs, args...)) | ||
if err != nil { | ||
t.Fatalf("%s: %v", td.Pos, err) | ||
} | ||
return out | ||
|
||
default: | ||
t.Fatalf("%s: unknown command: %q", td.Pos, td.Cmd) | ||
return "" // unreachable | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.