Postgresql 中文操作指南

36.1. The Concept #

嵌入式 SQL 程序由以普通编程语言(本例中为 C)编写的代码组成,与在专门标记的节中用 SQL 命令混合。为了构建程序,首先通过嵌入式 SQL 预处理器传递源代码 (*.pgc),将其转换为一个普通的 C 程序 (*.c),之后它可以由 C 编译器处理。(有关编译和链接的详细信息,请参阅 Section 36.10)。转换的 ECPG 应用程序通过嵌入式 SQL 库 (ecpglib) 调用 libpq 库中的函数,并使用正常的客户端-服务端协议与 PostgreSQL 服务端进行通信。

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 (*.pgc) is first passed through the embedded SQL preprocessor, which converts it to an ordinary C program (*.c), and afterwards it can be processed by a C compiler. (For details about the compiling and linking see Section 36.10.) 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.

对于从 C 代码处理 SQL 命令,嵌入式 SQL 优于其他方法。首先,它负责将信息与 C 程序中的变量进行传递的繁琐工作。其次,程序中的 SQL 代码在构建时会检查语法正确性。第三,C 语言中的嵌入式 SQL 在 SQL 标准中指定,并且受许多其他 SQL 数据库系统支持。PostgreSQL 实现旨在尽可能匹配此标准,并且通常可以相对轻松地将为其他 SQL 数据库编写的嵌入式 SQL 程序移植到 PostgreSQL。

Embedded SQL has advantages over other methods for handling SQL commands from C code. First, it takes care of the tedious passing of information to and from variables in your C program. Second, the SQL code in the program is checked at build time for syntactical correctness. Third, embedded SQL in C is specified in the SQL standard and supported by many other SQL database systems. The PostgreSQL implementation is designed to match this standard as much as possible, and it is usually possible to port embedded SQL programs written for other SQL databases to PostgreSQL with relative ease.

如前所述,为嵌入式 SQL 接口编写的程序是带有特殊代码的普通 C 程序,用于执行与数据库相关的操作。此特殊代码始终具有以下形式:

As already stated, programs written for the embedded SQL interface are normal C programs with special code inserted to perform database-related actions. This special code always has the form:

EXEC SQL ...;

这些语句的语法遵循 C 语句。根据特定语句,它们可以出现在全局级别或函数中。

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 语句遵循正常 SQL 代码的大小写敏感规则,而不是 C 代码的大小写敏感规则。按照 SQL 标准,它们还允许嵌套的 C 风格注释。然而,程序的 C 部分按照 C 标准不接受嵌套注释。同样,嵌入式 SQL 语句使用 SQL 规则而不是 C 规则来解析带引号的字符串和标识符。 (分别见 Section 4.1.2.1Section 4.1.1。请注意,ECPG 假定 standard_conforming_stringson)。当然,程序的 C 部分遵循 C 引用规则。

Embedded SQL statements follow the case-sensitivity rules of normal SQL 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 SQL statements likewise use SQL rules, not C rules, for parsing quoted strings and identifiers. (See Section 4.1.2.1 and Section 4.1.1 respectively. Note that ECPG assumes that standard_conforming_strings is on.) Of course, the C part of the program follows C quoting rules.

以下部分将说明所有嵌入式 SQL 语句。

The following sections explain all the embedded SQL statements.