Skip to content

Commit

Permalink
feat(grammar): Accept expressions in datatype nodes. This is necessar…
Browse files Browse the repository at this point in the history
…y to support things like "var number(some_constant)"
  • Loading branch information
felipebz committed Sep 18, 2024
1 parent 5b51cde commit 93f5995
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,14 @@
],
"sqlrf/constraint-4.sql" : [
8
],
"sqlrf/domain_display-5.sql" : [
6
],
"sqlrf/domain_name-5.sql" : [
6
],
"sqlrf/domain_order-5.sql" : [
6
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
5,
10
],
"lnpls/Supresses-warning-pragma-6009-9.sql" : [
8
],
"lnpls/WHILE-LOOP-statement-0.sql" : [
6,
11
Expand Down Expand Up @@ -435,6 +438,9 @@
"lnpls/plsql-data-types-3.sql" : [
8
],
"lnpls/plsql-data-types-32.sql" : [
12
],
"lnpls/plsql-data-types-34.sql" : [
13
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"lnpls/plsql-control-statements-70.sql" : [
7
],
"lnpls/plsql-data-types-28.sql" : [
21
],
"lnpls/plsql-data-types-60.sql" : [
27
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@
5,
5
],
"lnpls/Supresses-warning-pragma-6009-3.sql" : [
2
],
"lnpls/Supresses-warning-pragma-6009-4.sql" : [
2
],
"lnpls/Supresses-warning-pragma-6009-5.sql" : [
3
],
"lnpls/Supresses-warning-pragma-6009-6.sql" : [
3
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
"lnpls/dynamic-sql-18.sql" : [
2
],
"lnpls/plsql-data-types-32.sql" : [
4,
5,
6
],
"lnpls/plsql-data-types-36.sql" : [
11,
13
],
"lnpls/plsql-error-handling-34.sql" : [
5,
9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
2
],
"lnpls/COVERAGE-pragma-3.sql" : [
3
10
],
"lnpls/CREATE-FUNCTION-statement-0.sql" : [
2
Expand Down Expand Up @@ -269,21 +269,6 @@
"lnpls/SQL_MACRO-clause-6.sql" : [
7
],
"lnpls/Supresses-warning-pragma-6009-1.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-10.sql" : [
7
],
"lnpls/Supresses-warning-pragma-6009-3.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-5.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-9.sql" : [
4
],
"lnpls/dynamic-sql-11.sql" : [
2
],
Expand Down Expand Up @@ -350,18 +335,9 @@
"lnpls/plsql-control-statements-63.sql" : [
2
],
"lnpls/plsql-data-types-28.sql" : [
5
],
"lnpls/plsql-data-types-31.sql" : [
2
],
"lnpls/plsql-data-types-32.sql" : [
3
],
"lnpls/plsql-data-types-36.sql" : [
4
],
"lnpls/plsql-error-handling-15.sql" : [
2
],
Expand Down Expand Up @@ -1533,7 +1509,7 @@
2
],
"sqlrf/ALTER-TABLE-8.sql" : [
3
4
],
"sqlrf/ALTER-TABLE-81.sql" : [
2
Expand Down Expand Up @@ -2528,15 +2504,6 @@
"sqlrf/Data-Types-16.sql" : [
2
],
"sqlrf/Data-Types-32.sql" : [
3
],
"sqlrf/Data-Types-33.sql" : [
3
],
"sqlrf/Data-Types-35.sql" : [
2
],
"sqlrf/Data-Types-36.sql" : [
4
],
Expand Down Expand Up @@ -3288,7 +3255,7 @@
2
],
"sqlrf/domain_check-9.sql" : [
7
10
],
"sqlrf/domain_check_type-0.sql" : [
2
Expand All @@ -3315,35 +3282,26 @@
2
],
"sqlrf/domain_check_type-8.sql" : [
7
10
],
"sqlrf/domain_display-0.sql" : [
2
],
"sqlrf/domain_display-2.sql" : [
3
],
"sqlrf/domain_display-5.sql" : [
7
],
"sqlrf/domain_display-6.sql" : [
3
],
"sqlrf/domain_name-0.sql" : [
2
],
"sqlrf/domain_name-5.sql" : [
7
],
"sqlrf/domain_order-0.sql" : [
2
],
"sqlrf/domain_order-2.sql" : [
3
],
"sqlrf/domain_order-5.sql" : [
7
],
"sqlrf/domain_order-6.sql" : [
3
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"lnpls/plsql-control-statements-69.sql" : [
6
],
"lnpls/plsql-data-types-32.sql" : [
11
],
"lnpls/plsql-language-fundamentals-20.sql" : [
7
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
"lnpls/plsql-data-types-34.sql" : [
5
],
"lnpls/plsql-data-types-36.sql" : [
18
],
"lnpls/plsql-error-handling-29.sql" : [
18,
21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"adjsn/using-PLSQL-object-types-for-JSON-2.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-1.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-3.sql" : [
5
],
"lnpls/Supresses-warning-pragma-6009-9.sql" : [
4
],
"lnpls/dynamic-sql-12.sql" : [
7
],
Expand Down Expand Up @@ -29,6 +38,13 @@
"lnpls/plsql-control-statements-38.sql" : [
3
],
"lnpls/plsql-data-types-28.sql" : [
15,
16,
17,
18,
19
],
"lnpls/plsql-data-types-60.sql" : [
5,
6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum class PlSqlGrammar : GrammarRuleKey {
DATATYPE,
DATATYPE_LENGTH,
CHARACTER_SET_CLAUSE,
NUMERIC_PRECISION,
NUMERIC_SCALE,
NUMERIC_DATATYPE_CONSTRAINT,
NUMERIC_DATATYPE,
LOB_DATATYPE,
Expand Down Expand Up @@ -364,10 +366,14 @@ enum class PlSqlGrammar : GrammarRuleKey {
}

private fun createDatatypes(b: PlSqlGrammarBuilder) {
b.rule(DATATYPE_LENGTH).define(b.firstOf(INTEGER_LITERAL, INQUIRY_DIRECTIVE)).skip()
b.rule(DATATYPE_LENGTH).define(b.firstOf(EXPRESSION, INQUIRY_DIRECTIVE)).skip()

b.rule(NUMERIC_PRECISION).define(b.firstOf(MULTIPLICATION, DATATYPE_LENGTH))

b.rule(NUMERIC_SCALE).define(DATATYPE_LENGTH)

b.rule(NUMERIC_DATATYPE_CONSTRAINT).define(
LPARENTHESIS, b.firstOf(DATATYPE_LENGTH, MULTIPLICATION), b.optional(COMMA, b.optional(MINUS), DATATYPE_LENGTH), RPARENTHESIS
LPARENTHESIS, NUMERIC_PRECISION, b.optional(COMMA, NUMERIC_SCALE), RPARENTHESIS
)

b.rule(NUMERIC_DATATYPE).define(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.sonar.plugins.plsqlopen.api.symbols.datatype

import com.felipebz.flr.api.AstNode
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlTokenType
import org.sonar.plugins.plsqlopen.api.symbols.PlSqlType

class CharacterDatatype : PlSqlDatatype {
Expand All @@ -47,7 +46,7 @@ class CharacterDatatype : PlSqlDatatype {
constructor(node: AstNode? = null) {
val constraint = node?.firstChildOrNull?.getFirstChildOrNull(PlSqlGrammar.CHARACTER_DATATYPE_CONSTRAINT)
length = constraint
?.getFirstChildOrNull(PlSqlTokenType.INTEGER_LITERAL)
?.getFirstChildOrNull(PlSqlGrammar.LITERAL)
?.tokenValue?.toInt()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.sonar.plugins.plsqlopen.api.symbols.datatype

import com.felipebz.flr.api.AstNode
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.PlSqlTokenType
import org.sonar.plugins.plsqlopen.api.symbols.PlSqlType

class NumericDatatype : PlSqlDatatype {
Expand All @@ -31,42 +30,48 @@ class NumericDatatype : PlSqlDatatype {
override val name: String
get() = if (this.length == null)
"NUMBER()"
else if (this.precision == null)
else if (this.scale == null)
"NUMBER(${this.length})"
else
"NUMBER(${this.length}, ${this.precision})"
"NUMBER(${this.length}, ${this.scale})"

val length: Int?
val precision: Int?
val scale: Int?

constructor() {
length = null
precision = null
scale = null
}

constructor(length: Int?, precision: Int?) {
constructor(length: Int?, scale: Int?) {
this.length = if (length != null && length > 0) length else null
this.precision = if (precision != null && precision > 0) precision else null
this.scale = if (scale != null && scale > 0) scale else null
}

constructor(node: AstNode? = null) {
val constraint = node?.firstChildOrNull?.getFirstChildOrNull(PlSqlGrammar.NUMERIC_DATATYPE_CONSTRAINT)
if (constraint != null) {
val precisionNode = constraint.getFirstChildOrNull(PlSqlGrammar.NUMERIC_PRECISION)
length = if (precisionNode != null && precisionNode.hasDirectChildren(PlSqlGrammar.LITERAL)) {
precisionNode.tokenValue.toInt()
} else {
null
}

val numericConstraints = constraint?.getChildren(PlSqlTokenType.INTEGER_LITERAL)

if (numericConstraints != null) {
length = numericConstraints.first().tokenValue.toInt()
precision = if (numericConstraints.size > 1) {
numericConstraints.last().tokenValue.toInt()
} else null
val scaleNode = constraint.getFirstChildOrNull(PlSqlGrammar.NUMERIC_SCALE)
scale = if (scaleNode != null && scaleNode.hasDirectChildren(PlSqlGrammar.LITERAL)) {
scaleNode.tokenValue.toInt()
} else {
null
}
} else {
length = null
precision = null
scale = null
}
}

override fun toString(): String {
return "Numeric{length=$length, precision=$precision}"
return "Numeric{length=$length, scale=$scale}"
}

}
Loading

0 comments on commit 93f5995

Please sign in to comment.