最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - Is there a way to forbid line breaks inside single and double-quoted strings? - Stack Overflow

programmeradmin2浏览0评论

I discovered today, to my horror, that Postgres accepts newline characters inside single- and double-quoted string literals:

postgres=# select 'a
postgres'# b';
┌──────────┐
│ ?column? │
├──────────┤
│ a       ↵│
│ b        │
└──────────┘
(1 row)

This lead to a really confusing syntax error today in a query with a typo:

  SELECT "updateFields"->>'enumTypeName"
  FROM input
  ...

I wish Postgres would report a syntax error at the end of the line after 'enumTypeName" instead of in a random location way down in the query...is there any way to turn off tolerating newlines inside non-$$-quoted string literals? It does more harm than good.

I discovered today, to my horror, that Postgres accepts newline characters inside single- and double-quoted string literals:

postgres=# select 'a
postgres'# b';
┌──────────┐
│ ?column? │
├──────────┤
│ a       ↵│
│ b        │
└──────────┘
(1 row)

This lead to a really confusing syntax error today in a query with a typo:

  SELECT "updateFields"->>'enumTypeName"
  FROM input
  ...

I wish Postgres would report a syntax error at the end of the line after 'enumTypeName" instead of in a random location way down in the query...is there any way to turn off tolerating newlines inside non-$$-quoted string literals? It does more harm than good.

Share Improve this question edited Feb 5 at 6:28 DarkBee 15.6k8 gold badges72 silver badges116 bronze badges asked Feb 4 at 22:05 AndyAndy 8,6925 gold badges59 silver badges68 bronze badges 4
  • 3 The answer is no. – Adrian Klaver Commented Feb 4 at 22:22
  • Why would that be an error? – Frank Heikens Commented Feb 4 at 23:27
  • Most programming languages I’ve worked with don’t allow newlines in string literals, so I didn’t anticipate that SQL is like this. It’s fine when you’re writing code in an IDE with syntax checking, but pretty painful when your dynamically generated query has a syntax error that’s hard to locate – Andy Commented Feb 6 at 15:54
  • If you’re very used to SQL you probably take it for granted. If you’re not it seems very questionable. I’ve been writing SQL for half a dozen years without realizing this – Andy Commented Feb 6 at 15:57
Add a comment  | 

1 Answer 1

Reset to default 2

There is not a possibility in standard Postgres. But you can write a own extension and assign it to post analyze hook, and proposed check can be implemented there.

I don't know any database, that disallow new line in the string.

发布评论

评论列表(0)

  1. 暂无评论