]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-develop.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-develop.txt
1
2 34.17. Internals #
3
4    This section explains how ECPG works internally. This information can
5    occasionally be useful to help users understand how to use ECPG.
6
7    The first four lines written by ecpg to the output are fixed lines. Two
8    are comments and two are include lines necessary to interface to the
9    library. Then the preprocessor reads through the file and writes
10    output. Normally it just echoes everything to the output.
11
12    When it sees an EXEC SQL statement, it intervenes and changes it. The
13    command starts with EXEC SQL and ends with ;. Everything in between is
14    treated as an SQL statement and parsed for variable substitution.
15
16    Variable substitution occurs when a symbol starts with a colon (:). The
17    variable with that name is looked up among the variables that were
18    previously declared within a EXEC SQL DECLARE section.
19
20    The most important function in the library is ECPGdo, which takes care
21    of executing most commands. It takes a variable number of arguments.
22    This can easily add up to 50 or so arguments, and we hope this will not
23    be a problem on any platform.
24
25    The arguments are:
26
27    A line number #
28           This is the line number of the original line; used in error
29           messages only.
30
31    A string #
32           This is the SQL command that is to be issued. It is modified by
33           the input variables, i.e., the variables that where not known at
34           compile time but are to be entered in the command. Where the
35           variables should go the string contains ?.
36
37    Input variables #
38           Every input variable causes ten arguments to be created. (See
39           below.)
40
41    ECPGt_EOIT #
42           An enum telling that there are no more input variables.
43
44    Output variables #
45           Every output variable causes ten arguments to be created. (See
46           below.) These variables are filled by the function.
47
48    ECPGt_EORT #
49           An enum telling that there are no more variables.
50
51    For every variable that is part of the SQL command, the function gets
52    ten arguments:
53     1. The type as a special symbol.
54     2. A pointer to the value or a pointer to the pointer.
55     3. The size of the variable if it is a char or varchar.
56     4. The number of elements in the array (for array fetches).
57     5. The offset to the next element in the array (for array fetches).
58     6. The type of the indicator variable as a special symbol.
59     7. A pointer to the indicator variable.
60     8. 0
61     9. The number of elements in the indicator array (for array fetches).
62    10. The offset to the next element in the indicator array (for array
63        fetches).
64
65    Note that not all SQL commands are treated in this way. For instance,
66    an open cursor statement like:
67 EXEC SQL OPEN cursor;
68
69    is not copied to the output. Instead, the cursor's DECLARE command is
70    used at the position of the OPEN command because it indeed opens the
71    cursor.
72
73    Here is a complete example describing the output of the preprocessor of
74    a file foo.pgc (details might change with each particular version of
75    the preprocessor):
76 EXEC SQL BEGIN DECLARE SECTION;
77 int index;
78 int result;
79 EXEC SQL END DECLARE SECTION;
80 ...
81 EXEC SQL SELECT res INTO :result FROM mytable WHERE index = :index;
82
83    is translated into:
84 /* Processed by ecpg (2.6.0) */
85 /* These two include files are added by the preprocessor */
86 #include <ecpgtype.h>;
87 #include <ecpglib.h>;
88
89 /* exec sql begin declare section */
90
91 #line 1 "foo.pgc"
92
93  int index;
94  int result;
95 /* exec sql end declare section */
96 ...
97 ECPGdo(__LINE__, NULL, "SELECT res FROM mytable WHERE index = ?     ",
98         ECPGt_int,&(index),1L,1L,sizeof(int),
99         ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT,
100         ECPGt_int,&(result),1L,1L,sizeof(int),
101         ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
102 #line 147 "foo.pgc"
103
104
105    (The indentation here is added for readability and not something the
106    preprocessor does.)