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

36.13. C++アプリケーション #

<title><acronym>C++</acronym> Applications</title>

ECPG has some limited support for C++ applications. This section describes some caveats. ECPGはC++アプリケーションを多少の制限がありますがサポートします。 本節ではいくつかの注意を説明します。

The <command>ecpg</command> preprocessor takes an input file written in C (or something like C) and embedded SQL commands, converts the embedded SQL commands into C language chunks, and finally generates a <filename>.c</filename> file. The header file declarations of the library functions used by the C language chunks that <command>ecpg</command> generates are wrapped in <literal>extern "C" { ... }</literal> blocks when used under C++, so they should work seamlessly in C++. ecpgプリプロセッサはC(またはCのようなもの)と埋め込みSQLコマンドで記述された入力ファイルを取り、埋め込みSQLコマンドをC言語の小塊に変換し、最終的に.cファイルを作成します。 ecpgが生成するC言語の小塊で使用されるライブラリ関数のヘッダファイル定義は、C++で使用される場合extern "C" { ... }で囲まれます。 このためC++でも継ぎ目なく動作するはずです。

In general, however, the <command>ecpg</command> preprocessor only understands C; it does not handle the special syntax and reserved words of the C++ language. So, some embedded SQL code written in C++ application code that uses complicated features specific to C++ might fail to be preprocessed correctly or might not work as expected. しかし一般的には、ecpgプリプロセッサはCのみを理解しています。 C++言語の特殊な構文や予約語を取り扱いません。 このため、C++に特化した複雑な機能を使用するC++アプリケーションコードの中に記述された埋め込みSQLコードの一部は、正しく前処理することに失敗する、または想定通りに動作しないかもしれません。

A safe way to use the embedded SQL code in a C++ application is hiding the ECPG calls in a C module, which the C++ application code calls into to access the database, and linking that together with the rest of the C++ code. See <xref linkend="ecpg-cpp-and-c"/> about that. C++アプリケーションで埋め込みSQLコードを使用する安全な方法は、ECPGの呼び出しをCモジュール内に隠蔽し、残りのC++コードとまとめてリンクすることです。C++アプリケーションコードがデータベースにアクセスするためにはそのCモジュールを呼び出します。 36.13.2を参照してください。

36.13.1. ホスト変数のスコープ #

<title>Scope for Host Variables</title>

The <command>ecpg</command> preprocessor understands the scope of variables in C. In the C language, this is rather simple because the scopes of variables is based on their code blocks. In C++, however, the class member variables are referenced in a different code block from the declared position, so the <command>ecpg</command> preprocessor will not understand the scope of the class member variables. ecpgプリプロセッサはCにおける変数のスコープを理解しています。 C言語では、変数のスコープはコードブロックに基づきますので、どちらかといえば単純です。 しかしC++では クラスメンバ変数は宣言場所とは異なるコードブロック内で参照されます。 このためecpgプリプロセッサはクラスメンバ変数のスコープを理解していません。

For example, in the following case, the <command>ecpg</command> preprocessor cannot find any declaration for the variable <literal>dbname</literal> in the <literal>test</literal> method, so an error will occur. 例えば、以下の場合、ecpgプリプロセッサはtestメソッド内のdbname変数の定義を見つけることができません。 このためエラーになります。

class TestCpp
{
    EXEC SQL BEGIN DECLARE SECTION;
    char dbname[1024];
    EXEC SQL END DECLARE SECTION;

  public:
    TestCpp();
    void test();
    ~TestCpp();
};

TestCpp::TestCpp()
{
    EXEC SQL CONNECT TO testdb1;
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
}

void Test::test()
{
    EXEC SQL SELECT current_database() INTO :dbname;
    printf("current_database = %s\n", dbname);
}

TestCpp::~TestCpp()
{
    EXEC SQL DISCONNECT ALL;
}

This code will result in an error like this: このコードは以下のようなエラーになります。

ecpg test_cpp.pgc
test_cpp.pgc:28: ERROR: variable "dbname" is not declared

To avoid this scope issue, the <literal>test</literal> method could be modified to use a local variable as intermediate storage. But this approach is only a poor workaround, because it uglifies the code and reduces performance. このスコープ問題を回避するためには、testメソッドを中間格納領域としてローカル変数を使用するように変更することができます。 しかしこの手法は悪い回避策でしかありません。コードを醜くしますし性能も劣化させます。

void TestCpp::test()
{
    EXEC SQL BEGIN DECLARE SECTION;
    char tmp[1024];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT current_database() INTO :tmp;
    strlcpy(dbname, tmp, sizeof(tmp));

    printf("current_database = %s\n", dbname);
}

36.13.2. 外部のCモジュールを用いたC++アプリケーションの開発 #

<title>C++ Application Development with External C Module</title>

If you understand these technical limitations of the <command>ecpg</command> preprocessor in C++, you might come to the conclusion that linking C objects and C++ objects at the link stage to enable C++ applications to use ECPG features could be better than writing some embedded SQL commands in C++ code directly. This section describes a way to separate some embedded SQL commands from C++ application code with a simple example. In this example, the application is implemented in C++, while C and ECPG is used to connect to the PostgreSQL server. C++におけるecpgの技術的な制限を理解しているのであれば、ECPG機能を使用するC++アプリケーションを実現するためには、リンク段階でCオブジェクトとC++オブジェクトをリンクする方が、C++コード内で埋め込みSQLコマンドを直接記述することより優れているという結論に至るでしょう。 本節では、簡単な例を用いて、C++アプリケーションコードから埋め込みSQLコマンドを分離する方法について説明します。 この例では、アプリケーションはC++で実装し、PostgreSQLサーバに接続するためにCおよびECPGを使用します。

Three kinds of files have to be created: a C file (<filename>*.pgc</filename>), a header file, and a C++ file: Cファイル(*.pgc)、ヘッダファイル、C++ファイルという3種類のファイルを作成しなければなりません。

test_mod.pgc #

A sub-routine module to execute SQL commands embedded in C. It is going to be converted into <filename>test_mod.c</filename> by the preprocessor. C内に埋め込まれたサブルーチンモジュールです。 プリプロセッサによりtest_mod.cに変換されます。

#include "test_mod.h"
#include <stdio.h>

void
db_connect()
{
    EXEC SQL CONNECT TO testdb1;
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
}

void
db_test()
{
    EXEC SQL BEGIN DECLARE SECTION;
    char dbname[1024];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT current_database() INTO :dbname;
    printf("current_database = %s\n", dbname);
}

void
db_disconnect()
{
    EXEC SQL DISCONNECT ALL;
}

test_mod.h #

A header file with declarations of the functions in the C module (<filename>test_mod.pgc</filename>). It is included by <filename>test_cpp.cpp</filename>. This file has to have an <literal>extern "C"</literal> block around the declarations, because it will be linked from the C++ module. Cモジュール(test_mod.pgc)内の関数宣言を持つヘッダファイルです。 test_cpp.cppでインクルードされます。 このファイルは、C++モジュールからリンクされますので、宣言を囲むextern "C"ブロックを持たなければなりません。

#ifdef __cplusplus
extern "C" {
#endif

void db_connect();
void db_test();
void db_disconnect();

#ifdef __cplusplus
}
#endif

test_cpp.cpp #

The main code for the application, including the <function>main</function> routine, and in this example a C++ class. mainルーチンとこの例でのC++クラスを含む、アプリケーションの主要コードです。

#include "test_mod.h"

class TestCpp
{
  public:
    TestCpp();
    void test();
    ~TestCpp();
};

TestCpp::TestCpp()
{
    db_connect();
}

void
TestCpp::test()
{
    db_test();
}

TestCpp::~TestCpp()
{
    db_disconnect();
}

int
main(void)
{
    TestCpp *t = new TestCpp();

    t->test();
    return 0;
}

To build the application, proceed as follows. Convert <filename>test_mod.pgc</filename> into <filename>test_mod.c</filename> by running <command>ecpg</command>, and generate <filename>test_mod.o</filename> by compiling <filename>test_mod.c</filename> with the C compiler: アプリケーションを構築するためには、以下の処理を行います。 ecpgを実行してtest_mod.pgctest_mod.cに変換します。 そしてCコンパイラを用いてtest_mod.cをコンパイルしtest_mod.oを生成します。

ecpg -o test_mod.c test_mod.pgc
cc -c test_mod.c -o test_mod.o

Next, generate <filename>test_cpp.o</filename> by compiling <filename>test_cpp.cpp</filename> with the C++ compiler: 次にC++コンパイラを用いてtest_cpp.cppをコンパイルしtest_cpp.oを生成します。

c++ -c test_cpp.cpp -o test_cpp.o

Finally, link these object files, <filename>test_cpp.o</filename> and <filename>test_mod.o</filename>, into one executable, using the C++ compiler driver: 最後に、C++コンパイラドライバを用いてtest_cpp.oおよびtest_mod.oというオブジェクトファイルを実行形式ファイルにリンクします。

c++ test_cpp.o test_mod.o -lecpg -o test_cpp