From 83f1c7b742e80d5aa15e6710ecb324e388d007b3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 12 Jul 2022 17:05:46 -0400 Subject: Fix ECPG's handling of type names that match SQL keywords. Previously, ECPG could only cope with variable declarations whose type names either weren't any SQL keyword, or were at least partially reserved. If you tried to use something in the unreserved_keyword category, you got a syntax error. This is pretty awful, not only because it says right on the tin that those words are not reserved, but because the set of such keywords tends to grow over time. Thus, an ECPG program that was just fine last year could fail when recompiled with a newer SQL grammar. We had to work around this recently when STRING became a keyword, but it's time for an actual fix instead of a band-aid. To fix, borrow a trick from C parsers and make the lexer's behavior change when it sees a word that is known as a typedef. This is not free of downsides: if you try to use such a name as a SQL keyword in EXEC SQL later in the program, it won't be recognized as a SQL keyword, leading to a syntax error there instead. So in a real sense this is just trading one hazard for another. But there is an important difference: with this, whether your ECPG program works depends only on what typedef names and SQL commands are used in the program text. If it compiles today it'll still compile next year, even if more words have become SQL keywords. Discussion: https://postgr.es/m/3661437.1653855582@sss.pgh.pa.us --- doc/src/sgml/ecpg.sgml | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index 7f8b4dd5c02..29333e37679 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -1483,6 +1483,10 @@ EXEC SQL END DECLARE SECTION; Typedefs + + typedef + in ECPG + Use the typedef keyword to map new types to already @@ -1497,8 +1501,41 @@ EXEC SQL END DECLARE SECTION; EXEC SQL TYPE serial_t IS long; - This declaration does not need to be part of a declare section. + This declaration does not need to be part of a declare section; + that is, you can also write typedefs as normal C statements. + + + Any word you declare as a typedef cannot be used as a SQL keyword + in EXEC SQL commands later in the same program. + For example, this won't work: + +EXEC SQL BEGIN DECLARE SECTION; + typedef int start; +EXEC SQL END DECLARE SECTION; +... +EXEC SQL START TRANSACTION; + + ECPG will report a syntax error for START + TRANSACTION, because it no longer + recognizes START as a SQL keyword, + only as a typedef. + (If you have such a conflict, and renaming the typedef + seems impractical, you could write the SQL command + using dynamic SQL.) + + + + + In PostgreSQL releases before v16, use + of SQL keywords as typedef names was likely to result in syntax + errors associated with use of the typedef itself, rather than use + of the name as a SQL keyword. The new behavior is less likely to + cause problems when an existing ECPG application is recompiled in + a new PostgreSQL release with new + keywords. + + -- cgit v1.2.3