Skip to content

Commit

Permalink
feat(grammar): Support JSON_VALUE (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jun 15, 2024
1 parent b1c7a4b commit ec9b08d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2384,12 +2384,6 @@
"JSON_TRANSFORM-1.sql" : [
2
],
"JSON_VALUE-1.sql" : [
2
],
"JSON_VALUE-9.sql" : [
3
],
"LOCK-TABLE-0.sql" : [
2
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
JSON_SCALAR("json_scalar"),
JSON_SERIALIZE("json_serialize"),
JSON_TABLE("json_table"),
JSON_VALUE("json_value"),
KEEP("keep"),
KEY("key"),
KEYS("keys"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_VALUE_RETURN_OBJECT_INSTANCE,
JSON_EXISTS_ON_ERROR_CLAUSE,
JSON_EXISTS_ON_EMPTY_CLAUSE,
JSON_VALUE_RETURNING_CLAUSE,
XML_COLUMN,
XML_NAMESPACE,
XMLNAMESPACES_CLAUSE,
Expand All @@ -80,6 +81,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_SERIALIZE_EXPRESSION,
JSON_QUERY_EXPRESSION,
JSON_TABLE_EXPRESSION,
JSON_VALUE_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
XMLFOREST_EXPRESSION,
Expand Down Expand Up @@ -120,6 +122,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_SERIALIZE_EXPRESSION,
JSON_QUERY_EXPRESSION,
JSON_TABLE_EXPRESSION,
JSON_VALUE_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
XMLFOREST_EXPRESSION,
Expand Down Expand Up @@ -431,12 +434,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
)

b.rule(JSON_PASSING_CLAUSE).define(
PASSING,
b.oneOrMore(
EXPRESSION,
b.optional(AS, IDENTIFIER_NAME),
b.optional(COMMA)
)
PASSING, EXPRESSION, AS, IDENTIFIER_NAME, b.zeroOrMore(COMMA, EXPRESSION, AS, IDENTIFIER_NAME)
)

b.rule(JSON_QUERY_RETURNING_CLAUSE).define(
Expand Down Expand Up @@ -667,6 +665,24 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
FALSE
), ON, EMPTY
)

b.rule(JSON_VALUE_EXPRESSION).define(
JSON_VALUE,
LPARENTHESIS,
EXPRESSION,
b.optional(FORMAT, JSON),
COMMA,
STRING_LITERAL,
b.optional(JSON_PASSING_CLAUSE),
b.optional(JSON_VALUE_RETURNING_CLAUSE),
b.optional(JSON_VALUE_ON_ERROR_CLAUSE),
b.optional(JSON_VALUE_ON_EMPTY_CLAUSE),
b.optional(JSON_VALUE_ON_MISMATCH_CLAUSE),
b.optional(TYPE, b.firstOf(STRICT, LAX)),
RPARENTHESIS
)

b.rule(JSON_VALUE_RETURNING_CLAUSE).define(RETURNING, JSON_VALUE_RETURN_TYPE, b.optional(ASCII))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class JsonQueryExpressionTest : RuleTest() {

@Test
fun matchesJsonQueryWithPassing() {
assertThat(p).matches("json_query(foo, '$' passing bar)")
assertThat(p).matches("json_query(foo, '$' passing 'x' as bar)")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* 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 JsonValueTest : RuleTest() {

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

@Test
fun matchesJsonValue() {
assertThat(p).matches("json_value(doc, '$')")
}

@Test
fun matchesJsonValueFormatJson() {
assertThat(p).matches("json_value('{}' format json, '$')")
}

@Test
fun matchesJsonValueWithPassingClause() {
assertThat(p).matches("json_value(doc, '$' passing 1 as x, 2 as y)")
}

@Test
fun matchesJsonValueWithReturning() {
assertThat(p).matches("json_value(doc, '$' returning clob)")
}

@Test
fun matchesJsonValueWithReturningAscii() {
assertThat(p).matches("json_value(doc, '$' returning clob ascii)")
}

@Test
fun matchesLongJsonValue() {
assertThat(p).matches("""
json_value(doc, '$'
passing 'a' as a
returning varchar2(10) truncate
error on error
error on empty
error on mismatch
type strict)""")
}

}

0 comments on commit ec9b08d

Please sign in to comment.