From 547cf56cb3400dd8cc7135a568bf395d6730bcdc Mon Sep 17 00:00:00 2001 From: Felipe Zorzo Date: Tue, 20 Aug 2024 20:09:34 -0300 Subject: [PATCH] feat(grammar): Support TRUNCATE TABLE statement --- .../antlr-grammars-v4/ParsingErrorCheck.json | 6 -- .../oracle-database_23/ParsingErrorCheck.json | 6 -- .../utPLSQL2/CommitRollbackCheck.json | 5 ++ .../utPLSQL2/ExplicitInParameterCheck.json | 22 ++++++ .../utPLSQL2/InsertWithoutColumnsCheck.json | 8 +++ .../expected/utPLSQL2/ParsingErrorCheck.json | 3 - .../QueryWithoutExceptionHandlingCheck.json | 3 + .../utPLSQL2/SelectAllColumnsCheck.json | 4 ++ .../sonar/plugins/plsqlopen/api/DdlGrammar.kt | 27 ++++++- .../plugins/plsqlopen/api/PlSqlKeyword.kt | 1 + .../plsqlopen/api/ddl/TruncateTableTest.kt | 71 +++++++++++++++++++ 11 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/TruncateTableTest.kt diff --git a/zpa-checks/src/integrationTest/resources/expected/antlr-grammars-v4/ParsingErrorCheck.json b/zpa-checks/src/integrationTest/resources/expected/antlr-grammars-v4/ParsingErrorCheck.json index 134d4232..08da0e77 100644 --- a/zpa-checks/src/integrationTest/resources/expected/antlr-grammars-v4/ParsingErrorCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/antlr-grammars-v4/ParsingErrorCheck.json @@ -443,9 +443,6 @@ "examples/truncate_cluster.sql" : [ 1 ], - "examples/truncate_table.sql" : [ - 1 - ], "examples/unified.sql" : [ 1 ], @@ -461,9 +458,6 @@ "hw-examples/alter_outline.sql" : [ 1 ], - "hw-examples/truncate_table.sql" : [ - 1 - ], "long-running/order_by07.sql" : [ 2 ], diff --git a/zpa-checks/src/integrationTest/resources/expected/oracle-database_23/ParsingErrorCheck.json b/zpa-checks/src/integrationTest/resources/expected/oracle-database_23/ParsingErrorCheck.json index 12fae17f..fd67f0b5 100644 --- a/zpa-checks/src/integrationTest/resources/expected/oracle-database_23/ParsingErrorCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/oracle-database_23/ParsingErrorCheck.json @@ -2936,12 +2936,6 @@ "sqlrf/TRUNCATE-CLUSTER-0.sql" : [ 2 ], - "sqlrf/TRUNCATE-TABLE-0.sql" : [ - 2 - ], - "sqlrf/TRUNCATE-TABLE-1.sql" : [ - 2 - ], "sqlrf/Type-Constructor-Expressions-1.sql" : [ 2 ], diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/CommitRollbackCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/CommitRollbackCheck.json index 999e43f7..973a6758 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/CommitRollbackCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/CommitRollbackCheck.json @@ -1,4 +1,9 @@ { + "examples/mybooks_setup.sql" : [ + 55, + 61, + 67 + ], "examples/test_te_employee.pkb" : [ 191, 237, diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ExplicitInParameterCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ExplicitInParameterCheck.json index 00025b29..0e9b1654 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ExplicitInParameterCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ExplicitInParameterCheck.json @@ -1,4 +1,26 @@ { + "examples/mybooks_setup.sql" : [ + 28, + 29, + 30, + 31, + 31, + 31, + 32, + 32, + 32, + 33, + 38, + 45, + 52, + 52, + 52, + 58, + 58, + 58, + 64, + 70 + ], "examples/te_employee.pkb" : [ 936 ], diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/InsertWithoutColumnsCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/InsertWithoutColumnsCheck.json index 21a74593..7b441b59 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/InsertWithoutColumnsCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/InsertWithoutColumnsCheck.json @@ -1,4 +1,12 @@ { + "examples/mybooks_setup.sql" : [ + 19, + 20, + 21, + 22, + 23, + 54 + ], "source/ut_receq.pkb" : [ 245, 255 diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ParsingErrorCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ParsingErrorCheck.json index be0ba3fa..e478fc6c 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ParsingErrorCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/ParsingErrorCheck.json @@ -2,9 +2,6 @@ "examples/filepath1.pkg" : [ 104 ], - "examples/mybooks_setup.sql" : [ - 17 - ], "examples/str2list.pkg" : [ 62 ], diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/QueryWithoutExceptionHandlingCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/QueryWithoutExceptionHandlingCheck.json index 9b7196cd..977409ce 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/QueryWithoutExceptionHandlingCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/QueryWithoutExceptionHandlingCheck.json @@ -1,4 +1,7 @@ { + "examples/mybooks_setup.sql" : [ + 48 + ], "examples/te_employee.pkb" : [ 863, 874, diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/SelectAllColumnsCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/SelectAllColumnsCheck.json index bbd499ef..5dc655d3 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/SelectAllColumnsCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL2/SelectAllColumnsCheck.json @@ -3,6 +3,10 @@ 199, 238 ], + "examples/mybooks_setup.sql" : [ + 41, + 72 + ], "examples/test_te_employee.pkb" : [ 179 ], diff --git a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/DdlGrammar.kt b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/DdlGrammar.kt index 47f42ff8..723c5e0f 100644 --- a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/DdlGrammar.kt +++ b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/DdlGrammar.kt @@ -77,7 +77,8 @@ enum class DdlGrammar : GrammarRuleKey { SUBPARTITION_BY_HASH, SUBPARTITION_TEMPLATE, CREATE_DIRECTORY, - DROP_DIRECTORY; + DROP_DIRECTORY, + TRUNCATE_TABLE; companion object { fun buildOn(b: PlSqlGrammarBuilder) { @@ -653,6 +654,27 @@ enum class DdlGrammar : GrammarRuleKey { b.rule(DROP_DIRECTORY).define( DROP, DIRECTORY, b.optional(IF, EXISTS), IDENTIFIER_NAME, b.optional(SEMICOLON)) + b.rule(TRUNCATE_TABLE).define( + TRUNCATE, TABLE, UNIT_NAME, + b.optional( + b.firstOf( + PRESERVE, PURGE + ), + MATERIALIZED, VIEW, LOG + ), + b.optional( + b.firstOf( + b.sequence( + DROP, b.optional(ALL) + ), + REUSE + ), + STORAGE + ), + b.optional(CASCADE), + b.optional(SEMICOLON) + ) + b.rule(DDL_COMMAND).define(b.firstOf( DDL_COMMENT, CREATE_TABLE, @@ -665,7 +687,8 @@ enum class DdlGrammar : GrammarRuleKey { CREATE_SEQUENCE, CREATE_DIRECTORY, DROP_DIRECTORY, - DROP_COMMAND)) + DROP_COMMAND, + TRUNCATE_TABLE)) } } diff --git a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/PlSqlKeyword.kt b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/PlSqlKeyword.kt index 1d35a212..d0ad633a 100644 --- a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/PlSqlKeyword.kt +++ b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/PlSqlKeyword.kt @@ -416,6 +416,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa PRESERVE("preserve"), PRETTY("pretty"), PRIMARY("primary"), + PURGE("purge"), QUOTES("quotes"), RAISE("raise"), RANGE_KEYWORD("range"), diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/TruncateTableTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/TruncateTableTest.kt new file mode 100644 index 00000000..fd26418b --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/TruncateTableTest.kt @@ -0,0 +1,71 @@ +/** + * Z PL/SQL Analyzer + * Copyright (C) 2015-2024 Felipe Zorzo + * mailto:felipe AT felipezorzo DOT com DOT br + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.plugins.plsqlopen.api.ddl + +import com.felipebz.flr.tests.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.sonar.plugins.plsqlopen.api.DdlGrammar +import org.sonar.plugins.plsqlopen.api.RuleTest + +class TruncateTableTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(DdlGrammar.TRUNCATE_TABLE) + } + + @Test + fun matchesSimpleTruncate() { + assertThat(p).matches("truncate table foo;") + } + + @Test + fun matchesTruncatePreserveMaterializedViewLog() { + assertThat(p).matches("truncate table foo preserve materialized view log;") + } + + @Test + fun matchesTruncatePurgeMaterializedViewLog() { + assertThat(p).matches("truncate table foo purge materialized view log;") + } + + @Test + fun matchesTruncateDropStorage() { + assertThat(p).matches("truncate table foo drop storage;") + assertThat(p).matches("truncate table foo drop all storage;") + } + + @Test + fun matchesTruncateReuseStorage() { + assertThat(p).matches("truncate table foo reuse storage;") + } + + @Test + fun matchesTruncateCascade() { + assertThat(p).matches("truncate table foo cascade;") + } + + @Test + fun matchesLongTruncate() { + assertThat(p).matches("truncate table sch.foo purge materialized view log drop all storage cascade;") + } + +}