u/Cristian_tallica

Built sqlc-php: a PHP 8.4 code generator that turns your SQL schema + annotated queries into typed PDO classes
▲ 32 r/PHP

Built sqlc-php: a PHP 8.4 code generator that turns your SQL schema + annotated queries into typed PDO classes

Built sqlc-php: a PHP 8.4 code generator that turns your SQL schema + annotated queries into typed PDO classes.

Inspired by sqlc for Go, I built a CLI tool that reads your schema.sql and annotated query files and generates fully-typed PHP classes.

Source: https://github.com/phpibe/sqlc-php

What it does:

Infers PHP types from the schema (INT → int, TINYINT → int, TIMESTAMP → string, etc.)

Resolves :param types by matching them to schema columns — including camelCase→snake_case (updatedAt → updated_at)

Generates result DTOs for JOIN queries and aggregate expressions (COUNT → int, SUM → ?int, AVG → ?float, MAX → ?{col type})

type_overrides in sqlc.yaml to remap any column or db type (TINYINT → bool, TIMESTAMP → \DateTimeImmutable)

Four return types: :many (array), :one (throws), :opt (nullable), :exec (void)

Generated classes use PHP 8.4 readonly class with PDO under the hood — zero dependencies

Still early but functional. 152 unit tests covering the full pipeline.

Source: https://github.com/phpibe/sqlc-php

Happy to hear feedback from anyone who's done something similar or has opinions on the approach.

u/Cristian_tallica — 2 days ago