Skip to content

Commit

Permalink
feat(grammar): Support JSON_EQUAL condition (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jun 21, 2024
1 parent 04243e8 commit e7df8bb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum class ConditionsGrammar : GrammarRuleKey {
JSON_MODIFIER_LIST,
JSON_COLUMN_MODIFIER,
JSON_SCALAR_MODIFIER,
JSON_EQUAL_ON_ERROR_CLAUSE,

// conditions
RELATIONAL_CONDITION,
Expand All @@ -45,6 +46,7 @@ enum class ConditionsGrammar : GrammarRuleKey {
SUBMULTISET_CONDITION,
IS_OF_CONDITION,
IS_JSON_CONDITION,
JSON_EQUAL_CONDITION,
CONDITION;

companion object {
Expand Down Expand Up @@ -176,14 +178,33 @@ enum class ConditionsGrammar : GrammarRuleKey {
)
)

b.rule(JSON_EQUAL_CONDITION).define(
JSON_EQUAL,
PlSqlPunctuator.LPARENTHESIS,
PlSqlGrammar.EXPRESSION,
PlSqlPunctuator.COMMA,
PlSqlGrammar.EXPRESSION,
b.optional(JSON_EQUAL_ON_ERROR_CLAUSE),
PlSqlPunctuator.RPARENTHESIS
)

b.rule(JSON_EQUAL_ON_ERROR_CLAUSE).define(
b.firstOf(
ERROR,
TRUE,
FALSE
), ON, ERROR
)

b.rule(CONDITION).define(
b.firstOf(
RELATIONAL_CONDITION,
LIKE_CONDITION,
BETWEEN_CONDITION,
MULTISET_CONDITION,
IS_JSON_CONDITION,
IS_OF_CONDITION
IS_OF_CONDITION,
JSON_EQUAL_CONDITION
)
).skip()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
JSON("json"),
JSON_ARRAY("json_array"),
JSON_ARRAYAGG("json_arrayagg"),
JSON_EQUAL("json_equal"),
JSON_MERGEPATCH("json_mergepatch"),
JSON_OBJECT("json_object"),
JSON_OBJECTAGG("json_objectagg"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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.conditions

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.ConditionsGrammar
import org.sonar.plugins.plsqlopen.api.RuleTest

class JsonEqualConditionTest : RuleTest() {

@BeforeEach
fun init() {
setRootRule(ConditionsGrammar.CONDITION)
}

@Test
fun matchesSimpleJsonEqual() {
assertThat(p).matches("json_equal('{}', '{}')")
}

@Test
fun matchesJsonEqualOnErrorClause() {
assertThat(p).matches("json_equal('{}', '{}' error on error)")
assertThat(p).matches("json_equal('{}', '{}' true on error)")
assertThat(p).matches("json_equal('{}', '{}' false on error)")
}

}

0 comments on commit e7df8bb

Please sign in to comment.