]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-concept.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-concept.txt
1
2 34.1. The Concept #
3
4    An embedded SQL program consists of code written in an ordinary
5    programming language, in this case C, mixed with SQL commands in
6    specially marked sections. To build the program, the source code
7    (*.pgc) is first passed through the embedded SQL preprocessor, which
8    converts it to an ordinary C program (*.c), and afterwards it can be
9    processed by a C compiler. (For details about the compiling and linking
10    see Section 34.10.) Converted ECPG applications call functions in the
11    libpq library through the embedded SQL library (ecpglib), and
12    communicate with the PostgreSQL server using the normal
13    frontend-backend protocol.
14
15    Embedded SQL has advantages over other methods for handling SQL
16    commands from C code. First, it takes care of the tedious passing of
17    information to and from variables in your C program. Second, the SQL
18    code in the program is checked at build time for syntactical
19    correctness. Third, embedded SQL in C is specified in the SQL standard
20    and supported by many other SQL database systems. The PostgreSQL
21    implementation is designed to match this standard as much as possible,
22    and it is usually possible to port embedded SQL programs written for
23    other SQL databases to PostgreSQL with relative ease.
24
25    As already stated, programs written for the embedded SQL interface are
26    normal C programs with special code inserted to perform
27    database-related actions. This special code always has the form:
28 EXEC SQL ...;
29
30    These statements syntactically take the place of a C statement.
31    Depending on the particular statement, they can appear at the global
32    level or within a function.
33
34    Embedded SQL statements follow the case-sensitivity rules of normal SQL
35    code, and not those of C. Also they allow nested C-style comments as
36    per the SQL standard. The C part of the program, however, follows the C
37    standard of not accepting nested comments. Embedded SQL statements
38    likewise use SQL rules, not C rules, for parsing quoted strings and
39    identifiers. (See Section 4.1.2.1 and Section 4.1.1 respectively. Note
40    that ECPG assumes that standard_conforming_strings is on.) Of course,
41    the C part of the program follows C quoting rules.
42
43    The following sections explain all the embedded SQL statements.