Skip to content

Commit 9e21a47

Browse files
authored
Merge pull request #255 from domaframework/fix/expand-directive-incorrectly-treated
Expand directive incorrectly treated as bind variable directive
2 parents 4df9469 + a8f5e29 commit 9e21a47

File tree

9 files changed

+55
-3
lines changed

9 files changed

+55
-3
lines changed

src/main/kotlin/org/domaframework/doma/intellij/common/validation/result/ValidationTestDataResult.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@ import org.domaframework.doma.intellij.common.psi.PsiParentClass
2424
class ValidationTestDataResult(
2525
override val identify: PsiElement?,
2626
override val shortName: String = "",
27+
private val expand: Boolean,
2728
) : ValidationResult(identify, null, shortName) {
2829
override fun setHighlight(
2930
highlightRange: TextRange,
3031
identify: PsiElement,
3132
holder: ProblemsHolder,
3233
parent: PsiParentClass?,
3334
) {
35+
val message =
36+
if (expand) {
37+
MessageBundle.message("inspection.invalid.sql.expand")
38+
} else {
39+
MessageBundle.message("inspection.invalid.sql.testdata")
40+
}
3441
val project = identify.project
3542
holder.registerProblem(
3643
identify,
37-
MessageBundle.message("inspection.invalid.sql.testdata"),
44+
message,
3845
problemHighlightType(project, shortName),
3946
highlightRange,
4047
)

src/main/kotlin/org/domaframework/doma/intellij/inspection/sql/visitor/SqlTestDataInspectionVisitor.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,20 @@ class SqlTestDataInspectionVisitor(
5151
super.visitBlockComment(element)
5252
if (hasOtherBindVariable(element)) return
5353

54-
val result = ValidationTestDataResult(element, shortName)
5554
val nextElement = element.nextSibling
55+
val expand = isExpand(element)
56+
val result = ValidationTestDataResult(element, shortName, expand)
57+
58+
if (expand) {
59+
if (!checkExpandDirective(nextElement)) result.highlightElement(holder)
60+
return
61+
}
62+
5663
if (nextElement == null) {
5764
result.highlightElement(holder)
5865
return
5966
}
67+
6068
if (isSqlLiteral(nextElement)) return
6169
if (isMatchListTestData(element)) return
6270

@@ -91,6 +99,11 @@ class SqlTestDataInspectionVisitor(
9199
listOf("true", "false", "null").contains(element.text.lowercase()) ||
92100
element.text.matches(Regex("^\\d+$"))
93101

102+
private fun isExpand(element: PsiElement): Boolean =
103+
PsiTreeUtil.getChildOfType(element, PsiElement::class.java)?.nextSibling?.elementType == SqlTypes.EL_EXPAND
104+
105+
private fun checkExpandDirective(element: PsiElement): Boolean = element.elementType == SqlTypes.ASTERISK
106+
94107
/**
95108
* Determines if the given element matches the pattern for "List type test data."
96109
*

src/main/resources/messages/DomaToolsBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ inspection.invalid.dao.parameter=The bind variable [{1}] does not exist in the D
99
inspection.invalid.sql.topType=Can't get type of first element
1010
inspection.invalid.sql.staticProperty=[{0}] is not a public or static property in the class [{1}]
1111
inspection.invalid.sql.testdata=Test data is required after a bind variable directive or a literal variable directive
12+
inspection.invalid.sql.expand=Asterisk is required after expand variable
1213
inspection.invalid.sql.classpath=A non-existent package or class name was specified [{0}]
1314
inspection.invalid.sql.iterable=The type that can be used in the loop directive is an Iterable type
1415
inspection.invalid.dao.duplicate=An element name that is a duplicate of an element name defined in SQL is used

src/main/resources/messages/DomaToolsBundle_ja.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ inspection.invalid.dao.parameter=DAO\u30E1\u30BD\u30C3\u30C9[{0}]\u306B\u5B58\u5
99
inspection.invalid.sql.topType=\u6700\u521D\u306E\u8981\u7D20\u306E\u578B\u304C\u53D6\u5F97\u3067\u304D\u307E\u305B\u3093
1010
inspection.invalid.sql.staticProperty=[{0}]\u0020\u306F\u30AF\u30E9\u30B9\u0020[{1}]\u0020\u306E\u0020\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u307E\u305F\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u30D7\u30ED\u30D1\u30C6\u30A3\u3067\u306F\u3042\u308A\u307E\u305B\u3093
1111
inspection.invalid.sql.testdata=\u30D0\u30A4\u30F3\u30C9\u5909\u6570\u30C7\u30A3\u30EC\u30AF\u30C6\u30A3\u30D6\u3001\u30EA\u30C6\u30E9\u30EB\u5909\u6570\u30C7\u30A3\u30EC\u30AF\u30C6\u30A3\u30D6\u306E\u5F8C\u308D\u306B\u306F\u30C6\u30B9\u30C8\u30C7\u30FC\u30BF\u304C\u5FC5\u8981\u3067\u3059
12+
inspection.invalid.sql.expand=\u0045\u0078\u0070\u0061\u006E\u0064\u30C7\u30A3\u30EC\u30AF\u30C6\u30A3\u30D6\u306E\u5F8C\u308D\u306B\u306F\u30A2\u30B9\u30BF\u30EA\u30B9\u30AF\u304C\u5FC5\u8981\u3067\u3059
1213
inspection.invalid.sql.classpath=\u5B58\u5728\u3057\u306A\u3044\u30D1\u30C3\u30B1\u30FC\u30B8\u307E\u305F\u306F\u30AF\u30E9\u30B9\u540D\u304C\u6307\u5B9A\u3055\u308C\u307E\u3057\u305F\u3002:[{0}]
1314
inspection.invalid.sql.iterable=\u30EB\u30FC\u30D7\u30C7\u30A3\u30EC\u30AF\u30C6\u30A3\u30D6\u306B\u4F7F\u7528\u3067\u304D\u308B\u578B\u306F\u0049\u0074\u0065\u0072\u0061\u0062\u006C\u0065\u578B\u3067\u3059
1415
inspection.invalid.dao.duplicate=\u0053\u0051\u004C\u5185\u3067\u5B9A\u7FA9\u3055\u308C\u305F\u8981\u7D20\u540D\u3068\u91CD\u8907\u3057\u305F\u8981\u7D20\u540D\u304C\u4F7F\u7528\u3055\u308C\u3066\u3044\u307E\u3059

src/test/kotlin/org/domaframework/doma/intellij/inspection/sql/TestDataCheckTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class TestDataCheckTest : DomaSqlTest() {
3434
"$packageName/$testDaoName/commentBlock.sql",
3535
"$packageName/$testDaoName/populateDirective.sql",
3636
"$packageName/$testDaoName/invalidTestData.sql",
37+
"$packageName/$testDaoName/expandDirective.sql",
38+
"$packageName/$testDaoName/invalidExpandDirective.sql",
3739
)
3840
myFixture.enableInspections(SqlTestDataInspection())
3941
}
@@ -88,4 +90,22 @@ class TestDataCheckTest : DomaSqlTest() {
8890

8991
myFixture.testHighlighting(false, false, false, sqlFile)
9092
}
93+
94+
fun testExpandDirective() {
95+
val sqlFile =
96+
findSqlFile("$packageName/$testDaoName/expandDirective.sql")
97+
assertNotNull("Not Found SQL File", sqlFile)
98+
if (sqlFile == null) return
99+
100+
myFixture.testHighlighting(false, false, false, sqlFile)
101+
}
102+
103+
fun testInvalidExpandDirective() {
104+
val sqlFile =
105+
findSqlFile("$packageName/$testDaoName/invalidExpandDirective.sql")
106+
assertNotNull("Not Found SQL File", sqlFile)
107+
if (sqlFile == null) return
108+
109+
myFixture.testHighlighting(false, false, false, sqlFile)
110+
}
91111
}

src/test/testData/src/main/java/doma/example/dao/inspection/TestDataCheckDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ interface TestDataCheckDao {
3030
@Insert(sqlFile=true)
3131
int invalidTestData(Employee employee);
3232

33+
@Select
34+
int expandDirective(Employee employee);
35+
36+
@Select
37+
int invalidExpandDirective(Employee employee);
38+
3339
}
3440

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT /*%expand*/* FROM employee
2+
WHERE rank < <error descr="Test data is required after a bind variable directive or a literal variable directive">/* employee.rank */</error>*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT <error descr="Asterisk is required after expand variable">/*%expand*/</error>0 FROM employee
2+
WHERE rank < /* employee.rank */0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
UPDATE employee
22
SET /*%populate*/id = id
3-
WHERE rank < <error descr="Test data is required after a bind variable directive or a literal variable directive">/* emplotee.rank */</error>
3+
WHERE rank < <error descr="Test data is required after a bind variable directive or a literal variable directive">/* employee.rank */</error>

0 commit comments

Comments
 (0)