|
| 1 | +[](https://travis-ci.org/nilportugues/sql-query-formatter) SQL Query Formatter |
| 2 | +================= |
| 3 | + |
| 4 | +[](https://packagist.org/packages/nilportugues/sql-query-formatter) [](https://packagist.org/packages/nilportugues/sql-query-formatter) [](https://packagist.org/packages/nilportugues/sql-query-formatter) [](https://packagist.org/packages/nilportugues/sql-query-formatter) |
| 5 | + |
| 6 | + |
| 7 | +A very lightweight PHP class that re-formats unreadable and computer-generated SQL query statements to human-friendly, readable text. |
| 8 | + |
| 9 | +* [1.Installation](#block1) |
| 10 | +* [2. Features](#block2) |
| 11 | +* [3. Usage](#block3) |
| 12 | +* [4. Code Quality](#block5) |
| 13 | +* [5. Author](#block6) |
| 14 | +* [6. Special Thanks](#block6) |
| 15 | +* [7. License](#block7) |
| 16 | + |
| 17 | +<a name="block1"></a> |
| 18 | +# 1.Installation |
| 19 | +Add the following to your `composer.json` file : |
| 20 | + |
| 21 | +```js |
| 22 | +{ |
| 23 | + "require": { |
| 24 | + "nilportugues/sql-query-formatter": "1.0.0" |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | +--- |
| 29 | +<a name="block2"></a> |
| 30 | +# 2. Features |
| 31 | + |
| 32 | +**Human readable SQL formatting** |
| 33 | + |
| 34 | +- Human readable plain text. No colours, no highlighting. Plain text is good enough in most cases. |
| 35 | + |
| 36 | +**Data Binding Awareness** |
| 37 | + |
| 38 | +- SQL Query Formatter takes data binding seriously. |
| 39 | +- Placeholder syntax such as `:variable` or `?` is taken into account and is preserved when formatting. |
| 40 | + |
| 41 | +<a name="block3"></a> |
| 42 | +# 3. Usage |
| 43 | + |
| 44 | +Sample code: |
| 45 | +```php |
| 46 | +<?php |
| 47 | +use NilPortugues\SqlQueryFormatter\Formatter; |
| 48 | + |
| 49 | +$query = <<<SQL |
| 50 | +SELECT user.user_id, user.username, (SELECT role.role_name FROM role |
| 51 | +WHERE (role.role_id = :v1) LIMIT :v2, :v3 ) AS user_role, (SELECT role.role_name FROM |
| 52 | +role WHERE (role.role_id = :v4) LIMIT :v5, :v6 ) AS role FROM user WHERE (user.user_id = :v7) |
| 53 | +SQL; |
| 54 | + |
| 55 | +$formatter = new Formatter(); |
| 56 | +echo $formatter->format($query); |
| 57 | +``` |
| 58 | + |
| 59 | +Real output: |
| 60 | +```sql |
| 61 | +SELECT |
| 62 | + user.user_id, |
| 63 | + user.username, |
| 64 | + ( |
| 65 | + SELECT |
| 66 | + role.role_name |
| 67 | + FROM |
| 68 | + role |
| 69 | + WHERE |
| 70 | + (role.role_id = :v1) |
| 71 | + LIMIT |
| 72 | + :v2, |
| 73 | + :v3 |
| 74 | + ) AS user_role, |
| 75 | + ( |
| 76 | + SELECT |
| 77 | + role.role_name |
| 78 | + FROM |
| 79 | + role |
| 80 | + WHERE |
| 81 | + (role.role_id = :v4) |
| 82 | + LIMIT |
| 83 | + :v5, |
| 84 | + :v6 |
| 85 | + ) AS role |
| 86 | +FROM |
| 87 | + user |
| 88 | +WHERE |
| 89 | + (user.user_id = :v7) |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +<a name="block4"></a> |
| 94 | +# 4. Fully tested |
| 95 | +Testing has been done using PHPUnit and [Travis-CI](https://travis-ci.org). All code has been tested to be compatible from PHP 5.3 up to PHP 5.6 and [HHVM](http://hhvm.com/). |
| 96 | + |
| 97 | +To run the test suite, you need [Composer](http://getcomposer.org): |
| 98 | + |
| 99 | +```bash |
| 100 | + $ php composer.phar install --dev |
| 101 | + $ vendor/bin/phpunit |
| 102 | +``` |
| 103 | +--- |
| 104 | + |
| 105 | +<a name="block5"></a> |
| 106 | +# 5. Author |
| 107 | +Nil Portugués Calderó |
| 108 | + |
| 109 | + - <contact@nilportugues.com> |
| 110 | + - [http://nilportugues.com](http://nilportugues.com) |
| 111 | + |
| 112 | +--- |
| 113 | +<a name="block6"></a> |
| 114 | +# 6. Special Thanks |
| 115 | +I would like to thank the following people: |
| 116 | + |
| 117 | +- Jeremy Dorn <jeremy@jeremydorn.com> for his [sql-formatter](https://github.com/jdorn/sql-formatter) implementation I used as a basis for building this version. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +<a name="block7"></a> |
| 122 | +# 7. License |
| 123 | +SQL Query Builder is licensed under the MIT license. |
| 124 | + |
| 125 | +``` |
| 126 | +Copyright (c) 2014 Nil Portugués Calderó |
| 127 | +
|
| 128 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 129 | +of this software and associated documentation files (the "Software"), to deal |
| 130 | +in the Software without restriction, including without limitation the rights |
| 131 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 132 | +copies of the Software, and to permit persons to whom the Software is |
| 133 | +furnished to do so, subject to the following conditions: |
| 134 | +
|
| 135 | +The above copyright notice and this permission notice shall be included in |
| 136 | +all copies or substantial portions of the Software. |
| 137 | +
|
| 138 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 139 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 140 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 141 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 142 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 143 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 144 | +THE SOFTWARE. |
| 145 | +``` |
0 commit comments