k-kinzal / sql-semantics-mysql
MySQL statement models, SQL reconstruction, and schema binding for SQL Semantics
Requires
- php: ^8.1
- k-kinzal/sql-parser: dev-main
- k-kinzal/sql-semantics: dev-main
Requires (Dev)
- brianium/paratest: ^7.3
- deptrac/deptrac: ^3.0
- fakerphp/faker: ^1.23
- friendsofphp/php-cs-fixer: ^3.95.25
- k-kinzal/bison-parser: dev-main
- k-kinzal/php-ai-toolkit: dev-main
- k-kinzal/sql-faker: dev-main
- k-kinzal/sql-formatter: dev-main
- nikic/php-fuzzer: ^0.0.11
- phpbench/phpbench: ^1.4
- phpcompatibility/php-compatibility: ^10.0.0-alpha2
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^4.0.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-28 02:33:48 UTC
README
SQL Semantics for MySQL adds MySQL to SQL Semantics: the typed statement models of the official MySQL grammars, the MySQL rules for reading declarations, and the MySQL builder that composes values under stable names. Installing it also installs the shared SQL Semantics runtime, and Dialect::MySql selects MySQL in the runtime's Semantics, and Mode::fromString() reads a session's sql_mode for it. No database connection is needed.
Requirements
- PHP 8.1+ with the zlib extension
Support Syntax
The following grammar versions are supported. Pass the version tag as the second argument of Semantics; omitting it uses the default. State declarations and composition support all listed versions; common table expressions in Builder need MySQL 8.0 or later, as does a select() with a condition but no table, and cast() to FLOAT needs 8.0.17 and to YEAR 8.0.22.
| Version | Version tag | Default |
|---|---|---|
| 5.6.51 | mysql-5.6.51 |
|
| 5.7.44 | mysql-5.7.44 |
|
| 8.0.44 | mysql-8.0.44 |
|
| 8.1.0 | mysql-8.1.0 |
|
| 8.2.0 | mysql-8.2.0 |
|
| 8.3.0 | mysql-8.3.0 |
|
| 8.4.7 | mysql-8.4.7 |
Yes |
| 9.0.1 | mysql-9.0.1 |
|
| 9.1.0 | mysql-9.1.0 |
Installation
composer require k-kinzal/sql-semantics-mysql
Usage
use SqlSemantics\Facade\Semantics; use SqlSemantics\Platform\MySql\Dialect; $statement = (new Semantics(Dialect::MySql))->analyze("INSERT INTO users (id, name) VALUES (1, 'Alice') ON DUPLICATE KEY UPDATE name = 'Alice'"); $statement->toString(); // "INSERT INTO users ( id , name ) VALUES( 1 , 'Alice' ) ON DUPLICATE KEY UPDATE name = 'Alice'"
Read SQL as the session reads it, and compose values spelled for that session:
use SqlSemantics\Facade\Semantics; use SqlSemantics\Platform\MySql\Dialect; use SqlSemantics\Platform\MySql\Mode; use SqlSemantics\Statement\Writer; $semantics = new Semantics(Dialect::MySql, mode: Mode::fromString('ANSI_QUOTES,NO_BACKSLASH_ESCAPES')); $semantics->analyze('SELECT "name" FROM users')->toString(); // "name" is an identifier under ANSI_QUOTES $builder = $semantics->builder(); Writer::render($builder->compare($builder->column('select'), '=', $builder->string('C:\path'))); // "`select` = 'C:\path'"
See the SQL Semantics documentation for statement models, traversal, dependencies, and composition.
License
MIT License. See LICENSE for details.