34.1. The Concept#
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 seeSection 34.10.) Converted ECPG applications call functions in the libpq library through the embedded SQL library (ecpglib), and communicate with the Postgres Pro server using the normal frontend-backend protocol.
EmbeddedSQL has advantages over other methods for handlingSQL commands from C code. First, it takes care of the tedious passing of information to and from variables in yourC program. Second, the SQL code in the program is checked at build time for syntactical correctness. Third, embeddedSQL in C is specified in theSQL standard and supported by many otherSQL database systems. ThePostgres Pro implementation is designed to match this standard as much as possible, and it is usually possible to port embeddedSQL programs written for other SQL databases toPostgres Pro with relative ease. As already stated, programs written for the embeddedSQL interface are normal C programs with special code inserted to perform database-related actions. This special code always has the form: 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. EmbeddedSQL statements follow the case-sensitivity rules of normalSQL 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. EmbeddedSQL statements likewise use SQL rules, not C rules, for parsing quoted strings and identifiers. (SeeSection 4.1.2.1 andSection 4.1.1 respectively. Note that ECPG assumes that The following sections explain all the embedded SQL statements.EXEC SQL ...;
standard_conforming_strings
ison
.) Of course, the C part of the program follows C quoting rules.