Skip to content

Commit fc5b0a7

Browse files
authored
Merge pull request #24 from greg0ire/drop-features
Drop features unrelated to formatting
2 parents 7dea318 + 69e764b commit fc5b0a7

File tree

4 files changed

+0
-251
lines changed

4 files changed

+0
-251
lines changed

README.md

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -104,89 +104,3 @@ Output:
104104
```sql
105105
SELECT Id as temp, DateCreated as Created FROM MyTable;
106106
```
107-
108-
### Remove Comments
109-
110-
If you want to keep all original whitespace formatting and just remove comments,
111-
you can use the removeComments method instead of compress.
112-
113-
```sql
114-
-- This is a comment
115-
SELECT
116-
/* This is another comment
117-
On more than one line */
118-
Id #This is one final comment
119-
as temp, DateCreated as Created FROM MyTable;
120-
```
121-
122-
```php
123-
<?php
124-
echo (new SqlFormatter())->removeComments($query);
125-
```
126-
127-
Output:
128-
```sql
129-
130-
SELECT
131-
132-
Id
133-
as temp, DateCreated as Created FROM MyTable;
134-
```
135-
136-
### Split SQL String into Queries
137-
138-
Another feature, which is unrelated to formatting, is the ability to break up a
139-
SQL string into multiple queries.
140-
141-
For Example:
142-
143-
```sql
144-
DROP TABLE IF EXISTS MyTable;
145-
CREATE TABLE MyTable ( id int );
146-
INSERT INTO MyTable (id)
147-
VALUES
148-
(1),(2),(3),(4);
149-
SELECT * FROM MyTable;
150-
```
151-
152-
```php
153-
<?php
154-
$queries = (new SqlFormatter())->splitQuery($sql);
155-
```
156-
157-
Result:
158-
159-
1. `DROP TABLE IF EXISTS MyTable`;
160-
2. `CREATE TABLE MyTable ( id int )`;
161-
3. `INSERT INTO MyTable (id) VALUES (1),(2),(3),(4)`;
162-
4. `SELECT * FROM MyTable`;
163-
164-
#### Why Not Regular Expressions?
165-
166-
Why not just use `explode(';', $sql)` or a regular expression?
167-
168-
The following example sql and others like it are _impossible_ to split
169-
correctly using regular expressions, no matter how complex.
170-
171-
```sql
172-
SELECT ";"; SELECT ";\"; a;";
173-
SELECT ";
174-
abc";
175-
SELECT a,b #comment;
176-
FROM test;
177-
```
178-
179-
SqlFormatter breaks the string into tokens instead of using regular expressions
180-
and will correctly produce:
181-
182-
1. `SELECT ";"`;
183-
2. `SELECT ";\"; a;"`;
184-
3. `SELECT "; abc"`;
185-
4. `SELECT a,b #comment;
186-
FROM test`;
187-
188-
Please note, the splitQuery method will still fail in the following cases:
189-
* The DELIMITER command can be used to change the delimiter from the default
190-
';' to something else.
191-
* The CREATE PROCEDURE command has a ';' in the middle of it
192-
* The USE command is not terminated with a ';'

examples/examples.php

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -248,66 +248,5 @@
248248
</tr>
249249
<?php endforeach ?>
250250
</table>
251-
252-
253-
<h1>Splitting SQL Strings Into Individual Queries</h1>
254-
255-
<div>
256-
Usage:
257-
<pre>
258-
<?php highlight_string(
259-
'<?php' . "\n" . '$queries = (new SqlFormatter())->splitQuery($sql);' . "\n" . '?>'
260-
); ?>
261-
</pre>
262-
</div>
263-
<table>
264-
<tr>
265-
<th>Original</th>
266-
<th>Split</th>
267-
</tr>
268-
<?php foreach ($splitStatements as $sql) : ?>
269-
<tr>
270-
<td>
271-
<pre><?= $formatter->highlight($sql); ?></pre>
272-
</td>
273-
<td>
274-
<ol>
275-
<?php foreach ($formatter->splitQuery($sql) as $query) :?>
276-
<li><pre><?= $formatter->highlight($query) ?></pre></li>
277-
<?php endforeach ?>
278-
</ol>
279-
</td>
280-
</tr>
281-
<?php endforeach ?>
282-
</table>
283-
284-
285-
<h1>Removing Comments</h1>
286-
287-
<div>
288-
Usage:
289-
<pre>
290-
<?php highlight_string(
291-
'<?php' . "\n" . '$nocomments = (new SqlFormatter())->removeComments($sql);' . "\n" . '?>'
292-
); ?>
293-
</pre>
294-
</div>
295-
<table>
296-
<tr>
297-
<th>Original</th>
298-
<th>Comments Removed</th>
299-
</tr>
300-
<?php foreach ($commentStatements as $sql) : ?>
301-
<tr>
302-
<td>
303-
<pre><?= $formatter->highlight($sql); ?></pre>
304-
</td>
305-
<td>
306-
<pre><?= $formatter->highlight($formatter->removeComments($sql)) ?></pre>
307-
</td>
308-
</tr>
309-
<?php endforeach ?>
310-
</table>
311-
312251
</body>
313252
</html>

src/SqlFormatter.php

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,79 +1428,6 @@ public function highlight(string $string) : string
14281428
return $this->highlighter->output($return);
14291429
}
14301430

1431-
/**
1432-
* Split a SQL string into multiple queries.
1433-
* Uses ";" as a query delimiter.
1434-
*
1435-
* @param string $string The SQL string
1436-
*
1437-
* @return string[] An array of individual query strings without trailing semicolons
1438-
*/
1439-
public function splitQuery(string $string) : array
1440-
{
1441-
$queries = [];
1442-
$currentQuery = '';
1443-
$empty = true;
1444-
1445-
$tokens = $this->tokenize($string);
1446-
1447-
foreach ($tokens as $token) {
1448-
// If this is a query separator
1449-
if ($token[self::TOKEN_VALUE] === ';') {
1450-
if (! $empty) {
1451-
$queries[] = $currentQuery . ';';
1452-
}
1453-
1454-
$currentQuery = '';
1455-
$empty = true;
1456-
continue;
1457-
}
1458-
1459-
// If this is a non-empty character
1460-
if ($token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE &&
1461-
$token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_COMMENT &&
1462-
$token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_BLOCK_COMMENT) {
1463-
$empty = false;
1464-
}
1465-
1466-
$currentQuery .= $token[self::TOKEN_VALUE];
1467-
}
1468-
1469-
if (! $empty) {
1470-
$queries[] = trim($currentQuery);
1471-
}
1472-
1473-
return $queries;
1474-
}
1475-
1476-
/**
1477-
* Remove all comments from a SQL string
1478-
*
1479-
* @param string $string The SQL string
1480-
*
1481-
* @return string The SQL string without comments
1482-
*/
1483-
public function removeComments(string $string) : string
1484-
{
1485-
$result = '';
1486-
1487-
$tokens = $this->tokenize($string);
1488-
1489-
foreach ($tokens as $token) {
1490-
// Skip comment tokens
1491-
if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT ||
1492-
$token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
1493-
continue;
1494-
}
1495-
1496-
$result .= $token[self::TOKEN_VALUE];
1497-
}
1498-
1499-
$result = $this->format($result, false);
1500-
1501-
return $result;
1502-
}
1503-
15041431
/**
15051432
* Compress a query by collapsing white space and removing comments
15061433
*

tests/SqlFormatterTest.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use function defined;
1313
use function explode;
1414
use function file_get_contents;
15-
use function implode;
1615
use function pack;
1716
use function trim;
1817

@@ -112,36 +111,6 @@ public function testUsePre() : void
112111
$this->assertEquals($actual, $expected);
113112
}
114113

115-
public function testSplitQuery() : void
116-
{
117-
$expected = [
118-
"SELECT 'test' FROM MyTable;",
119-
'SELECT Column2 FROM SomeOther Table WHERE (test = true);',
120-
];
121-
122-
$actual = self::$formatter->splitQuery(implode(';', $expected));
123-
124-
$this->assertEquals($expected, $actual);
125-
}
126-
127-
public function testSplitQueryEmpty() : void
128-
{
129-
$sql = "SELECT 1;SELECT 2;\n-- This is a comment\n;SELECT 3";
130-
$expected = ['SELECT 1;','SELECT 2;','SELECT 3'];
131-
$actual = self::$formatter->splitQuery($sql);
132-
133-
$this->assertEquals($expected, $actual);
134-
}
135-
136-
public function testRemoveComments() : void
137-
{
138-
$expected = self::$formatter->format("SELECT\n * FROM\n MyTable", false);
139-
$sql = "/* this is a comment */SELECT#This is another comment\n * FROM-- One final comment\n MyTable";
140-
$actual = self::$formatter->removeComments($sql);
141-
142-
$this->assertEquals($expected, $actual);
143-
}
144-
145114
public function testCacheStats() : void
146115
{
147116
$stats = self::$formatter->getCacheStats();

0 commit comments

Comments
 (0)