jtfnetoo / sql-parser
Fork of phpmyadmin https://github.com/phpmyadmin/sql-parser.
Requires
- php: >=5.3.0
- ext-mbstring: *
Requires (Dev)
- phpunit/php-code-coverage: *
- phpunit/phpunit: ~4.8 || ~5.7
Suggests
- phpmyadmin/motranslator: Translate messages to your favorite locale
Conflicts
- phpmyadmin/motranslator: <3.0
- dev-master
- 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
- dev-motranslator
This package is not auto-updated.
Last update: 2025-03-21 23:18:21 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/highlight-query --query "SELECT 1"
Command line utility to lint SQL query:
./vendor/bin/lint-query --query "SELECT 1"
Command line utility to tokenize SQL query:
./vendor/bin/tokenize-query --query "SELECT 1"
Formatting SQL query
echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, array('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'];
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('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 3.0
or newer:
composer require phpmyadmin/motranslator:^3.0
The locale is automatically detected from your enrivonment, you can also set a different locale
From cli:
LC_ALL=pl ./vendor/bin/lint-query --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.