Skip to content

Commit

Permalink
feat(grammar): Improve support for TO_TIMESTAMP syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Sep 27, 2024
1 parent 65043bb commit 516cc2c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@
"examples/single_statement.sql" : [
1
],
"examples/to_timestamp.sql" : [
6
],
"examples/to_timestamp_tz.sql" : [
6
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2858,9 +2858,6 @@
"sqlrf/Syntax-for-Schema-Objects-and-Parts-in-SQL-Statements-4.sql" : [
3
],
"sqlrf/TO_TIMESTAMP-1.sql" : [
3
],
"sqlrf/TO_TIMESTAMP_TZ-2.sql" : [
3
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@
"test/ut3_tester/core/test_ut_utils.pkb" : [
5,
147,
168,
175,
182,
219,
289,
Expand Down Expand Up @@ -148,9 +146,6 @@
12
],
"test/ut3_user/expectations/test_matchers.pkb" : [
244,
245,
246,
253,
254,
255,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
TO_DATE("to_date"),
TO_DSINTERVAL("to_dsinterval"),
TO_NUMBER("to_number"),
TO_TIMESTAMP("to_timestamp"),
TRAILING("trailing"),
TRANSACTION("transaction"),
TREAT("treat"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
TO_DATE_EXPRESSION,
TO_DSINTERVAL_EXPRESSION,
TO_NUMBER_EXPRESSION,
TO_TIMESTAMP_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
Expand Down Expand Up @@ -191,6 +192,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
TO_DATE_EXPRESSION,
TO_DSINTERVAL_EXPRESSION,
TO_NUMBER_EXPRESSION,
TO_TIMESTAMP_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
Expand Down Expand Up @@ -276,6 +278,14 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
RPARENTHESIS
)

b.rule(TO_TIMESTAMP_EXPRESSION).define(
TO_TIMESTAMP, 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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ToTimestampExpressionTest : RuleTest() {

@BeforeEach
fun init() {
setRootRule(PlSqlGrammar.EXPRESSION)
}

@Test
fun matchesSimple() {
assertThat(p).matches("to_timestamp(foo)")
}

@Test
fun matchesDefaultClause() {
assertThat(p).matches("to_timestamp(foo default systimestamp on conversion error)")
}

@Test
fun matchesMultipleArguments() {
assertThat(p).matches("to_timestamp(foo, 'dd/mm/rrrr', 'NLS_DATE_LANGUAGE = American')")
}

}

0 comments on commit 516cc2c

Please sign in to comment.