[英]Has anyone ever discussed adding unquote syntax to PostgreSQL dollar-quoted strings?
Remember in LISP macros you could have those back-quotes to place evaluated expressions into larger string constants?还记得在 LISP 宏中,您可以使用那些反引号将求值表达式放入更大的字符串常量中吗?
Likewise, javascript has the `backtick quoting where you can place ${2 * 5} expressions`
in which get evaluated to form the final string (here: "backtick quoting where you can place 10 expressions").同样,javascript 具有
`backtick quoting where you can place ${2 * 5} expressions`
的反引号”,在其中进行计算以形成最终字符串(这里:“可以放置 10 个表达式的反引号”)。
In PostgreSQL we have the dollar-quoting which is useful, as is backtick quoting, to write string literals with newlines and all, usually used for encapsulated program text (as in CREATE FUNCTION, etc.)在 PostgreSQL 中,我们有美元引用,它和反引号一样有用,可以用换行符和所有内容编写字符串文字,通常用于封装的程序文本(如 CREATE FUNCTION 等)
It would be immensely useful if we could have some unquote syntax inside the dollar quoted strings.如果我们可以在美元引用的字符串中使用一些反引号语法,那将非常有用。 Such as, for instance marked with
\{ }
(which should be backwards compatible if we were to allow the opening $-quote to have an E preceding it to form an escape-string.例如,用
\{ }
标记(如果我们允许开头的 $-quote 在它前面有一个 E 以形成转义字符串,它应该是向后兼容的。
Then I could write:然后我可以写:
select E$$Lorem ipsum \{a * b} blah blah$$
from (select 2 a, 5 b) x
to get the string "Lorem ipsum 10 blah blah", and this would be extremely useful when we use the database to generate strings, including to generate executable SQL or PLpgsql bodies.获取字符串“Lorem ipsum 10 blah blah”,这在我们使用数据库生成字符串时非常有用,包括生成可执行文件 SQL 或 PLpgsql 主体。
I know this feature does not currently exist.我知道目前不存在此功能。 But what I don't know is if it was ever discussed or considered?
但我不知道是否曾经讨论或考虑过它? I could not find anything by web search.
我通过 web 搜索找不到任何东西。
That's pretty much what format() will do:这几乎就是 format() 将要做的事情:
select format($$Lorem ipsum %s blah blah$$, a * b)
from (select 2 a, 5 b) x
Especially if you use SQL to generate SQL, you should use format()
together with the placeholder for identifiers: %I
and string literals %L
to get them quoted properly.特别是如果您使用 SQL 生成 SQL,您应该将
format()
与标识符的占位符一起使用: %I
和字符串文字%L
以正确引用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.