Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CASE WHEN format #101

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use function array_unshift;
use function assert;
use function current;
use function in_array;
use function preg_replace;
use function reset;
use function rtrim;
use function str_repeat;
use function str_replace;
use function strlen;
use function strtoupper;
use function trim;

use const PHP_SAPI;
Expand Down Expand Up @@ -252,10 +254,28 @@ public function format(string $string, string $indentString = ' '): string
$highlighted = preg_replace('/\s+/', ' ', $highlighted);
}

//if SQL 'LIMIT' clause, start variable to reset newline
if ($token->value() === 'LIMIT' && ! $inlineParentheses) {
// if SQL 'LIMIT' clause, start variable to reset newline
if (strtoupper($token->value()) === 'LIMIT' && ! $inlineParentheses) {
$clauseLimit = true;
}
} elseif (strtoupper($token->value()) === 'CASE') {
$increaseBlockIndent = true;
} elseif (in_array(strtoupper($token->value()), ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
if (strtoupper($token->value()) !== 'THEN') {
array_shift($indentTypes);
$indentLevel--;

$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'CASE') {
$return = rtrim($return, ' ');
$return .= "\n" . str_repeat($tab, $indentLevel);
}
}

if (strtoupper($token->value()) === 'THEN' || strtoupper($token->value()) === 'ELSE') {
$newline = true;
$increaseBlockIndent = true;
}
} elseif (
$clauseLimit &&
$token->value() !== ',' &&
Expand All @@ -265,7 +285,7 @@ public function format(string $string, string $indentString = ' '): string
$clauseLimit = false;
} elseif ($token->value() === ',' && ! $inlineParentheses) {
// Commas start a new line (unless within inline parentheses or SQL 'LIMIT' clause)
//If the previous TOKEN_VALUE is 'LIMIT', resets new line
// If the previous TOKEN_VALUE is 'LIMIT', resets new line
if ($clauseLimit === true) {
$newline = false;
$clauseLimit = false;
Expand Down
36 changes: 36 additions & 0 deletions tests/clihighlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,39 @@
---
SELECT
1 :: text;
---
SELECT
case when name = 1 then
10
when name = 2 then
20
when name = 3 then
case when age > 10 then
30
else
31
end
else
40
end AS case1,
(
SELECT
case name
when 1 then
10
when 2 then
20
when 3 then
case age
when 10 then
30
else
31
end
else
40
end
) case2,
name
FROM
user
2 changes: 2 additions & 0 deletions tests/compress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ WITH cte1 AS (SELECT a, b FROM table1), cte2 AS (SELECT c, d FROM table2) SELECT
SELECT a, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS) AS no_others, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) AS current_row, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP) AS grp, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE TIES) AS tie, GROUP_CONCAT(b, '.') FILTER (WHERE c != 'two') OVER (ORDER BY a) AS filtered, CONVERT(VARCHAR(20), AVG(SalesYTD) OVER (PARTITION BY TerritoryID ORDER BY DATEPART(yy, ModifiedDate)), 1) AS MovingAvg, AVG(starting_salary) OVER w2 AVG, MIN(starting_salary) OVER w2 MIN_STARTING_SALARY, MAX(starting_salary) OVER (w1 ORDER BY hire_date), LISTAGG(arg, ',') OVER (PARTITION BY part ORDER BY ord ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS LISTAGG_ROWS, LISTAGG(arg, ',') OVER (PARTITION BY part ORDER BY ord RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS LISTAGG_RANGE, MIN(Revenue) OVER (PARTITION BY DepartmentID ORDER BY RevenueYear ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS MinRevenueBeyond FROM t1 WINDOW w1 AS (PARTITION BY department, division), w2 AS (w1 ORDER BY hire_date);
---
SELECT 1::text;
---
SELECT case when name = 1 then 10 when name = 2 then 20 when name = 3 then case when age > 10 then 30 else 31 end else 40 end AS case1, (SELECT case name when 1 then 10 when 2 then 20 when 3 then case age when 10 then 30 else 31 end else 40 end) case2, name FROM user
36 changes: 36 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,39 @@
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
<span style="color: green;">1</span> <span >::</span> <span style="color: #333;">text</span><span >;</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
<span style="font-weight:bold;">case</span> <span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">1</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">10</span>
<span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">2</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">20</span>
<span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">3</span> <span style="font-weight:bold;">then</span>
<span style="font-weight:bold;">case</span> <span style="font-weight:bold;">when</span> <span style="color: #333;">age</span> <span >&gt;</span> <span style="color: green;">10</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">30</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">31</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">40</span>
<span style="font-weight:bold;">end</span> <span style="font-weight:bold;">AS</span> <span style="color: #333;">case1</span><span >,</span>
(
<span style="font-weight:bold;">SELECT</span>
<span style="font-weight:bold;">case</span> <span style="color: #333;">name</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">1</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">10</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">2</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">20</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">3</span> <span style="font-weight:bold;">then</span>
<span style="font-weight:bold;">case</span> <span style="color: #333;">age</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">10</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">30</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">31</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">40</span>
<span style="font-weight:bold;">end</span>
) <span style="color: #333;">case2</span><span >,</span>
<span style="color: #333;">name</span>
<span style="font-weight:bold;">FROM</span>
<span style="color: #333;">user</span></pre>
36 changes: 36 additions & 0 deletions tests/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,39 @@ WINDOW
---
SELECT
1 :: text;
---
SELECT
case when name = 1 then
10
when name = 2 then
20
when name = 3 then
case when age > 10 then
30
else
31
end
else
40
end AS case1,
(
SELECT
case name
when 1 then
10
when 2 then
20
when 3 then
case age
when 10 then
30
else
31
end
else
40
end
) case2,
name
FROM
user
34 changes: 34 additions & 0 deletions tests/highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,37 @@
<span style="font-weight:bold;">WINDOW</span> <span style="color: #333;">w1</span> <span style="font-weight:bold;">AS</span> (<span style="font-weight:bold;">PARTITION BY</span> <span style="color: #333;">department</span><span >,</span> <span style="color: #333;">division</span>)<span >,</span> <span style="color: #333;">w2</span> <span style="font-weight:bold;">AS</span> (<span style="color: #333;">w1</span> <span style="font-weight:bold;">ORDER BY</span> <span style="color: #333;">hire_date</span>)<span >;</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: green;">1</span><span >::</span><span style="color: #333;">text</span><span >;</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
<span style="font-weight:bold;">case</span> <span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">1</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">10</span>
<span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">2</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">20</span>
<span style="font-weight:bold;">when</span> <span style="color: #333;">name</span> <span >=</span> <span style="color: green;">3</span> <span style="font-weight:bold;">then</span>
<span style="font-weight:bold;">case</span> <span style="font-weight:bold;">when</span> <span style="color: #333;">age</span> <span >&gt;</span> <span style="color: green;">10</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">30</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">31</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">40</span>
<span style="font-weight:bold;">end</span> <span style="font-weight:bold;">AS</span> <span style="color: #333;">case1</span><span >,</span>
(<span style="font-weight:bold;">SELECT</span>
<span style="font-weight:bold;">case</span> <span style="color: #333;">name</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">1</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">10</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">2</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">20</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">3</span> <span style="font-weight:bold;">then</span>
<span style="font-weight:bold;">case</span> <span style="color: #333;">age</span>
<span style="font-weight:bold;">when</span> <span style="color: green;">10</span> <span style="font-weight:bold;">then</span>
<span style="color: green;">30</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">31</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">else</span>
<span style="color: green;">40</span>
<span style="font-weight:bold;">end</span>) <span style="color: #333;">case2</span><span >,</span>
<span style="color: #333;">name</span>
<span style="font-weight:bold;">FROM</span>
<span style="color: #333;">user</span></pre>
10 changes: 5 additions & 5 deletions tests/performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

$formatter = new SqlFormatter();

//the sample query file is filled with install scripts for PrestaShop
//and some sample catalog data from Magento
// the sample query file is filled with install scripts for PrestaShop
// and some sample catalog data from Magento
$contents = file_get_contents(__DIR__ . '/sql.sql');

assert($contents !== false);
$queries = explode("\n---\n", $contents);

//track time and memory usage
// track time and memory usage
$start = microtime(true);
$ustart = memory_get_usage(true);

//track number of queries and size of queries
// track number of queries and size of queries
$num = 0;
$chars = 0;

foreach ($queries as $query) {
//do formatting and highlighting
// do formatting and highlighting
$formatter->format($query);

$num++;
Expand Down
34 changes: 34 additions & 0 deletions tests/sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,37 @@ FROM t1
WINDOW w1 AS (PARTITION BY department, division), w2 AS (w1 ORDER BY hire_date);
---
SELECT 1::text;
---
SELECT
case when name = 1 then
10
when name = 2 then
20
when name = 3 then
case when age > 10 then
30
else
31
end
else
40
end AS case1,
(SELECT
case name
when 1 then
10
when 2 then
20
when 3 then
case age
when 10 then
30
else
31
end
else
40
end) case2,
name
FROM
user