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 handle for exception
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 31, 2022
1 parent 49446b7 commit c5e34ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import net.sf.jsqlparser.util.TablesNamesFinder
import org.archguard.ident.mysql.model.SimpleRelation

class MysqlIdentApp {
fun analysis(sql: String): SimpleRelation {
fun analysis(sql: String): SimpleRelation? {
val table = SimpleRelation()

val statement: Statement = CCJSqlParserUtil.parse(sql)
val selectStatement: Select = statement as Select
val tablesNamesFinder = TablesNamesFinder()
try {
val statement: Statement = CCJSqlParserUtil.parse(sql)
val selectStatement: Select = statement as Select
val tablesNamesFinder = TablesNamesFinder()

table.tableNames = tablesNamesFinder.getTableList(selectStatement)
table.tableNames = tablesNamesFinder.getTableList(selectStatement)
} catch (e: Exception) {
println(e)
return null
}

return table
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ import org.junit.jupiter.api.Test

internal class MysqlIdentAppTest {
@Test
internal fun identForSqlQuery() {
internal fun should_ident_table_name_in_query_annotation() {
val relation =
MysqlIdentApp().analysis("select source_package as sourcePackage, source_class as sourceClass, source_method as sourceMethod, target_url as targetUrl, target_http_method as targetHttpMethod, system_id as systemId from container_demand where system_id = :systemId")

assertEquals("container_demand", relation.tableNames.joinToString(","))
assertEquals("container_demand", relation!!.tableNames.joinToString(","))
}

@Test
internal fun identForCreateQuery() {
internal fun should_ident_table_name_in_create_query() {
val relation =
MysqlIdentApp().analysis("select a.clzname aClz, b.clzname bClz from code_method a, code_method b, code_ref_method_callees mc where a.id = mc.a and b.id = mc.b and a.module='${'$'}module' and b.module='${'$'}module' and a.system_id='${'$'}systemId' and b.system_id='${'$'}systemId' and mc.system_id='${'$'}systemId'")

assertEquals("code_method,code_ref_method_callees", relation.tableNames.joinToString(","))
assertEquals("code_method,code_ref_method_callees", relation!!.tableNames.joinToString(","))
}

@Test
internal fun should_handle_exception() {
val code = "select id, system_name as systemName,\"+\" language from system_info where id in (<ids>)"
val relation = MysqlIdentApp().analysis(code)

assertEquals(null, relation)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package org.archguard.scanner.sourcecode.database

import chapi.domain.core.CodeDataStruct
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class MysqlLog(
Expand Down

0 comments on commit c5e34ba

Please sign in to comment.