Skip to content

Commit 1253ea6

Browse files
committed
Support for EXISTS clause format
1 parent ab0a522 commit 1253ea6

File tree

8 files changed

+161
-3
lines changed

8 files changed

+161
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.formatter.block.group.keyword.option
17+
18+
import com.intellij.lang.ASTNode
19+
import org.domaframework.doma.intellij.formatter.block.SqlBlock
20+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
22+
import org.domaframework.doma.intellij.formatter.util.IndentType
23+
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
24+
25+
class SqlExistsGroupBlock(
26+
node: ASTNode,
27+
context: SqlBlockFormattingContext,
28+
) : SqlKeywordGroupBlock(node, IndentType.OPTIONS, context) {
29+
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
30+
super.setParentGroupBlock(lastGroup)
31+
indent.indentLen = createBlockIndentLen()
32+
indent.groupIndentLen = createGroupIndentLen()
33+
}
34+
35+
override fun createBlockIndentLen(): Int = parentBlock?.indent?.groupIndentLen?.plus(1) ?: 1
36+
37+
override fun createGroupIndentLen(): Int {
38+
parentBlock?.let { parent ->
39+
return parent.indent.groupIndentLen.plus(topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) })
40+
}
41+
return topKeywordBlocks.sumOf { it.getNodeText().length.plus(1) }.minus(1)
42+
}
43+
44+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
45+
if (lastGroup is SqlCreateTableColumnDefinitionRawGroupBlock) {
46+
return false
47+
}
48+
return super.isSaveSpace(lastGroup)
49+
}
50+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ class SqlInGroupBlock(
4040

4141
override fun createBlockIndentLen(): Int {
4242
parentBlock?.let { parent ->
43-
if (parent is SqlElConditionLoopCommentBlock) return parent.indent.groupIndentLen
43+
if (parent is SqlElConditionLoopCommentBlock &&
44+
parent.checkConditionLoopDirectiveParentBlock(this)
45+
) {
46+
return parent.indent.indentLen
47+
}
4448
val prevChildren = this.prevBlocks
4549
val children = prevChildren.filter { it !is SqlDefaultCommentBlock }
4650
val firstChild = children.firstOrNull()
@@ -52,13 +56,16 @@ class SqlInGroupBlock(
5256
}
5357

5458
val dotCount = sumChildren.count { it.node.elementType == SqlTypes.DOT }
59+
val parentText = (parent as? SqlElConditionLoopCommentBlock)?.parentBlock?.getNodeText()?.length ?: 0
60+
5561
return sumChildren
5662
.sumOf { prev ->
5763
prev
5864
.getChildrenTextLen()
5965
.plus(prev.getNodeText().length.plus(1))
6066
}.minus(dotCount * 2)
6167
.plus(parent.indent.groupIndentLen)
68+
.plus(parentText)
6269
.plus(1)
6370
}
6471
return 0

src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubQueryGroupBlock.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.domaframework.doma.intellij.formatter.block.SqlBlock
2121
import org.domaframework.doma.intellij.formatter.block.comment.SqlDefaultCommentBlock
2222
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock
24+
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2425
import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock
2526
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlJoinQueriesGroupBlock
2627
import org.domaframework.doma.intellij.formatter.block.group.keyword.with.SqlWithCommonTableGroupBlock
@@ -65,7 +66,11 @@ open class SqlSubQueryGroupBlock(
6566
is SqlJoinQueriesGroupBlock -> return parent.indent.indentLen
6667
is SqlJoinGroupBlock -> return parent.indent.groupIndentLen.plus(1)
6768
else -> {
68-
val children = prevChildren?.filter { it !is SqlDefaultCommentBlock }
69+
val children =
70+
prevChildren?.filter {
71+
it !is SqlDefaultCommentBlock &&
72+
(parent as? SqlKeywordGroupBlock)?.topKeywordBlocks?.contains(it) == false
73+
}
6974
// Retrieve the list of child blocks excluding the conditional directive that appears immediately before this block,
7075
// as it is already included as a child block.
7176
val sumChildren =

src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlBlockRelationBuilder.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordG
2828
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock
2929
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineGroupBlock
3030
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
31+
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlExistsGroupBlock
3132
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlInGroupBlock
3233
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlLateralGroupBlock
3334
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlReturningGroupBlock
@@ -64,6 +65,7 @@ class SqlBlockRelationBuilder(
6465
SqlColumnDefinitionRawGroupBlock::class,
6566
SqlLateralGroupBlock::class,
6667
SqlInGroupBlock::class,
68+
SqlExistsGroupBlock::class,
6769
)
6870

6971
private val TOP_LEVEL_EXPECTED_TYPES =

src/main/kotlin/org/domaframework/doma/intellij/formatter/handler/JoinClauseHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object JoinClauseHandler {
3030
child: ASTNode,
3131
sqlBlockFormattingCtx: SqlBlockFormattingContext,
3232
): SqlBlock =
33-
if (SqlKeywordUtil.Companion.isJoinKeyword(keywordText)) {
33+
if (SqlKeywordUtil.isJoinKeyword(keywordText)) {
3434
SqlJoinGroupBlock(
3535
child,
3636
sqlBlockFormattingCtx,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ class SqlFormatterTest : BasePlatformTestCase() {
250250
formatSqlFile("ConditionalSubquery.sql", "ConditionalSubquery$formatDataPrefix.sql")
251251
}
252252

253+
fun testConditionalExistsFormatter() {
254+
formatSqlFile("ConditionalExists.sql", "ConditionalExists$formatDataPrefix.sql")
255+
}
256+
253257
private fun formatSqlFile(
254258
beforeFile: String,
255259
afterFile: String,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
SELECT e.id
2+
, e.name
3+
, d.department_name
4+
FROM employees e
5+
WHERE
6+
/*%if isNot*/
7+
NOT
8+
/*%end*/
9+
/*%if filterByHighPerformers */
10+
EXISTS ( SELECT 1
11+
FROM performance_reviews pr
12+
WHERE pr.employee_id = e.id
13+
/*%if reviewPeriod != null */
14+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
15+
/*%end*/
16+
/*%if minScore != null */
17+
AND pr.performance_score >= /* minScore */4.0
18+
/*%end*/ )
19+
/*%elseif filterByLowPerformers */
20+
/*%if isNot*/
21+
NOT
22+
/*%end*/
23+
EXISTS ( SELECT 1
24+
FROM performance_reviews pr
25+
WHERE pr.employee_id = e.id
26+
/*%if reviewPeriod != null */
27+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
28+
/*%end*/
29+
/*%if minScore != null */
30+
AND pr.performance_score >= /* minScore */4.0
31+
/*%end*/ )
32+
/*%elseif filterByMediumPerformers */
33+
NOT
34+
EXISTS
35+
( SELECT 1
36+
FROM performance_reviews pr
37+
WHERE pr.employee_id = e.id
38+
/*%if reviewPeriod != null */
39+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
40+
/*%end*/
41+
/*%if minScore != null */
42+
AND pr.performance_score >= /* minScore */4.0
43+
/*%end*/ )
44+
/*%else*/
45+
e.is_active = true
46+
/*%end*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
SELECT e.id
2+
, e.name
3+
, d.department_name
4+
FROM employees e
5+
WHERE
6+
/*%if isNot*/
7+
NOT
8+
/*%end*/
9+
/*%if filterByHighPerformers */
10+
EXISTS ( SELECT 1
11+
FROM performance_reviews pr
12+
WHERE pr.employee_id = e.id
13+
/*%if reviewPeriod != null */
14+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
15+
/*%end*/
16+
/*%if minScore != null */
17+
AND pr.performance_score >= /* minScore */4.0
18+
/*%end*/ )
19+
/*%elseif filterByLowPerformers */
20+
/*%if isNot*/
21+
NOT
22+
/*%end*/
23+
EXISTS ( SELECT 1
24+
FROM performance_reviews pr
25+
WHERE pr.employee_id = e.id
26+
/*%if reviewPeriod != null */
27+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
28+
/*%end*/
29+
/*%if minScore != null */
30+
AND pr.performance_score >= /* minScore */4.0
31+
/*%end*/ )
32+
/*%elseif filterByMediumPerformers */
33+
NOT EXISTS ( SELECT 1
34+
FROM performance_reviews pr
35+
WHERE pr.employee_id = e.id
36+
/*%if reviewPeriod != null */
37+
AND pr.review_date BETWEEN /* reviewPeriod.startDate */'2023-01-01' AND /* reviewPeriod.endDate */'2023-12-31'
38+
/*%end*/
39+
/*%if minScore != null */
40+
AND pr.performance_score >= /* minScore */4.0
41+
/*%end*/ )
42+
/*%else*/
43+
e.is_active = true
44+
/*%end*/

0 commit comments

Comments
 (0)