Documentation
¶
Overview ¶
Package sqlformat wraps the cockroachdb-parser module to provide SQL pretty-printing. The primary entry point is Format, which parses a SQL string and re-emits it in CockroachDB's canonical pretty-printed form using tree.DefaultPrettyCfg.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format parses sql using the CockroachDB parser and returns the pretty-printed canonical form. Multiple statements in the input are separated by ";\n" in the output. An empty input returns an empty string with no error. Parse or formatting errors are returned as a Go error; the caller decides how to surface them.
func FormatParsed ¶
func FormatParsed(stmts statements.Statements) (string, error)
FormatParsed pretty-prints already-parsed statements. Exposed so callers that already invoked parser.Parse (e.g. to also run version.Inspect on the same AST) can reuse the parsed output rather than reparsing. The only error surface is the pretty-printer itself (cfg.Pretty); parser errors are no longer possible at this layer.
func Highlight ¶
Highlight returns formatted with ANSI SGR escape sequences wrapping SQL keywords (bold cyan), string literals (green), and numeric literals (magenta). Identifiers, punctuation, and whitespace are emitted uncolored. The function preserves byte ranges between tokens verbatim, so the visible (de-escaped) output is byte-identical to formatted.
Tokenization is performed with scanner.Inspect, which yields token start/end offsets into formatted. If any token comes back as ERROR or as the incomplete-token sentinel (negative ID, documented in scanner.Inspect), Highlight returns formatted unchanged rather than emit a half-colored garbage string. In normal use this fallback never fires because callers feed already-validated SQL, but the defensive check protects against bounds-violating slices if a future caller passes hand-constructed input.
Highlight is intended for terminal output only. JSON envelopes must never contain escape sequences, so callers gate this behind a text-mode + TTY check.
func StripShellPrompts ¶
StripShellPrompts removes cockroach sql REPL prompt prefixes from pasted input. Two prompt forms are recognized:
<user>@<host-info>><SP> // primary prompt (any user) <pad>-><SP> // continuation prompt (right-aligned)
Continuation lines are stripped only after a primary prompt has already been seen earlier in the input. This guards SQL-only pastes that legitimately contain the JSON field-access operator '->' at line start (e.g. " -> 'key'") from being mangled. A transcript paste always starts with a primary prompt, so the guard does not interfere with the intended use case.
The function is a no-op when the input contains no prompts. Lines without a recognized prompt — including blank lines and lines inside multi-line string literals — pass through unchanged.
Limitation: a multi-line string literal whose interior line happens to begin with a primary-prompt-shaped sequence will be incorrectly stripped. This is exceedingly rare in REPL transcripts and not worth the complexity of full lexer-aware line classification at this layer.
Types ¶
type StripResult ¶
StripResult is the value produced by StripShellPromptsWithMap. It pairs the stripped SQL with an offset map that lets callers translate byte offsets in the stripped buffer back to the original input — needed so parser/cluster error positions can be reported in the user's original coordinates after the stripper has shortened earlier lines.
Removed reports whether any prompt bytes were dropped. When false, Stripped is byte-identical to the function input and Translate is the identity, so handlers can safely treat the no-op case the same as the strip-applied case.
segments is the per-removal cumulative-delta table consumed by Translate. Each entry records the offset in Stripped where a stretch of "no further bytes removed" begins, plus the total number of bytes dropped from the original before that offset. Lookup is a binary search for the largest StrippedStart ≤ query offset, so translation is O(log N) in the number of stripped lines.
func StripShellPromptsWithMap ¶
func StripShellPromptsWithMap(sql string) StripResult
StripShellPromptsWithMap behaves like StripShellPrompts but also returns an offset map. Use this when you intend to surface error positions back to the caller, so a "syntax error at column 30" in the stripped buffer can be reported as the matching column in the original paste. For pure formatting / display use cases the simpler StripShellPrompts is sufficient.
func (StripResult) Translate ¶
func (r StripResult) Translate(strippedOffset int) int
Translate maps a 0-based byte offset in r.Stripped to the matching 0-based byte offset in the original input. Negative offsets clamp to 0; offsets past len(r.Stripped) clamp to len(original).
Translation works by binary-searching segments for the largest StrippedStart ≤ strippedOffset and adding its CumDelta. When no segments were produced (no prompts removed), Translate is the identity.