有谁知道检测 current_query()
的结果是否是准备好的语句的好方法?
我似乎不能简单地使用字符串函数,因为这将是准备好的语句的示例:
UPDATE table SET "x" = $1 WHERE "y" = $2 AND "z" = $3
但这不会:
UPDATE table SET "x" = '$1 + $2 = $3' WHERE "y"='$1' AND "z" = 1
是否有其他功能我可以与/一起使用而不是 current_query()
或者您有任何其他想法吗?
最佳答案
在剥离所有字符串的文本后,您可以通过查找 \$[[:digit:]]
来检测 current_query() 是否是准备好的语句。下面的查询可以,但是在复杂的引号嵌套的情况下它可能会失败:
with
queries(curr_query) as (
values ($$UPDATE table SET "x" = '$1||''a'' + $2 = $3' WHERE "y"='$1' AND "z" = 1$$),
($$UPDATE table SET "x" = $r1$a$r1$||$1 WHERE "y" = $2 AND "z" = $3||$r1$b$r1$ $$),
($$UPDATE table SET "x" = $1 WHERE "y" = $2 AND "z" = $3$$)
),
stripped as (
select *,
regexp_replace(
regexp_replace(
regexp_replace(curr_query, '(["'']).*?\1', '', 'g'),
'\$([[:alpha:]]*?)\$.*?\$\1\$', '', 'g'),
'\$([[:alpha:]][[:alnum:]]*?)\$.*?\$\1\$', '', 'g') as stripped_query
from queries
)
select *, stripped_query ~ '\$[[:digit:]]' AS is_prepared
from stripped
关于postgresql - 检测 current_query() 是否是准备好的语句的干净方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170886/