All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archguard.scanner.analyser.database.MysqlIdentApp.kt Maven / Gradle / Ivy

Go to download

ArchGuard is a architecture governance tool which can analysis architecture in container, component, code level, create architecture fitness functions, and anaysis system dependencies..

There is a newer version: 2.2.0
Show newest version
package org.archguard.scanner.analyser.database

import mu.KotlinLogging
import net.sf.jsqlparser.parser.CCJSqlParserUtil
import net.sf.jsqlparser.statement.Statement
import net.sf.jsqlparser.util.TablesNamesFinder

object MysqlIdentApp {
    private val UPDATE_SQL = "update\\s+([a-zA-Z_]+)".toRegex()
    private val SELECT_FROM_SQL = "select\\s+.*\\s+from\\s+([a-zA-Z_]+)".toRegex()
    private val logger = KotlinLogging.logger {}

    fun analysis(sql: String): SimpleDbStructure? {
        val table = SimpleDbStructure()

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

            table.tableNames = tablesNamesFinder.getTableList(statement).map { it ->
                it.removeSurrounding("`")
            }
        } catch (e: Exception) {
            // try used regex to match for CRUD by tables
            if (UPDATE_SQL.find(sql) != null) {
                val tableName = UPDATE_SQL.find(sql)!!.groups[1]!!.value
                table.tableNames = arrayListOf(tableName)
                return table
            }

            if (SELECT_FROM_SQL.find(sql) != null) {
                val tableName = SELECT_FROM_SQL.find(sql)!!.groups[1]!!.value
                table.tableNames = arrayListOf(tableName)
                return table
            }

            logger.info("analysis failure for sql: $sql")
            return null
        }

        return table
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy