【问题标题】:Groovy method return value is changedGroovy 方法返回值改变
【发布时间】:2019-02-17 05:52:42
【问题描述】:

以下 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

【问题讨论】:

    标签: groovy


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!完全忽略了关闭!
    猜你喜欢
    • 2012-11-20
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2012-01-20
    相关资源
    最近更新 更多