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

Commit

Permalink
feat(mybatis): support for includes archguard/archguard#9
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 13, 2022
1 parent e39a705 commit 160fd19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.archguard.scanner.sourcecode.xml.mybatis

import org.apache.ibatis.builder.MapperBuilderAssistant
import org.apache.ibatis.builder.xml.XMLIncludeTransformer
import org.apache.ibatis.builder.xml.XMLMapperEntityResolver
import org.apache.ibatis.mapping.ResultSetType
import org.apache.ibatis.parsing.XNode
import org.apache.ibatis.parsing.XPathParser
import org.apache.ibatis.scripting.xmltags.*
import org.apache.ibatis.scripting.xmltags.XMLScriptBuilder
import org.apache.ibatis.session.Configuration
import org.archguard.scanner.sourcecode.xml.BasedXmlHandler
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -37,23 +39,43 @@ class MyBatisHandler : BasedXmlHandler() {
return streamToSqls(inputStream, filePath)
}

fun streamToSqls(inputStream: FileInputStream, filePath: String): MybatisEntry {
val config = Configuration()
config.defaultResultSetType = ResultSetType.SCROLL_INSENSITIVE
config.isShrinkWhitespacesInSql = true
fun streamToSqls(inputStream: FileInputStream, resource: String): MybatisEntry {
val configuration = Configuration()
configuration.defaultResultSetType = ResultSetType.SCROLL_INSENSITIVE
configuration.isShrinkWhitespacesInSql = true
//
// val builder = XMLMapperBuilder(inputStream, configuration, resource, configuration.sqlFragments)
// builder.parse()
// inputStream.close()

val parser = XPathParser(inputStream, true, config.variables, XMLMapperEntityResolver())
val parser = XPathParser(inputStream, true, configuration.variables, XMLMapperEntityResolver())
val context = parser.evalNode("/mapper")
val namespace = context.getStringAttribute("namespace")


// alias to configurationElement
val builderAssistant = MapperBuilderAssistant(configuration, resource)

// todo: add sql element
val sqlNodes = context.evalNodes("/mapper/sql")
sqlNodes.forEach {
var id = it.getStringAttribute("id")
id = builderAssistant.applyCurrentNamespace(id, false)
configuration.sqlFragments[id] = it
}

val list = context.evalNodes("select|insert|update|delete")

val entry = MybatisEntry(namespace)

list.forEach {
try {
val methodName = it.getStringAttribute("id")
val xmlScriptBuilder = XMLScriptBuilder(config, it)
// Include Fragments before parsing
val includeParser = XMLIncludeTransformer(configuration, builderAssistant)
includeParser.applyIncludes(it.node)

val xmlScriptBuilder = XMLScriptBuilder(configuration, it)
val sqlSource = xmlScriptBuilder.parseScriptNode()

// if is a foreach
Expand All @@ -62,7 +84,7 @@ class MyBatisHandler : BasedXmlHandler() {
val sqlString = sqlSource.getBoundSql(params).sql
entry.methodSqlMap[methodName] = sqlString
} catch (e: Exception) {
logger.info("process: $filePath error")
logger.info("process: $resource error")
logger.info(e.toString())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ internal class MyBatisHandlerTest {
val toURI = resource.toURI().toPath().toAbsolutePath()
val sqls = MyBatisHandler().streamToSqls(FileInputStream(toURI.toString()), "mybatis/PmsBrandMapper.xml")

assertEquals(4, sqls.methodSqlMap.size)
assertEquals(5, sqls.methodSqlMap.size)
}
}

0 comments on commit 160fd19

Please sign in to comment.