Overview
TheAnalyzer struct orchestrates the execution of pgvet rules against SQL files and directories. It handles file discovery, SQL parsing, rule execution, and diagnostic collection.
Analyzer Struct
Fields
The slice of rules to execute during analysis. Each rule implements the
Rule interface.Constructor
New
Creates a new Analyzer with the given rules.The rules to run during analysis. Pass an empty slice to run no rules.
*Analyzer instance
Example:
Methods
AnalyzePaths
Analyzes one or more file or directory paths.Paths to analyze. Can include:
- Individual
.sqlfiles - Directories (recursively scanned for
.sqlfiles) - Mix of both files and directories
[]rule.Diagnostic: All diagnostics found, sorted by file, line, and columnerror: Any error that occurred during analysis (file not found, permission denied, etc.)
- Recursively walks directories looking for
.sqlfiles - Parses each SQL file and splits it into individual statements
- Runs all configured rules against each statement
- Collects and sorts diagnostics by location
- Reports parse errors as diagnostics with rule name
"parse-error"
AnalyzeStdin
Analyzes SQL from stdin.The SQL code to analyze, typically read from standard input
[]rule.Diagnostic: All diagnostics founderror: Any error during parsing or analysis
- Parses the SQL string and splits it into statements
- Runs all configured rules against each statement
- Sets the file name to
"<stdin>"in diagnostics - Reports parse errors as diagnostics
How Analysis Works
The analyzer follows this workflow:-
File Discovery
- For directories: recursively finds all
.sqlfiles - For files: reads them directly
- For directories: recursively finds all
-
SQL Parsing
- Splits SQL into individual statements using
pg_query.SplitWithParser - Parses each statement into an AST using
pg_query.Parse - Parse errors are reported as diagnostics
- Splits SQL into individual statements using
-
Rule Execution
- For each statement, runs every configured rule’s
Check()method - Handles special multi-statement rules separately
- Collects all diagnostics
- For each statement, runs every configured rule’s
-
Post-Processing
- Sets the
Filefield on each diagnostic - Sorts diagnostics by file, line, and column
- Returns sorted results
- Sets the
Diagnostic Sorting
Diagnostics are sorted in this order:- File path (alphabetically)
- Line number (ascending)
- Column number (ascending)
Error Handling
The analyzer handles errors at multiple levels:- File system errors: Returned as errors (file not found, permission denied)
- Parse errors: Converted to diagnostics with rule name
"parse-error"and severity"error" - Rule errors: Rules should not panic; return empty slice if statement doesn’t match
Performance Considerations
- The analyzer processes files sequentially
- Each SQL file is parsed once
- All rules run on each statement (O(statements × rules))
- For large codebases, consider running in parallel externally
See Also
- Rule Interface - Implement custom rules
- Walker - Traverse PostgreSQL AST nodes
- Built-in Rules - Available rules