Skip to content

Commit bbb1360

Browse files
committed
Adds FUNCTION AND PROCEDURE to DROP statement verification
1 parent f5dda37 commit bbb1360

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/com/premiumminds/sonar/postgres/visitors/RobustStatementsVisitorCheck.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public void visit(DropStmt dropStmt) {
5353
.map(x -> x.getString().getSval())
5454
.collect(Collectors.toList());
5555

56+
final List<String> functions = dropStmt.getObjectsList()
57+
.stream()
58+
.map(y -> y.getObjectWithArgs()
59+
.getObjnameList())
60+
.flatMap(List::stream)
61+
.map(x -> x.getString().getSval())
62+
.collect(Collectors.toList());
63+
5664
if(!dropStmt.getMissingOk()){
5765
final ObjectType removeType = dropStmt.getRemoveType();
5866
switch (removeType){
@@ -86,6 +94,12 @@ public void visit(DropStmt dropStmt) {
8694
case OBJECT_STATISTIC_EXT:
8795
newIssue("Add IF EXISTS to DROP STATISTICS " + String.join(", ", names));
8896
break;
97+
case OBJECT_FUNCTION:
98+
newIssue("Add IF EXISTS to DROP FUNCTION " + String.join(", ", functions));
99+
break;
100+
case OBJECT_PROCEDURE:
101+
newIssue("Add IF EXISTS to DROP PROCEDURE " + String.join(", ", functions));
102+
break;
89103
default:
90104
newIssue("Add IF EXISTS");
91105
break;

src/test/java/com/premiumminds/sonar/postgres/PostgresSqlSensorTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ void preferRobustStmts() {
241241
createFile(contextTester, "file29.sql", "CREATE STATISTICS foo_stats(dependencies) ON id1, id2 FROM foo;");
242242
createFile(contextTester, "file30.sql", "DROP STATISTICS foo_stats, bar_stats;");
243243
createFile(contextTester, "file31.sql", "CREATE FUNCTION add(integer, integer) RETURNS integer AS 'select $1 + $2;' LANGUAGE SQL;");
244+
createFile(contextTester, "file32.sql", "DROP FUNCTION foo, bar;");
245+
createFile(contextTester, "file33.sql", "DROP PROCEDURE foo, bar;");
246+
createFile(contextTester, "file34.sql", "CREATE PROCEDURE foo() LANGUAGE SQL AS $$ select 1;$$;");
244247

245248
final RuleKey rule = RULE_PREFER_ROBUST_STMTS;
246249
PostgresSqlSensor sensor = getPostgresSqlSensor(rule);
@@ -343,7 +346,16 @@ void preferRobustStmts() {
343346
assertEquals("Add OR REPLACE to add",
344347
fileMap.get(":file31.sql").primaryLocation().message());
345348

346-
assertEquals(31, fileMap.size());
349+
assertEquals("Add IF EXISTS to DROP FUNCTION foo, bar",
350+
fileMap.get(":file32.sql").primaryLocation().message());
351+
352+
assertEquals("Add IF EXISTS to DROP PROCEDURE foo, bar",
353+
fileMap.get(":file33.sql").primaryLocation().message());
354+
355+
assertEquals("Add OR REPLACE to foo",
356+
fileMap.get(":file34.sql").primaryLocation().message());
357+
358+
assertEquals(34, fileMap.size());
347359
}
348360

349361
@Test

0 commit comments

Comments
 (0)