phpmyadmin/sql-parser
phpmyadmin/sql-parser is a validating SQL lexer and parser for PHP with a focus on MySQL. It lets you parse, lint, tokenize, format, and analyze SQL queries, and ships with CLI utilities and a Composer-installable library for query discovery and formatting.
phpmyadmin/sql-parser package is a validating SQL lexer/parser specialized for MySQL dialect, making it ideal for:
ALTER TABLE, CREATE TABLE statements).Illuminate\Database\Query\Processors\Processor) for validation.illuminate.query events).| Risk Area | Assessment | Mitigation |
|---|---|---|
| False Positives/Negatives | Parser may misclassify valid MySQL syntax (e.g., dialect-specific functions). 6.0.1 fixes two real false-negative sources: index validation with sort order (#636) and null-alias handling in getAliases() (#658). |
Unit test edge cases; benchmark against real-world queries; re-run golden-query suite on upgrade. |
| Performance Overhead | Parsing adds latency to query execution (unchanged by 6.0.1). | Cache parsed results; use async validation where possible. |
| Breaking Changes | GPL-2.0 license may conflict with proprietary Laravel extensions. 6.0.1 is a patch release — no breaking API changes. | Audit license compliance; consider MIT/BSD alternatives if needed; pin to ^6.0. |
| Limited Error Context | Parser may not provide line/column details for complex queries. Partially improved: null-property crash (#609) and PHP 8.5 deprecation noise (#664) no longer mask the real error. | Extend error handling to include query snippets. |
| Maintenance / Release Cadence | Downgraded risk. The prior "no active maintenance" concern is contradicted by 6.0.1 shipping targeted bug fixes and forward PHP support. Residual risk is a small maintainer pool, not abandonment. | Track releases; fork if critical; keep upgrade window small to avoid large jumps. |
| Formatter Output Drift | JSON path operator spacing (->, ->>) changed in 6.0.1. |
Snapshot-test any SQL we round-trip through the formatter (query rewriting, generated-SQL assertions). |
^6.0 and take patch releases automatically, or gate every upgrade behind the golden-query suite? (New: 6.0.1 is a low-risk, high-value patch — recommend auto-adopting within ^6.0.)sqlparser-php, Doctrine DBAL) been evaluated for broader dialect support?DB::connection()->getQueryGrammar() hooks.retrieving, saving) to validate raw SQL.php artisan migrate for schema validation — directly benefits from the #636 index sort-order fix, since index definitions in migrations are a common parse failure.| Phase | Action | Tools/Dependencies |
|---|---|---|
| Proof of Concept | Parse 100 sample queries; validate against known edge cases. | PHPUnit, Pest |
| 6.0.1 Bump | Adopt as patch upgrade within ^6.0; re-run golden-query suite + formatter snapshots. |
Composer, CI pipeline |
| Core Integration | Wrap parser in a Laravel service; hook into QueryException handling. |
Laravel Service Container, Facade |
| Validation Layer | Add middleware to reject malformed SQL in APIs. | Laravel HTTP Middleware |
| Migration Tooling | Extend php artisan migrate to parse SQL files pre-execution. |
Laravel Artisan Commands |
| Optimization | Cache parsed results; batch-validate queries. | Redis, Symfony Cache |
JSON_TABLE) without manual rules.laravel/scout). PHPUnit 13 support is confined to require-dev.LIMIT syntax) if needed (highest risk). Re-baseline formatter snapshots due to the JSON-path spacing change.SqlParser class for project-specific validations.read property "keyword" on null, null alias offsets) are fixed, so failures now surface as structured parse errors rather than fatal errors.failed_queries.log for analysis.{"error": "Invalid SQL", "line": 42}).CREATE INDEX ... ASC/DESC and JSON-path queries post-upgrade.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Parser throws on malformed SQL | API timeouts; migration failures. | Fallback to try-catch; log and reject. 6.0.1 removes several fatal null-dereference paths. |
| False positives block valid SQL | Legitimate queries rejected. | Whitelist known-good queries; adjust rules. Re-test index sort-order and JSON-path queries — fixed in 6.0.1. |
| Dialect drift (MySQL 8.0+) | New syntax breaks parsing. | Test against latest MySQL; update parser rules. |
| Formatter snapshot churn | CI failures on SQL-format assertions after upgrade. | Re-baseline snapshots as part of the 6.0.1 bump commit; isolate formatter tests. |
| License compliance issues | Legal risk if used in proprietary code. | Audit dependencies; consider alternative licenses. |
README.md snippet for common use cases (e.g., API validation).SyntaxErrorException).How can I help you explore Laravel packages today?