我想用Groovy和Freemarker生成代码(尤其是GFreeMarker) - 我想生成一个用于调用存储过程的Java包装器。所以我用这个语句查询ORACLE:
select PACKAGE_NAME, OBJECT_NAME, POSITION, ARGUMENT_NAME, DATA_TYPE, IN_OUT
from USER_ARGUMENTS
此数据被非规范化(每个参数在一行上)和分层:
- Package (PACKAGE_NAME)
- Function (OBJECT_NAME)
- Arguments (ARGUMENT_NAME, DATA_TYPE, IN_OUT)
我想将其存储到层次结构中,以将其提供给Freemarker以生成代码。这意味着对于每一行,我必须创建一个新的Package实例或选择一个现有的on,然后创建一个新的Function实例或选择一个现有的实例并创建一个新的参数。
这可以通过ObjectGraphBuilder完成,还是应该与地图和列表结合使用? 这是如何以“时髦的方式”完成的?谢谢你的帮助!
答案 0 :(得分:0)
Groovy与否,我是通过以下方式做到的,请提供更好的答案,我会接受它!
首先,我构建以下结构:
Map packages = new LinkedHashMap()
class Package {
String name
Map functions = [:]
}
class Function {
String name
String returnType
boolean isFunction = false
Map parameters = [:]
}
class Parameter {
String name
String dataType
String inOut
}
然后我用
阅读sql.eachRow("""\
select PACKAGE_NAME, OBJECT_NAME, POSITION, ARGUMENT_NAME, DATA_TYPE, IN_OUT
from USER_ARGUMENTS
order by PACKAGE_NAME, OBJECT_NAME, POSITION""")
{
// create or get package
def thePackage = packages[it.PACKAGE_NAME ?: '']
if (thePackage == null) {
thePackage = new Package(name : it.PACKAGE_NAME ?: '')
packages[it.PACKAGE_NAME ?: ''] = thePackage
}
// create or get function
def theFunction = thePackage.functions[it.OBJECT_NAME ?: '']
if (theFunction == null) {
theFunction = new Function(name: it.OBJECT_NAME ?: '')
thePackage.functions[it.OBJECT_NAME ?: ''] = theFunction
}
// Position 0 is the return value
if (it.POSITION == 0) {
theFunction.isFunction = true
theFunction.returnType = it.DATA_TYPE
}
else {
// create the argument
def theParameter = new Parameter(
name: it.ARGUMENT_NAME, dataType: it.DATA_TYPE, inOut: it.IN_OUT)
theFunction.parameters[it.ARGUMENT_NAME ?: ''] = theParameter
}
}