Skip to content

Commit

Permalink
feat(grammar): Improve parsing of ALTER TRIGGER/PROCEDURE/FUNCTION/PA…
Browse files Browse the repository at this point in the history
…CKAGE
  • Loading branch information
felipebz committed Mar 31, 2024
1 parent ffa0f67 commit 7cae5e0
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;")
}

}
Original file line number Diff line number Diff line change
@@ -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;")
}

}
Original file line number Diff line number Diff line change
@@ -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;")
}

}

0 comments on commit 7cae5e0

Please sign in to comment.