Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(mysql): Add database analyzer for MySQL
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 <[email protected]>
  • Loading branch information
kyleconroy and claude committed Nov 29, 2025
commit e52cd4e2e85822106193e0c82b469f7de1eadc55
2 changes: 2 additions & 0 deletions examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ sql:
engine: mysql
database:
uri: "${VET_TEST_EXAMPLES_MYSQL_AUTHORS}"
analyzer:
database: false
rules:
- sqlc/db-prepare
# - mysql-query-too-costly
Expand Down
3 changes: 3 additions & 0 deletions examples/booktest/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"database": {
"uri": "${VET_TEST_EXAMPLES_MYSQL_BOOKTEST}"
},
"analyzer": {
"database": false
},
"rules": [
"sqlc/db-prepare"
]
Expand Down
3 changes: 3 additions & 0 deletions examples/ondeck/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"database": {
"uri": "${VET_TEST_EXAMPLES_MYSQL_ONDECK}"
},
"analyzer": {
"database": false
},
"rules": [
"sqlc/db-prepare"
],
Expand Down
39 changes: 22 additions & 17 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,30 @@ func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
Column: convertColumn(p.Column),
})
}
if len(prev.Columns) == len(cols) {
for i := range prev.Columns {
// Only override column types if the analyzer provides a specific type
// (not "any"), since the catalog-based inference may have better info
if cols[i].DataType != "any" {
prev.Columns[i].DataType = cols[i].DataType
prev.Columns[i].IsArray = cols[i].IsArray
prev.Columns[i].ArrayDims = cols[i].ArrayDims
// Only update columns if the analyzer returned column info
// An empty slice from the analyzer means it couldn't determine columns,
// so we should keep the catalog-inferred columns
if len(cols) > 0 {
if len(prev.Columns) == len(cols) {
for i := range prev.Columns {
// Only override column types if the analyzer provides a specific type
// (not "any"), since the catalog-based inference may have better info
if cols[i].DataType != "any" {
prev.Columns[i].DataType = cols[i].DataType
prev.Columns[i].IsArray = cols[i].IsArray
prev.Columns[i].ArrayDims = cols[i].ArrayDims
}
}
}
} else {
embedding := false
for i := range prev.Columns {
if prev.Columns[i].EmbedTable != nil {
embedding = true
} else {
embedding := false
for i := range prev.Columns {
if prev.Columns[i].EmbedTable != nil {
embedding = true
}
}
if !embedding {
prev.Columns = cols
}
}
if !embedding {
prev.Columns = cols
}
}
if len(prev.Parameters) == len(params) {
Expand Down
10 changes: 10 additions & 0 deletions internal/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/dbmanager"
"github.com/sqlc-dev/sqlc/internal/engine/dolphin"
mysqlanalyze "github.com/sqlc-dev/sqlc/internal/engine/dolphin/analyzer"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
pganalyze "github.com/sqlc-dev/sqlc/internal/engine/postgresql/analyzer"
"github.com/sqlc-dev/sqlc/internal/engine/sqlite"
Expand Down Expand Up @@ -55,6 +56,15 @@ func NewCompiler(conf config.SQL, combo config.CombinedSettings) (*Compiler, err
c.parser = dolphin.NewParser()
c.catalog = dolphin.NewCatalog()
c.selector = newDefaultSelector()
if conf.Database != nil {
if conf.Analyzer.Database == nil || *conf.Analyzer.Database {
c.analyzer = analyzer.Cached(
mysqlanalyze.New(combo.Global.Servers, *conf.Database),
combo.Global,
*conf.Database,
)
}
}
case config.EnginePostgreSQL:
c.parser = postgresql.NewParser()
c.catalog = postgresql.NewCatalog()
Expand Down
7 changes: 4 additions & 3 deletions internal/endtoend/testdata/create_view/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/endtoend/testdata/create_view/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
SELECT * FROM first_view;

-- name: GetSecond :many
SELECT * FROM second_view WHERE val2 = $1;
SELECT * FROM second_view WHERE val2 = ?;
4 changes: 2 additions & 2 deletions internal/endtoend/testdata/cte_count/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/endtoend/testdata/cte_count/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- name: CTECount :many
WITH all_count AS (
SELECT count(*) FROM bar
SELECT count(*) AS count FROM bar
), ready_count AS (
SELECT count(*) FROM bar WHERE ready = true
SELECT count(*) AS count FROM bar WHERE ready = true
)
SELECT all_count.count, ready_count.count
FROM all_count, ready_count;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/endtoend/testdata/cte_filter/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- name: CTEFilter :many
WITH filter_count AS (
SELECT count(*) FROM bar WHERE ready = ?
SELECT count(*) AS count FROM bar WHERE ready = ?
)
SELECT filter_count.count
FROM filter_count;
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/in_union/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/endtoend/testdata/in_union/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-- name: GetAuthors :many
SELECT * FROM authors
WHERE author_id IN (SELECT author_id FROM book1 UNION SELECT author_id FROM book2);
WHERE id IN (SELECT author_id FROM book1 UNION SELECT author_id FROM book2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* name: InsertValues :exec */
INSERT INTO public.foo (a, b) VALUES (?, ?);
INSERT INTO foo (a, b) VALUES (?, ?);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"contexts": ["base"],
"meta": {
"invalid_schema": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ SELECT a.id,
p.name as alias_name
FROM authors a
LEFT JOIN authors p
ON (authors.parent_id = p.id);
ON (a.parent_id = p.id);
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/mysql_vector/mysql/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/show_warnings/mysql/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["base"]
}
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/vet_explain/mysql/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}
41 changes: 23 additions & 18 deletions internal/endtoend/testdata/wrap_errors/mysql/db/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/endtoend/testdata/wrap_errors/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
WHERE id = ? LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
Expand All @@ -10,21 +10,21 @@ ORDER BY name;
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
?, ?
);

-- name: DeleteAuthorExec :exec
DELETE FROM authors
WHERE id = $1;
WHERE id = ?;

-- name: DeleteAuthorExecRows :execrows
DELETE FROM authors
WHERE id = $1;
WHERE id = ?;

-- name: DeleteAuthorExecLastID :execlastid
DELETE FROM authors
WHERE id = $1;
WHERE id = ?;

-- name: DeleteAuthorExecResult :execresult
DELETE FROM authors
WHERE id = $1;
WHERE id = ?;
Loading
Loading