summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 04ee4425ddfe806e0f2d1f7594b0d252878638cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# SQL Parser

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

## Code status

[![Build Status](https://travis-ci.org/phpmyadmin/sql-parser.svg?branch=master)](https://travis-ci.org/phpmyadmin/sql-parser)
[![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)

## Installation

Please use [Composer][1] 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:

```sh
./vendor/bin/highlight-query --query "SELECT 1"
```

Command line utility to lint SQL query:

```sh
./vendor/bin/lint-query --query "SELECT 1"
```

Command line utility to tokenize SQL query:

```sh
./vendor/bin/tokenize-query --query "SELECT 1"
```

All commands are able to parse input from stdin (standard in), such as:

```sh
echo "SELECT 1" | ./vendor/bin/highlight-query
cat example.sql | ./vendor/bin/lint-query
```
### 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'];
```

### 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('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:
```sh
composer require phpmyadmin/motranslator:^3.0
```

The locale is automatically detected from your enrivonment, you can also set a different locale

**From cli**:
```sh
LC_ALL=pl ./vendor/bin/lint-query --query "SELECT 1"
```

**From php**:
```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.

[1]:https://getcomposer.org/