Documentation
¶
Overview ¶
Package sqltext implements the `sql/statement-predicate` rule type: a predicate over individual SQL statements, sourced both from `.sql` files (used verbatim) and from SQL embedded in Go string literals (`.go` files, reassembled heuristically — see sqlextract).
A statement is "selected" when its text matches the required `table` regex. Each selected statement must satisfy every `require` token regex and match none of the `forbid` token regexes; a selected statement that violates the predicate yields one finding. Files that are neither `.go` nor `.sql` yield no findings. A `.go` file that fails to parse is a returned error (the engine turns it into an exit-2 config/engine error), never a silent pass.
COMMENTS ARE NOT STATEMENT TEXT. `--` and `/* … */` comments are blanked before a statement is selected or judged (splitStatements, via preprocess.DecommentSQL), so a token carried only by a comment neither satisfies a `require`, selects through `table`, nor fires a `forbid`, and a ';' inside a comment does not terminate a statement. Comment-shaped bytes inside `'…'`, `"…"` and `$tag$…$tag$` are string data and survive. Without this, a developer's comment explaining why a column is absent satisfies the rule that exists to demand it — the accidental shape, not an adversarial one (#137, reported downstream by the validating port). No `require` regex can express it instead: these are RE2 patterns, which have no negative lookahead.
NEITHER IS A ';' INSIDE A STRING LITERAL A STATEMENT BOUNDARY (#139). The statement split is lexical rather than byte-wise WHERE IT CAN BE: `'…'`, `E'…'`, `"…"` and `$tag$…$tag$` are skipped whole, so a semicolon in a delimiter constant, an error template or a string_agg separator no longer truncates the statement around it. That was a FALSE POSITIVE — the rule failed conforming code — and it silently produced a second, unselectable fragment out of the tail. Text the scan cannot lex (an unterminated literal) or cannot place (a sqlextract fragment that may open mid-literal) keeps the byte-wise split instead, because a wrong skip merges statements and a merged statement is a statement not judged; splitStatements states that trade in full. Why not the real grammar: sqlparse hands whole text to pg_query and takes ITS splitting, but this rule type must also judge the fragments sqlextract yields for a composition it could not reassemble, which do not parse; and the package doc there records that nothing outside sqlparse imports go-pgquery.
HEURISTIC LIMITS remain, and are sqlextract's: SQL composed by fmt.Sprintf, by concatenation with an operand that is not a Go constant, or through strings.Builder is not reassembled, so a statement built that way is split into fragments or dropped entirely. Constant folding is the boundary, and it is the Go language's: a named constant is read wherever the module declares it, through the walk the file came from (sqlextract's consts.go). One further limit lives here: `require` is RE2 MatchString over the whole statement, so a single data-modifying CTE carrying two INSERT arms is satisfied by the compliant arm.