PHPackages                             k-kinzal/ztd-query-mysql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. k-kinzal/ztd-query-mysql

ActiveLibrary

k-kinzal/ztd-query-mysql
========================

MySQL platform support for ZTD Query

v0.1.1(2w ago)016↓100%1MITPHPPHP ^8.1

Since Mar 6Pushed 2w agoCompare

[ Source](https://github.com/k-kinzal/ztd-query-mysql)[ Packagist](https://packagist.org/packages/k-kinzal/ztd-query-mysql)[ RSS](/packages/k-kinzal-ztd-query-mysql/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (11)Versions (3)Used By (1)

ZTD Query MySQL
===============

[](#ztd-query-mysql)

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)[![PHP Version](https://camo.githubusercontent.com/7535257ca228724c93658bd52583d4e47a9bab02c356abf6e54c1d575f2151e6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c75652e737667)](https://www.php.net/)

MySQL platform support for [ZTD Query PHP](https://github.com/k-kinzal/ztd-query-core). Provides SQL parsing, classification, rewriting, and schema management for MySQL.

Overview
--------

[](#overview)

This package implements the MySQL-specific logic for ZTD (Zero Table Dependency) query transformation. It handles:

- **SQL Parsing** - Parse MySQL statements using [phpMyAdmin SQL Parser](https://github.com/phpmyadmin/sql-parser)
- **Query Classification** - Classify queries as READ, WRITE\_SIMULATED, or DDL\_SIMULATED
- **CTE Rewriting** - Transform SELECT queries to use CTE-shadowed fixture data
- **Result Select Query** - Convert INSERT/UPDATE/DELETE/REPLACE into SELECT queries returning affected rows
- **Schema Management** - Reflect and track MySQL table definitions for virtual DDL operations
- **Error Classification** - Identify MySQL-specific error codes for unknown schema detection

This package is used internally by the [PDO adapter](https://github.com/k-kinzal/ztd-query-pdo-adapter) and [MySQLi adapter](https://github.com/k-kinzal/ztd-query-mysqli-adapter), but can also be used directly for custom adapter implementations.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- [k-kinzal/ztd-query-php](https://github.com/k-kinzal/ztd-query-core) (core)

Installation
------------

[](#installation)

```
composer require k-kinzal/ztd-query-mysql
```

Usage
-----

[](#usage)

### Creating a MySQL Session

[](#creating-a-mysql-session)

`MySqlSessionFactory` is the main entry point. It creates a fully configured `Session` instance for MySQL:

```
use ZtdQuery\Config\ZtdConfig;
use ZtdQuery\Platform\MySql\MySqlSessionFactory;

// $connection implements ZtdQuery\Connection\ConnectionInterface
$session = MySqlSessionFactory::create($connection, ZtdConfig::default());
```

The factory automatically:

1. Reflects the database schema via `SHOW CREATE TABLE`
2. Sets up the SQL parser, query guard, and all transformers
3. Configures the shadow store for virtual write tracking

### Query Classification

[](#query-classification)

`MySqlQueryGuard` classifies SQL statements into query kinds:

```
use ZtdQuery\Platform\MySql\MySqlQueryGuard;
use ZtdQuery\Platform\MySql\MySqlParser;
use ZtdQuery\Rewrite\QueryKind;

$parser = new MySqlParser();
$guard = new MySqlQueryGuard($parser);

$guard->classify('SELECT * FROM users');
// => QueryKind::READ

$guard->classify('INSERT INTO users (name) VALUES ("Alice")');
// => QueryKind::WRITE_SIMULATED

$guard->classify('CREATE TABLE logs (id INT)');
// => QueryKind::DDL_SIMULATED

$guard->classify('BEGIN');
// => null (unsupported)
```

### SQL Rewriting

[](#sql-rewriting)

`MySqlRewriter` transforms SQL statements for ZTD execution:

```
use ZtdQuery\Platform\MySql\MySqlRewriter;

// Rewrite a single statement
$plan = $rewriter->rewrite('SELECT email FROM users WHERE id = 1');
// $plan->sql() returns the CTE-shadowed query
// $plan->kind() returns the QueryKind

// Rewrite multiple statements (e.g., multi-query)
$plans = $rewriter->rewriteMultiple('SELECT 1; SELECT 2');
```

### Error Classification

[](#error-classification)

`MySqlErrorClassifier` identifies MySQL error codes related to unknown schemas:

```
use ZtdQuery\Platform\MySql\MySqlErrorClassifier;

$classifier = new MySqlErrorClassifier();

$classifier->isUnknownSchemaError(1146); // true (Table doesn't exist)
$classifier->isUnknownSchemaError(1054); // true (Unknown column)
$classifier->isUnknownSchemaError(1064); // false (Syntax error)
```

Architecture
------------

[](#architecture)

```
MySqlSessionFactory
    |
    +-- MySqlParser (SQL parsing via phpmyadmin/sql-parser)
    +-- MySqlQueryGuard (query classification)
    +-- MySqlSchemaReflector (database schema reflection)
    +-- MySqlSchemaParser (CREATE TABLE parsing)
    +-- MySqlRewriter (query rewriting orchestrator)
    |       +-- MySqlTransformer
    |       |       +-- SelectTransformer (CTE injection)
    |       |       +-- InsertTransformer (INSERT -> SELECT)
    |       |       +-- UpdateTransformer (UPDATE -> SELECT)
    |       |       +-- DeleteTransformer (DELETE -> SELECT)
    |       |       +-- ReplaceTransformer (REPLACE -> SELECT)
    |       +-- MySqlMutationResolver (virtual DDL tracking)
    +-- MySqlErrorClassifier (error code classification)

```

SQL Support
-----------

[](#sql-support)

### Fully Supported

[](#fully-supported)

- **SELECT**: All clauses including JOIN, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, subqueries, CTEs, window functions
- **INSERT**: VALUES, SELECT, ON DUPLICATE KEY UPDATE, IGNORE
- **REPLACE**
- **UPDATE**: Single/multi-table with ORDER BY/LIMIT
- **DELETE**: Single/multi-table with ORDER BY/LIMIT
- **TRUNCATE**
- **DDL**: CREATE TABLE, ALTER TABLE, DROP TABLE (virtual schema)
- **WITH**: CTE and recursive CTE

### Unsupported

[](#unsupported)

- Stored procedures, triggers, functions, views
- Database/schema operations
- User/permission management
- Server operations (FLUSH, RESET, etc.)

Development
-----------

[](#development)

```
# Run tests
composer test

# Run linter (PHP-CS-Fixer + PHPStan level max)
composer lint

# Run fuzz tests
composer fuzz:robustness
composer fuzz:robustness:classify
composer fuzz:robustness:rewrite

# Fix code style
composer format
```

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

###  Health Score

38

—

LowBetter than 86% of packages

Maintenance97

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

16d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4458a22db62c885006248e08715301bcb73e78c1057e2bbf686af001729165d5?d=identicon)[kinzal](/maintainers/kinzal)

---

Top Contributors

[![k-kinzal](https://avatars.githubusercontent.com/u/1281825?v=4)](https://github.com/k-kinzal "k-kinzal (5 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/k-kinzal-ztd-query-mysql/health.svg)

```
[![Health](https://phpackages.com/badges/k-kinzal-ztd-query-mysql/health.svg)](https://phpackages.com/packages/k-kinzal-ztd-query-mysql)
```

###  Alternatives

[phpmyadmin/phpmyadmin

A web interface for MySQL and MariaDB

15374.5k4](/packages/phpmyadmin-phpmyadmin)[calebdw/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

1477.6k2](/packages/calebdw-larastan)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
