Search by

k-kinzal / sql-semantics-mysql

kinzal

MySQL statement models, SQL reconstruction, and schema binding for SQL Semantics

Package info

github.com/k-kinzal/sql-semantics-mysql

Documentation

pkg:composer/k-kinzal/sql-semantics-mysql

Statistics

Installs: 0

Dependents: 1

Suggesters: 0

Stars: 0

dev-main 2026-09-28 02:33 UTC

This package is auto-updated.

Last update: 2026-09-28 02:33:48 UTC


README

Packagist Downloads PHP Version License: MIT Docs Ask DeepWiki

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.