From 5f28b21eb3c5c2fb72c24608bc686acd7c9b113c Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 3 Aug 2020 09:46:12 -0400 Subject: Fix behavior of ecpg's "EXEC SQL elif name". This ought to work much like C's "#elif defined(name)"; but the code implemented it in a way equivalent to endif followed by ifdef, so that it didn't matter whether any previous branch of the IF construct had succeeded. Fix that; add some test cases covering elif and nested IFs; and improve the documentation, which also seemed a bit confused. AFAICS the code has been like this since the feature was added in 1999 (commit b57b0e044). So while it's surely wrong, there might be code out there relying on the current behavior. Hence, don't back-patch into stable branches. It seems all right to fix it in v13 though. Per report from Ashutosh Sharma. Reviewed by Ashutosh Sharma and Michael Meskes. Discussion: https://postgr.es/m/CAE9k0P=dQk9X0cU2tN49S7a9tv733-e1pVdpB1P-pWJ5PdTktg@mail.gmail.com --- doc/src/sgml/ecpg.sgml | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index 106ae0984e8..1f9d35eeb3e 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -5695,7 +5695,7 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER; - ifdef, ifndef, else, elif, and endif Directives + ifdef, ifndef, elif, else, and endif Directives You can use the following directives to compile code sections conditionally: @@ -5705,7 +5705,7 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER; Checks a name and processes subsequent lines if - name has been created with EXEC SQL define + name has been defined via EXEC SQL define name. @@ -5716,30 +5716,40 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER; Checks a name and processes subsequent lines if - name has not been created with + name has not been defined via EXEC SQL define name. - EXEC SQL else; + EXEC SQL elif name; - Starts processing an alternative section to a section introduced by - either EXEC SQL ifdef name or - EXEC SQL ifndef name. + Begins an optional alternative section after an + EXEC SQL ifdef name or + EXEC SQL ifndef name + directive. Any number of elif sections can appear. + Lines following an elif will be processed + if name has been + defined and no previous section of the same + ifdef/ifndef...endif + construct has been processed. - EXEC SQL elif name; + EXEC SQL else; - Checks name and starts an alternative section if - name has been created with EXEC SQL define - name. + Begins an optional, final alternative section after an + EXEC SQL ifdef name or + EXEC SQL ifndef name + directive. Subsequent lines will be processed if no previous section + of the same + ifdef/ifndef...endif + construct has been processed. @@ -5748,7 +5758,9 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER; EXEC SQL endif; - Ends an alternative section. + Ends an + ifdef/ifndef...endif + construct. Subsequent lines are processed normally. @@ -5756,14 +5768,20 @@ EXEC SQL UPDATE Tbl SET col = MYNUMBER; - Example: + ifdef/ifndef...endif + constructs can be nested, up to 127 levels deep. + + + + This example will compile exactly one of the three SET + TIMEZONE commands: -EXEC SQL ifndef TZVAR; -EXEC SQL SET TIMEZONE TO 'GMT'; +EXEC SQL ifdef TZVAR; +EXEC SQL SET TIMEZONE TO TZVAR; EXEC SQL elif TZNAME; EXEC SQL SET TIMEZONE TO TZNAME; EXEC SQL else; -EXEC SQL SET TIMEZONE TO TZVAR; +EXEC SQL SET TIMEZONE TO 'GMT'; EXEC SQL endif; -- cgit v1.2.3