Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 20-Jan-2022 | - | ||||
locale/ | H | 20-Jan-2022 | - | |||
src/ | H | 20-Jan-2022 | - | 21,123 | 12,651 | |
CHANGELOG.md | H A D | 20-Jan-2022 | 15.4 KiB | 490 | 328 | |
README.md | H A D | 20-Jan-2022 | 4 KiB | 138 | 92 | |
composer.json | H A D | 20-Jan-2022 | 1.7 KiB | 60 | 59 |
README.md
1# SQL Parser 2 3A validating SQL lexer and parser with a focus on MySQL dialect. 4 5## Code status 6 7![Tests](https://github.com/phpmyadmin/sql-parser/workflows/Run%20tests/badge.svg?branch=master) 8[![Code Coverage](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/?branch=master) 9[![codecov.io](https://codecov.io/github/phpmyadmin/sql-parser/coverage.svg?branch=master)](https://codecov.io/github/phpmyadmin/sql-parser?branch=master) 10[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/phpmyadmin/sql-parser/?branch=master) 11[![Translation status](https://hosted.weblate.org/widgets/phpmyadmin/-/svg-badge.svg)](https://hosted.weblate.org/engage/phpmyadmin/?utm_source=widget) 12[![Packagist](https://img.shields.io/packagist/dt/phpmyadmin/sql-parser.svg)](https://packagist.org/packages/phpmyadmin/sql-parser) 13[![Open Source Helpers](https://www.codetriage.com/phpmyadmin/sql-parser/badges/users.svg)](https://www.codetriage.com/phpmyadmin/sql-parser) 14[![Type coverage](https://shepherd.dev/github/phpmyadmin/sql-parser/coverage.svg)](https://shepherd.dev/github/phpmyadmin/sql-parser) 15[![Infection MSI](https://badge.stryker-mutator.io/github.com/phpmyadmin/sql-parser/master)](https://infection.github.io) 16 17## Installation 18 19Please use [Composer][1] to install: 20 21```sh 22composer require phpmyadmin/sql-parser 23``` 24 25## Documentation 26 27The API documentation is available at 28<https://develdocs.phpmyadmin.net/sql-parser/>. 29 30## Usage 31 32### Command line utilities 33 34Command line utility to syntax highlight SQL query: 35 36```sh 37./vendor/bin/highlight-query --query "SELECT 1" 38``` 39 40Command line utility to lint SQL query: 41 42```sh 43./vendor/bin/lint-query --query "SELECT 1" 44``` 45 46Command line utility to tokenize SQL query: 47 48```sh 49./vendor/bin/tokenize-query --query "SELECT 1" 50``` 51 52All commands are able to parse input from stdin (standard in), such as: 53 54```sh 55echo "SELECT 1" | ./vendor/bin/highlight-query 56cat example.sql | ./vendor/bin/lint-query 57``` 58 59### Formatting SQL query 60 61```php 62echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, ['type' => 'html']); 63``` 64 65### Discoverying query type 66 67```php 68use PhpMyAdmin\SqlParser\Parser; 69use PhpMyAdmin\SqlParser\Utils\Query; 70 71$query = 'OPTIMIZE TABLE tbl'; 72$parser = new Parser($query); 73$flags = Query::getFlags($parser->statements[0]); 74 75echo $flags['querytype']; 76``` 77 78### Parsing and building SQL query 79 80```php 81require __DIR__ . '/vendor/autoload.php'; 82 83$query1 = 'select * from a'; 84$parser = new PhpMyAdmin\SqlParser\Parser($query1); 85 86// inspect query 87var_dump($parser->statements[0]); // outputs object(PhpMyAdmin\SqlParser\Statements\SelectStatement) 88 89// modify query by replacing table a with table b 90$table2 = new \PhpMyAdmin\SqlParser\Components\Expression('', 'b', '', ''); 91$parser->statements[0]->from[0] = $table2; 92 93// build query again from an array of object(PhpMyAdmin\SqlParser\Statements\SelectStatement) to a string 94$statement = $parser->statements[0]; 95$query2 = $statement->build(); 96var_dump($query2); // outputs string(19) 'SELECT * FROM `b` ' 97 98// Change SQL mode 99PhpMyAdmin\SqlParser\Context::setMode('ANSI_QUOTES'); 100 101// build the query again using different quotes 102$query2 = $statement->build(); 103var_dump($query2); // outputs string(19) 'SELECT * FROM "b" ' 104``` 105 106## Localization 107 108You can localize error messages installing `phpmyadmin/motranslator` version `5.0` or newer: 109 110```sh 111composer require phpmyadmin/motranslator:^5.0 112``` 113 114The locale is automatically detected from your environment, you can also set a different locale 115 116**From cli**: 117 118```sh 119LC_ALL=pl ./vendor/bin/lint-query --query "SELECT 1" 120``` 121 122**From php**: 123 124```php 125require __DIR__ . '/vendor/autoload.php'; 126 127$GLOBALS['lang'] = 'pl'; 128 129$query1 = 'select * from a'; 130$parser = new PhpMyAdmin\SqlParser\Parser($query1); 131``` 132 133## More information 134 135This library was originally created during the Google Summer of Code 2015 and has been used by phpMyAdmin since version 4.5. 136 137[1]:https://getcomposer.org/ 138