-
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: fix double-quote escaping for JSON values with format=sql
A previous commit (98f9c8a) made an attempt to fix how JSON values are escaped when they contain invalid UTF8 codes and are displayed in the CLI using the --format=sql flag (see #107518). That commit ended up breaking how JSON values are escaped when they contain double quotes. Luckily it turns out that both problems were actually caused by a long-lived mistake in the `clisqlexec.FormatVal` function. It shouldn't use `fmt.Sprintf("%+q", s)` to escape a string that has invalid characters, as that conflicts with how SQL strings are normally escaped. The proper way is to use `lexbase.EscapeSQLString(s)`. Release note (bug fix): Fixed a bug where the CLI would not correctly escape JSON values that had double-quotes inside of a string when using the --format=sql flag.
- Loading branch information
Showing
6 changed files
with
78 additions
and
33 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,71 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package clisqlexec_test | ||
|
||
import "github.com/cockroachdb/cockroach/pkg/cli" | ||
|
||
func Example_json_sql_format() { | ||
c := cli.NewCLITest(cli.TestCLIParams{}) | ||
defer c.Cleanup() | ||
|
||
testData := []string{ | ||
`e'{"a": "bc"}'`, | ||
`e'{"a": "b\u0099c"}'`, | ||
`e'{"a": "b\\"c"}'`, | ||
`'"there are \"quotes\" in this json string"'`, | ||
`'""'`, | ||
`'{}'`, | ||
} | ||
|
||
for _, s := range testData { | ||
query := `SELECT ` + s + `::json` | ||
c.RunWithArgs([]string{"sql", "--format=sql", "-e", query}) | ||
} | ||
|
||
// Output: | ||
// sql --format=sql -e SELECT e'{"a": "bc"}'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES ('{"a": "bc"}'); | ||
// -- 1 row | ||
// sql --format=sql -e SELECT e'{"a": "b\u0099c"}'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES (e'{"a": "b\\u0099c"}'); | ||
// -- 1 row | ||
// sql --format=sql -e SELECT e'{"a": "b\\"c"}'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES (e'{"a": "b\\"c"}'); | ||
// -- 1 row | ||
// sql --format=sql -e SELECT '"there are \"quotes\" in this json string"'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES (e'"there are \\"quotes\\" in this json string"'); | ||
// -- 1 row | ||
// sql --format=sql -e SELECT '""'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES ('""'); | ||
// -- 1 row | ||
// sql --format=sql -e SELECT '{}'::json | ||
// CREATE TABLE results ( | ||
// jsonb STRING | ||
// ); | ||
// | ||
// INSERT INTO results VALUES ('{}'); | ||
// -- 1 row | ||
} |
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