diff --git a/src/main/java/org/domaframework/doma/intellij/Sql.flex b/src/main/java/org/domaframework/doma/intellij/Sql.flex index 95b48860..cb133898 100644 --- a/src/main/java/org/domaframework/doma/intellij/Sql.flex +++ b/src/main/java/org/domaframework/doma/intellij/Sql.flex @@ -33,6 +33,8 @@ import org.domaframework.doma.intellij.psi.SqlTypes; "case", "change", "check", + "conflict", + "constraint", "column", "comment", "create", @@ -42,6 +44,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes; "delete", "desc", "distinct", + "do", "drop", "else", "end", @@ -67,6 +70,7 @@ import org.domaframework.doma.intellij.psi.SqlTypes; "like", "limit", "not", + "nothing", "null", "modify", "offset", diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt index 3c3e8eb0..b50d52ed 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlock.kt @@ -31,12 +31,15 @@ import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlLineCommentBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElSymbolBlock import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlDataTypeBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock @@ -45,7 +48,6 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlC import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnAssignmentSymbolBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock @@ -112,7 +114,9 @@ open class SqlBlock( setParentPropertyBlock(lastGroup) } - open fun setParentPropertyBlock(lastGroup: SqlBlock?) {} + open fun setParentPropertyBlock(lastGroup: SqlBlock?) { + // This method can be overridden to set additional properties on the parent block if needed. + } open val isNeedWhiteSpace: Boolean = true @@ -209,12 +213,18 @@ open class SqlBlock( SqlColumnDefinitionRawGroupBlock::class, SqlCreateTableColumnDefinitionGroupBlock::class, SqlUpdateColumnAssignmentSymbolBlock::class, + SqlDoGroupBlock::class, ) if (isExpectedClassType(expectedClassTypes, childBlock)) return true if (isNewLineSqlComment(child, childBlock)) return true + if (lastGroup is SqlConflictClauseBlock) return false + if (childBlock is SqlColumnRawGroupBlock) { + return !childBlock.isFirstColumnGroup + } + return ( isNewLineGroupBlockAfterRegistrationChild(childBlock, lastGroup) || (childBlock is SqlRightPatternBlock && childBlock.isNewLine(lastGroup)) @@ -417,15 +427,20 @@ open class SqlBlock( return blockUtil.getSubGroupBlock(lastGroup, child) } - SqlTypes.OTHER -> return if (lastGroup is SqlUpdateSetGroupBlock && - lastGroup.columnDefinitionGroupBlock != null - ) { - SqlUpdateColumnAssignmentSymbolBlock(child, defaultFormatCtx) - } else { - SqlOtherBlock( - child, - defaultFormatCtx, - ) + SqlTypes.OTHER -> { + return if (lastGroup is SqlUpdateSetGroupBlock && + lastGroup.columnDefinitionGroupBlock != null + ) { + SqlUpdateColumnAssignmentSymbolBlock( + child, + defaultFormatCtx, + ) + } else { + SqlOtherBlock( + child, + defaultFormatCtx, + ) + } } SqlTypes.RIGHT_PAREN -> return SqlRightPatternBlock( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt index c378d92d..c0fa2d33 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommaBlock.kt @@ -17,13 +17,14 @@ package org.domaframework.doma.intellij.formatter.block import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock +import org.domaframework.doma.intellij.common.util.TypeUtil +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext import org.domaframework.doma.intellij.psi.SqlTypes @@ -63,8 +64,14 @@ open class SqlCommaBlock( return 0 } + val parentIndentSyncBlockTypes = + listOf( + SqlUpdateColumnGroupBlock::class, + SqlUpdateValueGroupBlock::class, + SqlInsertColumnGroupBlock::class, + ) val parentIndentLen = parent.indent.groupIndentLen - if (parent is SqlUpdateColumnGroupBlock || parent is SqlUpdateValueGroupBlock) { + if (TypeUtil.isExpectedClassType(parentIndentSyncBlockTypes, parent)) { return parentIndentLen } @@ -74,9 +81,7 @@ open class SqlCommaBlock( val grandIndentLen = grand.indent.groupIndentLen return grandIndentLen.plus(parentIndentLen).minus(1) } - if (grand is SqlInsertQueryGroupBlock) { - return parentIndentLen - } + if (grand is SqlColumnRawGroupBlock) { val grandIndentLen = grand.indent.groupIndentLen var prevTextLen = 1 @@ -84,7 +89,7 @@ open class SqlCommaBlock( return grandIndentLen.plus(prevTextLen).plus(1) } } - return parentIndentLen + return parentIndentLen.plus(1) } else { var prevLen = 0 parent.childBlocks diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt index 8402e58d..1b16e832 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlKeywordBlock.kt @@ -18,6 +18,7 @@ package org.domaframework.doma.intellij.formatter.block import com.intellij.formatting.Indent import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -42,11 +43,11 @@ open class SqlKeywordBlock( indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) } -// override fun setParentPropertyBlock(lastGroup: SqlBlock?) { -// if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) { -// lastGroup.doQueryBlock = this -// } -// } + override fun setParentPropertyBlock(lastGroup: SqlBlock?) { + if (getNodeText() == "nothing" && lastGroup is SqlDoGroupBlock) { + lastGroup.doQueryBlock = this + } + } override fun buildChildren(): MutableList = mutableListOf() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt index d71640fc..682e2886 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlRightPatternBlock.kt @@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.common.util.TypeUtil.isExpectedClassType import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock @@ -26,6 +27,7 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlI import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -62,6 +64,11 @@ open class SqlRightPatternBlock( return } + if (parent.parentBlock is SqlConflictClauseBlock) { + preSpaceRight = false + return + } + if (parent is SqlSubQueryGroupBlock) { val prevKeywordBlock = parent.childBlocks diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlockCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlBlockCommentBlock.kt similarity index 100% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlBlockCommentBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlBlockCommentBlock.kt diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlCommentBlock.kt similarity index 100% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlCommentBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlCommentBlock.kt diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlLineCommentBlock.kt similarity index 100% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlLineCommentBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/comment/SqlLineCommentBlock.kt diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlWhereGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/OnConflictKeywordType.kt similarity index 60% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlWhereGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/OnConflictKeywordType.kt index 7900cf53..651925c0 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlWhereGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/OnConflictKeywordType.kt @@ -13,13 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.keyword +package org.domaframework.doma.intellij.formatter.block.conflict -import com.intellij.lang.ASTNode -import org.domaframework.doma.intellij.formatter.util.IndentType -import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext - -class SqlWhereGroupBlock( - node: ASTNode, - context: SqlBlockFormattingContext, -) : SqlKeywordGroupBlock(node, IndentType.SECOND, context) +enum class OnConflictKeywordType { + CONFLICT, + CONSTRAINT, + UNKNOWN, +} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlConflictClauseBlock.kt similarity index 54% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlConflictClauseBlock.kt index b44f5f37..0c3dac69 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlConflictClauseBlock.kt @@ -13,34 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.function +package org.domaframework.doma.intellij.formatter.block.conflict import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock +import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext -import org.domaframework.doma.intellij.psi.SqlTypes -/** - * Parameter block of the function call - */ -class SqlParamBlock( +class SqlConflictClauseBlock( node: ASTNode, context: SqlBlockFormattingContext, -) : SqlBlock( +) : SqlKeywordGroupBlock( node, - context.wrap, - context.alignment, - null, - context.spacingBuilder, - context.enableFormat, - context.formatMode, + IndentType.TOP, + context, ) { - val isFirstParam = node.elementType != SqlTypes.COMMA - override var parentBlock: SqlBlock? - get() = super.parentBlock as? SqlParamGroupBlock - set(value) { - if (value is SqlParamGroupBlock) { - super.parentBlock = value - } - } + var conflictType: OnConflictKeywordType = OnConflictKeywordType.CONFLICT + var doBlock: SqlDoGroupBlock? = null + + override fun setParentGroupBlock(lastGroup: SqlBlock?) { + super.setParentGroupBlock(lastGroup) + indent.indentLevel = IndentType.CONFLICT + indent.indentLen = createBlockIndentLen() + indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlDoGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlDoGroupBlock.kt new file mode 100644 index 00000000..1a452779 --- /dev/null +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/conflict/SqlDoGroupBlock.kt @@ -0,0 +1,48 @@ +/* + * Copyright Doma Tools Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.domaframework.doma.intellij.formatter.block.conflict + +import com.intellij.lang.ASTNode +import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock +import org.domaframework.doma.intellij.formatter.util.IndentType +import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext + +class SqlDoGroupBlock( + node: ASTNode, + context: SqlBlockFormattingContext, +) : SqlKeywordGroupBlock( + node, + IndentType.CONFLICT, + context, + ) { + /** + * **NOTHING** or **UPDATE** + */ + var doQueryBlock: SqlBlock? = null + + override fun setParentGroupBlock(lastGroup: SqlBlock?) { + super.setParentGroupBlock(lastGroup) + indent.indentLen = 0 + indent.groupIndentLen = getNodeText().length + } + + override fun setParentPropertyBlock(lastGroup: SqlBlock?) { + if (lastGroup is SqlConflictClauseBlock) { + lastGroup.doBlock = this + } + } +} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt index dbb0274f..21cef758 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElBlockCommentBlock.kt @@ -87,7 +87,6 @@ open class SqlElBlockCommentBlock( SqlElFunctionCallBlock( child, context, - createSpacingBuilder(), ) SqlTypes.BLOCK_COMMENT_CONTENT -> @@ -116,26 +115,6 @@ open class SqlElBlockCommentBlock( Spacing.createSpacing(0, 0, 0, false, 0), ) - private fun createStaticFieldSpacingBuilder(): SqlCustomSpacingBuilder = - SqlCustomSpacingBuilder() - .withSpacing( - SqlTypes.AT_SIGN, - SqlTypes.EL_CLASS, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.AT_SIGN, - SqlTypes.EL_IDENTIFIER, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.EL_IDENTIFIER, - SqlTypes.DOT, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.EL_IDENTIFIER, - SqlTypes.EL_PARAMETERS, - Spacing.createSpacing(0, 0, 0, false, 0), - ) - override fun getSpacing( child1: Block?, child2: Block, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt index 2747d486..1ba59660 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElConditionLoopCommentBlock.kt @@ -28,10 +28,10 @@ import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock import org.domaframework.doma.intellij.formatter.block.SqlOperationBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.IndentType @@ -133,7 +133,6 @@ class SqlElConditionLoopCommentBlock( SqlElFunctionCallBlock( child, context, - createSpacingBuilder(), ) SqlTypes.BLOCK_COMMENT_CONTENT -> @@ -162,26 +161,6 @@ class SqlElConditionLoopCommentBlock( Spacing.createSpacing(0, 0, 0, false, 0), ) - private fun createStaticFieldSpacingBuilder(): SqlCustomSpacingBuilder = - SqlCustomSpacingBuilder() - .withSpacing( - SqlTypes.AT_SIGN, - SqlTypes.EL_CLASS, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.AT_SIGN, - SqlTypes.EL_IDENTIFIER, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.EL_IDENTIFIER, - SqlTypes.DOT, - Spacing.createSpacing(0, 0, 0, false, 0), - ).withSpacing( - SqlTypes.EL_IDENTIFIER, - SqlTypes.EL_PARAMETERS, - Spacing.createSpacing(0, 0, 0, false, 0), - ) - override fun getSpacing( child1: Block?, child2: Block, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFieldAccessBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFieldAccessBlock.kt index df3c40b6..47a56628 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFieldAccessBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFieldAccessBlock.kt @@ -59,7 +59,7 @@ class SqlElFieldAccessBlock( SqlElIdentifierBlock(child, context) SqlTypes.EL_PARAMETERS -> - SqlElParametersBlock(child, context, customSpacingBuilder) + SqlElParametersBlock(child, context) else -> SqlBlock( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFunctionCallBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFunctionCallBlock.kt index 9f6e9bde..c931eaf0 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFunctionCallBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElFunctionCallBlock.kt @@ -19,14 +19,12 @@ import com.intellij.lang.ASTNode import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext import org.domaframework.doma.intellij.psi.SqlTypes class SqlElFunctionCallBlock( node: ASTNode, private val context: SqlBlockFormattingContext, - customSpacingBuilder: SqlCustomSpacingBuilder?, ) : SqlExprBlock( node, context, @@ -53,7 +51,7 @@ class SqlElFunctionCallBlock( SqlElIdentifierBlock(child, context) SqlTypes.EL_PARAMETERS -> - SqlElParametersBlock(child, context, createSpacingBuilder()) + SqlElParametersBlock(child, context) else -> SqlBlock( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt index 0d2b66f6..092a8bb5 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/expr/SqlElParametersBlock.kt @@ -18,14 +18,12 @@ package org.domaframework.doma.intellij.formatter.block.expr import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlUnknownBlock -import org.domaframework.doma.intellij.formatter.builder.SqlCustomSpacingBuilder import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext import org.domaframework.doma.intellij.psi.SqlTypes class SqlElParametersBlock( node: ASTNode, private val context: SqlBlockFormattingContext, - customSpacingBuilder: SqlCustomSpacingBuilder?, ) : SqlExprBlock( node, context, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnBlock.kt similarity index 100% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/SqlColumnBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnBlock.kt diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnDefinitionRawGroupBlock.kt similarity index 98% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnDefinitionRawGroupBlock.kt index 4b005a41..103b5f5d 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/SqlColumnDefinitionRawGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnDefinitionRawGroupBlock.kt @@ -33,7 +33,7 @@ open class SqlColumnDefinitionRawGroupBlock( context, ) { // TODO:Customize indentation within an inline group - val defaultOffset = 5 + open val defaultOffset = 0 val isFirstColumnRaw = node.elementType != SqlTypes.COMMA open var columnBlock: SqlBlock? = if (isFirstColumnRaw) this else null diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnRawGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnRawGroupBlock.kt similarity index 93% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnRawGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnRawGroupBlock.kt index 304c69f4..d03b87d1 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnRawGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnRawGroupBlock.kt @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.subgroup +package org.domaframework.doma.intellij.formatter.block.group.column import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.block.group.column.SqlRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlSelectKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -38,8 +38,6 @@ class SqlColumnRawGroupBlock( node, context, ) { - var isFirstColumnGroup = getNodeText() != "," - override fun setParentGroupBlock(lastGroup: SqlBlock?) { super.setParentGroupBlock(lastGroup) indent.indentLevel = IndentType.COLUMN diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnSelectionGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnSelectionGroupBlock.kt similarity index 86% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnSelectionGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnSelectionGroupBlock.kt index 47fff8ec..78b5e006 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlColumnSelectionGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlColumnSelectionGroupBlock.kt @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.subgroup +package org.domaframework.doma.intellij.formatter.block.group.column import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext abstract class SqlColumnSelectionGroupBlock( diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlRawGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlRawGroupBlock.kt index 430a41be..82de996c 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlRawGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/column/SqlRawGroupBlock.kt @@ -35,6 +35,8 @@ abstract class SqlRawGroupBlock( context.enableFormat, context.formatMode, ) { + var isFirstColumnGroup = getNodeText() != "," + override val indent = ElementIndent( IndentType.COLUMN, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlFunctionGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlFunctionGroupBlock.kt deleted file mode 100644 index 29cc54a8..00000000 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlFunctionGroupBlock.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright Doma Tools Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.domaframework.doma.intellij.formatter.block.group.function - -import com.intellij.lang.ASTNode -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock -import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext - -/** - * Function call group - * - * For Example: - * -- FUNC is [SqlFunctionGroupBlock] - * SELECT FUNC(index -- "(" that after FUNC is [SqlParamGroupBlock] - * , (SELECT number -- "index" and "," is [SqlParamBlock] - * FROM demo) - * , (num1 + num2)) - */ -class SqlFunctionGroupBlock( - node: ASTNode, - context: SqlBlockFormattingContext, -) : SqlSubGroupBlock(node, context) { - val parameterGroupBlock: SqlParamGroupBlock? = null -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt deleted file mode 100644 index a7be2d14..00000000 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/SqlUpdateKeywordGroupBlock.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright Doma Tools Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.domaframework.doma.intellij.formatter.block.group.keyword - -import com.intellij.formatting.Indent -import com.intellij.lang.ASTNode -import com.intellij.psi.formatter.common.AbstractBlock -import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.util.IndentType -import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext - -open class SqlUpdateKeywordGroupBlock( - node: ASTNode, - context: SqlBlockFormattingContext, -) : SqlKeywordGroupBlock( - node, - IndentType.SECOND, - context, - ) { - override fun setParentGroupBlock(block: SqlBlock?) { - super.setParentGroupBlock(block) - indent.indentLevel = IndentType.SECOND - indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(getNodeText().length) - } - - override fun buildChildren(): MutableList = mutableListOf() - - override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen) - - override fun createBlockIndentLen(): Int = - if (!isAdjustIndentOnEnter()) { - parentBlock?.let { - if (it.indent.indentLevel == IndentType.SUB) { - it.indent.groupIndentLen.plus(1) - } else { - val parentTextLen = it.getNodeText().length - val diffTextLen = parentTextLen.minus(getNodeText().length) - it.indent.indentLen.plus(diffTextLen) - } - } ?: 0 - } else { - 0 - } -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateTableColumnDefinitionRawGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateTableColumnDefinitionRawGroupBlock.kt index 5a45103c..7ba794b7 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateTableColumnDefinitionRawGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/create/SqlCreateTableColumnDefinitionRawGroupBlock.kt @@ -31,6 +31,9 @@ class SqlCreateTableColumnDefinitionRawGroupBlock( ) { var columnDataTypeBlock: SqlDataTypeBlock? = null + // TODO:Customize indentation within an inline group + override val defaultOffset = 5 + override var parentBlock: SqlBlock? get() = super.parentBlock as? SqlCreateTableColumnDefinitionGroupBlock set(value) { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertColumnGroupBlock.kt index fc92a5ec..8e6253f6 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertColumnGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertColumnGroupBlock.kt @@ -40,16 +40,6 @@ class SqlInsertColumnGroupBlock( // TODO:Customize indentation private val offset = 2 - /** - * A list of row blocks representing the target columns in an `INSERT` statement, - * distinct from the column line group. - * This list may include: - * **Column row blocks** - * **Subgroup blocks** - * **Conditional or rule directives** - */ - val columnRaws: MutableList = mutableListOf() - override fun setParentGroupBlock(lastGroup: SqlBlock?) { super.setParentGroupBlock(lastGroup) indent.indentLen = createBlockIndentLen() diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertQueryGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertQueryGroupBlock.kt index a688c10a..9fdc20d7 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertQueryGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertQueryGroupBlock.kt @@ -20,7 +20,6 @@ import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -34,7 +33,7 @@ open class SqlInsertQueryGroupBlock( ) { var columnDefinitionGroupBlock: SqlInsertColumnGroupBlock? = null var valueKeywordBlock: SqlKeywordGroupBlock? = null - var valueGroupBlock: SqlSubGroupBlock? = null + var valueGroupBlock: SqlInsertValueGroupBlock? = null override fun setParentGroupBlock(lastGroup: SqlBlock?) { super.setParentGroupBlock(lastGroup) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertValueGroupBlock.kt similarity index 52% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertValueGroupBlock.kt index 679aff38..1513153f 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/function/SqlParamGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/insert/SqlInsertValueGroupBlock.kt @@ -13,20 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.function +package org.domaframework.doma.intellij.formatter.block.group.keyword.insert import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext -/** - * Group block for parameters in a function call. - */ -class SqlParamGroupBlock( +class SqlInsertValueGroupBlock( node: ASTNode, context: SqlBlockFormattingContext, -) : SqlSubGroupBlock(node, context) { - // Contains a SqlSubGroupBlock or SqlParamBlock - val paramGroupBlock: MutableList = mutableListOf() +) : SqlSubGroupBlock( + node, + context, + ) { + override fun setParentGroupBlock(lastGroup: SqlBlock?) { + super.setParentGroupBlock(lastGroup) + indent.groupIndentLen = createBlockIndentLen() + } + + override fun setParentPropertyBlock(lastGroup: SqlBlock?) { + if (lastGroup is SqlKeywordGroupBlock && lastGroup.getNodeText() == "values") { + (lastGroup as? SqlInsertQueryGroupBlock)?.valueGroupBlock = this + } + } + + override fun createBlockIndentLen(): Int { + parentBlock?.let { parent -> + return parent.indent.groupIndentLen.plus(1) + } ?: return 1 + } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/top/SqlTopQueryGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/top/SqlTopQueryGroupBlock.kt index 9968e8d1..816bb702 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/top/SqlTopQueryGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/top/SqlTopQueryGroupBlock.kt @@ -21,8 +21,8 @@ import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext abstract class SqlTopQueryGroupBlock( - private val node: ASTNode, - private val context: SqlBlockFormattingContext, + node: ASTNode, + context: SqlBlockFormattingContext, ) : SqlKeywordGroupBlock( node, IndentType.TOP, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateColumnGroupBlock.kt index a55f9a1d..ec9f0bcf 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateColumnGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateColumnGroupBlock.kt @@ -17,8 +17,8 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.update import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnSelectionGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnSelectionGroupBlock import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext /** diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateQueryGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateQueryGroupBlock.kt index e6456db7..44e608a2 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateQueryGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateQueryGroupBlock.kt @@ -17,10 +17,9 @@ package org.domaframework.doma.intellij.formatter.block.group.keyword.update import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlTopQueryGroupBlock -import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext -import org.domaframework.doma.intellij.formatter.util.SqlKeywordUtil class SqlUpdateQueryGroupBlock( node: ASTNode, @@ -42,27 +41,22 @@ class SqlUpdateQueryGroupBlock( createGroupIndentLen() } + override fun setParentPropertyBlock(lastGroup: SqlBlock?) { + (lastGroup as? SqlDoGroupBlock)?.doQueryBlock = this + } + override fun getBaseIndentLen( preChildBlock: SqlBlock?, - block: SqlBlock?, + lastGroup: SqlBlock?, ): Int { - if (block == null) { + if (lastGroup == null) { return createBlockIndentLen(preChildBlock) } - if (preChildBlock == null) return createBlockIndentLen(preChildBlock) - if (preChildBlock.indent.indentLevel == this.indent.indentLevel && - !SqlKeywordUtil.Companion.isSetLineKeyword(getNodeText(), preChildBlock.getNodeText()) - ) { - if (indent.indentLevel == IndentType.SECOND) { - val diffPreBlockTextLen = getNodeText().length.minus(preChildBlock.getNodeText().length) - return preChildBlock.indent.indentLen.minus(diffPreBlockTextLen) - } else { - val diffPretextLen = getNodeText().length.minus(preChildBlock.getNodeText().length) - return preChildBlock.indent.indentLen.minus(diffPretextLen) - } + return if (lastGroup is SqlDoGroupBlock) { + lastGroup.getNodeText().length.plus(1) } else { - return createBlockIndentLen(preChildBlock) + createBlockIndentLen(preChildBlock) } } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateSetGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateSetGroupBlock.kt index 50bf59f0..83710db3 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateSetGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateSetGroupBlock.kt @@ -20,7 +20,6 @@ import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -32,8 +31,6 @@ open class SqlUpdateSetGroupBlock( IndentType.SECOND, context, ) { - val updateColumnRaws: MutableList = mutableListOf() - // A block used for bulk updates. // It contains a **column group**, an **equals sign (`=`)**, and a **value group block**. var columnDefinitionGroupBlock: SqlUpdateColumnGroupBlock? = null @@ -56,17 +53,13 @@ open class SqlUpdateSetGroupBlock( override fun getIndent(): Indent? = Indent.getSpaceIndent(indent.indentLen) override fun createBlockIndentLen(preChildBlock: SqlBlock?): Int = - if (!isAdjustIndentOnEnter()) { + if (isAdjustIndentOnEnter()) { + 0 + } else { parentBlock?.let { parent -> - if (parent.indent.indentLevel == IndentType.SUB) { - parent.indent.groupIndentLen.plus(1) - } else { - val parentTextLen = parent.getNodeText().length - val diffTextLen = parentTextLen.minus(getNodeText().length) - parent.indent.indentLen.plus(diffTextLen) - } + val parentTextLen = parent.getNodeText().length + val diffTextLen = parentTextLen.minus(getNodeText().length) + parent.indent.indentLen.plus(diffTextLen) } ?: 0 - } else { - 0 } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateValueGroupBlock.kt similarity index 91% rename from src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt rename to src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateValueGroupBlock.kt index 26416931..32a6fc98 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateValueGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/keyword/update/SqlUpdateValueGroupBlock.kt @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.domaframework.doma.intellij.formatter.block.group.subgroup +package org.domaframework.doma.intellij.formatter.block.group.keyword.update import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext /** @@ -49,6 +49,7 @@ class SqlUpdateValueGroupBlock( override fun createBlockIndentLen(): Int { parentBlock?.let { parent -> + // TODO Update Values用のクラスを作る if (parent is SqlUpdateSetGroupBlock) { return parent.indent.indentLen .plus(parent.getNodeText().length) diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt index 9e6ed5f7..3f211370 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlSubGroupBlock.kt @@ -21,6 +21,7 @@ import com.intellij.lang.ASTNode import com.intellij.psi.formatter.common.AbstractBlock import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock import org.domaframework.doma.intellij.formatter.block.group.SqlNewGroupBlock import org.domaframework.doma.intellij.formatter.util.IndentType import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext @@ -53,6 +54,10 @@ abstract class SqlSubGroupBlock( } ?: indent.indentLen.plus(getNodeText().length) } + override fun setParentPropertyBlock(lastGroup: SqlBlock?) { + (lastGroup as? SqlDoGroupBlock)?.doQueryBlock = this + } + override fun addChildBlock(childBlock: SqlBlock) { childBlocks.add(childBlock) if (!isFirstLineComment) { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt deleted file mode 100644 index 873d6504..00000000 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/block/group/subgroup/SqlUpdateColumnGroupBlock.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright Doma Tools Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.domaframework.doma.intellij.formatter.block.group.subgroup - -import com.intellij.lang.ASTNode -import com.intellij.psi.formatter.common.AbstractBlock -import org.domaframework.doma.intellij.formatter.block.SqlBlock -import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlUpdateKeywordGroupBlock -import org.domaframework.doma.intellij.formatter.util.SqlBlockFormattingContext -import org.domaframework.doma.intellij.psi.SqlTypes - -/** - * In an UPDATE statement using the row value constructor, - * a group representing the column list - */ -class SqlUpdateColumnGroupBlock( - node: ASTNode, - context: SqlBlockFormattingContext, -) : SqlSubGroupBlock( - node, - context, - ) { - override fun setParentGroupBlock(block: SqlBlock?) { - super.setParentGroupBlock(block) - indent.indentLen = createBlockIndentLen() - indent.groupIndentLen = indent.indentLen.plus(1) - updateParentGroupIndentLen() - } - - override fun buildChildren(): MutableList = mutableListOf() - - override fun createBlockIndentLen(): Int { - parentBlock?.let { - if (it is SqlUpdateKeywordGroupBlock) { - var parentLen = 0 - val keywords = - it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } - keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) - } - return it.indent.indentLen - .plus(it.getNodeText().length) - .plus(1) - .plus(parentLen) - } - // TODO:Customize indentation - return 2 - } ?: return 2 - } - - private fun updateParentGroupIndentLen() { - parentBlock?.let { - if (it is SqlUpdateKeywordGroupBlock) { - var parentLen = 0 - val keywords = - it.childBlocks.dropLast(1).takeWhile { it.node.elementType == SqlTypes.KEYWORD } - keywords.forEach { keyword -> - parentLen = parentLen.plus(keyword.getNodeText().length).plus(1) - } - it.indent.groupIndentLen = - it.indent.indentLen - .plus(it.getNodeText().length) - .plus(parentLen) - } - } - } -} diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt index 695d28b2..9b949953 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/builder/SqlCustomSpacingBuilder.kt @@ -26,10 +26,10 @@ import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDef import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateTableColumnDefinitionGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlParallelListBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlRightPatternBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock class SqlCustomSpacingBuilder { companion object { diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt index 8fe0d6d1..8e56caa2 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/processor/SqlSetParentGroupProcessor.kt @@ -20,11 +20,11 @@ import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.SqlCommaBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.create.SqlCreateViewGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlRightPatternBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock import org.domaframework.doma.intellij.formatter.builder.SqlBlockBuilder diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/InsertClauseUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/InsertClauseUtil.kt index dd5e43dd..9e4227e1 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/InsertClauseUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/InsertClauseUtil.kt @@ -17,18 +17,24 @@ package org.domaframework.doma.intellij.formatter.util import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertQueryGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlInsertValueGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubGroupBlock object InsertClauseUtil { fun getInsertClauseSubGroup( lastGroup: SqlBlock, child: ASTNode, sqlBlockFormattingCtx: SqlBlockFormattingContext, - ): SqlInsertColumnGroupBlock? { + ): SqlSubGroupBlock? { if (lastGroup is SqlInsertQueryGroupBlock) { return SqlInsertColumnGroupBlock(child, sqlBlockFormattingCtx) } + if (lastGroup is SqlKeywordGroupBlock && lastGroup.getNodeText() == "values") { + return SqlInsertValueGroupBlock(child, sqlBlockFormattingCtx) + } return null } } diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt index 75532159..000caf6b 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlBlockUtil.kt @@ -31,12 +31,17 @@ import org.domaframework.doma.intellij.formatter.block.SqlTableBlock import org.domaframework.doma.intellij.formatter.block.SqlWordBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlBlockCommentBlock import org.domaframework.doma.intellij.formatter.block.comment.SqlCommentBlock +import org.domaframework.doma.intellij.formatter.block.conflict.OnConflictKeywordType +import org.domaframework.doma.intellij.formatter.block.conflict.SqlConflictClauseBlock +import org.domaframework.doma.intellij.formatter.block.conflict.SqlDoGroupBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElBlockCommentBlock import org.domaframework.doma.intellij.formatter.block.expr.SqlElConditionLoopCommentBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnBlock import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnDefinitionRawGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.column.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlInlineSecondGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlJoinGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.SqlKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionKeywordGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.condition.SqlConditionalExpressionGroupBlock @@ -48,7 +53,6 @@ import org.domaframework.doma.intellij.formatter.block.group.keyword.insert.SqlI import org.domaframework.doma.intellij.formatter.block.group.keyword.top.SqlSelectQueryGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateQueryGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlColumnRawGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlDataTypeParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlFunctionParamBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock @@ -120,6 +124,20 @@ class SqlBlockUtil( } } + IndentType.CONFLICT -> { + if (lastGroupBlock is SqlConflictClauseBlock) { + lastGroupBlock.conflictType = + when (keywordText) { + "conflict" -> OnConflictKeywordType.CONFLICT + "constraint" -> OnConflictKeywordType.CONSTRAINT + else -> OnConflictKeywordType.UNKNOWN + } + return SqlKeywordBlock(child, indentLevel, sqlBlockFormattingCtx) + } else { + return SqlConflictClauseBlock(child, sqlBlockFormattingCtx) + } + } + else -> return SqlKeywordBlock(child, indentLevel, sqlBlockFormattingCtx) } return SqlKeywordBlock(child, indentLevel, sqlBlockFormattingCtx) @@ -130,10 +148,10 @@ class SqlBlockUtil( keywordText: String, child: ASTNode, lastGroupBlock: SqlBlock?, - ): SqlBlock = + ): SqlBlock { when (indentLevel) { IndentType.JOIN -> { - JoinGroupUtil.getJoinKeywordGroupBlock( + return JoinGroupUtil.getJoinKeywordGroupBlock( lastGroupBlock, keywordText, child, @@ -142,14 +160,14 @@ class SqlBlockUtil( } IndentType.INLINE_SECOND -> { - SqlInlineSecondGroupBlock( + return SqlInlineSecondGroupBlock( child, sqlBlockFormattingCtx, ) } IndentType.TOP -> { - when (keywordText) { + return when (keywordText) { "select" -> SqlSelectQueryGroupBlock( child, @@ -168,6 +186,12 @@ class SqlBlockUtil( sqlBlockFormattingCtx, ) + "do" -> + SqlDoGroupBlock( + child, + sqlBlockFormattingCtx, + ) + "update" -> SqlUpdateQueryGroupBlock( child, @@ -184,7 +208,7 @@ class SqlBlockUtil( } IndentType.SECOND -> { - if (keywordText == "set") { + return if (keywordText == "set") { SqlUpdateSetGroupBlock( child, sqlBlockFormattingCtx, @@ -199,7 +223,12 @@ class SqlBlockUtil( } IndentType.SECOND_OPTION -> { - if (SqlKeywordUtil.isConditionKeyword(keywordText)) { + return if (keywordText == "on" && lastGroupBlock !is SqlJoinGroupBlock) { + SqlConflictClauseBlock( + child, + sqlBlockFormattingCtx, + ) + } else if (SqlKeywordUtil.isConditionKeyword(keywordText)) { SqlConditionKeywordGroupBlock( child, sqlBlockFormattingCtx, @@ -213,14 +242,31 @@ class SqlBlockUtil( } } + IndentType.CONFLICT -> { + return if (lastGroupBlock is SqlConflictClauseBlock) { + SqlKeywordBlock( + child, + indentLevel, + sqlBlockFormattingCtx, + ) + } else { + SqlKeywordGroupBlock( + child, + indentLevel, + sqlBlockFormattingCtx, + ) + } + } + else -> { - SqlKeywordGroupBlock( + return SqlKeywordGroupBlock( child, indentLevel, sqlBlockFormattingCtx, ) } } + } fun getSubGroupBlock( lastGroup: SqlBlock?, diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt index 851dd07d..23b4ba8c 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/SqlKeywordUtil.kt @@ -20,6 +20,7 @@ enum class IndentType( private val group: Boolean = false, ) { FILE(0, true), + CONFLICT(1, true), TOP(1, true), SECOND(2, true), JOIN(3, true), diff --git a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/UpdateClauseUtil.kt b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/UpdateClauseUtil.kt index 9d435b84..78ada5ff 100644 --- a/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/UpdateClauseUtil.kt +++ b/src/main/kotlin/org/domaframework/doma/intellij/formatter/util/UpdateClauseUtil.kt @@ -19,8 +19,8 @@ import com.intellij.lang.ASTNode import org.domaframework.doma.intellij.formatter.block.SqlBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateColumnGroupBlock import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateSetGroupBlock +import org.domaframework.doma.intellij.formatter.block.group.keyword.update.SqlUpdateValueGroupBlock import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlSubQueryGroupBlock -import org.domaframework.doma.intellij.formatter.block.group.subgroup.SqlUpdateValueGroupBlock object UpdateClauseUtil { fun getUpdateClauseSubGroup( diff --git a/src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt b/src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt index afa321a2..c3fe10a4 100644 --- a/src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt +++ b/src/test/kotlin/org/domaframework/doma/intellij/formatter/SqlFormatterTest.kt @@ -27,6 +27,8 @@ class SqlFormatterTest : BasePlatformTestCase() { override fun getTestDataPath(): String? = "src/test/testData/sql/formatter" + private val formatDataPrefix = "_format" + override fun setUp() { super.setUp() settingSqlFormat(true) @@ -49,39 +51,51 @@ class SqlFormatterTest : BasePlatformTestCase() { } fun testSelectFormatter() { - formatSqlFile("Select.sql", "Select_format.sql") + formatSqlFile("Select.sql", "Select$formatDataPrefix.sql") } fun testCreateTableFormatter() { - formatSqlFile("CreateTable.sql", "CreateTable_format.sql") + formatSqlFile("CreateTable.sql", "CreateTable$formatDataPrefix.sql") } fun testCreateViewFormatter() { - formatSqlFile("CreateView.sql", "CreateView_format.sql") + formatSqlFile("CreateView.sql", "CreateView$formatDataPrefix.sql") } fun testInsertFormatter() { - formatSqlFile("Insert.sql", "Insert_format.sql") + formatSqlFile("Insert.sql", "Insert$formatDataPrefix.sql") } fun testInsertWithBindVariableFormatter() { - formatSqlFile("InsertWithBindVariable.sql", "InsertWithBindVariable_format.sql") + formatSqlFile("InsertWithBindVariable.sql", "InsertWithBindVariable$formatDataPrefix.sql") } fun testUpdateFormatter() { - formatSqlFile("Update.sql", "Update_format.sql") + formatSqlFile("Update.sql", "Update$formatDataPrefix.sql") } fun testUpdateBindVariableFormatter() { - formatSqlFile("UpdateBindVariable.sql", "UpdateBindVariable_format.sql") + formatSqlFile("UpdateBindVariable.sql", "UpdateBindVariable$formatDataPrefix.sql") } fun testUpdateTupleAssignmentFormatter() { - formatSqlFile("UpdateTupleAssignment.sql", "UpdateTupleAssignment_format.sql") + formatSqlFile("UpdateTupleAssignment.sql", "UpdateTupleAssignment$formatDataPrefix.sql") } fun testDeleteFormatter() { - formatSqlFile("Delete.sql", "Delete_format.sql") + formatSqlFile("Delete.sql", "Delete$formatDataPrefix.sql") + } + + fun testInsertConflictUpdateFormatter() { + formatSqlFile("InsertConflictUpdate.sql", "InsertConflictUpdate$formatDataPrefix.sql") + } + + fun testInsertConflictNothingFormatter() { + formatSqlFile("InsertConflictNothing.sql", "InsertConflictNothing$formatDataPrefix.sql") + } + + fun testInsertConflictUpdateWithOutTableFormatter() { + formatSqlFile("InsertConflictUpdateWithOutTable.sql", "InsertConflictUpdateWithOutTable$formatDataPrefix.sql") } private fun formatSqlFile( diff --git a/src/test/testData/sql/formatter/InsertConflictNothing.sql b/src/test/testData/sql/formatter/InsertConflictNothing.sql new file mode 100644 index 00000000..76cf9510 --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictNothing.sql @@ -0,0 +1,2 @@ +insert into employee (id, username) values ( /* employees.id */0, /* employees.name */'name') +on conflict (username) do nothing diff --git a/src/test/testData/sql/formatter/InsertConflictNothing_format.sql b/src/test/testData/sql/formatter/InsertConflictNothing_format.sql new file mode 100644 index 00000000..21dffa03 --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictNothing_format.sql @@ -0,0 +1,7 @@ +INSERT INTO employee + (id + , username) + VALUES ( /* employees.id */0 + , /* employees.name */'name') +ON CONFLICT (username) +DO NOTHING diff --git a/src/test/testData/sql/formatter/InsertConflictUpdate.sql b/src/test/testData/sql/formatter/InsertConflictUpdate.sql new file mode 100644 index 00000000..b46f546a --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictUpdate.sql @@ -0,0 +1,3 @@ +insert into users (username, email) +values ('user', 'user@example.com') +on CONFLICT(username) ON constraint do update set email = EXCLUDED.email, created_at = CURRENT_TIMESTAMP diff --git a/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable.sql b/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable.sql new file mode 100644 index 00000000..778ffc51 --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable.sql @@ -0,0 +1,3 @@ +insert into users (username, email) +values ('user', 'user@example.com') +on CONFLICT ON constraint do update set email = EXCLUDED.email, created_at = CURRENT_TIMESTAMP diff --git a/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable_format.sql b/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable_format.sql new file mode 100644 index 00000000..418178fc --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictUpdateWithOutTable_format.sql @@ -0,0 +1,9 @@ +INSERT INTO users + (username + , email) + VALUES ('user' + , 'user@example.com') +ON CONFLICT ON CONSTRAINT +DO UPDATE + SET email = EXCLUDED.email + , created_at = CURRENT_TIMESTAMP diff --git a/src/test/testData/sql/formatter/InsertConflictUpdate_format.sql b/src/test/testData/sql/formatter/InsertConflictUpdate_format.sql new file mode 100644 index 00000000..5ca9311e --- /dev/null +++ b/src/test/testData/sql/formatter/InsertConflictUpdate_format.sql @@ -0,0 +1,9 @@ +INSERT INTO users + (username + , email) + VALUES ('user' + , 'user@example.com') +ON CONFLICT (username) ON CONSTRAINT +DO UPDATE + SET email = EXCLUDED.email + , created_at = CURRENT_TIMESTAMP