From beac75be9c30c7bfb79e37b84adc004e39f9666c Mon Sep 17 00:00:00 2001 From: Felipe Zorzo Date: Thu, 26 Sep 2024 23:52:20 -0300 Subject: [PATCH] feat(grammar): Improve support for TO_TIMESTAMP_TZ syntax --- .../antlr-grammars-v4/ParsingErrorCheck.json | 3 -- .../oracle-database_23/ParsingErrorCheck.json | 3 -- ...leInitializationWithFunctionCallCheck.json | 12 ----- .../plugins/plsqlopen/api/PlSqlKeyword.kt | 1 + .../api/SingleRowSqlFunctionsGrammar.kt | 10 ++++ .../ToTimestampTzExpressionTest.kt | 50 +++++++++++++++++++ 6 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToTimestampTzExpressionTest.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 30e42ada..81c5edc4 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 @@ -413,9 +413,6 @@ "examples/single_statement.sql" : [ 1 ], - "examples/to_timestamp_tz.sql" : [ - 6 - ], "examples/truncate_cluster.sql" : [ 1 ], 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 64dc6038..1cb58fde 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_TIMESTAMP_TZ-2.sql" : [ - 3 - ], "sqlrf/TO_YMINTERVAL-2.sql" : [ 3 ], diff --git a/zpa-checks/src/integrationTest/resources/expected/utPLSQL3/VariableInitializationWithFunctionCallCheck.json b/zpa-checks/src/integrationTest/resources/expected/utPLSQL3/VariableInitializationWithFunctionCallCheck.json index bea6cb89..2dfc328c 100644 --- a/zpa-checks/src/integrationTest/resources/expected/utPLSQL3/VariableInitializationWithFunctionCallCheck.json +++ b/zpa-checks/src/integrationTest/resources/expected/utPLSQL3/VariableInitializationWithFunctionCallCheck.json @@ -116,7 +116,6 @@ "test/ut3_tester/core/test_ut_utils.pkb" : [ 5, 147, - 182, 219, 289, 438, @@ -140,19 +139,8 @@ 3, 4, 6, - 8, - 9, - 10, 12 ], - "test/ut3_user/expectations/test_matchers.pkb" : [ - 253, - 254, - 255, - 262, - 263, - 264 - ], "test/ut3_user/reporters/test_coverage/test_coverage_standalone.pkb" : [ 42, 72 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 04d9b0c4..7b8da9a0 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 @@ -522,6 +522,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa TO_DSINTERVAL("to_dsinterval"), TO_NUMBER("to_number"), TO_TIMESTAMP("to_timestamp"), + TO_TIMESTAMP_TZ("to_timestamp_tz"), 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 f45ef84b..0a4ac3dd 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 @@ -135,6 +135,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { TO_DSINTERVAL_EXPRESSION, TO_NUMBER_EXPRESSION, TO_TIMESTAMP_EXPRESSION, + TO_TIMESTAMP_TZ_EXPRESSION, TRIM_EXPRESSION, TABLE_EXPRESSION, THE_EXPRESSION, @@ -193,6 +194,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { TO_DSINTERVAL_EXPRESSION, TO_NUMBER_EXPRESSION, TO_TIMESTAMP_EXPRESSION, + TO_TIMESTAMP_TZ_EXPRESSION, TRIM_EXPRESSION, TABLE_EXPRESSION, THE_EXPRESSION, @@ -286,6 +288,14 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey { RPARENTHESIS ) + b.rule(TO_TIMESTAMP_TZ_EXPRESSION).define( + TO_TIMESTAMP_TZ, LPARENTHESIS, + EXPRESSION, + b.optional(DEFAULT_ON_ERROR_CLAUSE), + b.optional(COMMA, EXPRESSION, b.optional(COMMA, EXPRESSION)), + 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/ToTimestampTzExpressionTest.kt b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToTimestampTzExpressionTest.kt new file mode 100644 index 00000000..43faaa07 --- /dev/null +++ b/zpa-core/src/test/kotlin/org/sonar/plugins/plsqlopen/api/expressions/ToTimestampTzExpressionTest.kt @@ -0,0 +1,50 @@ +/** + * 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 ToTimestampTzExpressionTest : RuleTest() { + + @BeforeEach + fun init() { + setRootRule(PlSqlGrammar.EXPRESSION) + } + + @Test + fun matchesSimple() { + assertThat(p).matches("to_timestamp_tz(foo)") + } + + @Test + fun matchesDefaultClause() { + assertThat(p).matches("to_timestamp_tz(foo default systimestamp on conversion error)") + } + + @Test + fun matchesMultipleArguments() { + assertThat(p).matches("to_timestamp_tz(foo, 'dd/mm/rrrr', 'NLS_DATE_LANGUAGE = American')") + } + +}