Skip to content

Commit 4d6bc8a

Browse files
authored
Merge pull request #116 from mvorisek/fix_oracle_throw
Add support for Oracle LOOP statement
2 parents db72b86 + 6423197 commit 4d6bc8a

7 files changed

+42
-0
lines changed

src/SqlFormatter.php

+8
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ public function format(string $string, string $indentString = ' '): string
278278
} elseif (strtoupper($token->value()) === 'BEGIN') {
279279
$newline = true;
280280
$increaseBlockIndent = true;
281+
} elseif (strtoupper($token->value()) === 'LOOP') {
282+
// https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/basic-LOOP-statement.html
283+
284+
$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
285+
if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'END') {
286+
$newline = true;
287+
$increaseBlockIndent = true;
288+
}
281289
} elseif (in_array(strtoupper($token->value()), ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
282290
if (strtoupper($token->value()) !== 'THEN') {
283291
$decreaseIndentationLevelFx();

tests/clihighlight.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,9 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
10981098
throw;
10991099
end
11001100
end catch
1101+
---
1102+
BEGIN
1103+
FOR i IN 1..5 LOOP
1104+
DBMS_OUTPUT.PUT_LINE(i);
1105+
END LOOP;
1106+
END;

tests/compress.txt

+2
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_1(); MY_NON_TOP_LEVEL_KEYWORD_FX_2(); SELECT x FROM
9999
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
100100
---
101101
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
102+
---
103+
BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;

tests/format-highlight.html

+6
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,9 @@
10981098
<span style="color: #333;">throw</span><span >;</span>
10991099
<span style="font-weight:bold;">end</span>
11001100
<span style="font-weight:bold;">end</span> <span style="color: #333;">catch</span></pre>
1101+
---
1102+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span>
1103+
<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>
1104+
<span style="color: #333;">DBMS_OUTPUT</span><span >.</span><span style="color: #333;">PUT_LINE</span>(<span style="color: #333;">i</span>)<span >;</span>
1105+
<span style="font-weight:bold;">END</span> <span style="color: #333;">LOOP</span><span >;</span>
1106+
<span style="font-weight:bold;">END</span><span >;</span></pre>

tests/format.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1096,3 +1096,9 @@ else
10961096
throw;
10971097
end
10981098
end catch
1099+
---
1100+
BEGIN
1101+
FOR i IN 1..5 LOOP
1102+
DBMS_OUTPUT.PUT_LINE(i);
1103+
END LOOP;
1104+
END;

tests/highlight.html

+7
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,10 @@
380380
<span style="color: #333;">throw</span><span >;</span>
381381
<span style="font-weight:bold;">end</span>
382382
<span style="font-weight:bold;">end</span> <span style="color: #333;">catch</span></pre>
383+
---
384+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span>
385+
<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>
386+
<span style="color: #333;">LOOP</span>
387+
<span style="color: #333;">DBMS_OUTPUT</span><span >.</span><span style="color: #333;">PUT_LINE</span>(<span style="color: #333;">i</span>)<span >;</span>
388+
<span style="font-weight:bold;">END</span> <span style="color: #333;">LOOP</span><span >;</span>
389+
<span style="font-weight:bold;">END</span><span >;</span></pre>

tests/sql.sql

+7
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,10 @@ begin catch
380380
throw;
381381
end
382382
end catch
383+
---
384+
BEGIN
385+
FOR i IN 1..5
386+
LOOP
387+
DBMS_OUTPUT.PUT_LINE(i);
388+
END LOOP;
389+
END;

0 commit comments

Comments
 (0)