Skip to content

Commit

Permalink
Fix fatal error for Oracle LOOP statement
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 23, 2024
1 parent 192ed9d commit 958c49c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ public function format(string $string, string $indentString = ' '): string
} elseif (strtoupper($token->value()) === 'BEGIN') {
$newline = true;
$increaseBlockIndent = true;
} elseif (strtoupper($token->value()) === 'LOOP') {
// https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/basic-LOOP-statement.html

$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'END') {
$newline = true;
$increaseBlockIndent = true;
}
} elseif (in_array(strtoupper($token->value()), ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
if (strtoupper($token->value()) !== 'THEN') {
array_shift($indentTypes);
Expand Down
6 changes: 6 additions & 0 deletions tests/clihighlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,9 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
throw;
end
end catch
---
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
2 changes: 2 additions & 0 deletions tests/compress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_1(); MY_NON_TOP_LEVEL_KEYWORD_FX_2(); SELECT x FROM
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
---
begin try insert into [t] ([name], [int], [float], [null]) values (N'Ewa', 1, 1.0, null); end try begin catch if ERROR_NUMBER() = 544 begin set IDENTITY_INSERT [t] on; begin try insert into [t] ([name], [int], [float], [null]) values (N'Ewa', 1, 1.0, null); set IDENTITY_INSERT [t] off; end try begin catch set IDENTITY_INSERT [t] off; throw; end catch end else begin throw; end end catch
---
BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;
6 changes: 6 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,9 @@
<span style="color: #333;">throw</span><span >;</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">end</span> <span style="color: #333;">catch</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span>
<span style="font-weight:bold;">FOR</span> <span style="color: #333;">i</span> <span style="font-weight:bold;">IN</span> <span style="color: green;">1</span><span >.</span><span >.</span><span style="color: green;">5</span> <span style="color: #333;">LOOP</span>
<span style="color: #333;">DBMS_OUTPUT</span><span >.</span><span style="color: #333;">PUT_LINE</span>(<span style="color: #333;">i</span>)<span >;</span>
<span style="font-weight:bold;">END</span> <span style="color: #333;">LOOP</span><span >;</span>
<span style="font-weight:bold;">END</span><span >;</span></pre>
6 changes: 6 additions & 0 deletions tests/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1096,3 +1096,9 @@ else
throw;
end
end catch
---
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
7 changes: 7 additions & 0 deletions tests/highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,10 @@
<span style="color: #333;">throw</span><span >;</span>
<span style="font-weight:bold;">end</span>
<span style="font-weight:bold;">end</span> <span style="color: #333;">catch</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span>
<span style="font-weight:bold;">FOR</span> <span style="color: #333;">i</span> <span style="font-weight:bold;">IN</span> <span style="color: green;">1</span><span >.</span><span >.</span><span style="color: green;">5</span>
<span style="color: #333;">LOOP</span>
<span style="color: #333;">DBMS_OUTPUT</span><span >.</span><span style="color: #333;">PUT_LINE</span>(<span style="color: #333;">i</span>)<span >;</span>
<span style="font-weight:bold;">END</span> <span style="color: #333;">LOOP</span><span >;</span>
<span style="font-weight:bold;">END</span><span >;</span></pre>
7 changes: 7 additions & 0 deletions tests/sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,10 @@ begin catch
throw;
end
end catch
---
BEGIN
FOR i IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;

0 comments on commit 958c49c

Please sign in to comment.