diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-07-12 17:05:46 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-07-12 17:05:46 -0400 |
commit | 83f1c7b742e80d5aa15e6710ecb324e388d007b3 (patch) | |
tree | 3c85900d94348391eedd802b5aeb6d796bdc1b46 /doc/src | |
parent | e64cdab003027acef29e713087fb667e2319f679 (diff) |
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
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/ecpg.sgml | 39 |
1 files changed, 38 insertions, 1 deletions
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; <sect4> <title>Typedefs</title> + <indexterm> + <primary>typedef</primary> + <secondary>in ECPG</secondary> + </indexterm> <para> Use the <literal>typedef</literal> keyword to map new types to already @@ -1497,8 +1501,41 @@ EXEC SQL END DECLARE SECTION; <programlisting> EXEC SQL TYPE serial_t IS long; </programlisting> - 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. </para> + + <para> + Any word you declare as a typedef cannot be used as a SQL keyword + in <literal>EXEC SQL</literal> commands later in the same program. + For example, this won't work: +<programlisting> +EXEC SQL BEGIN DECLARE SECTION; + typedef int start; +EXEC SQL END DECLARE SECTION; +... +EXEC SQL START TRANSACTION; +</programlisting> + ECPG will report a syntax error for <literal>START + TRANSACTION</literal>, because it no longer + recognizes <literal>START</literal> 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 <link linkend="ecpg-dynamic">dynamic SQL</link>.) + </para> + + <note> + <para> + In <productname>PostgreSQL</productname> 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 <productname>PostgreSQL</productname> release with new + keywords. + </para> + </note> </sect4> <sect4> |