Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpmyadmin/sql-parser
A validating SQL lexer and parser with a focus on MySQL dialect.
https://github.com/phpmyadmin/sql-parser
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
Last synced: 2 days ago
JSON representation
A validating SQL lexer and parser with a focus on MySQL dialect.
- Host: GitHub
- URL: https://github.com/phpmyadmin/sql-parser
- Owner: phpmyadmin
- License: gpl-2.0
- Created: 2015-05-25T21:56:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T11:47:59.000Z (about 2 months ago)
- Last Synced: 2024-10-29T14:51:28.495Z (about 1 month ago)
- 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
- Language: PHP
- Homepage: https://packagist.org/packages/phpmyadmin/sql-parser
- Size: 24.6 MB
- Stars: 443
- Watchers: 25
- Forks: 103
- Open Issues: 49
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- php-awesome - SQL Parser - SQL 解析器 (类库 / 数据库)
README
# SQL Parser
A validating SQL lexer and parser with a focus on MySQL dialect.
## Code status
![Tests](https://github.com/phpmyadmin/sql-parser/workflows/Run%20tests/badge.svg?branch=master)
[![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)
[![codecov.io](https://codecov.io/github/phpmyadmin/sql-parser/coverage.svg?branch=master)](https://codecov.io/github/phpmyadmin/sql-parser?branch=master)
[![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)
[![Translation status](https://hosted.weblate.org/widgets/phpmyadmin/-/svg-badge.svg)](https://hosted.weblate.org/engage/phpmyadmin/?utm_source=widget)
[![Packagist](https://img.shields.io/packagist/dt/phpmyadmin/sql-parser.svg)](https://packagist.org/packages/phpmyadmin/sql-parser)
[![Open Source Helpers](https://www.codetriage.com/phpmyadmin/sql-parser/badges/users.svg)](https://www.codetriage.com/phpmyadmin/sql-parser)
[![Type coverage](https://shepherd.dev/github/phpmyadmin/sql-parser/coverage.svg)](https://shepherd.dev/github/phpmyadmin/sql-parser)
[![Infection MSI](https://badge.stryker-mutator.io/github.com/phpmyadmin/sql-parser/master)](https://infection.github.io)## Installation
Please use [Composer][1] to install:
```sh
composer require phpmyadmin/sql-parser
```## Documentation
The API documentation is available at
.## Usage
### Command line utilities
Command line utility to syntax highlight SQL query:
```sh
./vendor/bin/sql-parser --highlight --query "SELECT 1"
```Command line utility to lint SQL query:
```sh
./vendor/bin/sql-parser --lint --query "SELECT 1"
```Command line utility to tokenize SQL query:
```sh
./vendor/bin/sql-parser --tokenize --query "SELECT 1"
```All commands are able to parse input from stdin (standard in), such as:
```sh
echo "SELECT 1" | ./vendor/bin/sql-parser --highlight
cat example.sql | ./vendor/bin/sql-parser --lint
```### Formatting SQL query
```php
echo PhpMyAdmin\SqlParser\Utils\Formatter::format($query, ['type' => 'html']);
```### Discoverying query type
```php
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
```php
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:
```sh
composer require phpmyadmin/motranslator:^5.0
```The locale is automatically detected from your environment, you can also set a different locale
**From cli**:
```sh
LC_ALL=pl ./vendor/bin/sql-parser --lint --query "SELECT 1"
```**From php**:
```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.
[1]:https://getcomposer.org/