Skip to content

Repository files navigation

SQLC PHP PDO Plugin (Fork)

⚠️Warning: This project is in a very early stage. Expect bugs and breaking changes. Use with caution in production environments.

A fork of the original SQLC plugin, this version generates PHP code using native PDO for type-safe database operations. It allows you to write SQL queries and automatically generates PHP classes with proper type hints and database abstraction, without requiring Doctrine DBAL.

Features

  • Generates PHP classes from SQL queries using native PDO
  • Supports MySQL and SQLite
  • Type-safe database operations
  • Support for various SQL operations:
    • SELECT queries (single and multiple results)
    • INSERT operations
    • UPDATE operations
    • DELETE operations
    • Complex joins
    • Parameterized queries
    • Array parameters

Installation

  1. Download the latest release from the releases page of this fork
  2. Add the plugin to your sqlc.yaml configuration:
version: "2"plugins:
- name: phpwasm:
url: https://github.com/aabajyan/sqlc-gen-php/releases/download/v0.0.15/sqlc-gen-php.wasmsha256: 950c201f00b8dbe4045c09a206793cc413ff8133c1d5e512b9e57a20427b046a

Configuration

Configure your SQL queries in sqlc.yaml:

sql:
- schema: sqlc/authors/mysql/schema.sqlqueries: sqlc/authors/mysql/query.sqlengine: mysql # or sqlitecodegen:
- out: src/Sqlc/MySQLplugin: phpoptions:
package: "App\\Sqlc\\MySQL"# or your preferred namespace

Example of a complete sqlc.yaml config:

version: '2'plugins:
- name: phpwasm:
url: https://github.com/aabajyan/sqlc-gen-php/releases/download/v0.0.15/sqlc-gen-php.wasmsha256: 950c201f00b8dbe4045c09a206793cc413ff8133c1d5e512b9e57a20427b046asql:
- schema: sqlc/authors/mysql/schema.sqlqueries: sqlc/authors/mysql/query.sqlengine: mysql # or sqlitecodegen:
- out: src/Sqlc/MySQLplugin: phpoptions:
package: "App\\Sqlc\\MySQL"# or your preferred namespace

Options

  • package: The PHP namespace for generated classes
  • out: Output directory for generated code

Example Usage

Schema Definition

CREATETABLEauthor (
author_id integerNOT NULL AUTO_INCREMENT PRIMARY KEY,
name textNOT NULL
) ENGINE=InnoDB;

Query Definition

/* name: GetAuthor :one */SELECT*FROM author
WHERE author_id = ?;

Generated PHP Code

The plugin will generate PHP classes for the Models and Queries that you can use in your application. Usage with PDO:

useApp\Sqlc\MySQL\QueriesImpl;
$pdo = newPDO($dsn, $user, $password);
$author = (newQueriesImpl($pdo))->getAuthor(authorId: 1);

Development Status

  • ✅ MySQL support
  • ✅ SQLite support
  • 🚧 PostgreSQL support (Work in Progress)
  • ✅ Basic CRUD operations
  • ✅ Complex queries with joins
  • ✅ Type-safe parameters
  • ✅ Array parameters

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages