Skip to content

Commit a77df00

Browse files
committed
Fix formatting handling for IF EXISTS in CREATE queries.
1 parent 7963abf commit a77df00

File tree

9 files changed

+65
-21
lines changed

9 files changed

+65
-21
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock
2121
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
22-
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
2322
import org.domaframework.doma.intellij.formatter.util.IndentType
2423
import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext
2524

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.condition
1818
import com.intellij.lang.ASTNode
1919
import org.domaframework.doma.intellij.formatter.block.SqlBlock
2020
import org.domaframework.doma.intellij.formatter.block.comment.SqlElConditionLoopCommentBlock
21+
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2122
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
2223
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlWhereGroupBlock
2324
import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock
@@ -30,9 +31,9 @@ class SqlConditionKeywordGroupBlock(
3031
node: ASTNode,
3132
context: SqlBlockFormattingContext,
3233
) : SqlSecondOptionKeywordGroupBlock(
33-
node,
34-
context,
35-
) {
34+
node,
35+
context,
36+
) {
3637
var conditionalExpressionGroupBlock: SqlConditionalExpressionGroupBlock? = null
3738

3839
override fun setParentGroupBlock(lastGroup: SqlBlock?) {
@@ -74,4 +75,11 @@ class SqlConditionKeywordGroupBlock(
7475
}
7576
return 0
7677
}
78+
79+
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
80+
if (lastGroup is SqlCreateKeywordGroupBlock) {
81+
return false
82+
}
83+
return super.isSaveSpace(lastGroup)
84+
}
7785
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +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.SqlKeywordGroupBlock
22-
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
22+
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock
2323
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlTableModifySecondGroupBlock
2424
import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTableModificationKeyword
2525
import org.domaframework.doma.intellij.formatter.util.IndentType
@@ -50,8 +50,9 @@ class SqlExistsGroupBlock(
5050
}
5151

5252
override fun isSaveSpace(lastGroup: SqlBlock?): Boolean {
53-
if (lastGroup is SqlCreateTableColumnDefinitionRawGroupBlock ||
54-
lastGroup is SqlTableModifySecondGroupBlock
53+
if (lastGroup is SqlTableModificationKeyword ||
54+
lastGroup is SqlTableModifySecondGroupBlock ||
55+
lastGroup is SqlCreateKeywordGroupBlock
5556
) {
5657
return false
5758
}

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockGenerator.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC
4747
import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionRawGroupBlock
4848
import org.domaframework.doma.intellij.formatter.block.group.keyword.inline.SqlInlineSecondGroupBlock
4949
import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock
50+
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlExistsGroupBlock
5051
import org.domaframework.doma.intellij.formatter.block.group.keyword.option.SqlSecondOptionKeywordGroupBlock
5152
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlFromGroupBlock
5253
import org.domaframework.doma.intellij.formatter.block.group.keyword.second.SqlSecondKeywordBlock
@@ -310,17 +311,32 @@ class SqlBlockGenerator(
310311
child,
311312
sqlBlockFormattingCtx,
312313
)
313-
} else {
314+
} else if ( rootBlock is SqlExistsGroupBlock){
315+
SqlKeywordBlock(
316+
child,
317+
IndentType.ATTACHED,
318+
sqlBlockFormattingCtx,
319+
)
320+
}
321+
else {
314322
SqlConflictClauseBlock(
315323
child,
316324
sqlBlockFormattingCtx,
317325
)
318326
}
319327
} else if (SqlKeywordUtil.isConditionKeyword(keywordText)) {
320-
SqlConditionKeywordGroupBlock(
321-
child,
322-
sqlBlockFormattingCtx,
323-
)
328+
if(lastGroupBlock is SqlCreateKeywordGroupBlock){
329+
SqlKeywordBlock(
330+
child,
331+
IndentType.ATTACHED,
332+
sqlBlockFormattingCtx,
333+
)
334+
}else {
335+
SqlConditionKeywordGroupBlock(
336+
child,
337+
sqlBlockFormattingCtx,
338+
)
339+
}
324340
} else {
325341
SqlSecondOptionKeywordGroupBlock(
326342
child,

src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class SqlKeywordUtil {
334334
"group" to setOf("within"),
335335
"by" to setOf("group", "order", "first", "partition"),
336336
"and" to setOf("between", "preceding"),
337-
"if" to setOf("table", "create"),
337+
// "if" to setOf("table", "index","view"),
338338
"exists" to setOf("if", "where"),
339339
"conflict" to setOf("on"),
340340
"nothing" to setOf("do"),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class SqlFormatterTest : BasePlatformTestCase() {
9090
formatSqlFile("CreateView.sql", "CreateView$formatDataPrefix.sql")
9191
}
9292

93+
fun testCreateViewOrReplaceFormatter() {
94+
formatSqlFile("CreateViewOrReplace.sql", "CreateViewOrReplace$formatDataPrefix.sql")
95+
}
96+
9397
fun testInsertFormatter() {
9498
formatSqlFile("Insert.sql", "Insert$formatDataPrefix.sql")
9599
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
ALTER TABLE /*# tableName */
2-
/*%for column : columns */
3-
ADD COLUMN IF NOT EXISTS /*# column.name */ /*# column.type */
4-
/*%if column_has_next */
5-
,
6-
/*%end */
7-
/*%end */
8-
DROP COLUMN /*# column2.name */ /*# column2.type */
1+
ALTER TABLE /*# tableName */
2+
/*%for column : columns */
3+
ADD COLUMN IF NOT EXISTS /*# column.name */ /*# column.type */
4+
/*%if column_has_next */
5+
,
6+
/*%end */
7+
/*%end */
8+
DROP COLUMN /*# column2.name */ /*# column2.type */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE
2+
OR REPLACE VIEW employee_view AS
3+
SELECT id
4+
, name
5+
, department
6+
, salary
7+
FROM employees
8+
WHERE active = true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE OR REPLACE VIEW employee_view
2+
AS
3+
SELECT id
4+
, name
5+
, department
6+
, salary
7+
FROM employees
8+
WHERE active = true

0 commit comments

Comments
 (0)