Skip to content

Commit ec5ab28

Browse files
authored
Merge pull request #284 from domaframework/feature/sql-format-condition-query-indent
Establish formatting rules for consecutive AND / OR clauses
2 parents cb8f87f + b7efea1 commit ec5ab28

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -260,36 +260,11 @@ open class SqlBlock(
260260
return@setParentGroups lastGroupBlock
261261
}
262262
} else if (lastIndentLevel == childBlock.indent.indentLevel) {
263-
// The AND following an OR will be a child of OR unless surrounded by a subgroup
264-
if (childBlock.getNodeText() == "and" && lastGroupBlock.getNodeText() == "or") {
265-
setParentGroups(
266-
childBlock,
267-
) { history ->
268-
return@setParentGroups lastGroupBlock
269-
}
270-
} else {
271-
if (childBlock.getNodeText() == "or" &&
272-
lastGroupBlock.getNodeText() == "and" &&
273-
lastGroupBlock.parentBlock?.getNodeText() == "or"
274-
) {
275-
val orParentIndex =
276-
blockBuilder.getGroupTopNodeIndex { block ->
277-
block is SqlKeywordGroupBlock && block.getNodeText() == "or"
278-
}
279-
blockBuilder.clearSubListGroupTopNodeIndexHistory(orParentIndex)
280-
setParentGroups(
281-
childBlock,
282-
) { history ->
283-
return@setParentGroups history.lastOrNull()?.second
284-
}
285-
} else {
286-
blockBuilder.removeLastGroupTopNodeIndexHistory()
287-
setParentGroups(
288-
childBlock,
289-
) { history ->
290-
return@setParentGroups lastGroupBlock.parentBlock
291-
}
292-
}
263+
blockBuilder.removeLastGroupTopNodeIndexHistory()
264+
setParentGroups(
265+
childBlock,
266+
) { history ->
267+
return@setParentGroups lastGroupBlock.parentBlock
293268
}
294269
} else if (lastIndentLevel < childBlock.indent.indentLevel) {
295270
setParentGroups(

src/test/testData/sql/formatter/FormattedSelect.sql

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,28 @@ SELECT COUNT(DISTINCT (x))
2929
-- Line3
3030
OR (p.flags & DBO.FPHOTOFLAGS('EDGE') = 0
3131
AND (p.psfmag_g - p.extinction_g) BETWEEN 15 AND 20)
32-
/*%if status == 2 */
33-
-- Line4
34-
AND u.propermotion > 2.
35-
/** And Group */
36-
AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i))
37-
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.)
38-
/*%end*/ ) AS o
32+
/*%if status == 2 */
33+
-- Line4
34+
AND u.propermotion > 2.
35+
/** And Group */
36+
AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i))
37+
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.
38+
AND p.extinction_g - u.propermotion > 0)
39+
/*%end*/ ) AS o
3940
LEFT OUTER JOIN ( SELECT n.objid
4041
, MIN(n.distance) AS nearest
4142
FROM neighbors n
4243
JOIN phototag x
4344
ON n.neighborobjid = x.objid
4445
AND n.neighbormode = DBO.FPHOTOMODE('Primary')
4546
OR n.MODE = /*# "active" */'mode'
46-
AND n.status = 2
47-
AND n.flag = true
47+
AND n.status = 2
48+
AND n.flag = true
4849
WHERE n.TYPE = DBO.FPHOTOTYPE('Star')
4950
AND n.MODE = DBO.FPHOTOMODE('Primary')
5051
OR n.neighbormode = DBO.FPHOTOMODE('Primary')
51-
AND (x.TYPE = DBO.FPHOTOTYPE('Star')
52-
AND x.TYPE = DBO.FPHOTOTYPE('Galaxy'))
52+
AND (x.TYPE = DBO.FPHOTOTYPE('Star')
53+
AND x.TYPE = DBO.FPHOTOTYPE('Galaxy'))
5354
OR x.modelmag_g BETWEEN 10 AND 21
5455
GROUP BY n.objid ) AS nbor
5556
ON o.objid = nbor.objid

src/test/testData/sql/formatter/Select.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ SELECT COUNT( DISTINCT (x)),o.*
3535
/** And Group */
3636
AND (p.psfmag_g - p.extinction_g + 5 * LOG(u.propermotion / 100.) + 5 > 16.136 + 2.727 * (p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i))
3737

38-
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0.)/*%end*/) AS o
38+
OR p.psfmag_g - p.extinction_g - (p.psfmag_i - p.extinction_i) < 0. AND p.extinction_g - u.propermotion > 0)
39+
40+
/*%end*/) AS o
3941

4042
LEFT OUTER JOIN ( SELECT n.objid
4143
, MIN(n.distance) AS nearest

0 commit comments

Comments
 (0)