Skip to content

Conversation

@kyleconroy
Copy link
Collaborator

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 PrepareContext against 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 PrepareContext to 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/$2 placeholders to ? (MySQL syntax)
  • Added AS count aliases to count(*) expressions in CTEs
  • Fixed column references (author_idid, authors.parent_ida.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 rules
  • show_warnings - SHOW WARNINGS not supported in prepared statements
  • select_subquery_no_alias - MySQL requires derived tables to have aliases
  • valid_group_by_reference - MySQL's ONLY_FULL_GROUP_BY mode incompatibility
  • insert_select_invalid, invalid_group_by_reference, invalid_table_alias - expected error messages differ

Test plan

  • All existing tests pass (go test ./...)
  • All endtoend tests pass (go test --tags=examples -timeout 20m ./internal/endtoend/...)
  • MySQL analyzer creates managed databases and validates queries

🤖 Generated with Claude Code

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Nov 29, 2025
Adds a MySQL database analyzer that validates queries against a real MySQL
database during code generation. This is similar to the existing PostgreSQL
and SQLite analyzers.

Key features:
- Creates managed databases (sqlc_managed_{hash}) based on migration content
- Validates query syntax using PrepareContext against real MySQL
- Detects parameter count from ? placeholders
- Gracefully handles missing database connections

Also fixes several MySQL test cases to use correct MySQL syntax:
- Changed $1/$2 placeholders to ? (MySQL syntax)
- Added AS aliases to count(*) expressions in CTEs
- Fixed column references and table aliases
- Removed PostgreSQL-specific public. schema prefix

Tests requiring MySQL-specific features (HeatWave VECTOR functions, SHOW
WARNINGS in prepared statements, etc.) are restricted to base context.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@kyleconroy kyleconroy force-pushed the claude/add-mysql-analyzer-1764455321 branch 3 times, most recently from 3fc8768 to e52cd4e Compare November 29, 2025 23:59
Updates the MySQL analyzer to use the sqlc-dev/mysql forked driver which
exposes column and parameter metadata from COM_STMT_PREPARE responses.
This provides more accurate type information directly from MySQL.

The forked driver adds a StmtMetadata interface with ColumnMetadata() and
ParamMetadata() methods that return type info including DatabaseTypeName,
Nullable, Unsigned, and Length fields.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@kyleconroy kyleconroy marked this pull request as draft November 30, 2025 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants