PHPackages                             sqlftw/sqlftw - 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. [Database &amp; ORM](/categories/database)
4. /
5. sqlftw/sqlftw

ActiveLibrary[Database &amp; ORM](/categories/database)

sqlftw/sqlftw
=============

Complete MySQL SQL dialect lexer, parser, AST and SQL serialisation

v0.1.17(1y ago)2587.4k—4.4%3[6 issues](https://github.com/SQLFTW/sqlftw/issues)[1 PRs](https://github.com/SQLFTW/sqlftw/pulls)1proprietaryPHPPHP ^7.4|^8.0

Since Dec 18Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/SQLFTW/sqlftw)[ Packagist](https://packagist.org/packages/sqlftw/sqlftw)[ Docs](https://github.com/sqlftw/sqlftw)[ RSS](/packages/sqlftw-sqlftw/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (7)Versions (22)Used By (1)

SQLFTW
======

[](#sqlftw)

MySQL (for now) lexer, parser, language model and static analyzer written in PHP

it is a validating parser which produces an object implementing SqlFtw\\Sql\\Command interface for each of approximately 140 supported SQL commands. Commands do model the syntactic aspect of SQL code, not domain aspect (models exactly how queries are written), however does not track white space and currently ignores some comments

this parser is intended as a basis for two other projects:

- one is doing static analysis of SQL code, especially safety and performance of migrations (currently using very basic SQL parser from phpMyAdmin project)
- another will hopefully help PHPStan (static analysis tool for PHP) better understand SQL queries and their results

on its own it can be used to validate syntax of SQL code (e.g. migrations)

SQL syntax support:
-------------------

[](#sql-syntax-support)

supports all SQL commands from MySQL 5.x to MySQL 8.0.34 and almost all language features

not supported features, that will fail to parse:

- support for ascii-incompatible multibyte encodings like `shift-jis`, `gb18030` or `utf-16` (fails to parse)
- quoted delimiters (not implemented, probably will fail)
- implicit string concatenation of double-quoted names in ANSI mode (`"foo" "bar"`; this is supported on strings, but not on names)

accepted, but ignored features (no model and serialization):

- resolving operator precedence in expressions (for now operators of the same tier are just parsed from left to right; will be implemented later)
- regular comments inside statements (comments before statement are collected)
- HeatWave plugin features (SECONDARY\_ENGINE)
- `SELECT ... PROCEDURE ANALYSE (...)` - removed in MySQL 8
- `WEIGHT_STRING(... LEVEL ...)` - removed in MySQL 8

features implemented other way than MySQL:

- parser produces an error on unterminated comments same as PostgreSQL does (MySQL is silent and according to tests, this might be a bug)
- parser produces an error when reading user variables with invalid name (MySQL silently ignores them and returns null)
- parser produces an error on optimizer hint with invalid syntax (MySQL produces a warning, AWS Aurora MySQL an error)

Architecture:
-------------

[](#architecture)

main layers:

- Lexer - tokenizes SQL, returns a Generator of parser Tokens
- Parser(s) - validates syntax and returns a Generator of parsed Command objects
- Command(s) - SQL commands parsed from plaintext to immutable object representation. can be serialized back to plaintext
- Platform - lists of features supported by particular platform
- Formatter - configurable SQL statements serializer
- Analyzer - static analysis rules and instrumentation for them

Basic usage:
------------

[](#basic-usage)

```
