Skip to content

Commit

Permalink
feat(grammar): Support JSON_ARRAYAGG (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebz committed Jun 11, 2024
1 parent be972b7 commit a518777
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2156,9 +2156,6 @@
"JSON_ARRAY-0.sql": [
3
],
"JSON_ARRAYAGG-0.sql": [
2
],
"JSON_OBJECTAGG-0.sql": [
2
],
Expand Down Expand Up @@ -2924,4 +2921,4 @@
"storage_clause-0.sql": [
6
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class AggregateSqlFunctionsGrammar : GrammarRuleKey {
LISTAGG_EXPRESSION,
XMLAGG_EXPRESSION,
COLLECT_EXPRESSION,
JSON_ARRAYAGG_EXPRESSION,
AGGREGATE_SQL_FUNCTION;

companion object {
Expand Down Expand Up @@ -58,11 +59,22 @@ enum class AggregateSqlFunctionsGrammar : GrammarRuleKey {
RPARENTHESIS
)

b.rule(JSON_ARRAYAGG_EXPRESSION).define(
JSON_ARRAYAGG, LPARENTHESIS,
EXPRESSION, b.optional(FORMAT, JSON),
b.optional(ORDER_BY_CLAUSE),
b.optional(SingleRowSqlFunctionsGrammar.JSON_ON_NULL_CLAUSE),
b.optional(SingleRowSqlFunctionsGrammar.JSON_RETURNING_CLAUSE),
b.optional(STRICT),
RPARENTHESIS
)

b.rule(AGGREGATE_SQL_FUNCTION).define(
b.firstOf(
LISTAGG_EXPRESSION,
XMLAGG_EXPRESSION,
COLLECT_EXPRESSION
COLLECT_EXPRESSION,
JSON_ARRAYAGG_EXPRESSION
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ enum class PlSqlKeyword(override val value: String, val isReserved: Boolean = fa
JOIN("join"),
JSON("json"),
JSON_ARRAY("json_array"),
JSON_ARRAYAGG("json_arrayagg"),
JSON_QUERY("json_query"),
KEEP("keep"),
KEY("key"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ enum class SingleRowSqlFunctionsGrammar : GrammarRuleKey {
b.firstOf(
b.sequence(
VARCHAR2,
b.optional(LPARENTHESIS, PlSqlTokenType.INTEGER_LITERAL, b.firstOf(CHAR, BYTE), RPARENTHESIS),
b.optional(LPARENTHESIS, PlSqlTokenType.INTEGER_LITERAL, b.optional(b.firstOf(CHAR, BYTE)), RPARENTHESIS),
b.optional(WITH, TYPENAME)
),
b.sequence(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* 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 JsonArrayAggTest : RuleTest() {

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

@Test
fun matchesJsonArrayAgg() {
assertThat(p).matches("json_arrayagg(1)")
}

@Test
fun matchesJsonArrayAggWithFormat() {
assertThat(p).matches("json_arrayagg('{}' format json)")
}

@Test
fun matchesJsonArrayAggWithOrderBy() {
assertThat(p).matches("json_arrayagg(foo order by col)")
}

@Test
fun matchesJsonArrayAggWithNullOnNull() {
assertThat(p).matches("json_arrayagg(foo null on null)")
}

@Test
fun matchesJsonArrayAggWithAbsentOnNull() {
assertThat(p).matches("json_arrayagg(foo absent on null)")
}

@Test
fun matchesJsonArrayAggWithReturning() {
assertThat(p).matches("json_arrayagg(foo returning json)")
}

@Test
fun matchesJsonArrayAggWithStrict() {
assertThat(p).matches("json_arrayagg(foo strict)")
}

@Test
fun matchesLongJsonArrayAgg() {
assertThat(p).matches("json_arrayagg(foo order by col null on null returning json strict)")
}

}

0 comments on commit a518777

Please sign in to comment.