Skip to content

Commit

Permalink
自動生成したマッパーを楽観ロック対応させるタスクにおいて列やディレクトリの柔軟性を向上
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjiyoshid-a committed Nov 5, 2024
1 parent 2bb95f6 commit f5749a3
Showing 1 changed file with 63 additions and 25 deletions.
88 changes: 63 additions & 25 deletions samples/web-csr/dressca-backend/infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,79 @@ tasks.named('test') {
bootJar.enabled = false
jar.enabled = true

task replaceXmlContent {
task updateMyBatisGeneratorMapperForOptimisticLocking {
doLast {

// 楽観ロック対応させるxmlファイルのリスト
def xmlFilePaths = ['src/main/resources/com/dressca/infrastructure/repository/mybatis/generated/mapper/CatalogItemMapper.xml']
// MyBatis Generator で生成されたマッパーの xml ファイルがあるディレクトリ
def generatedDirectory = 'src/main/resources/com/dressca/infrastructure/repository/mybatis/generated/mapper/'

xmlFilePaths.each{ path ->
// 楽観ロック対応させるマッパーの xml ファイルのリスト
def xmlFilePaths = ['CatalogItemMapper.xml']

def xmlFile = file(path)
// 楽観ロック制御を行う列名
def optimisticLockColumn = 'row_version'
// 楽観ロック制御を行う列のDB上のデータ型
def optimisticLockJdbcType = 'TIMESTAMP'
// エンティティに変換した際の楽観ロック制御を行う列名
def optimisticLockVariable = toLowerCamelCase(optimisticLockColumn)

xmlFilePaths.each { path ->

def xmlFile = file(generatedDirectory + path)
def xmlContent = xmlFile.text

// 正規表現と置換のペアをリストにまとめる
def replacements = [
[ /(<sql id="Update_By_Example_Where_Clause">(?:(?!and row_version = \#\{rowVersion,jdbcType=TIMESTAMP})[\s\S])*?)(<\/if>)/,
'$1and row_version = #{rowVersion,jdbcType=TIMESTAMP}\n $2' ],
[ /(<update id="updateByExampleSelective"[\s\S]*?)(row_version = \#\{row.rowVersion,jdbcType=TIMESTAMP},)/,
'$1row_version = CURRENT_TIMESTAMP,' ],
[ /(<update id="updateByExample"[\s\S]*?)(row_version = \#\{row.rowVersion,jdbcType=TIMESTAMP})/,
'$1row_version = CURRENT_TIMESTAMP' ],
[ /(<update id="updateByPrimaryKeySelective"[\s\S]*?)(row_version = \#\{rowVersion,jdbcType=TIMESTAMP},)/,
'$1row_version = CURRENT_TIMESTAMP,' ],
[ /(<update id="updateByPrimaryKeySelective"(?:(?!and row_version = \#\{rowVersion,jdbcType=TIMESTAMP})[\s\S])*?)(<\/update>)/,
'$1 and row_version = #{rowVersion,jdbcType=TIMESTAMP}\n $2' ],
[ /(<update id="updateByPrimaryKey"(?:(?!row_version = CURRENT_TIMESTAMP)[\s\S])*?)(row_version = \#\{rowVersion,jdbcType=TIMESTAMP})/,
'$1row_version = CURRENT_TIMESTAMP' ],
[ /(<update id="updateByPrimaryKey"(?:(?!and row_version = \#\{rowVersion,jdbcType=TIMESTAMP})[\s\S])*?)(<\/update>)/,
'$1 and row_version = #{rowVersion,jdbcType=TIMESTAMP}\n $2' ]
]

// 置換処理をループで実行
replacements.each { pattern, replacement ->
xmlContent = xmlContent.replaceAll(pattern, replacement)
[ /(<sql id="Update_By_Example_Where_Clause">(?:(?!and ${optimisticLockColumn} = \#\{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}})[\s\S])*?)(<\/if>)/,
"\$1and ${optimisticLockColumn} = #{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}}\n \$2" ],
[ /(<update id="updateByExampleSelective"[\s\S]*?)(${optimisticLockColumn} = \#\{row.${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}},)/,
"\$1${optimisticLockColumn} = CURRENT_TIMESTAMP," ],
[ /(<update id="updateByExample"[\s\S]*?)(${optimisticLockColumn} = \#\{row.${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}})/,
"\$1${optimisticLockColumn} = CURRENT_TIMESTAMP" ],
[ /(<update id="updateByPrimaryKeySelective"[\s\S]*?)(${optimisticLockColumn} = \#\{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}},)/,
"\$1${optimisticLockColumn} = CURRENT_TIMESTAMP," ],
[ /(<update id="updateByPrimaryKeySelective"(?:(?!and ${optimisticLockColumn} = \#\{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}})[\s\S])*?)(<\/update>)/,
"\$1 and ${optimisticLockColumn} = #{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}}\n \$2" ],
[ /(<update id="updateByPrimaryKey"(?:(?!${optimisticLockColumn} = CURRENT_TIMESTAMP)[\s\S])*?)(${optimisticLockColumn} = \#\{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}})/,
"\$1${optimisticLockColumn} = CURRENT_TIMESTAMP" ],
[ /(<update id="updateByPrimaryKey"(?:(?!and ${optimisticLockColumn} = \#\{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}})[\s\S])*?)(<\/update>)/,
"\$1 and ${optimisticLockColumn} = #{${optimisticLockVariable},jdbcType=${optimisticLockJdbcType}}\n \$2" ]
]

// 置換処理をループで実行
replacements.each { pattern, replacement ->
xmlContent = xmlContent.replaceAll(pattern, replacement)
}

xmlFile.write(xmlContent)
}
}
}

xmlFile.write(xmlContent)
// スネークケースをローワーキャメルケースに変換するメソッド
String toLowerCamelCase(String snakeCase) {
StringBuilder camelCase = new StringBuilder()
boolean nextCharUpperCase = false

for (int i = 0; i < snakeCase.length(); i++) {
char currentChar = snakeCase.charAt(i)

if (currentChar == '_') {
nextCharUpperCase = true
} else {
if (nextCharUpperCase) {
camelCase.append(Character.toUpperCase(currentChar))
nextCharUpperCase = false
} else {
camelCase.append(currentChar)
}
}
}

// 最初の文字を小文字にする
if (camelCase.length() > 0) {
camelCase.setCharAt(0, Character.toLowerCase(camelCase.charAt(0)))
}

return camelCase.toString()
}

0 comments on commit f5749a3

Please sign in to comment.