Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
34.9. Preprocessor Directives
Prev UpChapter 34. ECPG - EmbeddedSQL in CHome Next

34.9. Preprocessor Directives

Several preprocessor directives are available that modify how theecpg preprocessor parses and processes a file.

34.9.1. Including Files

To include an external file into your embedded SQL program, use:

EXEC SQL INCLUDEfilename;EXEC SQL INCLUDE <filename>;EXEC SQL INCLUDE "filename";

The embedded SQL preprocessor will look for a file namedfilename.h, preprocess it, and include it in the resulting C output. Thus, embedded SQL statements in the included file are handled correctly.

Theecpg preprocessor will search a file at several directories in following order:

  • current directory
  • /usr/local/include
  • PostgreSQL include directory, defined at build time (e.g.,/usr/local/pgsql/include)
  • /usr/include

But whenEXEC SQL INCLUDE "filename" is used, only the current directory is searched.

In each directory, the preprocessor will first look for the file name as given, and if not found will append.h to the file name and try again (unless the specified file name already has that suffix).

Note thatEXEC SQL INCLUDE isnot the same as:

#include <filename.h>

because this file would not be subject to SQL command preprocessing. Naturally, you can continue to use the C#include directive to include other header files.

Note

The include file name is case-sensitive, even though the rest of theEXEC SQL INCLUDE command follows the normal SQL case-sensitivity rules.

34.9.2. The define and undef Directives

Similar to the directive#define that is known from C, embedded SQL has a similar concept:

EXEC SQL DEFINEname;EXEC SQL DEFINEnamevalue;

So you can define a name:

EXEC SQL DEFINE HAVE_FEATURE;

And you can also define constants:

EXEC SQL DEFINE MYNUMBER 12;EXEC SQL DEFINE MYSTRING 'abc';

Useundef to remove a previous definition:

EXEC SQL UNDEF MYNUMBER;

Of course you can continue to use the C versions#define and#undef in your embedded SQL program. The difference is where your defined values get evaluated. If you useEXEC SQL DEFINE then theecpg preprocessor evaluates the defines and substitutes the values. For example if you write:

EXEC SQL DEFINE MYNUMBER 12;...EXEC SQL UPDATE Tbl SET col = MYNUMBER;

thenecpg will already do the substitution and your C compiler will never see any name or identifierMYNUMBER. Note that you cannot use#define for a constant that you are going to use in an embedded SQL query because in this case the embedded SQL precompiler is not able to see this declaration.

34.9.3. ifdef, ifndef, else, elif, and endif Directives

You can use the following directives to compile code sections conditionally:

EXEC SQL ifdefname;

Checks aname and processes subsequent lines ifname has been created withEXEC SQL definename.

EXEC SQL ifndefname;

Checks aname and processes subsequent lines ifname hasnot been created withEXEC SQL definename.

EXEC SQL else;

Starts processing an alternative section to a section introduced by eitherEXEC SQL ifdefname orEXEC SQL ifndefname.

EXEC SQL elifname;

Checksname and starts an alternative section ifname has been created withEXEC SQL definename.

EXEC SQL endif;

Ends an alternative section.

Example:

EXEC SQL ifndef TZVAR;EXEC SQL SET TIMEZONE TO 'GMT';EXEC SQL elif TZNAME;EXEC SQL SET TIMEZONE TO TZNAME;EXEC SQL else;EXEC SQL SET TIMEZONE TO TZVAR;EXEC SQL endif;


Prev Up Next
34.8. Error Handling Home 34.10. Processing Embedded SQL Programs
epubpdf
Go to PostgreSQL 9.6
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp