Skip to content

Commit

Permalink
feat(grammar): Improve support for TO_BOOLEAN syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Sep 27, 2024
1 parent eab1962 commit 4bd31fe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
TIMESTAMP("timestamp"),
TO_BINARY_DOUBLE("to_binary_double"),
TO_BINARY_FLOAT("to_binary_float"),
TO_BOOLEAN("to_boolean"),
TRAILING("trailing"),
TRANSACTION("transaction"),
TREAT("treat"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
CAST_EXPRESSION,
TO_BINARY_DOUBLE_EXPRESSION,
TO_BINARY_FLOAT_EXPRESSION,
TO_BOOLEAN_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
Expand Down Expand Up @@ -183,6 +184,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
CAST_EXPRESSION,
TO_BINARY_DOUBLE_EXPRESSION,
TO_BINARY_FLOAT_EXPRESSION,
TO_BOOLEAN_EXPRESSION,
TRIM_EXPRESSION,
TABLE_EXPRESSION,
THE_EXPRESSION,
Expand Down Expand Up @@ -238,6 +240,13 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
RPARENTHESIS
)

b.rule(TO_BOOLEAN_EXPRESSION).define(
TO_BOOLEAN, LPARENTHESIS,
EXPRESSION,
b.optional(DEFAULT_ON_ERROR_CLAUSE),
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,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 ToBooleanExpressionTest : RuleTest() {

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

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

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

}

0 comments on commit 4bd31fe

Please sign in to comment.