From 7cae5e09c81965886a4461a99ea6e79e1d720786 Mon Sep 17 00:00:00 2001 From: Felipe Zorzo Date: Sun, 31 Mar 2024 19:58:20 -0300 Subject: [PATCH] feat(grammar): Improve parsing of ALTER TRIGGER/PROCEDURE/FUNCTION/PACKAGE --- .../sonar/plugins/plsqlopen/api/DdlGrammar.kt | 62 +++++++++++++----- .../api/ddl/AlterFunctionUnitTest.kt | 45 +++++++++++++ ...sqlUnitTest.kt => AlterPackageUnitTest.kt} | 48 ++++---------- .../api/ddl/AlterProcedureUnitTest.kt | 45 +++++++++++++ .../plsqlopen/api/ddl/AlterTriggerUnitTest.kt | 65 +++++++++++++++++++ 5 files changed, 213 insertions(+), 52 deletions(-) create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterFunctionUnitTest.kt rename zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/{AlterPlsqlUnitTest.kt => AlterPackageUnitTest.kt} (68%) create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterProcedureUnitTest.kt create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterTriggerUnitTest.kt 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 a89e9e34..47f42ff8 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 @@ -39,11 +39,13 @@ enum class DdlGrammar : GrammarRuleKey { TABLE_RELATIONAL_PROPERTIES, CREATE_TABLE, ALTER_TABLE, - ALTER_PLSQL_UNIT, - ALTER_PROCEDURE_FUNCTION, COMPILE_CLAUSE, + COMPILER_PARAMETERS_CLAUSE, + ALTER_PROCEDURE, + ALTER_FUNCTION, ALTER_TRIGGER, ALTER_PACKAGE, + PACKAGE_COMPILE_CLAUSE, DROP_COMMAND, CREATE_SYNONYM, CREATE_SEQUENCE, @@ -573,29 +575,54 @@ enum class DdlGrammar : GrammarRuleKey { b.rule(ALTER_TABLE).define( ALTER, TABLE, UNIT_NAME, b.firstOf(ADD, DROP), TABLE_RELATIONAL_PROPERTIES, b.optional(SEMICOLON)) - b.rule(COMPILE_CLAUSE).define(COMPILE, b.optional(DEBUG), b.optional(REUSE, SETTINGS)) + b.rule(COMPILE_CLAUSE).define( + COMPILE, b.optional(DEBUG), + b.zeroOrMore(COMPILER_PARAMETERS_CLAUSE), + b.optional(REUSE, SETTINGS)) + + b.rule(COMPILER_PARAMETERS_CLAUSE).define( + IDENTIFIER_NAME, EQUALS_OPERATOR, CHARACTER_LITERAL) b.rule(ALTER_TRIGGER).define( - TRIGGER, UNIT_NAME, + ALTER, TRIGGER, b.optional(IF, EXISTS), UNIT_NAME, b.firstOf( ENABLE, DISABLE, b.sequence(RENAME, TO, IDENTIFIER_NAME), - COMPILE_CLAUSE) - ) + EDITIONABLE, + NONEDITIONABLE, + COMPILE_CLAUSE), + b.optional(SEMICOLON)) - b.rule(ALTER_PROCEDURE_FUNCTION).define( - b.firstOf(PROCEDURE, FUNCTION), UNIT_NAME, COMPILE_CLAUSE - ) + b.rule(ALTER_PROCEDURE).define( + ALTER, PROCEDURE, b.optional(IF, EXISTS), UNIT_NAME, + b.firstOf( + EDITIONABLE, + NONEDITIONABLE, + COMPILE_CLAUSE), + b.optional(SEMICOLON)) + + b.rule(ALTER_FUNCTION).define( + ALTER, FUNCTION, b.optional(IF, EXISTS), UNIT_NAME, + b.firstOf( + EDITIONABLE, + NONEDITIONABLE, + COMPILE_CLAUSE), + b.optional(SEMICOLON)) b.rule(ALTER_PACKAGE).define( - PACKAGE, UNIT_NAME, COMPILE, - b.optional(DEBUG), - b.optional(b.firstOf(PACKAGE, SPECIFICATION, BODY)), - b.optional(REUSE, SETTINGS) - ) + ALTER, PACKAGE, b.optional(IF, EXISTS), UNIT_NAME, + b.firstOf( + EDITIONABLE, + NONEDITIONABLE, + PACKAGE_COMPILE_CLAUSE), + b.optional(SEMICOLON)) - b.rule(ALTER_PLSQL_UNIT).define(ALTER, b.firstOf(ALTER_TRIGGER, ALTER_PROCEDURE_FUNCTION, ALTER_PACKAGE), b.optional(SEMICOLON)) + b.rule(PACKAGE_COMPILE_CLAUSE).define( + COMPILE, b.optional(DEBUG), + b.optional(b.firstOf(PACKAGE, SPECIFICATION, BODY)), + b.zeroOrMore(COMPILER_PARAMETERS_CLAUSE), + b.optional(REUSE, SETTINGS)) b.rule(DROP_COMMAND).define(DROP, b.oneOrMore(b.anyTokenButNot(b.firstOf(SEMICOLON, DIVISION, EOF))), b.optional(SEMICOLON)) @@ -630,7 +657,10 @@ enum class DdlGrammar : GrammarRuleKey { DDL_COMMENT, CREATE_TABLE, ALTER_TABLE, - ALTER_PLSQL_UNIT, + ALTER_TRIGGER, + ALTER_PROCEDURE, + ALTER_FUNCTION, + ALTER_PACKAGE, CREATE_SYNONYM, CREATE_SEQUENCE, CREATE_DIRECTORY, diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterFunctionUnitTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterFunctionUnitTest.kt new file mode 100644 index 00000000..46c8bec0 --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterFunctionUnitTest.kt @@ -0,0 +1,45 @@ +/** + * 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 AlterFunctionUnitTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(DdlGrammar.ALTER_FUNCTION) + } + + @Test + fun matchesAlterFunctionCompile() { + assertThat(p).matches("alter function foo compile;") + } + + @Test + fun matchesAlterFunctionIfExists() { + assertThat(p).matches("alter function if exists foo compile;") + } + +} diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPlsqlUnitTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPackageUnitTest.kt similarity index 68% rename from zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPlsqlUnitTest.kt rename to zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPackageUnitTest.kt index 9c494169..08180558 100644 --- a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPlsqlUnitTest.kt +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterPackageUnitTest.kt @@ -25,51 +25,21 @@ import org.junit.jupiter.api.Test import org.sonar.plugins.plsqlopen.api.DdlGrammar import org.sonar.plugins.plsqlopen.api.RuleTest -class AlterPlsqlUnitTest : RuleTest() { +class AlterPackageUnitTest : RuleTest() { @BeforeEach fun init() { - setRootRule(DdlGrammar.ALTER_PLSQL_UNIT) + setRootRule(DdlGrammar.ALTER_PACKAGE) } @Test - fun matchesAlterTriggerDisable() { - assertThat(p).matches("alter trigger foo disable;") - } - - @Test - fun matchesAlterTriggerEnable() { - assertThat(p).matches("alter trigger foo enable;") - } - - @Test - fun matchesAlterTriggerDisableWithSchema() { - assertThat(p).matches("alter trigger foo.bar disable;") - } - - @Test - fun matchesAlterTriggerDisableWithQuotedIdentifier() { - assertThat(p).matches("alter trigger \"foo\" rename to \"bar\";") - } - - @Test - fun matchesAlterTriggerCompile() { - assertThat(p).matches("alter trigger foo compile;") - } - - @Test - fun matchesAlterProcedureCompile() { - assertThat(p).matches("alter procedure foo compile;") + fun matchesAlterPackageCompile() { + assertThat(p).matches("alter package foo compile;") } @Test - fun matchesAlterFunctionCompile() { - assertThat(p).matches("alter function foo compile;") - } - - @Test - fun matchesAlterPackageCompile() { - assertThat(p).matches("alter package foo compile;") + fun matchesAlterPackageIfExists() { + assertThat(p).matches("alter package if exists foo compile;") } @Test @@ -96,4 +66,10 @@ class AlterPlsqlUnitTest : RuleTest() { fun matchesAlterPackageCompileSpecificationReuseSettings() { assertThat(p).matches("alter package foo compile debug specification reuse settings;") } + + @Test + fun matchesAlterPackageCompileSpecificationWithOptionAndReuseSettings() { + assertThat(p).matches("alter package foo compile debug specification plsql_ccflags='no_op:true' plsql_ccflags='no_op:true' reuse settings;") + } + } diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterProcedureUnitTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterProcedureUnitTest.kt new file mode 100644 index 00000000..b1245894 --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterProcedureUnitTest.kt @@ -0,0 +1,45 @@ +/** + * 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 AlterProcedureUnitTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(DdlGrammar.ALTER_PROCEDURE) + } + + @Test + fun matchesAlterProcedureCompile() { + assertThat(p).matches("alter procedure foo compile;") + } + + @Test + fun matchesAlterProcedureIfExists() { + assertThat(p).matches("alter procedure if exists foo compile;") + } + +} diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterTriggerUnitTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterTriggerUnitTest.kt new file mode 100644 index 00000000..1aead271 --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/ddl/AlterTriggerUnitTest.kt @@ -0,0 +1,65 @@ +/** + * 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 AlterTriggerUnitTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(DdlGrammar.ALTER_TRIGGER) + } + + @Test + fun matchesAlterTriggerDisable() { + assertThat(p).matches("alter trigger foo disable;") + } + + @Test + fun matchesAlterTriggerEnable() { + assertThat(p).matches("alter trigger foo enable;") + } + + @Test + fun matchesAlterTriggerDisableWithSchema() { + assertThat(p).matches("alter trigger foo.bar disable;") + } + + @Test + fun matchesAlterTriggerDisableWithQuotedIdentifier() { + assertThat(p).matches("alter trigger \"foo\" rename to \"bar\";") + } + + @Test + fun matchesAlterTriggerCompile() { + assertThat(p).matches("alter trigger foo compile;") + } + + @Test + fun matchesAlterTriggerIfExists() { + assertThat(p).matches("alter trigger if exists foo compile;") + } + +}