{"modulePath":"github.com/flanksource/commons-db","version":"v0.1.112","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqltext\n\n## Variables\n\n```go\nvar StatementTypes = []StatementType{StmtSelect, StmtInsert, StmtUpdate, StmtDelete, StmtMerge, StmtExec, StmtDDL, StmtOther}\n```\nStatementTypes is every StatementType ClassifyStatement can return.\n\n## Functions\n\n```go\nfunc DurationStyle(d time.Duration) string\n```\nDurationStyle 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.\n\n```go\nfunc ExtractTables(sql string) []string\n```\nExtractTables 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.\n\nFor 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.\n\nA 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.\n\n## Types\n\n```go\ntype StatementType string\n```\nStatementType 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.\n\n```go\nconst (\n\tStmtSelect StatementType = \"SELECT\"\n\tStmtInsert StatementType = \"INSERT\"\n\tStmtUpdate StatementType = \"UPDATE\"\n\tStmtDelete StatementType = \"DELETE\"\n\tStmtMerge  StatementType = \"MERGE\"\n\tStmtExec   StatementType = \"EXEC\"\n\tStmtDDL    StatementType = \"DDL\"\n\tStmtOther  StatementType = \"OTHER\"\n)\n```\n\n```go\nfunc ClassifyStatement(sql string) StatementType\n```\nClassifyStatement 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.\n\n","path":"github.com/flanksource/commons-db/query/sqltext","name":"sqltext","synopsis":"","isRedistributable":true}
