Update for modern Exposed

This commit is contained in:
Matt Soucy 2024-02-24 10:27:55 -05:00
parent da9a4e9602
commit d049197d7c
2 changed files with 42 additions and 41 deletions

View File

@ -5,6 +5,7 @@ import me.msoucy.gbat.mutableCopyOf
import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
class KnowledgeModel(val db: Database, val constant: Double, val riskModel: RiskModel) { class KnowledgeModel(val db: Database, val constant: Double, val riskModel: RiskModel) {
@ -71,7 +72,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
fun knowledgeSummary(lineNum: Int) = transaction(db) { fun knowledgeSummary(lineNum: Int) = transaction(db) {
LineKnowledge.select { LineKnowledge.selectAll().where {
LineKnowledge.linenum eq lineNum LineKnowledge.linenum eq lineNum
}.map { }.map {
val acct = getKnowledgeAcct(it[LineKnowledge.knowledgeacctid]) val acct = getKnowledgeAcct(it[LineKnowledge.knowledgeacctid])
@ -90,7 +91,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun getKnowledgeAcct(knowledgeAcctId: Int): KnowledgeAcct = transaction(db) { private fun getKnowledgeAcct(knowledgeAcctId: Int): KnowledgeAcct = transaction(db) {
KnowledgeAcctsTable.select { KnowledgeAcctsTable.selectAll().where {
KnowledgeAcctsTable.id eq knowledgeAcctId KnowledgeAcctsTable.id eq knowledgeAcctId
}.map { }.map {
KnowledgeAcct( KnowledgeAcct(
@ -103,8 +104,8 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
private fun destroyLineKnowledge(knowledgeId: Int, lineNum: Int) = transaction(db) { private fun destroyLineKnowledge(knowledgeId: Int, lineNum: Int) = transaction(db) {
LineKnowledge.deleteWhere { LineKnowledge.deleteWhere {
(LineKnowledge.knowledgeacctid eq knowledgeId) and (knowledgeacctid eq knowledgeId) and
(LineKnowledge.linenum eq lineNum) (linenum eq lineNum)
} }
} }
@ -137,7 +138,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun knowledgeInAcct(knowledgeAcctId: Int, lineNum: Int) = transaction(db) { private fun knowledgeInAcct(knowledgeAcctId: Int, lineNum: Int) = transaction(db) {
LineKnowledge.select { LineKnowledge.selectAll().where {
(LineKnowledge.knowledgeacctid eq knowledgeAcctId) and (LineKnowledge.knowledgeacctid eq knowledgeAcctId) and
(LineKnowledge.linenum eq lineNum) (LineKnowledge.linenum eq lineNum)
}.map { }.map {
@ -146,7 +147,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun nonSafeAcctsWithKnowledgeOf(lineNum: Int) = transaction(db) { private fun nonSafeAcctsWithKnowledgeOf(lineNum: Int) = transaction(db) {
LineKnowledge.select { LineKnowledge.selectAll().where {
(LineKnowledge.linenum eq lineNum) and (LineKnowledge.linenum eq lineNum) and
(LineKnowledge.knowledgeacctid neq SAFE_KNOWLEDGE_ACCT_ID) (LineKnowledge.knowledgeacctid neq SAFE_KNOWLEDGE_ACCT_ID)
}.map { }.map {
@ -155,7 +156,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun allAcctsWithKnowledgeOf(lineNum: Int) = transaction(db) { private fun allAcctsWithKnowledgeOf(lineNum: Int) = transaction(db) {
LineKnowledge.select { LineKnowledge.selectAll().where {
LineKnowledge.linenum eq lineNum LineKnowledge.linenum eq lineNum
}.map { }.map {
it[LineKnowledge.knowledgeacctid] it[LineKnowledge.knowledgeacctid]
@ -163,7 +164,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun adjustKnowledge(knowledgeAcctId: Int, lineNum: Int, adjustment: Double) = transaction(db) { private fun adjustKnowledge(knowledgeAcctId: Int, lineNum: Int, adjustment: Double) = transaction(db) {
val lineExists = LineKnowledge.select { val lineExists = LineKnowledge.selectAll().where {
(LineKnowledge.knowledgeacctid eq knowledgeAcctId) and (LineKnowledge.knowledgeacctid eq knowledgeAcctId) and
(LineKnowledge.linenum eq lineNum) (LineKnowledge.linenum eq lineNum)
}.count() > 0 }.count() > 0
@ -186,7 +187,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
private fun lookupOrCreateKnowledgeAcct(authors: List<String>) = transaction(db) { private fun lookupOrCreateKnowledgeAcct(authors: List<String>) = transaction(db) {
val authorStr = authors.sorted().joinToString("\n") val authorStr = authors.sorted().joinToString("\n")
KnowledgeAcctsTable.select { KnowledgeAcctsTable.selectAll().where {
KnowledgeAcctsTable.authors eq authorStr KnowledgeAcctsTable.authors eq authorStr
}.map { }.map {
it[KnowledgeAcctsTable.id].value it[KnowledgeAcctsTable.id].value
@ -194,7 +195,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
KnowledgeAcctsTable.insert { KnowledgeAcctsTable.insert {
it[KnowledgeAcctsTable.authors] = authorStr it[KnowledgeAcctsTable.authors] = authorStr
} }
val theNewId = KnowledgeAcctsTable.select { val theNewId = KnowledgeAcctsTable.selectAll().where {
KnowledgeAcctsTable.authors eq authorStr KnowledgeAcctsTable.authors eq authorStr
}.map { }.map {
it[KnowledgeAcctsTable.id].value it[KnowledgeAcctsTable.id].value
@ -214,7 +215,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
AuthorsTable.insertIgnore { AuthorsTable.insertIgnore {
it[author] = authorName it[author] = authorName
} }
AuthorsTable.select { AuthorsTable.selectAll().where {
AuthorsTable.author eq authorName AuthorsTable.author eq authorName
}.first().let { }.first().let {
it[AuthorsTable.id] it[AuthorsTable.id]
@ -222,7 +223,7 @@ class KnowledgeModel(val db: Database, val constant: Double, val riskModel: Risk
} }
private fun totalLineKnowledge(linenum: Int) = transaction(db) { private fun totalLineKnowledge(linenum: Int) = transaction(db) {
LineKnowledge.select { LineKnowledge.selectAll().where {
LineKnowledge.linenum eq linenum LineKnowledge.linenum eq linenum
}.map { }.map {
it[LineKnowledge.knowledge] it[LineKnowledge.knowledge]

View File

@ -166,7 +166,7 @@ class SummaryModel(val db: Database) {
} }
fun fpath(fileId: Int): Path = transaction(db) { fun fpath(fileId: Int): Path = transaction(db) {
FilesTable.select { FilesTable.selectAll().where {
FilesTable.id eq fileId FilesTable.id eq fileId
}.first().let { row -> }.first().let { row ->
val dirs = reconsDirs(row[FilesTable.dirid]) val dirs = reconsDirs(row[FilesTable.dirid])
@ -176,7 +176,7 @@ class SummaryModel(val db: Database) {
fun projectFiles(project: String): List<ProjectFilesResult> = transaction(db) { fun projectFiles(project: String): List<ProjectFilesResult> = transaction(db) {
val projectId = findOrCreateProject(project) val projectId = findOrCreateProject(project)
(FilesTable innerJoin DirsTable).select { (FilesTable innerJoin DirsTable).selectAll().where {
(FilesTable.dirid eq DirsTable.id) and (FilesTable.dirid eq DirsTable.id) and
(DirsTable.projectid eq projectId) (DirsTable.projectid eq projectId)
}.map { row -> }.map { row ->
@ -192,7 +192,7 @@ class SummaryModel(val db: Database) {
val parentDirIds = mutableListOf(0) val parentDirIds = mutableListOf(0)
while (parentDirIds.isNotEmpty()) { while (parentDirIds.isNotEmpty()) {
val parentId = parentDirIds.removeAt(0) val parentId = parentDirIds.removeAt(0)
DirsTable.select { DirsTable.parentdirid eq parentId } DirsTable.selectAll().where { DirsTable.parentdirid eq parentId }
.forEach { row -> .forEach { row ->
val dirId = row[DirsTable.id].value val dirId = row[DirsTable.id].value
theTree.getOrPut(parentId) { ProjectTree() }.dirs.add(dirId) theTree.getOrPut(parentId) { ProjectTree() }.dirs.add(dirId)
@ -203,13 +203,13 @@ class SummaryModel(val db: Database) {
// Then add the files // Then add the files
theTree.entries.forEach { entry -> theTree.entries.forEach { entry ->
FilesTable.select { FilesTable.dirid eq entry.key }.forEach { row -> FilesTable.selectAll().where { FilesTable.dirid eq entry.key }.forEach { row ->
entry.value.files[row[FilesTable.id].value] = FileEntry(row[FilesTable.fname]) entry.value.files[row[FilesTable.id].value] = FileEntry(row[FilesTable.fname])
} }
entry.value.files.entries.forEach { (fileId, fileEntry) -> entry.value.files.entries.forEach { (fileId, fileEntry) ->
lineAllocations lineAllocations
.slice(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum()) .select(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum())
.select { LinesTable.fileid eq fileId } .where { LinesTable.fileid eq fileId }
.groupBy(LinesTable.fileid) .groupBy(LinesTable.fileid)
.forEach { row -> .forEach { row ->
fileEntry.stats.totKnowledge = row[AllocationsTable.knowledge.sum()] ?: 0.0 fileEntry.stats.totKnowledge = row[AllocationsTable.knowledge.sum()] ?: 0.0
@ -219,12 +219,12 @@ class SummaryModel(val db: Database) {
} }
entry.value.files.entries.forEach { (fileId, fileEntry) -> entry.value.files.entries.forEach { (fileId, fileEntry) ->
lineAllocationGroups lineAllocationGroups
.slice( .select(
AllocationsTable.knowledge.sum(), AllocationsTable.knowledge.sum(),
AllocationsTable.risk.sum(), AllocationsTable.risk.sum(),
AllocationsTable.orphaned.sum(), AllocationsTable.orphaned.sum(),
AuthorsGroupsTable.authors AuthorsGroupsTable.authors
).select { LinesTable.fileid eq fileId } ).where { LinesTable.fileid eq fileId }
.groupBy(AllocationsTable.authorgroupid) .groupBy(AllocationsTable.authorgroupid)
.orderBy(AuthorsGroupsTable.authors) .orderBy(AuthorsGroupsTable.authors)
.forEach { row -> .forEach { row ->
@ -239,24 +239,24 @@ class SummaryModel(val db: Database) {
val projectTree = ProjectTreeResult(project, root) val projectTree = ProjectTreeResult(project, root)
allJoined allJoined
.slice( .select(
AllocationsTable.knowledge.sum(), AllocationsTable.knowledge.sum(),
AllocationsTable.risk.sum(), AllocationsTable.risk.sum(),
AllocationsTable.orphaned.sum(), AllocationsTable.orphaned.sum(),
AuthorsGroupsTable.authors AuthorsGroupsTable.authors
) )
.select { DirsTable.projectid eq projectId } .where { DirsTable.projectid eq projectId }
.groupBy(AuthorsGroupsTable.id) .groupBy(AuthorsGroupsTable.id)
.forEach { row -> .forEach { row ->
projectTree.authorRisks[row[AuthorsGroupsTable.authors]] = Statistics(row) projectTree.authorRisks[row[AuthorsGroupsTable.authors]] = Statistics(row)
} }
manyJoined manyJoined
.slice( .select(
AllocationsTable.knowledge.sum(), AllocationsTable.knowledge.sum(),
AllocationsTable.risk.sum(), AllocationsTable.risk.sum(),
AllocationsTable.orphaned.sum() AllocationsTable.orphaned.sum()
).select { ).where {
DirsTable.projectid eq projectId DirsTable.projectid eq projectId
}.first().let { row -> }.first().let { row ->
projectTree.stats = Statistics(row) projectTree.stats = Statistics(row)
@ -268,8 +268,8 @@ class SummaryModel(val db: Database) {
fun fileSummary(fileId: Int) = transaction(db) { fun fileSummary(fileId: Int) = transaction(db) {
var fileTree = FileTree() var fileTree = FileTree()
lineAllocationGroups lineAllocationGroups
.slice(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum(), AuthorsGroupsTable.authors) .select(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum(), AuthorsGroupsTable.authors)
.select { .where {
LinesTable.fileid eq fileId LinesTable.fileid eq fileId
}.groupBy(AuthorsGroupsTable.id).forEach { row -> }.groupBy(AuthorsGroupsTable.id).forEach { row ->
val authors = row[AuthorsGroupsTable.authors] val authors = row[AuthorsGroupsTable.authors]
@ -279,29 +279,29 @@ class SummaryModel(val db: Database) {
JoinType.LEFT, JoinType.LEFT,
LinesTable.fileid, FilesTable.id LinesTable.fileid, FilesTable.id
) )
.slice(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum()) .select(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum())
.select { LinesTable.fileid eq fileId } .where { LinesTable.fileid eq fileId }
.first().let { row -> .first().let { row ->
fileTree.stats = Statistics(row) fileTree.stats = Statistics(row)
} }
fileTree.name = FilesTable.select { FilesTable.id eq fileId }.map { it[FilesTable.fname] }.first() fileTree.name = FilesTable.selectAll().where { FilesTable.id eq fileId }.map { it[FilesTable.fname] }.first()
LinesTable.select { LinesTable.fileid eq fileId } LinesTable.selectAll().where { LinesTable.fileid eq fileId }
.map { it[LinesTable.id].value } .map { it[LinesTable.id].value }
.forEach { lineId -> .forEach { lineId ->
val lineDict = LineDict() val lineDict = LineDict()
lineAllocationGroups lineAllocationGroups
.slice(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum(), AuthorsGroupsTable.authors) .select(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum(), AuthorsGroupsTable.authors)
.select { .where {
LinesTable.id eq lineId LinesTable.id eq lineId
}.groupBy(AuthorsGroupsTable.id).forEach { lineRow -> }.groupBy(AuthorsGroupsTable.id).forEach { lineRow ->
lineDict.authorRisks[lineRow[AuthorsGroupsTable.authors]] = Statistics(lineRow) lineDict.authorRisks[lineRow[AuthorsGroupsTable.authors]] = Statistics(lineRow)
} }
lineAllocations lineAllocations
.slice(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum()) .select(AllocationsTable.knowledge.sum(), AllocationsTable.risk.sum(), AllocationsTable.orphaned.sum())
.select { .where {
LinesTable.id eq lineId LinesTable.id eq lineId
}.first().let { }.first().let {
lineDict.stats = Statistics(it) lineDict.stats = Statistics(it)
@ -312,7 +312,7 @@ class SummaryModel(val db: Database) {
} }
fun fileLines(fileId: Int): List<String> = transaction(db) { fun fileLines(fileId: Int): List<String> = transaction(db) {
LinesTable.select { LinesTable.selectAll().where {
LinesTable.fileid eq fileId LinesTable.fileid eq fileId
}.orderBy(LinesTable.linenum).map { }.orderBy(LinesTable.linenum).map {
it[LinesTable.line] it[LinesTable.line]
@ -338,7 +338,7 @@ class SummaryModel(val db: Database) {
val segs = mutableListOf<String>() val segs = mutableListOf<String>()
var newDirId = dirId var newDirId = dirId
while (newDirId != 0) { while (newDirId != 0) {
DirsTable.select { DirsTable.selectAll().where {
DirsTable.id eq newDirId DirsTable.id eq newDirId
}.forEach { }.forEach {
segs.add(it[DirsTable.dir]) segs.add(it[DirsTable.dir])
@ -362,7 +362,7 @@ class SummaryModel(val db: Database) {
private fun findOrCreateAuthorGroup(authors: List<String>): Int = transaction(db) { private fun findOrCreateAuthorGroup(authors: List<String>): Int = transaction(db) {
val authorsstr = authors.joinToString("\n") val authorsstr = authors.joinToString("\n")
var authorGroupId = AuthorsGroupsTable.select { var authorGroupId = AuthorsGroupsTable.selectAll().where {
AuthorsGroupsTable.authors eq authorsstr AuthorsGroupsTable.authors eq authorsstr
}.map { }.map {
it[AuthorsGroupsTable.id].value it[AuthorsGroupsTable.id].value
@ -387,7 +387,7 @@ class SummaryModel(val db: Database) {
AuthorsTable.insertIgnore { AuthorsTable.insertIgnore {
it[AuthorsTable.author] = author it[AuthorsTable.author] = author
} }
AuthorsTable.select { AuthorsTable.selectAll().where {
AuthorsTable.author eq author AuthorsTable.author eq author
}.map { }.map {
it[AuthorsTable.id].value it[AuthorsTable.id].value
@ -413,7 +413,7 @@ class SummaryModel(val db: Database) {
ProjectTable.insertIgnore { ProjectTable.insertIgnore {
it[ProjectTable.project] = project it[ProjectTable.project] = project
} }
ProjectTable.select { ProjectTable.selectAll().where {
ProjectTable.project eq project ProjectTable.project eq project
}.map { }.map {
it[ProjectTable.id].value it[ProjectTable.id].value
@ -426,7 +426,7 @@ class SummaryModel(val db: Database) {
it[parentdirid] = parentDirId it[parentdirid] = parentDirId
it[projectid] = projectId it[projectid] = projectId
} }
DirsTable.select { DirsTable.selectAll().where {
DirsTable.dir eq dirname DirsTable.dir eq dirname
DirsTable.parentdirid eq parentDirId DirsTable.parentdirid eq parentDirId
DirsTable.projectid eq projectId DirsTable.projectid eq projectId
@ -454,7 +454,7 @@ class SummaryModel(val db: Database) {
val dirs = mutableListOf<String>() val dirs = mutableListOf<String>()
var parentDirId = dirId var parentDirId = dirId
while (parentDirId != 0) { while (parentDirId != 0) {
DirsTable.select { DirsTable.selectAll().where {
DirsTable.id eq parentDirId DirsTable.id eq parentDirId
} }
.first() .first()