diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index 064fd58..b236b90 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -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; @@ -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() !== ',' && @@ -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; diff --git a/tests/clihighlight.txt b/tests/clihighlight.txt index 49032f2..fe0d17a 100644 --- a/tests/clihighlight.txt +++ b/tests/clihighlight.txt @@ -1009,3 +1009,39 @@ --- [37mSELECT[0m [32;1m1[0m ::[0m text[0m;[0m +--- +[37mSELECT[0m + [37mcase[0m [37mwhen[0m name[0m =[0m [32;1m1[0m [37mthen[0m + [32;1m10[0m + [37mwhen[0m name[0m =[0m [32;1m2[0m [37mthen[0m + [32;1m20[0m + [37mwhen[0m name[0m =[0m [32;1m3[0m [37mthen[0m + [37mcase[0m [37mwhen[0m age[0m >[0m [32;1m10[0m [37mthen[0m + [32;1m30[0m + [37melse[0m + [32;1m31[0m + [37mend[0m + [37melse[0m + [32;1m40[0m + [37mend[0m [37mAS[0m case1[0m,[0m + ( + [37mSELECT[0m + [37mcase[0m name[0m + [37mwhen[0m [32;1m1[0m [37mthen[0m + [32;1m10[0m + [37mwhen[0m [32;1m2[0m [37mthen[0m + [32;1m20[0m + [37mwhen[0m [32;1m3[0m [37mthen[0m + [37mcase[0m age[0m + [37mwhen[0m [32;1m10[0m [37mthen[0m + [32;1m30[0m + [37melse[0m + [32;1m31[0m + [37mend[0m + [37melse[0m + [32;1m40[0m + [37mend[0m + ) case2[0m,[0m + name[0m +[37mFROM[0m + user[0m diff --git a/tests/compress.txt b/tests/compress.txt index 70266fa..148399e 100644 --- a/tests/compress.txt +++ b/tests/compress.txt @@ -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 diff --git a/tests/format-highlight.html b/tests/format-highlight.html index af708fa..f941626 100644 --- a/tests/format-highlight.html +++ b/tests/format-highlight.html @@ -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 + userdiff --git a/tests/format.txt b/tests/format.txt index fbf9f18..743f841 100644 --- a/tests/format.txt +++ b/tests/format.txt @@ -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 diff --git a/tests/highlight.html b/tests/highlight.html index b722fdd..dd1f908 100644 --- a/tests/highlight.html +++ b/tests/highlight.html @@ -304,3 +304,37 @@ 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 + userdiff --git a/tests/performance.php b/tests/performance.php index 77152f7..a10c1be 100644 --- a/tests/performance.php +++ b/tests/performance.php @@ -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++; diff --git a/tests/sql.sql b/tests/sql.sql index 8e4a025..3ed0dfd 100644 --- a/tests/sql.sql +++ b/tests/sql.sql @@ -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