From c7534259e615de42d6004a29868ab9beee8b0052 Mon Sep 17 00:00:00 2001 From: Felipe Zorzo Date: Thu, 26 Sep 2024 23:56:54 -0300 Subject: [PATCH] feat(grammar): Improve support for TO_YMINTERVAL syntax --- .../oracle-database_23/ParsingErrorCheck.json | 3 -- .../plugins/plsqlopen/api/PlSqlKeyword.kt | 1 + .../api/SingleRowSqlFunctionsGrammar.kt | 9 ++++ .../expressions/ToYmintervalExpressionTest.kt | 45 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToYmintervalExpressionTest.kt 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 1cb58fde..fa81ac32 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 @@ -2858,9 +2858,6 @@ "sqlrf/Syntax-for-Schema-Objects-and-Parts-in-SQL-Statements-4.sql" : [ 3 ], - "sqlrf/TO_YMINTERVAL-2.sql" : [ - 3 - ], "sqlrf/TRANSLATE-USING-0.sql" : [ 10 ], 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 7b8da9a0..da358aa1 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 @@ -523,6 +523,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa TO_NUMBER("to_number"), TO_TIMESTAMP("to_timestamp"), TO_TIMESTAMP_TZ("to_timestamp_tz"), + TO_YMINTERVAL("to_yminterval"), TRAILING("trailing"), TRANSACTION("transaction"), TREAT("treat"), diff --git a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/SingleRowSqlFunctionsGrammar.kt b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/SingleRowSqlFunctionsGrammar.kt index 0a4ac3dd..4f1700c5 100644 --- a/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/SingleRowSqlFunctionsGrammar.kt +++ b/zpa-core/src/main/kotlin/org/sonar/plugins/plsqlopen/api/SingleRowSqlFunctionsGrammar.kt @@ -136,6 +136,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { TO_NUMBER_EXPRESSION, TO_TIMESTAMP_EXPRESSION, TO_TIMESTAMP_TZ_EXPRESSION, + TO_YMINTERVAL_EXPRESSION, TRIM_EXPRESSION, TABLE_EXPRESSION, THE_EXPRESSION, @@ -195,6 +196,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { TO_NUMBER_EXPRESSION, TO_TIMESTAMP_EXPRESSION, TO_TIMESTAMP_TZ_EXPRESSION, + TO_YMINTERVAL_EXPRESSION, TRIM_EXPRESSION, TABLE_EXPRESSION, THE_EXPRESSION, @@ -296,6 +298,13 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { RPARENTHESIS ) + b.rule(TO_YMINTERVAL_EXPRESSION).define( + TO_YMINTERVAL, LPARENTHESIS, + EXPRESSION, + b.optional(DEFAULT_ON_ERROR_CLAUSE), + RPARENTHESIS + ) + b.rule(TABLE_EXPRESSION).define( TABLE, LPARENTHESIS, b.firstOf(DmlGrammar.SELECT_EXPRESSION, EXPRESSION), diff --git a/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToYmintervalExpressionTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToYmintervalExpressionTest.kt new file mode 100644 index 00000000..23017809 --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToYmintervalExpressionTest.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.expressions + +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.PlSqlGrammar +import org.sonar.plugins.plsqlopen.api.RuleTest + +class ToYmintervalExpressionTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(PlSqlGrammar.EXPRESSION) + } + + @Test + fun matchesSimple() { + assertThat(p).matches("to_yminterval(foo)") + } + + @Test + fun matchesDefaultClause() { + assertThat(p).matches("to_yminterval(foo default '00-00' on conversion error)") + } + +}