A type-safe SQL toolkit for PHP built with Rust
Boring Database Engine (BDE) is a code generator that transforms your SQL queries into type-safe PHP functions. Inspired by the Go-based SQLC project, BDE brings the same productivity and safety benefits to the PHP ecosystem.
The "boring" philosophy is intentional - database interactions should be predictable, reliable, and free from unexpected behavior. BDE focuses on generating straightforward, maintainable code without unnecessary complexity.
- Type-safe SQL: Generate PHP functions with proper type signatures from your SQL queries
- Compile-time validation: Catch SQL errors before runtime
- Performance: Rust-powered parsing and code generation
- Minimal dependencies: Clean, straightforward PHP output with no runtime dependencies
- MySQL support: First-class support for MySQL (additional databases planned)
# Initialize a new project
bde init
# Generate code from your SQL files
bde generate
Define your SQL schema:
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
Write your queries:
-- name: GetUserByID :one
SELECT *
FROM users
WHERE id = $1;
-- name: ListUsers :many
SELECT *
FROM users
ORDER BY created_at DESC;
-- name: CreateUser :one
INSERT INTO users (username, email)
VALUES ($1, $2) RETURNING *;
BDE will generate type-safe PHP code:
<?php
class User
{
public int $id;
public string $name;
public string $email;
public string $created_at;
}
class Queries
{
private \PDO $pdo;
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
public function getUserById(int $id): ?User
{
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$row) {
return null;
}
$user = new User();
$user->id = (int)$row['id'];
$user->name = $row['name'];
$user->email = $row['email'];
$user->created_at = $row['created_at'];
return $user;
}
public function listUsers(): array
{
// Implementation generated by BDE
}
public function createUser(string $username, string $email): User
{
// Implementation generated by BDE
}
}
BDE is currently in development.
Contributions are currently not welcomed.
MIT
Peter Paravinja