← Winnitude! Search  ·  Reader Mode (992 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 122 LINKS

GitHub - phpmyadmin/sql-parser: A validating SQL lexer and parser with a focus on MySQL dialect.

Repository navigation

sql-parser
Public

About

A validating SQL lexer and parser with a focus on MySQL dialect.

packagist.org/packages/phpmyadmin/sql-parser

Topics

lexer-library linter mariadb mysql parser php-library php-sql-analyzer php-sql-highlighter php-sql-parser php-sql-tokenizer sql sql-analyzer sql-builder sql-lint sql-linter sql-parser sql-parsing sql-query sql-syntax-parser sql-tokenizing

Resources

Readme GPL-2.0 license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy Activity Custom properties

Stars

488 stars

Watchers

21 watching

Forks

125 forks Report repository
Branches

Tags
Go to Branches page Go to Tags page

Latest commit

 

History


2,635 Commits
View commit history for this file.

Folders and files

Name Name Last commit message Last commit date
.github .github    
bin bin    
locale locale    
src src    
tests tests    
tools tools    
.editorconfig .editorconfig    
.gitattributes .gitattributes    
.gitignore .gitignore    
.scrutinizer.yml .scrutinizer.yml    
.weblate .weblate    
CHANGELOG.md CHANGELOG.md    
CODE_OF_CONDUCT.md CODE_OF_CONDUCT.md    
CONTRIBUTING.md CONTRIBUTING.md    
LICENSE.txt LICENSE.txt    
README.md README.md    
composer.json composer.json    
infection.json.dist infection.json.dist    
phpbench.json phpbench.json    
phpcs.xml phpcs.xml    
phpstan-baseline.neon phpstan-baseline.neon    
phpstan.neon.dist phpstan.neon.dist    
phpunit.xml.dist phpunit.xml.dist    
psalm-baseline.xml psalm-baseline.xml    
psalm.xml psalm.xml    

Repository files navigation

SQL Parser

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';

PhpMyAdmin\SqlParser\Translator::setLocale('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.

About

A validating SQL lexer and parser with a focus on MySQL dialect.

packagist.org/packages/phpmyadmin/sql-parser

Topics

lexer-library linter mariadb mysql parser php-library php-sql-analyzer php-sql-highlighter php-sql-parser php-sql-tokenizer sql sql-analyzer sql-builder sql-lint sql-linter sql-parser sql-parsing sql-query sql-syntax-parser sql-tokenizing

Resources

Readme GPL-2.0 license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy Activity Custom properties

Stars

488 stars

Watchers

21 watching

Forks

125 forks Report repository

Releases

Sponsor this project

Used by

Contributors

Languages