postgres-flag transform\u null\u equals=on在jdbc特定情况下失败

xlpyo6sf  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(325)

我直接使用postgresjdbc驱动程序
该url只适用于一个sql,而不适用于另一个sql

String url = "jdbc:log4jdbc:postgresql://localhost:5432/staging_incomingdb?user=" +
            username + "&password=" + password +"&options=-c transform_null_equals=on";

请注意,transform_null_equals将所有c.col=null转换为c.col is null
然后我运行它,它工作。。

PreparedStatement st = conn.prepareStatement("select * from CUSTOMER_ORIGINAL_ADDRESS\n" +
                "customeror0_ left outer join CUSTOMER_PRACTICE practices1_ on customeror0_.id=practices1_.address_id\n" +
                "where customeror0_.onBehalfOfClientId=? and customeror0_.addr1=? and customeror0_.addr2=NULL");

        st.setString(1, "dean");
        st.setString(2, "200 Patewood Dr");

然后我运行这个不起作用的程序

PreparedStatement st = conn.prepareStatement("select * from CUSTOMER_ORIGINAL_ADDRESS\n" +
                "customeror0_ left outer join CUSTOMER_PRACTICE practices1_ on customeror0_.id=practices1_.address_id\n" +
                "where customeror0_.onBehalfOfClientId=? and customeror0_.addr1=? and customeror0_.addr2=?");

        st.setString(1, "dean");
        st.setString(2, "200 Patewood Dr");
        st.setString(3, null);

因此,当我将param设置为null时,它不会被转换。有什么办法解决这个问题吗?基本上,第二个查询在应该返回结果时返回0个结果。第一个查询返回由于设置而应该返回的结果。
我怀疑它与在将参数发送到postgres之前将查询发送到postgres有关。如果我是对的,这就引出了一个问题:有没有设置让postgres在收到所有参数之前不编译我的查询,因为我们总是将参数与查询一起发送?

l7mqbcuq

l7mqbcuq1#

我想你的期望是什么 transform_null_equals 这是错误的。
引用手册
请注意,此选项仅影响确切的形式 = NULL ,而不是其他比较运算符或其他在计算上等价于包含equals运算符的表达式的表达式。
(我的重点)
换句话说:它不会以这种方式处理产生null的表达式,只有在关键字 NULL 在sql字符串中显式使用。
我猜在这种情况下,准备语句的参数也被视为表达式。
在pl/pgsql中使用绑定变量时也会发生同样的情况:

execute 'select count(*) from some_table where some_column = $1' 
    into variable_one
    using some_parameter;

如果 some_parameter 为空,即使 transform_null_equals 打开并 some_column 包含空值。

ifmq2ha2

ifmq2ha22#

我怀疑,正如你所说, transform_null_equals 不适用于准备好的语句。
幸运的是,postgres支持 null -安全相等,带标准运算符 is [not] distinct from . 我建议更换:

and customeror0_.addr2 = ?

使用:

and customeror0_.addr2 is not distinct from ?

相关问题