简体   繁体   中英

Call postgres function with array param in jooq

How can I pass array param in postgreSQL function in native SQL with jooq?

The name of function compiled dynamically and I want to call it in string:

dsl().execute(someFunction + "(?)", Arrays.asList(1, 2, 3));

Functions take argument with type integer[]

I've found ugly solution:

dsl().execute(someFunction + "(string_to_array(?, ',')::INT[])", Joiner.on(",").join(1, 2, 3));

I am using jooq version 3.6.4

Don't use string concatenation

Both of your attempts use string concatenation to produce the function call: someFunction + "(?)" . While you're probably in full control of these strings, there's always a slight risk of adding unnecessary:

  • Syntax errors
  • SQL injection vulnerabilities

Better use jOOQ's built-in templating mechanism:

dsl().execute("{0}({1})", DSL.name("someFunction"), bindValue);

The templating mechanism is documented here: http://www.jooq.org/doc/latest/manual/sql-building/queryparts/plain-sql-queryparts

Pass array bind variables

It's easy. Just pass the bind variable as an array (not a list) in Java. For instance:

dsl().execute("someFunction({0})", DSL.val(new Integer[] { 1, 2, 3 }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM