Skip to content

Commit

Permalink
feat(grammar): Support NESTED clause (short-hand syntax for JSON_TABL…
Browse files Browse the repository at this point in the history
…E) (#182)
  • Loading branch information
felipebz committed Jun 15, 2024
1 parent 8f822c3 commit 0ac0549
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.sonar.plugins.plsqlopen.api.PlSqlGrammar.*
import org.sonar.plugins.plsqlopen.api.PlSqlKeyword.*
import org.sonar.plugins.plsqlopen.api.PlSqlPunctuator.*
import org.sonar.plugins.plsqlopen.api.PlSqlTokenType.INTEGER_LITERAL
import org.sonar.plugins.plsqlopen.api.SingleRowSqlFunctionsGrammar.*

enum class DmlGrammar : GrammarRuleKey {

Expand All @@ -42,6 +43,7 @@ enum class DmlGrammar : GrammarRuleKey {
OUTER_JOIN_TYPE,
QUERY_PARTITION_CLAUSE,
OUTER_JOIN_CLAUSE,
NESTED_CLAUSE,
JOIN_CLAUSE,
SELECT_COLUMN,
FROM_CLAUSE,
Expand Down Expand Up @@ -164,6 +166,17 @@ enum class DmlGrammar : GrammarRuleKey {
b.sequence(DML_TABLE_EXPRESSION_CLAUSE, b.optional(QUERY_PARTITION_CLAUSE),
b.optional(ON_OR_USING_EXPRESSION)))

b.rule(NESTED_CLAUSE).define(
NESTED, b.optional(PATH), IDENTIFIER_NAME,
b.optional(b.firstOf(
b.sequence(DOT, JSON_RELATIVE_OBJECT_ACCESS),
b.sequence(COMMA, JSON_BASIC_PATH_EXPRESSION)
)),
b.optional(JSON_TABLE_ON_ERROR_CLAUSE),
b.optional(JSON_TABLE_ON_EMPTY_CLAUSE),
JSON_COLUMNS_CLAUSE
)

b.rule(JOIN_CLAUSE).define(
b.firstOf(
b.sequence(
Expand All @@ -184,6 +197,7 @@ enum class DmlGrammar : GrammarRuleKey {
b.sequence(TABLE_REFERENCE, b.nextNot(LPARENTHESIS)),
OBJECT_REFERENCE
),
b.optional(NESTED_CLAUSE),
b.optional(b.nextNot(b.firstOf(PARTITION, CROSS, USING, FULL, NATURAL, INNER, LEFT, RIGHT, OUTER, JOIN, RETURN, RETURNING, LOG, EXCEPT)),
b.optional(AS),
ALIAS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,24 @@ class DmlTableExpressionClauseTest : RuleTest() {
assertThat(p).matches("table(xmlsequence(x))")
}

@Test
fun matchesNestedClause() {
assertThat(p).matches("tab nested doc columns (foo path foo) alias")
}

@Test
fun matchesNestedClauseWithExplicitPath() {
assertThat(p).matches("tab nested path doc columns (foo path foo)")
}

@Test
fun matchesNestedClauseWithRelativeObjectAccess() {
assertThat(p).matches("tab nested doc.c1[*].c2 columns (foo path foo)")
}

@Test
fun matchesNestedClauseWithSimplePathAccess() {
assertThat(p).matches("tab nested doc, '$.c1[*].c2' columns (foo path foo)")
}

}

0 comments on commit 0ac0549

Please sign in to comment.