sqlparse

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sqlparse wraps the cockroachdb-parser module to provide SQL parsing utilities consumed by both the CLI and MCP layers. The primary entry point is Classify, which parses a SQL string and returns a per-statement classification (DDL/DML/DCL/TCL), the statement tag (e.g. "SELECT", "ALTER TABLE"), the original SQL text, and a normalized form with literal constants replaced by placeholders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClassifiedStatement

type ClassifiedStatement struct {
	StatementType StatementType `json:"statement_type"`
	Tag           string        `json:"tag"`
	SQL           string        `json:"sql"`
	// Normalized is the SQL text with literal constants (numbers,
	// strings, booleans, NULL, etc.) replaced by placeholders,
	// produced by tree.FormatStatementHideConstants. Constants become
	// _ (or '_' for string literals), and long value lists are
	// collapsed (e.g. IN (_, _, __more1_10__)). Structurally
	// identical queries that differ only in constant values share
	// the same normalized form.
	Normalized string `json:"normalized"`
}

ClassifiedStatement is the per-statement result of parsing and classifying a SQL string. It is the JSON-serializable shape embedded in both the CLI envelope's Data field and the MCP tool result.

func Classify

func Classify(sql string) ([]ClassifiedStatement, error)

Classify parses sql using the CockroachDB parser and returns one ClassifiedStatement per parsed statement. Parse errors are returned as a Go error; the caller decides how to surface them (as output.Error entries in CLI mode, or as a tool-level error in MCP mode).

An empty input returns an empty slice with no error.

Callers that need both the classified output and the underlying AST (to layer additional analysis like version.Inspect on top) should call parser.Parse themselves and use ClassifyParsed instead, to avoid double-parsing.

func ClassifyParsed

func ClassifyParsed(stmts statements.Statements) []ClassifiedStatement

ClassifyParsed turns an already-parsed statement set into per- statement classifications. It is the second half of Classify, exposed so callers that already invoked parser.Parse (e.g. to also run type-checking or AST inspection) can reuse the parsed output rather than reparsing the SQL string.

type StatementType

type StatementType string

StatementType is the human-readable classification for a SQL statement. Values correspond to the tree.StatementType enum in cockroachdb-parser with the "Type" prefix stripped.

const (
	StatementTypeDDL     StatementType = "DDL"
	StatementTypeDML     StatementType = "DML"
	StatementTypeDCL     StatementType = "DCL"
	StatementTypeTCL     StatementType = "TCL"
	StatementTypeUnknown StatementType = "UNKNOWN"
)

StatementType constants.

Jump to

Keyboard shortcuts

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