Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli/sql: add a new table display format "raw"
**tl;dr** use `\set display_format raw` before a query to get the strings unmodified Background ---------- One of the inconveniences of SHOW CREATE TABLE is that its output, by default, encloses the generated CREATE TABLE statement in an ASCII art table, like this: ```` root@:26257/> SHOW CREATE TABLE t.kv; +-------+------------------------------------------------+ | Table | CreateTable | +-------+------------------------------------------------+ | t.kv | CREATE TABLE kv ( | | | k INT NOT NULL, | | | v INT NULL, | | | CONSTRAINT "primary" PRIMARY KEY (k ASC), | | | FAMILY "primary" (k, v) | | | ) | +-------+------------------------------------------------+ (1 row) ``` This prevents copy-pasting the output directly onto some other shell or text file. Meanwhile, our SQL CLI shell *does* provide alternative ways to print out the results, that can be selected with `\set display_format ...`; however, none of the formats defined so far are able to print out the above CREATE TABLE statement **in a way that can be copy-pasted elsewhere**; for example: ``` root@:26257/> \set display_format html root@:26257/> SHOW CREATE TABLE t.kv; <table> <thead><tr><th>Table</th><th>CreateTable</th></tr></head> <tbody> <tr><td>t.kv</td><td>CREATE TABLE kv (<br/> k INT NOT NULL,<br/> v INT NULL,<br/> CONSTRAINT "primary" PRIMARY KEY (k ASC),<br/> FAMILY "primary" (k, v)<br/>)</td></tr> </tbody> </table> ``` (This replaces all special characters) ``` root@:26257/> \set display_format csv root@:26257/> SHOW CREATE TABLE t.kv; 1 row Table,CreateTable t.kv,"CREATE TABLE kv ( k INT NOT NULL, v INT NULL, CONSTRAINT ""primary"" PRIMARY KEY (k ASC), FAMILY ""primary"" (k, v) )" ``` (This escapes the double quotes by doubling them, which renders the SQL invalid) And so forth. Solution: new display format ---------------------------- The `cockroach sql` CLI shell (as well as any built-in command that emits tables) can use a custom display format configurable either with `--format` on the command-line, or `\set display_format` in the shell. This patch adds a new format `raw` which causes the string representation of every record to be printed as-is, starting on its own line, so that it can be copy-pasted from a terminal. This format makes an extra mile to ensure the data can be further processed: each record is prefixed by an annotation of the form `# N` followed by a newline character, where *N* is the number of characters in the record, starting with the first character following the newline. A subsequent automated processing tool can use this information to determine record boundaries. Application ----------- ``` root@:26257/> select createtable from [show create table t.kv]; ... CREATE TABLE kv ( k INT NOT NULL, v INT NULL, CONSTRAINT "primary" PRIMARY KEY (k ASC), FAMILY "primary" (k, v) ) ... ``` Tada!
- Loading branch information