On Fri, 2021-04-09 at 19:44 +0200, I wrote:
> > SQL-standard function body
> > 
> > psql needs some new intelligence to keep track of function body
> > boundaries so that it doesn't send off statements when it sees
> > semicolons that are inside a function body.
> 
> This causes psql to fail to recognize the semicolon in the following
> statement as the end of a statement:
> 
>   IMPORT FOREIGN SCHEMA x FROM SERVER y INTO z OPTIONS (case 'lower');
> 
> The cause is the "case", which is recognized as the start of a function
> body.
> 
> Would it be an option to recognize BEGIN and CASE as starting a
> function body only if they are *not* inside parentheses, like in
> the attached?

Here is an improved patch, which treats END in the same fashion
(not properly indented for readability).

Yours,
Laurenz Albe
From b0aaf040a42e3a036ba43c9891e07bad73c35a5e Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.a...@cybertec.at>
Date: Sat, 10 Apr 2021 03:35:39 +0200
Subject: [PATCH] Improve parsing of SQL standard functions in psql

Handle BEGIN, CASE and END as special only if we are not inside parentheses.
That allows statements like the following to be parsed correctly:

  ALTER FOREIGN TABLE x OPTIONS (ADD case 'upper');
---
 src/fe_utils/psqlscan.l | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l
index 4ec57e96a9..caf6412164 100644
--- a/src/fe_utils/psqlscan.l
+++ b/src/fe_utils/psqlscan.l
@@ -871,6 +871,8 @@ other			.
 
 {identifier}	{
 					cur_state->identifier_count++;
+					if (cur_state->paren_depth == 0)
+					{
 					if (pg_strcasecmp(yytext, "begin") == 0
 						|| pg_strcasecmp(yytext, "case") == 0)
 					{
@@ -882,6 +884,7 @@ other			.
 						if (cur_state->begin_depth > 0)
 							cur_state->begin_depth--;
 					}
+					}
 					ECHO;
 				}
 
-- 
2.26.3

Reply via email to