-
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.
cli/sql: properly support special COPY input mode
When in COPY mode, multi-line input is disabled and the tab key inputs raw ASCII TAB characters. This commit achieves this by redirecting the input to either the interactive editor or the bufio-based editor, depending on whether the current mode is COPY. NB: using ctrl+c while in COPY mode will not work with this patch, but that is caused by a separate issue #93053 and the behavior will be restored when that separate issue is fixed. In the meantime the user can use `\.` or ctrl+d to terminate their input. Release note: None
- Loading branch information
Showing
4 changed files
with
127 additions
and
24 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
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,83 @@ | ||
// 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 ( | ||
"os" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// bimodalEditor redirects the input to either a "main" editor | ||
// (outside of COPY mode) or a "copy" editor (inside COPY). | ||
// This is because the main editor may be multi-line and | ||
// thus inadequate for input of COPY data. | ||
type bimodalEditor struct { | ||
sql sqlShell | ||
main editor | ||
copy editor | ||
} | ||
|
||
var _ editor = (*bimodalEditor)(nil) | ||
|
||
func (e *bimodalEditor) init( | ||
win, wout, werr *os.File, sqlS sqlShell, maxHistEntries int, histFile string, | ||
) (cleanupFn func(), err error) { | ||
e.sql = sqlS | ||
c1, err := e.main.init(win, wout, werr, sqlS, maxHistEntries, histFile) | ||
if err != nil { | ||
return c1, err | ||
} | ||
c2, err := e.copy.init(win, wout, werr, sqlS, maxHistEntries, histFile) | ||
cleanupFn = func() { | ||
c1() | ||
c2() | ||
} | ||
return cleanupFn, err | ||
} | ||
|
||
func (e *bimodalEditor) selected() editor { | ||
if e.sql.inCopy() { | ||
return e.copy | ||
} | ||
return e.main | ||
} | ||
|
||
func (e *bimodalEditor) errInterrupted() error { | ||
return e.selected().errInterrupted() | ||
} | ||
|
||
func (e *bimodalEditor) getOutputStream() *os.File { | ||
return e.selected().getOutputStream() | ||
} | ||
|
||
func (e *bimodalEditor) getLine() (string, error) { | ||
return e.selected().getLine() | ||
} | ||
|
||
func (e *bimodalEditor) addHistory(line string) error { | ||
return errors.CombineErrors( | ||
e.copy.addHistory(line), | ||
e.main.addHistory(line)) | ||
} | ||
|
||
func (e *bimodalEditor) canPrompt() bool { | ||
return e.main.canPrompt() | ||
} | ||
|
||
func (e *bimodalEditor) setPrompt(prompt string) { | ||
e.copy.setPrompt(prompt) | ||
e.main.setPrompt(prompt) | ||
} | ||
|
||
func (e *bimodalEditor) multilineEdit() bool { | ||
return e.selected().multilineEdit() | ||
} |
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