preprocessor

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package preprocessor provides SQL preprocessing capabilities for pgmi. It handles comment stripping, macro detection, and source mapping for the pgmi_test() macro.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommentStripper

type CommentStripper interface {
	// Strip removes SQL comments. String literals (single-quoted and
	// dollar-quoted) are preserved verbatim.
	Strip(sql string) string

	// RedactForMacros returns a length-preserved mask of the input where
	// bytes inside comments AND inside string literals are replaced with
	// ASCII spaces. All other bytes stay at their original positions.
	//
	// The macro detector runs on this mask so tokens like
	// "CALL pgmi_test();" that appear inside single-quoted or
	// dollar-quoted literals (or inside comments) are invisible to it.
	// Byte offsets returned by the detector are directly usable against
	// the ORIGINAL SQL because the mask preserves length.
	RedactForMacros(sql string) string
}

CommentStripper removes SQL comments while preserving string literals.

func NewCommentStripper

func NewCommentStripper() CommentStripper

NewCommentStripper creates a new CommentStripper instance.

type MacroCall

type MacroCall struct {
	Name     string // Always "pgmi_test"
	Pattern  string // Glob pattern argument, empty if NULL or no arg
	Callback string // Callback function name, empty if not specified
	StartPos int    // Byte offset in input (inclusive)
	EndPos   int    // Byte offset in input (exclusive)
	Line     int    // 1-based line number
	Column   int    // 1-based column number
}

MacroCall represents a detected macro invocation in SQL.

type MacroDetector

type MacroDetector interface {
	// Detect finds macro calls. The `sql` argument is the original text
	// (used to extract Pattern/Callback substrings). The `mask` argument is
	// a length-preserved redacted copy where comment/string bytes are
	// replaced with spaces (see CommentStripper.RedactForMacros) — the
	// regex runs over mask so it cannot match inside literals or comments.
	// Byte offsets in the returned MacroCall are positions in `sql`.
	Detect(sql string, mask string) []MacroCall
}

MacroDetector detects pgmi macro calls in SQL.

func NewMacroDetector

func NewMacroDetector() MacroDetector

NewMacroDetector creates a new MacroDetector instance. The detector expects comment-stripped SQL input.

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline preprocesses SQL by expanding macros.

func NewPipeline

func NewPipeline() *Pipeline

NewPipeline creates a new preprocessing pipeline.

func (*Pipeline) Process

func (p *Pipeline) Process(ctx context.Context, conn *pgxpool.Conn, sql string) (*PreprocessResult, error)

Process preprocesses SQL by expanding CALL pgmi_test() macros. Queries the pg_temp.pgmi_test_plan() function for test execution plan and generates EXECUTE-based SQL that fetches content from pgmi_test_source.

type PreprocessResult

type PreprocessResult struct {
	ExpandedSQL string // SQL with macros expanded
	MacroCount  int    // Number of macros expanded
}

PreprocessResult contains the result of preprocessing deploy.sql.

Jump to

Keyboard shortcuts

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