Skip to content

Commit

Permalink
feat(grammar): Support JSON_OBJECT (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jun 13, 2024
1 parent 444993b commit fdb40f5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2153,9 +2153,6 @@
"Interval-Expressions-0.sql" : [
2
],
"JSON_ARRAY-0.sql" : [
3
],
"JSON_OBJECTAGG-0.sql" : [
2
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,11 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
JSON_ARRAY("json_array"),
JSON_ARRAYAGG("json_arrayagg"),
JSON_MERGEPATCH("json_mergepatch"),
JSON_OBJECT("json_object"),
JSON_QUERY("json_query"),
KEEP("keep"),
KEY("key"),
KEYS("keys"),
LANGUAGE("language"),
LAST("last"),
LAX("lax"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_ARRAY_ENUMERATION_CONTENT,
JSON_ARRAY_QUERY_CONTENT,
JSON_ARRAY_ELEMENT,
JSON_OBJECT_ENTRY,
JSON_QUERY_RETURNING_CLAUSE,
JSON_QUERY_WRAPPER_CLAUSE,
JSON_QUERY_QUOTES_CLAUSE,
Expand All @@ -54,6 +55,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_CONSTRUCTOR,
JSON_ARRAY_EXPRESSION,
JSON_MERGEPATCH_EXPRESSION,
JSON_OBJECT_EXPRESSION,
JSON_QUERY_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
Expand Down Expand Up @@ -90,6 +92,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
JSON_CONSTRUCTOR,
JSON_ARRAY_EXPRESSION,
JSON_MERGEPATCH_EXPRESSION,
JSON_OBJECT_EXPRESSION,
JSON_QUERY_EXPRESSION,
XMLATTRIBUTES_EXPRESSION,
XMLELEMENT_EXPRESSION,
Expand Down Expand Up @@ -331,6 +334,26 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
b.optional(STRICT)
)

b.rule(JSON_OBJECT_EXPRESSION).define(
JSON_OBJECT,
LPARENTHESIS,
JSON_OBJECT_ENTRY, b.zeroOrMore(COMMA, JSON_OBJECT_ENTRY),
b.optional(JSON_ON_NULL_CLAUSE),
b.optional(JSON_RETURNING_CLAUSE),
b.optional(STRICT),
b.optional(WITH, UNIQUE, KEYS),
RPARENTHESIS
)

b.rule(JSON_OBJECT_ENTRY).define(
b.firstOf(
b.sequence(b.optional(KEY), EXPRESSION, VALUE, EXPRESSION),
b.sequence(STRING_LITERAL, COLON, EXPRESSION),
EXPRESSION
),
b.optional(FORMAT, JSON)
)

b.rule(JSON_QUERY_EXPRESSION).define(
JSON_QUERY,
LPARENTHESIS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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 JsonObjectExpressionTest : RuleTest() {

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

@Test
fun matchesSimpleJsonObject() {
assertThat(p).matches("json_object(foo)")
}

@Test
fun matchesJsonObjectWithKey() {
assertThat(p).matches("json_object(k value 'v')")
}

@Test
fun matchesJsonObjectWithExplicitKey() {
assertThat(p).matches("json_object(key k value 'v')")
}

@Test
fun matchesJsonObjectWithStringKeys() {
assertThat(p).matches("json_object('k': 'v')")
}

@Test
fun matchesJsonObjectWithFormatJson() {
assertThat(p).matches("json_object('k': '{}' format json)")
}

@Test
fun matchesJsonObjectWithReturning() {
assertThat(p).matches("json_object(foo returning clob)")
}

@Test
fun matchesJsonObjectWithAbsentOnNull() {
assertThat(p).matches("json_object(* absent on null)")
}

@Test
fun matchesLongJsonObject() {
assertThat(p).matches("""json_object('k' value 'v',
'a': '{}' format json
null on null
returning varchar2(200)
strict with unique keys)""")
}

}

0 comments on commit fdb40f5

Please sign in to comment.