如何将Postgres函数参数传递给JSON查询?

时间:2017-01-30 17:03:17

标签: json postgresql

@Bean
OracleAqQueueFactoryBean etlQueueFactory() throws JMSException, SQLException {
    final OracleAqQueueFactoryBean bean = new OracleAqQueueFactoryBean();
    bean.setConnectionFactory(jmsQueueConnectionFactory());
    bean.setOracleQueueUser("USER");
    bean.setOracleQueueName("QUEUE");
    return bean;
}


@Bean
DefaultMessageListenerContainer jmsContainer() throws JMSException, SQLException {
    final DefaultMessageListenerContainer bean = new DefaultMessageListenerContainer();
    bean.setConnectionFactory(jmsQueueConnectionFactory());
    bean.setDestination(etlQueueFactory().getObject());
    bean.setMessageListener(new MyListener());
    bean.setSessionTransacted(false);
    return bean;
}

public class MyListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        ...
    }
}

我无法将我的函数参数插入到这样的查询中。

2 个答案:

答案 0 :(得分:2)

在你的陈述中,甚至引号都不匹配......

此外,它容易受到SQL injection的攻击。<​​/ p>

尝试这样的事情:

CREATE OR REPLACE FUNCTION dynamicJsonValue(varchar(64)) RETURNS void AS
$$UPDATE "table" SET "field" = 'value'
   WHERE "json_field"
         @> CAST ('{"key": "' || replace($1, '"', '') || '" }' AS jsonb)$$
LANGUAGE sql STRICT;

答案 1 :(得分:2)

想出来:

CREATE OR REPLACE FUNCTION dynamicJsonValue(varchar(64)) RETURNS VOID AS
'UPDATE "table" SET "field" = ''value''
 WHERE "json_field" @> jsonb_build_object(''field'', $1);'
 LANGUAGE SQL VOLATILE;