Skip to content

Commit

Permalink
feat(grammar): Improve support for TO_DATE syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Sep 27, 2024
1 parent 4bd31fe commit 003a7ca
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@
"examples/single_statement.sql" : [
1
],
"examples/to_date.sql" : [
10
],
"examples/to_dsinterval.sql" : [
3
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"examples/to_date.sql" : [
1,
7,
10,
13,
16,
19
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
"examples/select_all_some_any.sql" : [
6
],
"examples/to_date.sql" : [
9
],
"examples/union07.sql" : [
21,
36
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_DATE-3.sql" : [
3
],
"sqlrf/TO_DSINTERVAL-2.sql" : [
3
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
3,
5
],
"lnpls/plsql-language-fundamentals-44.sql" : [
4,
7
],
"lnpls/plsql-optimization-and-tuning-106.sql" : [
51
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
"test/ut3_tester/core/test_ut_utils.pkb" : [
5,
147,
161,
168,
175,
182,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package org.sonar.plsqlopen.checks

import com.felipebz.flr.api.AstNode
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlPunctuator
import org.sonar.plugins.plsqlopen.api.SingleRowSqlFunctionsGrammar
import org.sonar.plugins.plsqlopen.api.annotations.*
import org.sonar.plugins.plsqlopen.api.matchers.MethodMatcher

@Rule(priority = Priority.MAJOR, tags = [Tags.BUG])
@ConstantRemediation("5min")
Expand All @@ -31,17 +31,13 @@ import org.sonar.plugins.plsqlopen.api.matchers.MethodMatcher
class ToDateWithoutFormatCheck : AbstractBaseCheck() {

override fun init() {
subscribeTo(PlSqlGrammar.METHOD_CALL)
subscribeTo(SingleRowSqlFunctionsGrammar.TO_DATE_EXPRESSION)
}

override fun visitNode(node: AstNode) {
val toDate = MethodMatcher.create().name("to_date").addParameter()

if (toDate.matches(node)) {
val argument = toDate.getArgumentsValues(node)[0]
if (!CheckUtils.isNullLiteralOrEmptyString(argument)) {
addIssue(node, getLocalizedMessage())
}
val firstArgument = node.getFirstChild(PlSqlPunctuator.LPARENTHESIS).nextSibling
if (!node.hasDirectChildren(PlSqlPunctuator.COMMA) && !CheckUtils.isNullLiteralOrEmptyString(firstArgument)) {
addIssue(node, getLocalizedMessage())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ create procedure foo(bar in varchar2 default func(x)) is
select 1 from dual;
begin
null;
end;
end;
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
TO_BINARY_DOUBLE("to_binary_double"),
TO_BINARY_FLOAT("to_binary_float"),
TO_BOOLEAN("to_boolean"),
TO_DATE("to_date"),
TRAILING("trailing"),
TRANSACTION("transaction"),
TREAT("treat"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
TO_BINARY_DOUBLE_EXPRESSION,
TO_BINARY_FLOAT_EXPRESSION,
TO_BOOLEAN_EXPRESSION,
TO_DATE_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
Expand Down Expand Up @@ -185,12 +186,13 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
TO_BINARY_DOUBLE_EXPRESSION,
TO_BINARY_FLOAT_EXPRESSION,
TO_BOOLEAN_EXPRESSION,
TO_DATE_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
CURSOR_EXPRESSION
)
).skip()
)
}

private fun createCharacterFunctions(b: PlSqlGrammarBuilder) {
Expand Down Expand Up @@ -247,6 +249,14 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
RPARENTHESIS
)

b.rule(TO_DATE_EXPRESSION).define(
TO_DATE, 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 ToDateExpressionTest : RuleTest() {

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

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

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

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

}

0 comments on commit 003a7ca

Please sign in to comment.