Skip to content

Commit 1ed8e32

Browse files
committed
Fix #120 - Added new rule G-7910: Never use DML within a SQL macro.
1 parent 4ed9ac4 commit 1ed8e32

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: SQL Macros
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# G-7910: Never use DML within a SQL macro.
2+
3+
!!! danger "Critical"
4+
Reliability, Testability
5+
6+
## Restriction
7+
8+
Oracle Database 21c (19c from version 19.7 for table macros alone)
9+
10+
## Reason
11+
12+
Doing DML (except for `select`) within a SQL macro can lead to disastrous side effects from calling the macro in a SQL query.
13+
14+
Logging macro calls via a call to a procedure that does DML in an autonomous transaction can be an exception to the rule.
15+
16+
## Example (bad)
17+
18+
``` sql
19+
create or replace function row_generator (
20+
num_rows_in in number(32,0)
21+
)
22+
return varchar2 sql_macro as
23+
begin
24+
insert into function_calls(name, called_at, parameter_value)
25+
values ($$PLSQL_UNIT, current_timestamp, num_rows_in);
26+
commit;
27+
28+
return 'select level as row_sequence from dual connect by level <= num_rows_in';
29+
end row_generator;
30+
/
31+
```
32+
33+
## Example (good)
34+
35+
``` sql
36+
create or replace function row_generator (
37+
num_rows_in in number(32,0)
38+
)
39+
return varchar2 sql_macro as
40+
begin
41+
return 'select level as row_sequence from dual connect by level <= num_rows_in';
42+
end row_generator;
43+
/
44+
```

docs/9-appendix/appendix.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ n/a | 7720 | Never use multiple UPDATE OF in trigger event clause. | Blocker |
124124
n/a | 7730 | Avoid multiple DML events per trigger. | Minor | | | &#10008; | | | | | &#10008;
125125
n/a | 7740 | Never handle multiple DML events per trigger if primary key is assigned in trigger. | Major | | &#10008; | | | &#10008; | | |
126126
n/a | 7810 | Never use SQL inside PL/SQL to read sequence numbers (or SYSDATE). | Major | | &#10008; | &#10008; | | | | |
127+
n/a | 7910 | Never use DML within a SQL macro. | Critical | | | | | &#10008; | | | &#10008;
127128
78 | 8110 | Never use SELECT COUNT(*) if you are only interested in the existence of a row. | Major | | &#10008; | | | | | |
128129
n/a | 8120 | Never check existence of a row to decide whether to create it or not. | Major | | &#10008; | | | &#10008; | | |
129130
79 | 8210 | Always use synonyms when accessing objects of another application schema. | Major | &#10008; | | &#10008; | | | | |

docs/stylesheets/extra.css

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
[id="object-types"],
223223
[id="triggers"],
224224
[id="sequences"],
225+
[id="sql-macros"],
225226
[id="patterns"],
226227
[id="access-objects-of-foreign-application-schemas"],
227228
[id="validating-input-parameter-size"],

tools/run-in-container/genpdf.sh

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ write_text "### Triggers"
156156
write_guidelines "4-language-usage/7-stored-objects/7-triggers" "####"
157157
write_text "### Sequences"
158158
write_guidelines "4-language-usage/7-stored-objects/8-sequences" "####"
159+
write_text "### SQL Macros"
160+
write_guidelines "4-language-usage/7-stored-objects/9-sql-macros" "####"
159161
write_text "## Patterns"
160162
write_text "### Checking the Number of Rows"
161163
write_guidelines "4-language-usage/8-patterns/1-checking-the-number-of-rows" "####"

0 commit comments

Comments
 (0)