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

36.10. 埋め込みSQLプログラムの処理 #

<title>Processing Embedded SQL Programs</title>

Now that you have an idea how to form embedded SQL C programs, you probably want to know how to compile them. Before compiling you run the file through the embedded <acronym>SQL</acronym> <acronym>C</acronym> preprocessor, which converts the <acronym>SQL</acronym> statements you used to special function calls. After compiling, you must link with a special library that contains the needed functions. These functions fetch information from the arguments, perform the <acronym>SQL</acronym> command using the <application>libpq</application> interface, and put the result in the arguments specified for output. ここまでで、埋め込みSQL Cプログラムの作成方法は理解できたと思います。 ここからはそのコンパイル方法についてお話しします。 コンパイルの前に、そのファイルを埋め込みSQL Cプリプロセッサに通します。 これは、使用するSQL文を特別な関数呼び出しに変換します。 コンパイル後、必要な関数を持つ特別なライブラリとリンクしなければなりません。 これらの関数は引数から情報を取り出し、libpqを使用してそのSQLを実行し、出力用に指定された引数にその結果を格納します。

The preprocessor program is called <filename>ecpg</filename> and is included in a normal <productname>PostgreSQL</productname> installation. Embedded SQL programs are typically named with an extension <filename>.pgc</filename>. If you have a program file called <filename>prog1.pgc</filename>, you can preprocess it by simply calling: プリプロセッサプログラムはecpgという名前で、通常PostgreSQLのインストレーションに含まれています。 通常、埋め込みSQLプログラムの拡張子は.pgcとします。 prog1.pgcという名前のプログラムファイルがある場合、単純に以下を呼び出すことで前処理を行うことができます。

ecpg prog1.pgc

This will create a file called <filename>prog1.c</filename>. If your input files do not follow the suggested naming pattern, you can specify the output file explicitly using the <option>-o</option> option. これはprog1.cという名前のファイルを作成します。 入力ファイルがこの提案パターンに従った名前でない場合、-o オプションを使用して明示的に出力ファイルを指定することができます。

The preprocessed file can be compiled normally, for example: 前処理後のファイルは普通にコンパイルできます。 以下に例を示します。

cc -c prog1.c

The generated C source files include header files from the <productname>PostgreSQL</productname> installation, so if you installed <productname>PostgreSQL</productname> in a location that is not searched by default, you have to add an option such as <literal>-I/usr/local/pgsql/include</literal> to the compilation command line. 生成されたCソースファイルはPostgreSQLインストレーションに付随するヘッダファイルをインクルードします。 ですので、デフォルトで検索されない場所にPostgreSQLをインストールした場合は、コンパイル用のコマンドラインに-I/usr/local/pgsql/includeのようなオプションを追加しなければなりません。

To link an embedded SQL program, you need to include the <filename>libecpg</filename> library, like so: 埋め込みSQLプログラムをリンクするためには、以下のように、libecpgライブラリを含めなければなりません。

cc -o myprog prog1.o prog2.o ... -lecpg

Again, you might have to add an option like <literal>-L/usr/local/pgsql/lib</literal> to that command line. 繰り返しになりますが、コマンドラインに-L/usr/local/pgsql/libといったオプションを追加する必要があるかもしれません。

You can use <command>pg_config</command><indexterm><primary>pg_config</primary><secondary sortas="ecpg">with ecpg</secondary></indexterm> or <command>pkg-config</command><indexterm><primary>pkg-config</primary><secondary sortas="ecpg">with ecpg</secondary></indexterm> with package name <literal>libecpg</literal> to get the paths for your installation. インストール先のパスを取得するために、パッケージ名libecpgpg_configまたはpkg-configを使うことができます。

If you manage the build process of a larger project using <application>make</application>, it might be convenient to include the following implicit rule to your makefiles: 大規模プロジェクトの構築処理をmakeを使用して管理している場合、以下の暗黙規則をMakefileに含めておくと便利です。

ECPG = ecpg

%.c: %.pgc
        $(ECPG) $<

The complete syntax of the <command>ecpg</command> command is detailed in <xref linkend="app-ecpg"/>. ecpgコマンドの完全な構文はecpgに説明があります。

The <application>ecpg</application> library is thread-safe by default. However, you might need to use some threading command-line options to compile your client code. デフォルトではecpgはスレッドセーフです。 しかしクライアントコードのコンパイル時に他のスレッド関連のコマンドラインオプションを使用する必要があるかもしれません。