sqltext

package
v0.1.79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 2 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

StatementTypes is every StatementType ClassifyStatement can return.

Functions

func DurationStyle

func DurationStyle(d time.Duration) string

DurationStyle is the tone a statement's duration renders in: muted under 1ms, then yellow, orange from 10ms, red from 100ms and bold red from 1s. Every SQL trace type styles its durations with it, so a slow statement reads the same whichever capture recorded it.

func ExtractTables

func ExtractTables(sql string) []string

ExtractTables returns the distinct table/object names referenced after FROM / JOIN / INTO / UPDATE / MERGE / USING / EXEC keywords. It is a best-effort parse of the SQL text — the only structured source XE exposes for a completed statement.

For a stored-procedure call the extracted name is the PROCEDURE, not the tables its body touches: the body never appears in the trace text. So a table filter of `usp_GetOrderTotals` matches the call while one naming a table the body reads does not. Capturing sp_statement_completed surfaces the inner statements, and those do carry their own table names.

A FROM clause is a comma-separated list of `table [AS] [alias] [hints]` refs, so once a FROM opens a list the parser keeps it open across alias/hint tokens and reopens table-expectation on each top-level comma — `FROM A a, B b` yields both A and B, not just A. List state is tracked PER parenthesis depth so a subquery's own column commas (`(SELECT a, b FROM X)`) never reopen the outer list, while the subquery's inner FROM is still scanned. A keyword in table position (e.g. MERGE's `UPDATE SET`) ends the clause instead of being taken as a table name. Names are schema- and bracket-stripped and de-duplicated case-insensitively, preserving the first-seen casing; an exotic shape (dynamic SQL, table-valued functions in a comma list) may still under-extract.

Types

type StatementType

type StatementType string

StatementType is the coarse classification of a captured statement. It is derived from the top-level keywords of the merged SQL — NOT a substring scan — so the SQL text never has to be string-matched by callers filtering a trace. A statement opening with CREATE/ALTER/DROP/TRUNCATE is StmtDDL; otherwise the first top-level DML verb decides, and anything with none is StmtOther.

const (
	StmtSelect StatementType = "SELECT"
	StmtInsert StatementType = "INSERT"
	StmtUpdate StatementType = "UPDATE"
	StmtDelete StatementType = "DELETE"
	StmtMerge  StatementType = "MERGE"
	StmtExec   StatementType = "EXEC"
	StmtDDL    StatementType = "DDL"
	StmtOther  StatementType = "OTHER"
)

func ClassifyStatement

func ClassifyStatement(sql string) StatementType

ClassifyStatement returns the StatementType of a SQL statement by finding the first DML verb at parenthesis depth 0. This naturally resolves CTEs (`WITH x AS (SELECT …) SELECT …` → SELECT, since the inner SELECT is nested) and `INSERT … SELECT` (→ INSERT, the first top-level verb), and skips leading comments/parens via the tokenizer. A leading DDL verb wins before that scan, since a DDL body may itself contain DML. Unrecognized leading verbs → StmtOther.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL