Skip to content

Commit 991489c

Browse files
committed
Enhance SQL formatting for DELETE conditions with subqueries
1 parent 267cc7a commit 991489c

File tree

8 files changed

+59
-30
lines changed

8 files changed

+59
-30
lines changed

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,25 @@ open class SqlBlock(
357357
return builder
358358
}
359359

360+
protected fun calculatePrevBlocksLength(
361+
children: List<SqlBlock>,
362+
parent: SqlBlock,
363+
): Int =
364+
children
365+
.sumOf { prev ->
366+
prev
367+
.getChildrenTextLen()
368+
.plus(
369+
if (prev.node.elementType == SqlTypes.DOT ||
370+
prev.node.elementType == SqlTypes.RIGHT_PAREN
371+
) {
372+
0
373+
} else {
374+
prev.getNodeText().length.plus(1)
375+
},
376+
)
377+
}.plus(parent.indent.groupIndentLen)
378+
360379
/**
361380
* Returns the child indentation for the block.
362381
*

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/condition/SqlConditionKeywordGroupBlock.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
2324
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2425

@@ -63,4 +64,14 @@ class SqlConditionKeywordGroupBlock(
6364
}
6465
} ?: return 1
6566
}
67+
68+
override fun createGroupIndentLen(): Int {
69+
parentBlock?.let { parent ->
70+
if (parent is SqlWhereGroupBlock) {
71+
return indent.indentLen.plus(getNodeText().length)
72+
}
73+
return super.createGroupIndentLen()
74+
}
75+
return 0
76+
}
6677
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/condition/SqlConditionalExpressionGroupBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SqlConditionalExpressionGroupBlock(
6262
}
6363
groupIndentLen + directiveParentTextLen
6464
} else {
65-
parent.indent.groupIndentLen.plus(1)
65+
calculatePrevBlocksLength(prevBlocks, parent).plus(1)
6666
}
6767
}
6868
?: offset

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/option/SqlInGroupBlock.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,9 @@ class SqlInGroupBlock(
5656
}
5757

5858
val dotCount = sumChildren.count { it.node.elementType == SqlTypes.DOT }
59-
val parentText = (parent as? SqlElConditionLoopCommentBlock)?.parentBlock?.getNodeText()?.length ?: 0
59+
val parentText = prevChildren.dropLast(1).filter { it !is SqlDefaultCommentBlock }
6060

61-
return sumChildren
62-
.sumOf { prev ->
63-
prev
64-
.getChildrenTextLen()
65-
.plus(prev.getNodeText().length.plus(1))
66-
}.minus(dotCount * 2)
67-
.plus(parent.indent.groupIndentLen)
68-
.plus(parentText)
69-
.plus(1)
61+
return calculatePrevBlocksLength(parentText, parent)
7062
}
7163
return 0
7264
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/word/SqlFunctionGroupBlock.kt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ class SqlFunctionGroupBlock(
5050
return baseIndent.plus(getNodeText().length)
5151
}
5252

53-
private fun calculatePrevBlocksLength(
54-
children: List<SqlBlock>,
55-
parent: SqlBlock,
56-
): Int =
57-
children
58-
.sumOf { prev ->
59-
prev
60-
.getChildrenTextLen()
61-
.plus(
62-
if (prev.node.elementType == SqlTypes.DOT ||
63-
prev.node.elementType == SqlTypes.RIGHT_PAREN
64-
) {
65-
0
66-
} else {
67-
prev.getNodeText().length.plus(1)
68-
},
69-
)
70-
}.plus(parent.indent.groupIndentLen)
71-
7253
private fun calculateBaseIndent(
7354
parent: SqlBlock,
7455
prevBlocksLength: Int,

src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class SqlFormatterTest : BasePlatformTestCase() {
166166
formatSqlFile("WithDelete.sql", "WithDelete$formatDataPrefix.sql")
167167
}
168168

169+
fun testDeleteWithSubQuery() {
170+
formatSqlFile("DeleteWithSubQuery.sql", "DeleteWithSubQuery$formatDataPrefix.sql")
171+
}
172+
169173
fun testNestedDirectivesFormatter() {
170174
formatSqlFile("NestedDirectives.sql", "NestedDirectives$formatDataPrefix.sql")
171175
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DELETE FROM user_session s
2+
WHERE s.count = ( SELECT COUNT(*)
3+
FROM user u
4+
WHERE u.id = /* id */1
5+
AND u.session_id = u.id
6+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
7+
OR EXISTS ( SELECT u.id
8+
FROM user u
9+
WHERE u.id = /* id */1
10+
AND u.session_id = u.id
11+
AND u.time_stamp < /* current */'2099-12-31 00:00:00')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DELETE FROM user_session s
2+
WHERE s.count = ( SELECT COUNT(*)
3+
FROM user u
4+
WHERE u.id = /* id */1
5+
AND u.session_id = u.id
6+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )
7+
OR EXISTS ( SELECT u.id
8+
FROM user u
9+
WHERE u.id = /* id */1
10+
AND u.session_id = u.id
11+
AND u.time_stamp < /* current */'2099-12-31 00:00:00' )

0 commit comments

Comments
 (0)