2 34.9. Preprocessor Directives #
4 34.9.1. Including Files
5 34.9.2. The define and undef Directives
6 34.9.3. ifdef, ifndef, elif, else, and endif Directives
8 Several preprocessor directives are available that modify how the ecpg
9 preprocessor parses and processes a file.
11 34.9.1. Including Files #
13 To include an external file into your embedded SQL program, use:
14 EXEC SQL INCLUDE filename;
15 EXEC SQL INCLUDE <filename>;
16 EXEC SQL INCLUDE "filename";
18 The embedded SQL preprocessor will look for a file named filename.h,
19 preprocess it, and include it in the resulting C output. Thus, embedded
20 SQL statements in the included file are handled correctly.
22 The ecpg preprocessor will search a file at several directories in
26 * PostgreSQL include directory, defined at build time (e.g.,
27 /usr/local/pgsql/include)
30 But when EXEC SQL INCLUDE "filename" is used, only the current
31 directory is searched.
33 In each directory, the preprocessor will first look for the file name
34 as given, and if not found will append .h to the file name and try
35 again (unless the specified file name already has that suffix).
37 Note that EXEC SQL INCLUDE is not the same as:
40 because this file would not be subject to SQL command preprocessing.
41 Naturally, you can continue to use the C #include directive to include
46 The include file name is case-sensitive, even though the rest of the
47 EXEC SQL INCLUDE command follows the normal SQL case-sensitivity rules.
49 34.9.2. The define and undef Directives #
51 Similar to the directive #define that is known from C, embedded SQL has
54 EXEC SQL DEFINE name value;
56 So you can define a name:
57 EXEC SQL DEFINE HAVE_FEATURE;
59 And you can also define constants:
60 EXEC SQL DEFINE MYNUMBER 12;
61 EXEC SQL DEFINE MYSTRING 'abc';
63 Use undef to remove a previous definition:
64 EXEC SQL UNDEF MYNUMBER;
66 Of course you can continue to use the C versions #define and #undef in
67 your embedded SQL program. The difference is where your defined values
68 get evaluated. If you use EXEC SQL DEFINE then the ecpg preprocessor
69 evaluates the defines and substitutes the values. For example if you
71 EXEC SQL DEFINE MYNUMBER 12;
73 EXEC SQL UPDATE Tbl SET col = MYNUMBER;
75 then ecpg will already do the substitution and your C compiler will
76 never see any name or identifier MYNUMBER. Note that you cannot use
77 #define for a constant that you are going to use in an embedded SQL
78 query because in this case the embedded SQL precompiler is not able to
81 If multiple input files are named on the ecpg preprocessor's command
82 line, the effects of EXEC SQL DEFINE and EXEC SQL UNDEF do not carry
83 across files: each file starts with only the symbols defined by -D
84 switches on the command line.
86 34.9.3. ifdef, ifndef, elif, else, and endif Directives #
88 You can use the following directives to compile code sections
91 EXEC SQL ifdef name; #
92 Checks a name and processes subsequent lines if name has been
93 defined via EXEC SQL define name.
95 EXEC SQL ifndef name; #
96 Checks a name and processes subsequent lines if name has not
97 been defined via EXEC SQL define name.
100 Begins an optional alternative section after an EXEC SQL ifdef
101 name or EXEC SQL ifndef name directive. Any number of elif
102 sections can appear. Lines following an elif will be processed
103 if name has been defined and no previous section of the same
104 ifdef/ifndef...endif construct has been processed.
107 Begins an optional, final alternative section after an EXEC SQL
108 ifdef name or EXEC SQL ifndef name directive. Subsequent lines
109 will be processed if no previous section of the same
110 ifdef/ifndef...endif construct has been processed.
113 Ends an ifdef/ifndef...endif construct. Subsequent lines are
116 ifdef/ifndef...endif constructs can be nested, up to 127 levels deep.
118 This example will compile exactly one of the three SET TIMEZONE
120 EXEC SQL ifdef TZVAR;
121 EXEC SQL SET TIMEZONE TO TZVAR;
122 EXEC SQL elif TZNAME;
123 EXEC SQL SET TIMEZONE TO TZNAME;
125 EXEC SQL SET TIMEZONE TO 'GMT';