-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Explain과 Explain Anaylze 쿼리 실행하도록 수정
- Loading branch information
1 parent
a0aac58
commit c37ef36
Showing
9 changed files
with
116 additions
and
68 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
api-repo/src/test/kotlin/com/few/api/repo/explain/ExplainGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.few.api.repo.explain | ||
|
||
import org.jooq.DSLContext | ||
import org.jooq.Query | ||
|
||
class ExplainGenerator { | ||
companion object { | ||
/** | ||
* Execute EXPLAIN and EXPLAIN ANALYZE FORMAT=TREE | ||
*/ | ||
fun execute(dslContext: DSLContext, query: Query): String { | ||
val sql = query.sql | ||
val values = query.bindValues | ||
mapSqlAndValues(sql, values).let { | ||
val explain = StringBuilder() | ||
explain.append("EXPLAIN $it\n") | ||
explain.append("\n") | ||
dslContext.fetch("EXPLAIN $it").let { | ||
explain.append(it) | ||
} | ||
explain.append("\n\n") | ||
explain.append("EXPLAIN ANALYZE FORMAT=TREE $it\n") | ||
explain.append("\n") | ||
dslContext.fetch("EXPLAIN ANALYZE FORMAT=TREE $it").let { | ||
it.forEach { record -> | ||
explain.append(record[0].toString()) | ||
} | ||
} | ||
return explain.toString() | ||
} | ||
} | ||
|
||
/** | ||
* Execute EXPLAIN | ||
*/ | ||
fun explain(dslContext: DSLContext, query: Query): String { | ||
val sql = query.sql | ||
val values = query.bindValues | ||
mapSqlAndValues(sql, values).let { | ||
val explain = StringBuilder() | ||
explain.append("EXPLAIN $it\n") | ||
explain.append("\n") | ||
dslContext.fetch("EXPLAIN $it").let { | ||
explain.append(it) | ||
} | ||
return explain.toString() | ||
} | ||
} | ||
|
||
/** | ||
* Execute EXPLAIN ANALYZE FORMAT=TREE | ||
*/ | ||
fun analyzeFormatTree(dslContext: DSLContext, query: Query): String { | ||
val sql = query.sql | ||
val values = query.bindValues | ||
mapSqlAndValues(sql, values).let { | ||
val explain = StringBuilder() | ||
explain.append("EXPLAIN ANALYZE FORMAT=TREE $it\n") | ||
explain.append("\n") | ||
dslContext.fetch("EXPLAIN ANALYZE FORMAT=TREE $it").let { | ||
it.forEach { record -> | ||
explain.append(record[0].toString()) | ||
} | ||
} | ||
return explain.toString() | ||
} | ||
} | ||
|
||
private fun mapSqlAndValues(sql: String, values: List<Any>) = | ||
values.foldIndexed(sql) { index, acc, value -> | ||
if (value is String) { | ||
acc.replaceFirst("?", "'$value'") | ||
} else { | ||
acc.replaceFirst("?", "$value") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,17 @@ import com.few.api.repo.dao.member.query.BrowseWorkbookWritersQuery | |
import com.few.api.repo.dao.member.query.SelectMemberByEmailNotConsiderDeletedAtQuery | ||
import com.few.api.repo.dao.member.query.SelectMemberByEmailQuery | ||
import com.few.api.repo.dao.member.query.SelectWriterQuery | ||
import com.few.api.repo.dao.member.support.WriterDescription | ||
import com.few.api.repo.dao.member.support.WriterDescriptionJsonMapper | ||
import com.few.api.repo.explain.ExplainGenerator | ||
import com.few.api.repo.explain.InsertUpdateExplainGenerator | ||
import com.few.api.repo.explain.ResultGenerator | ||
import com.few.api.repo.jooq.JooqTestSpec | ||
import com.few.data.common.code.MemberType | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import jooq.jooq_dsl.tables.Member | ||
import org.jooq.DSLContext | ||
import org.jooq.JSON | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import java.net.URL | ||
|
||
@Tag("explain") | ||
class MemberDaoExplainGenerateTest : JooqTestSpec() { | ||
|
@@ -39,47 +35,13 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
@Autowired | ||
private lateinit var writerDescriptionJsonMapper: WriterDescriptionJsonMapper | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
log.debug { "===== start setUp =====" } | ||
dslContext.deleteFrom(Member.MEMBER).execute() | ||
dslContext.insertInto(Member.MEMBER) | ||
.set(Member.MEMBER.ID, 1) | ||
.set(Member.MEMBER.EMAIL, "[email protected]") | ||
.set(Member.MEMBER.TYPE_CD, MemberType.NORMAL.code) | ||
.execute() | ||
|
||
val writerDescription = writerDescriptionJsonMapper.toJson( | ||
WriterDescription( | ||
"few2", | ||
URL("http://localhost:8080/writers/url2"), | ||
URL("https://github.com/user-attachments/assets/28df9078-488c-49d6-9375-54ce5a250742") | ||
) | ||
) | ||
|
||
dslContext.insertInto(Member.MEMBER) | ||
.set(Member.MEMBER.ID, 2) | ||
.set(Member.MEMBER.EMAIL, "[email protected]") | ||
.set(Member.MEMBER.TYPE_CD, MemberType.WRITER.code) | ||
.set(Member.MEMBER.DESCRIPTION, JSON.valueOf(writerDescription)) | ||
.execute() | ||
|
||
dslContext.insertInto(Member.MEMBER) | ||
.set(Member.MEMBER.ID, 3) | ||
.set(Member.MEMBER.EMAIL, "[email protected]") | ||
.set(Member.MEMBER.TYPE_CD, MemberType.WRITER.code) | ||
.set(Member.MEMBER.DESCRIPTION, JSON.valueOf(writerDescription)) | ||
.execute() | ||
log.debug { "===== finish setUp =====" } | ||
} | ||
|
||
@Test | ||
fun selectWriterQueryExplain() { | ||
val query = SelectWriterQuery(1).let { | ||
memberDao.selectWriterQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectWriterQueryExplain") | ||
} | ||
|
@@ -90,7 +52,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
memberDao.selectWritersQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectWritersQueryExplain") | ||
} | ||
|
@@ -101,7 +63,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
memberDao.selectWritersQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectWritersQueryExplainByWorkbookIds") | ||
} | ||
|
@@ -112,7 +74,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
memberDao.selectMemberByEmailQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectMemberByEmailQueryExplainExplain") | ||
} | ||
|
@@ -123,7 +85,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
memberDao.selectMemberByEmailQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectMemberByEmailNotConsiderDeletedAtQueryExplain") | ||
} | ||
|
@@ -148,7 +110,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() { | |
memberDao.selectMemberIdAndTypeQuery(it) | ||
} | ||
|
||
val explain = dslContext.explain(query).toString() | ||
val explain = ExplainGenerator.execute(dslContext, query) | ||
|
||
ResultGenerator.execute(query, explain, "selectMemberIdAndTypeQueryExplain") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.