node.js - How to convert MySQL-style question mark `?` bound parameters to Postgres-style `$1` bound parameter -


i converting existing project mysql postgres. there quite few raw sql literals in code use ? placeholder, e.g.

  select     id       users       name = ? 

but error:

db query error: error: operator not exist: character varying = ? 

i don't want convert existing sql ? postgres-style operators $1.

is there way of having node-postgres accept question marks instead, or utility can convert postgres style params?

note sort of regex-based hack not acceptable because question marks can inside quotes, or backslash escaped depth.

is there way of having node-postgres accept question marks instead?

no. , there no direct correspondence between ? , $1 syntax, because latter implies parameter re-use, while ? doesn't allow it. example, using ? ? ? implies have 3 formatting parameters, while $1 $2 $2 implies have 2 formatting parameters.

or utility can convert postgres style params?

not likely, since there no direct correspondence, conversion possible one-way, make such utility useless. can replace yourself, single regular expression, replacing each ? $ + index + 1.

i don't want convert existing sql ? postgres-style operators $1.

you don't have choice in this. has done. besides, $1 way more flexible ?, due parameter re-use, plus optional extensions. example, pg-promise extends them nicely, various formatting modifiers needed frequently: ^, ~, :json, :csv, etc...

note sort of regex-based hack not acceptable because question marks can inside quotes, or backslash escaped depth.

you spend less time converting sql hand, time write utility one-way proper conversion.


Comments