From 10aab583c985546b835749db5f64a5a9b561887c Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Tue, 21 Mar 2023 19:21:40 -0500 Subject: [PATCH] fbsql disconnect from database with `\c -` (#2338) * fbsql disconnect from database with `\c -` This adds the ability to disconnect from the current database by passing a hyphen to the `\c` meta-command. * Update cli/cli.go Co-authored-by: Matthew Jaffee --------- Co-authored-by: Matthew Jaffee --- cli/cli.go | 7 ++++++- cli/testdata/database | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cli/cli.go b/cli/cli.go index 5f902be87..40906cfd0 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -487,7 +487,12 @@ func (cmd *Command) connectToDatabase(dbName string) error { p = newNopPrinter() } - if dbName == "" { + // Providing a blank ("") or hyphen ("-") dbName is the equivalent of + // disconnecting from the current database. We support the hyphen option + // because calling the `\c` meta-command without an argument is how you + // print the current connection. + switch dbName { + case "-", "": cmd.databaseID = "" cmd.databaseName = "" p.Printf(cmd.connectionMessage()) diff --git a/cli/testdata/database b/cli/testdata/database index 65f59cefa..10f6cfd9f 100644 --- a/cli/testdata/database +++ b/cli/testdata/database @@ -43,3 +43,11 @@ EXPECT:executing meta command: meta command 'connect' takes zero or one argument // Connect to a database. SEND:\c db1 EXPECTCOMP:WithFormat:You are now connected to database "db1" ({uuid}). + +// Disconnect from the current database. +SEND:\c - +EXPECT:You are not connected to a database. + +// Connect to a database again. +SEND:\c db1 +EXPECTCOMP:WithFormat:You are now connected to database "db1" ({uuid}). \ No newline at end of file