Postgresql 中文操作指南
36.17. Internals #
本节解释了 ECPG 的内部工作原理。此信息偶尔可帮助用户了解如何使用 ECPG。
This section explains how ECPG works internally. This information can occasionally be useful to help users understand how to use ECPG.
ecpg 写入输出的前四行是固定行。两行是注释,两行是与库连接的必要 include 行。然后,预处理器通读文件并写入输出。通常,它将所有内容回显至输出。
The first four lines written by ecpg to the output are fixed lines. Two are comments and two are include lines necessary to interface to the library. Then the preprocessor reads through the file and writes output. Normally it just echoes everything to the output.
当它看到 EXEC SQL 语句时,它会进行干预并更改它。命令以 EXEC SQL 开始,以 ; 结束。介于这两者之间的一切内容都将被视为 SQL 语句并解析以进行变量替换。
When it sees an EXEC SQL statement, it intervenes and changes it. The command starts with EXEC SQL and ends with ;. Everything in between is treated as an SQL statement and parsed for variable substitution.
当符号以冒号 (:) 开头时,便会进行变量替换。将在之前在 EXEC SQL DECLARE 节中声明的变量中查找具有该名称的变量。
Variable substitution occurs when a symbol starts with a colon (:). The variable with that name is looked up among the variables that were previously declared within a EXEC SQL DECLARE section.
库中最重要的功能是 ECPGdo,它负责执行大多数命令。它获取可变数量的参数。这可以轻松地累加至 50 个参数,并且我们希望这在任何平台上都不会成为问题。
The most important function in the library is ECPGdo, which takes care of executing most commands. It takes a variable number of arguments. This can easily add up to 50 or so arguments, and we hope this will not be a problem on any platform.
参数为:
The arguments are:
-
A line number #
-
This is the line number of the original line; used in error messages only.
-
-
A string #
-
This is the SQL command that is to be issued. It is modified by the input variables, i.e., the variables that where not known at compile time but are to be entered in the command. Where the variables should go the string contains ?.
-
-
Input variables #
-
Every input variable causes ten arguments to be created. (See below.)
-
-
ECPGt_EOIT #
-
An enum telling that there are no more input variables.
-
-
Output variables #
-
Every output variable causes ten arguments to be created. (See below.) These variables are filled by the function.
-
-
ECPGt_EORT #
-
An enum telling that there are no more variables.
-
对于 SQL 命令的一部分的每个变量,该函数都会获取十个参数:
For every variable that is part of the SQL command, the function gets ten arguments:
请注意,并非所有 SQL 命令都以这种方式处理。例如,以下打开游标语句:
Note that not all SQL commands are treated in this way. For instance, an open cursor statement like:
EXEC SQL OPEN cursor;
不会复制到输出。相反,游标的 DECLARE 命令用于 OPEN 命令的位置,因为它确实打开了游标。
is not copied to the output. Instead, the cursor’s DECLARE command is used at the position of the OPEN command because it indeed opens the cursor.
以下是一个完整示例,描述了文件 foo.pgc 的预处理器的输出(详细内容可能因预处理器的每个特定版本而异):
Here is a complete example describing the output of the preprocessor of a file foo.pgc (details might change with each particular version of the preprocessor):
EXEC SQL BEGIN DECLARE SECTION;
int index;
int result;
EXEC SQL END DECLARE SECTION;
...
EXEC SQL SELECT res INTO :result FROM mytable WHERE index = :index;
翻译成:
is translated into:
/* Processed by ecpg (2.6.0) */
/* These two include files are added by the preprocessor */
#include <ecpgtype.h>;
#include <ecpglib.h>;
/* exec sql begin declare section */
#line 1 "foo.pgc"
int index;
int result;
/* exec sql end declare section */
...
ECPGdo(__LINE__, NULL, "SELECT res FROM mytable WHERE index = ? ",
ECPGt_int,&(index),1L,1L,sizeof(int),
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
ECPGt_int,&(result),1L,1L,sizeof(int),
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
#line 147 "foo.pgc"
(此处的缩进是为了可读性而添加的,而不是预处理器执行的操作。)
(The indentation here is added for readability and not something the preprocessor does.)