phpmyadmin / sql-parser
A validating SQL lexer and parser with a focus on MySQL dialect.
Fund package maintenance!
Other
Installs: 33 385 140
Dependents: 51
Suggesters: 1
Security: 0
Stars: 442
Watchers: 25
Forks: 103
Open Issues: 49
Requires
- php: ^7.2 || ^8.0
- symfony/polyfill-mbstring: ^1.3
- symfony/polyfill-php80: ^1.16
Requires (Dev)
- phpbench/phpbench: ^1.1
- phpmyadmin/coding-standard: ^3.0
- phpmyadmin/motranslator: ^4.0 || ^5.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.9.12
- phpstan/phpstan-phpunit: ^1.3.3
- phpunit/phpunit: ^8.5 || ^9.6
- psalm/plugin-phpunit: ^0.16.1
- vimeo/psalm: ^4.11
- zumba/json-serializer: ~3.0.2
Suggests
- ext-mbstring: For best performance
- phpmyadmin/motranslator: Translate messages to your favorite locale
Conflicts
- phpmyadmin/motranslator: <3.0
- dev-master / 6.0.x-dev
- 5.11.x-dev
- 5.10.x-dev
- 5.10.1
- 5.10.0
- 5.9.x-dev
- 5.9.1
- 5.9.0
- 5.8.x-dev
- 5.8.2
- 5.8.1
- 5.8.0
- 5.7.0
- 5.6.0
- 5.5.0
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 4.7.x-dev
- 4.7.3
- 4.7.2
- 4.7.1
- 4.7.0
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.2
- v4.3.1
- v4.3.0
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.10
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.1
- v4.0.0
- v3.4.17
- v3.4.16
- v3.4.15
- v3.4.14
- v3.4.13
- v3.4.12
- v3.4.11
- v3.4.10
- v3.4.9
- v3.4.8
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
This package is auto-updated.
Last update: 2024-11-20 17:00:15 UTC
README
A validating SQL lexer and parser with a focus on MySQL dialect.
Code status
Installation
Please use Composer to install:
composer require phpmyadmin/sql-parser
Documentation
The API documentation is available at https://develdocs.phpmyadmin.net/sql-parser/.
Usage
Command line utilities
Command line utility to syntax highlight SQL query:
./vendor/bin/sql-parser --highlight --query "SELECT 1"
Command line utility to lint SQL query:
./vendor/bin/sql-parser --lint --query "SELECT 1"
Command line utility to tokenize SQL query:
./vendor/bin/sql-parser --tokenize --query "SELECT 1"
All commands are able to parse input from stdin (standard in), such as:
echo "SELECT 1" | ./vendor/bin/sql-parser --highlight cat example.sql | ./vendor/bin/sql-parser --lint
Formatting SQL query
echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, ['type' => 'html']);
Discoverying query type
use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Utils\Query; $query = 'OPTIMIZE TABLE tbl'; $parser = new Parser($query); $flags = Query::getFlags($parser->statements[0]); echo $flags->queryType?->value;
Parsing and building SQL query
require __DIR__ . '/vendor/autoload.php'; $query1 = 'select * from a'; $parser = new PhpMyAdmin\SqlParser\Parser($query1); // inspect query var_dump($parser->statements[0]); // outputs object(PhpMyAdmin\SqlParser\Statements\SelectStatement) // modify query by replacing table a with table b $table2 = new \PhpMyAdmin\SqlParser\Components\Expression('', 'b', '', ''); $parser->statements[0]->from[0] = $table2; // build query again from an array of object(PhpMyAdmin\SqlParser\Statements\SelectStatement) to a string $statement = $parser->statements[0]; $query2 = $statement->build(); var_dump($query2); // outputs string(19) 'SELECT * FROM `b` ' // Change SQL mode PhpMyAdmin\SqlParser\Context::setMode(PhpMyAdmin\SqlParser\Context::SQL_MODE_ANSI_QUOTES); // build the query again using different quotes $query2 = $statement->build(); var_dump($query2); // outputs string(19) 'SELECT * FROM "b" '
Localization
You can localize error messages installing phpmyadmin/motranslator
version 5.0
or newer:
composer require phpmyadmin/motranslator:^5.0
The locale is automatically detected from your environment, you can also set a different locale
From cli:
LC_ALL=pl ./vendor/bin/sql-parser --lint --query "SELECT 1"
From php:
require __DIR__ . '/vendor/autoload.php'; $GLOBALS['lang'] = 'pl'; $query1 = 'select * from a'; $parser = new PhpMyAdmin\SqlParser\Parser($query1);
More information
This library was originally created during the Google Summer of Code 2015 and has been used by phpMyAdmin since version 4.5.