sqlc
sqlc copied to clipboard
feat(mysql): Add database analyzer for MySQL
Summary
- Adds a MySQL database analyzer that validates queries against a real MySQL database during code generation
- Creates managed databases (
sqlc_managed_{hash}) based on migration content hash for caching - Validates query syntax using
PrepareContextagainst real MySQL - Fixes several MySQL test cases to use correct MySQL syntax instead of PostgreSQL syntax
Implementation Details
The MySQL analyzer (internal/engine/dolphin/analyzer/analyze.go) follows the same pattern as the SQLite analyzer:
- For managed databases: creates a temporary database with schema based on migration hash
- For non-managed databases: connects directly to the configured URI
- Uses
PrepareContextto validate queries against the real database - Returns parameter info but defers column inference to the catalog (MySQL prepared statements don't provide reliable column metadata with NULL parameters)
Test Fixes
Fixed MySQL test cases that were using PostgreSQL-specific syntax:
- Changed
$1/$2placeholders to?(MySQL syntax) - Added
AS countaliases tocount(*)expressions in CTEs - Fixed column references (
author_id→id,authors.parent_id→a.parent_id) - Removed PostgreSQL-specific
public.schema prefix
Tests restricted to base context (incompatible with real MySQL):
mysql_vector- requires MySQL HeatWave (DISTANCE/STRING_TO_VECTOR functions)vet_explain- requires MySQL env vars for explain rulesshow_warnings- SHOW WARNINGS not supported in prepared statementsselect_subquery_no_alias- MySQL requires derived tables to have aliasesvalid_group_by_reference- MySQL's ONLY_FULL_GROUP_BY mode incompatibilityinsert_select_invalid,invalid_group_by_reference,invalid_table_alias- expected error messages differ
Test plan
- [x] All existing tests pass (
go test ./...) - [x] All endtoend tests pass (
go test --tags=examples -timeout 20m ./internal/endtoend/...) - [x] MySQL analyzer creates managed databases and validates queries
🤖 Generated with Claude Code