db icon indicating copy to clipboard operation
db copied to clipboard

Does the upper.io/db.v3/lib/sqlbuilder support Postgres's "$1" placeholder?

Open uynap opened this issue 7 years ago • 2 comments
trafficstars

Would someone be able to direct me to the documents or give me a definite answer for this question? I would like to build the exact SQL below:

Select name, host, version from a_table
Where name like $1 OR host like $2
Order by id asc
Limit $3 offset $4

Thanks in advance.

uynap avatar Jul 06 '18 13:07 uynap

Hi @uynap, this should not be a problem, you can use $n or a list of ? placeholders too.

xiam avatar Jul 07 '18 12:07 xiam

Update:

Fortunately I found a solution which avoid using the ? operator: http://www.booneputney.com/development/postgresql-jsonb-question-mark-operator-alternative/

Where("(jsonb_col->'authors'->?) IS NOT NULL", "johndoe")

The above line did the trick.

==================

Original post:

Is it possible to customize the placeholder other than "?"? Postgresql supports JOSNB type and "?" has been defined as an operator:

select jsonb_col->'authors' ? 'johndoe' from posts;

Here "authors" is a map from user name to other properties, and "?" is the operator to test if 'johndoe' is the key in "authors".

I tried Where("jsonb_col->'authors' ? $1", "johndoe"), but got the following error: pq: syntax error at or near "$1"

Thanks.

linuxerwang avatar Feb 28 '19 01:02 linuxerwang