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

36.1. 概念 #

<title>The Concept</title>

An embedded SQL program consists of code written in an ordinary programming language, in this case C, mixed with SQL commands in specially marked sections. To build the program, the source code (<filename>*.pgc</filename>) is first passed through the embedded SQL preprocessor, which converts it to an ordinary C program (<filename>*.c</filename>), and afterwards it can be processed by a C compiler. (For details about the compiling and linking see <xref linkend="ecpg-process"/>.) Converted ECPG applications call functions in the libpq library through the embedded SQL library (ecpglib), and communicate with the PostgreSQL server using the normal frontend-backend protocol. 埋め込みSQLプログラムは通常のプログラミング言語(ここではC)で記述されたコードで、特別にマークされたセクション内のSQLコマンドとともに使用されます。 このプログラムを構築するには、まずソースコード (*.pgc) を埋め込みSQLプリプロセッサに渡します。 ソースコードは、プリプロセッサによって通常のCプログラム (*.c) に変換され、その後Cコンパイラによって処理されます。 (コンパイルとリンクの詳細については 36.10 を参照してください。) 変換されたECPGアプリケーションは、libpqライブラリにある関数を埋め込みSQLライブラリ (ecpglib) を介して呼び出し、通常のフロントエンド・バックエンドプロトコルを使ってPostgreSQLサーバと通信します。

Embedded <acronym>SQL</acronym> has advantages over other methods for handling <acronym>SQL</acronym> commands from C code. First, it takes care of the tedious passing of information to and from variables in your <acronym>C</acronym> program. Second, the SQL code in the program is checked at build time for syntactical correctness. Third, embedded <acronym>SQL</acronym> in C is specified in the <acronym>SQL</acronym> standard and supported by many other <acronym>SQL</acronym> database systems. The <productname>PostgreSQL</productname> implementation is designed to match this standard as much as possible, and it is usually possible to port embedded <acronym>SQL</acronym> programs written for other SQL databases to <productname>PostgreSQL</productname> with relative ease. CコードからSQLコマンドを扱う場合は、埋め込みSQLの方が他の手法よりも有効です。 まず、埋め込みSQLはCプログラムの変数との面倒な双方間の情報移行を処理してくれます。 さらに、プログラム内のSQLコードは構築時に正確な構文になっているかどうか検査されます。 また、C言語での埋め込みSQLは標準SQLで既に定義されており、他の様々なSQLデータベースシステムでサポートされています。 PostgreSQLの実装は可能な限りこの標準に準拠するよう設計されています。 また通常の場合、他のSQLデータベース用に作成された埋め込みSQLプログラムを比較的簡単にPostgreSQLへ移植することができます。

As already stated, programs written for the embedded <acronym>SQL</acronym> interface are normal C programs with special code inserted to perform database-related actions. This special code always has the form: 先に述べた通り、埋め込みSQLインタフェース用のプログラムは、通常のCプログラムに、データベース関連処理を行うための特別なコードを加えたものです。 この特別なコードは、常に、次のような形式になっています。

EXEC SQL ...;

These statements syntactically take the place of a C statement. Depending on the particular statement, they can appear at the global level or within a function. このSQL文は、構文上でC言語の文の置き換えとなります。 SQL文によりますが、グローバルレベル、または関数内で記述することができます。

Embedded <acronym>SQL</acronym> statements follow the case-sensitivity rules of normal <acronym>SQL</acronym> code, and not those of C. Also they allow nested C-style comments as per the SQL standard. The C part of the program, however, follows the C standard of not accepting nested comments. Embedded <acronym>SQL</acronym> statements likewise use SQL rules, not C rules, for parsing quoted strings and identifiers. (See <xref linkend="sql-syntax-strings"/> and <xref linkend="sql-syntax-identifiers"/> respectively. Note that ECPG assumes that <varname>standard_conforming_strings</varname> is <literal>on</literal>.) Of course, the C part of the program follows C quoting rules. 埋め込みSQL文における大文字小文字の区別の有無は、Cコードではなく、通常のSQLコードの規則に従います。 また、SQL標準に従い入れ子のC形式のコメントを許します。 しかし、プログラムのCの部分では、入れ子のコメントを受け付けないというC標準に従います。 同様に、埋め込みSQL文は引用符付きの文字列や識別子の解析に、Cの規則ではなく、SQLの規則を使います。 (それぞれ4.1.2.14.1.1を参照してください。 ECPGはstandard_conforming_stringsonであることを仮定することに注意してください。) もちろん、プログラムのCの部分では、Cの引用の規則に従います。

The following sections explain all the embedded SQL statements. 以下の節で、すべての埋め込みSQL文について説明します。