Skip to content

Commit

Permalink
feat(grammar): Support JSON_MERGEPATCH (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jun 12, 2024
1 parent 5ef627a commit 444993b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
JSON("json"),
JSON_ARRAY("json_array"),
JSON_ARRAYAGG("json_arrayagg"),
JSON_MERGEPATCH("json_mergepatch"),
JSON_QUERY("json_query"),
KEEP("keep"),
KEY("key"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
// internals
JSON_PASSING_CLAUSE,
JSON_ON_NULL_CLAUSE,
JSON_ON_ERROR_CLAUSE,
JSON_RETURNING_CLAUSE,
JSON_ARRAY_ENUMERATION_CONTENT,
JSON_ARRAY_QUERY_CONTENT,
Expand All @@ -52,6 +53,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
EXTRACT_DATETIME_EXPRESSION,
JSON_CONSTRUCTOR,
JSON_ARRAY_EXPRESSION,
JSON_MERGEPATCH_EXPRESSION,
JSON_QUERY_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
Expand Down Expand Up @@ -87,6 +89,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
EXTRACT_DATETIME_EXPRESSION,
JSON_CONSTRUCTOR,
JSON_ARRAY_EXPRESSION,
JSON_MERGEPATCH_EXPRESSION,
JSON_QUERY_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
Expand Down Expand Up @@ -289,6 +292,8 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {

b.rule(JSON_ON_NULL_CLAUSE).define(b.firstOf(NULL, ABSENT), ON, NULL)

b.rule(JSON_ON_ERROR_CLAUSE).define(b.firstOf(NULL, ERROR), ON, ERROR)

b.rule(JSON_RETURNING_CLAUSE).define(
RETURNING,
b.firstOf(
Expand All @@ -305,6 +310,20 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
)
)

b.rule(JSON_MERGEPATCH_EXPRESSION).define(
JSON_MERGEPATCH,
LPARENTHESIS,
EXPRESSION,
COMMA,
EXPRESSION,
b.optional(JSON_RETURNING_CLAUSE),
b.optional(PRETTY),
b.optional(ASCII),
b.optional(TRUNCATE),
b.optional(JSON_ON_ERROR_CLAUSE),
RPARENTHESIS
)

b.rule(JSON_ARRAY_QUERY_CONTENT).define(
DmlGrammar.SELECT_EXPRESSION,
b.optional(JSON_ON_NULL_CLAUSE),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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 JsonMergepatchExpressionTest : RuleTest() {

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

@Test
fun matchesSimpleJsonMergepatch() {
assertThat(p).matches("json_mergepatch(target, patch)")
}

@Test
fun matchesJsonMergepatchWithReturning() {
assertThat(p).matches("json_mergepatch(target, patch returning clob)")
}

@Test
fun matchesJsonMergepatchWithNullOnError() {
assertThat(p).matches("json_mergepatch(target, patch null on error)")
}

@Test
fun matchesLongJsonMergepatch() {
assertThat(p).matches("""json_mergepatch(target, patch
returning varchar2(200)
pretty ascii truncate
error on error)""")
}

}

0 comments on commit 444993b

Please sign in to comment.