![](/img/trans.png)
[英]How to get value of $1 parameter from executed prepared statement (inside a trigger using a current_query())
[英]clean way to detect if current_query() is a prepared statement?
有沒有人知道檢測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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.