Groovy方法的返回值已更改

时间:2019-02-17 05:52:42

标签: groovy

以下Groovy方法返回false(即使两个查询结果均为0)。我一无所知:(

boolean checkObjects() {
    Sql.withInstance("jdbc:oracle:thin:@//${db_host}:${db_port}/${db_servicename}", "${db_username}", "${db_password}") { sql ->
        result1 = sql.firstRow('select count(status) as count from all_objects where status=\'INVALID\'')
        result2 = sql.firstRow('select count(status) as count from user_objects where status=\'INVALID\'')

        boolean output = ("${result1.count}".toString() == "0") && ("${result2.count}".toString() == "0")
        println output // prints true
        return output
    }
}

println checkObjects() // prints false

1 个答案:

答案 0 :(得分:2)

在groovy中使用闭包时,这是一个常见错误: 输出是闭包的返回值,但是函数的返回值是withInstance

尝试在闭包外部声明输出,并将其作为函数的结果返回