Skip to content

Commit

Permalink
Merge #54796
Browse files Browse the repository at this point in the history
54796: cli/sql: add some additional client-side commands r=otan a=knz

Fixes  #25178 
Supports use cases promoted by  #54771 

Adds `\p`, `\r` `\echo` and especially `\i` / `\ir` (recursive include).


Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Sep 30, 2020
2 parents 5a7f32f + 88939ce commit a99227d
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 90 deletions.
43 changes: 43 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2089,3 +2089,46 @@ func Example_read_from_file() {
// ERROR: column "undefined" does not exist
// SQLSTATE: 42703
}

// Example_includes tests the \i command.
func Example_includes() {
c := newCLITest(cliTestParams{})
defer c.cleanup()

c.RunWithArgs([]string{"sql", "-f", "testdata/i_twolevels1.sql"})
c.RunWithArgs([]string{"sql", "-f", "testdata/i_multiline.sql"})
c.RunWithArgs([]string{"sql", "-f", "testdata/i_stopmiddle.sql"})
c.RunWithArgs([]string{"sql", "-f", "testdata/i_maxrecursion.sql"})

// Output:
// sql -f testdata/i_twolevels1.sql
// > SELECT 123;
// ?column?
// 123
// > SELECT 789;
// ?column?
// 789
// ?column?
// 456
// sql -f testdata/i_multiline.sql
// ERROR: at or near "\": syntax error
// SQLSTATE: 42601
// DETAIL: source SQL:
// SELECT -- incomplete statement, \i invalid
// \i testdata/i_twolevels2.sql
// ^
// HINT: try \h SELECT
// ERROR: at or near "\": syntax error
// SQLSTATE: 42601
// DETAIL: source SQL:
// SELECT -- incomplete statement, \i invalid
// \i testdata/i_twolevels2.sql
// ^
// HINT: try \h SELECT
// sql -f testdata/i_stopmiddle.sql
// ?column?
// 123
// sql -f testdata/i_maxrecursion.sql
// \i: too many recursion levels (max 10)
// ERROR: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: testdata/i_maxrecursion.sql: \i: too many recursion levels (max 10)
}
19 changes: 18 additions & 1 deletion pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func setCliContextDefaults() {
cliCtx.allowUnencryptedClientPassword = false
}

// sqlCtx captures the command-line parameters of the `sql` command.
// sqlCtx captures the configuration of the `sql` command.
// See below for defaults.
var sqlCtx = struct {
*cliContext
Expand Down Expand Up @@ -238,6 +238,19 @@ var sqlCtx = struct {

// Determine whether to show raw durations.
verboseTimings bool

// Determines whether to stop the client upon encountering an error.
errExit bool

// Determines whether to perform client-side syntax checking.
checkSyntax bool

// autoTrace, when non-empty, encloses the executed statements
// by suitable SET TRACING and SHOW TRACE FOR SESSION statements.
autoTrace string

// The string used to produce the value of fullPrompt.
customPromptPattern string
}{cliContext: &cliCtx}

// setSQLContextDefaults set the default values in sqlCtx. This
Expand All @@ -254,6 +267,10 @@ func setSQLContextDefaults() {
sqlCtx.echo = false
sqlCtx.enableServerExecutionTimings = false
sqlCtx.verboseTimings = false
sqlCtx.errExit = false
sqlCtx.checkSyntax = false
sqlCtx.autoTrace = ""
sqlCtx.customPromptPattern = defaultPromptPattern
}

// zipCtx captures the command-line parameters of the `zip` command.
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/interactive_tests/test_local_cmds.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ eexpect "with no argument"
eexpect root@
end_test

start_test "Check that \\echo behaves well."
send "\\echo\r"
eexpect "\r\n"
eexpect "\r\n"
eexpect root@

send "\\echo hello world\r"
# echo removes double spaces within the line. That's expected.
eexpect "hello world"
eexpect root@
end_test

start_test "Check that commands are also recognized with a final semicolon."
send "\\set;\r"
eexpect "display_format"
Expand Down
19 changes: 15 additions & 4 deletions pkg/cli/interactive_tests/test_multiline_statements.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ send "2, 3\r"
eexpect " ->"
end_test

start_test "Test that \show does what it says."
send "\\show\r"
start_test "Test that \p does what it says."
send "\\p\r"
eexpect "select 1,"
eexpect "2, 3"
eexpect " ->"
Expand All @@ -49,6 +49,17 @@ eexpect "2, 3"
eexpect ";"
end_test

start_test "Test that \r does what it says."
# backspace to erase the semicolon
send "\010"
# newline to get a prompt
send "\r"
eexpect " ->"
# Now send \r followed by a carriage return.
send "\\r\r"
eexpect root@
end_test

start_test "Test that Ctrl+C after the first line merely cancels the statement and presents the prompt."
send "\r"
eexpect root@
Expand All @@ -58,10 +69,10 @@ interrupt
eexpect root@
end_test

start_test "Test that \show does what it says."
start_test "Test that \p does what it says."
send "select\r"
eexpect " ->"
send "\\show\r"
send "\\p\r"
eexpect "select\r\n*->"
interrupt
end_test
Expand Down
Loading

0 comments on commit a99227d

Please sign in to comment.