Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat(database): add log to dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 31, 2022
1 parent 92e845c commit bc21df2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@ package org.archguard.scanner.sourcecode.database

import chapi.domain.core.CodeDataStruct
import kotlinx.serialization.Serializable
import org.archguard.ident.mysql.MysqlIdentApp

@Serializable
data class MysqlLog(
data class SqlRecord(
val Package: String = "",
val ClassName: String = "",
val FunctionName: String = "",
val Tables: List<String> = listOf(),
val Sql: List<String> = listOf()
)

class MysqlAnalyser {
fun analysisByNode(node: CodeDataStruct, workspace: String): MutableList<MysqlLog> {
val logs: MutableList<MysqlLog> = mutableListOf()
fun analysisByNode(node: CodeDataStruct, workspace: String): MutableList<SqlRecord> {
val logs: MutableList<SqlRecord> = mutableListOf()
// by annotation: identify
val sqls: MutableList<String> = mutableListOf()
node.Functions.forEach { function ->
val tables: MutableList<String> = mutableListOf()

function.Annotations.forEach {
if(it.Name == "SqlQuery") {
val value = it.KeyValues[0].Value
sqls += sqlify(value)
val pureValue = sqlify(it.KeyValues[0].Value)
if (MysqlIdentApp.analysis(pureValue) != null) {
tables += MysqlIdentApp.analysis(pureValue)!!.tableNames
}
sqls += pureValue
}
}

function.FunctionCalls.forEach {
val callMethodName = it.FunctionName.split(".").last()
if (callMethodName == "createQuery") {
val paramValue = it.Parameters[0].TypeValue
sqls += sqlify(paramValue)
val pureValue = sqlify(it.Parameters[0].TypeValue)
if (MysqlIdentApp.analysis(pureValue) != null) {
tables += MysqlIdentApp.analysis(pureValue)!!.tableNames
}
sqls += pureValue
}
}

if(sqls.size > 0) {
logs += MysqlLog (
logs += SqlRecord (
Package = node.Package,
ClassName = node.NodeName,
FunctionName = function.Name,
Tables = tables,
Sql = sqls
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class MysqlAnalyserTest {
val nodes = KotlinAnalyserApp().analysisNodeByPath(path)
val mysqlAnalyser = MysqlAnalyser()

val logs: List<MysqlLog> = nodes.flatMap {
val logs: List<SqlRecord> = nodes.flatMap {
mysqlAnalyser.analysisByNode(it, "")
}

Expand All @@ -30,10 +30,12 @@ internal class MysqlAnalyserTest {
val nodes = KotlinAnalyserApp().analysisNodeByPath(path)
val mysqlAnalyser = MysqlAnalyser()

val logs: List<MysqlLog> = nodes.flatMap {
val sqlRecord: List<SqlRecord> = nodes.flatMap {
mysqlAnalyser.analysisByNode(it, "")
}

assertEquals(2, logs.size)
assertEquals(2, sqlRecord.size)
assertEquals("code_method,code_ref_method_callees", sqlRecord[0].Tables.joinToString(","))
assertEquals("code_ref_class_dependencies", sqlRecord[1].Tables.joinToString(","))
}
}

0 comments on commit bc21df2

Please sign in to comment.