バージョンごとのドキュメント一覧

36.9. プリプロセッサ指示子 #

<title>Preprocessor Directives</title>

Several preprocessor directives are available that modify how the <command>ecpg</command> preprocessor parses and processes a file. ecpgプリプロセッサがファイルを解析および処理する方法を変更することができる、プリプロセッサ指示子が複数あります。

36.9.1. ファイルのインクルード #

<title>Including Files</title>

To include an external file into your embedded SQL program, use: 埋め込みSQLプログラムに外部ファイルをインクルードするには、以下を使用します。

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

The embedded SQL preprocessor will look for a file named <literal><replaceable>filename</replaceable>.h</literal>, preprocess it, and include it in the resulting C output. Thus, embedded SQL statements in the included file are handled correctly. 埋め込みSQLプリプロセッサは、filename.hという名前のファイルを探し、その前処理を行い、最終的にC出力の中に含めます。 このようにして、ヘッダファイル内の埋め込みSQL文が正しく扱われます。

The <command>ecpg</command> preprocessor will search a file at several directories in following order: ecpgプリプロセッサは以下の順番で複数のディレクトリからファイルを検索します。

<listitem><simpara>current directory</simpara></listitem>
  • カレントディレクトリ
  • /usr/local/include
  • <listitem><simpara>PostgreSQL include directory, defined at build time (e.g., <filename>/usr/local/pgsql/include</filename>)</simpara></listitem>
  • ビルド時に設定されたPostgreSQLのインクルードディレクトリ (例えば、/usr/local/pgsql/include)
  • /usr/include

But when <literal>EXEC SQL INCLUDE "<replaceable>filename</replaceable>"</literal> is used, only the current directory is searched. しかしEXEC SQL INCLUDE "filename"が使われる場合、現在のディレクトリのみが検索されます。

In each directory, the preprocessor will first look for the file name as given, and if not found will append <literal>.h</literal> to the file name and try again (unless the specified file name already has that suffix). 各ディレクトリの中で、プリプロセッサはまず指定されたファイル名を探します。 見つからなければ(指定されたファイル名がこの接尾辞を持っていない限り)ファイル名に.hを付けて再検索します。

Note that <command>EXEC SQL INCLUDE</command> is <emphasis>not</emphasis> the same as: EXEC SQL INCLUDEは以下とは異なることに注意してください。

#include <filename.h>

because this file would not be subject to SQL command preprocessing. Naturally, you can continue to use the C <literal>#include</literal> directive to include other header files. このファイルにはSQLコマンド用前処理が行われないためです。 当然ながら、他のヘッダファイルをインクルードするCの#includeディレクティブを使用することができます。

注記

The include file name is case-sensitive, even though the rest of the <literal>EXEC SQL INCLUDE</literal> command follows the normal SQL case-sensitivity rules. 通常のSQLの大文字小文字の区別規則に従うEXEC SQL INCLUDEコマンドの一部であったとしても、インクルードファイルの名前は大文字小文字が区別されます。

36.9.2. defineおよびundef指示子 #

<title>The define and undef Directives</title>

Similar to the directive <literal>#define</literal> that is known from C, embedded SQL has a similar concept: Cで既知の#define指示子と同様、埋め込みSQLでも似たような概念を持ちます。

EXEC SQL DEFINE name;
EXEC SQL DEFINE name value;

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';

Use <literal>undef</literal> to remove a previous definition: 事前の定義を削除するにはundefを使用します。

EXEC SQL UNDEF MYNUMBER;

Of course you can continue to use the C versions <literal>#define</literal> and <literal>#undef</literal> in your embedded SQL program. The difference is where your defined values get evaluated. If you use <literal>EXEC SQL DEFINE</literal> then the <command>ecpg</command> preprocessor evaluates the defines and substitutes the values. For example if you write: 当然、Cの#define#undefを埋め込みSQLプログラムで使用することは可能です。 違いは宣言した値がどこで評価されるかです。 EXEC SQL DEFINEを使用する場合、ecpgプリプロセッサがその定義を評価し、その値を置換します。 例えば、

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

then <command>ecpg</command> will already do the substitution and your C compiler will never see any name or identifier <literal>MYNUMBER</literal>. Note that you cannot use <literal>#define</literal> 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. と記載した場合、ecpgによる置換がすでに行われていますので、CコンパイラではMYNUMBERという名前や識別子を参照することはありません。 埋め込みSQL問い合わせで使用する予定の定数に#defineを使用することはできませんので注意してください。 この場合、埋め込みSQLプリプロセッサがこの宣言を参照することができないためです。

36.9.3. ifdef、ifndef、elif、else、endif指示子 #

<title>ifdef, ifndef, elif, else, and endif Directives</title>

You can use the following directives to compile code sections conditionally: 以下の指示子を使用して、コンパイルするコード部分を選択することができます。

EXEC SQL ifdef name; #

Checks a <replaceable>name</replaceable> and processes subsequent lines if <replaceable>name</replaceable> has been defined via <literal>EXEC SQL define <replaceable>name</replaceable></literal>. nameを検査し、そのnameEXEC SQL define nameで定義されていた場合に後続の行を処理します。

EXEC SQL ifndef name; #

Checks a <replaceable>name</replaceable> and processes subsequent lines if <replaceable>name</replaceable> has <emphasis>not</emphasis> been defined via <literal>EXEC SQL define <replaceable>name</replaceable></literal>. nameを検査し、そのnameEXEC SQL define nameで定義されていない場合に後続の行を処理します。

EXEC SQL elif name; #

Begins an optional alternative section after an <literal>EXEC SQL ifdef <replaceable>name</replaceable></literal> or <literal>EXEC SQL ifndef <replaceable>name</replaceable></literal> directive. Any number of <literal>elif</literal> sections can appear. Lines following an <literal>elif</literal> will be processed if <replaceable>name</replaceable> has been defined <emphasis>and</emphasis> no previous section of the same <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal> construct has been processed. EXEC SQL ifdef nameまたはEXEC SQL ifndef name指示子の後で省略可能な代替セクションを開始します。 elifセクションはいくつでも現れることがあり得ます。 elifに続く行は、nameが定義されていて、かつ、同じifdef/ifndef...endif構文の前のセクションが処理されていない場合に、処理されます。

EXEC SQL else; #

Begins an optional, final alternative section after an <literal>EXEC SQL ifdef <replaceable>name</replaceable></literal> or <literal>EXEC SQL ifndef <replaceable>name</replaceable></literal> directive. Subsequent lines will be processed if no previous section of the same <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal> construct has been processed. EXEC SQL ifdef nameまたはEXEC SQL ifndef name指示子の後で最後の代替セクションを開始します。 同じifdef/ifndef...endif構文の前のセクションが処理されていない場合に、後続の行が処理されます。

EXEC SQL endif; #

Ends an <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal> construct. Subsequent lines are processed normally. ifdef/ifndef...endif構文を終了します。 後続の行は普通に処理されます。

<literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal> constructs can be nested, up to 127 levels deep. ifdef/ifndef...endif構文は127段階まで入れ子にできます。

This example will compile exactly one of the three <literal>SET TIMEZONE</literal> commands: この例は3つのSET TIMEZONEコマンドのうちちょうど1つをコンパイルします。

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 'GMT';
EXEC SQL endif;