postgresql - 检测 current_query() 是否是准备好的语句的干净方法?

标签 postgresql

有谁知道检测 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/

相关文章:

postgresql - postgres csv 日期空导入错误

performance - PostgreSQL:存在与左连接

sql - 如何在 PostgreSQL 上将所有列转换为正确的大小写/标题大小写?

c++ - 简单的 Postgresql libpq 代码太慢?

postgresql - 复制旧表 - 在 sql\plpgsql 中触发

sql - 如何根据列值选择不同百分比的数据?

Django 删除多对多字段

postgresql - 运行 postgresql 脚本时奇怪的从左到右的箭头

postgresql - psql -U someuser -W some_db 不尊重密码?

ruby-on-rails - 为什么在 postgres 上的 rails 迁移过程中出现 ExclusiveLock 错误