ECPGPlus overview v15
EDB enhanced ECPG (the PostgreSQL precompiler) to create ECPGPlus. ECPGPlus is a Pro*C-compatible version of the PostgreSQL C precompiler. ECPGPlus translates a program that combines C code and embedded SQL statements into an equivalent C program. As it performs the translation, ECPGPlus verifies that the syntax of each SQL construct is correct.
The following diagram charts the path of a program containing embedded SQL statements as it's compiled into an executable:
Compilation of a program containing embedded SQL statements
To produce an executable from a C program that contains embedded SQL statements:
- Pass the program (
my_program.pgc
in the diagram) to the ECPGPlus precompiler. ECPGPlus translates each SQL statement inmy_program.pgc
into C code that calls theecpglib
API and produces a C program (my_program.c
). - Pass the C program to a C compiler. The C compiler generates an object file (
my_program.o
). - Pass the object file (
my_program.o
) as well as theecpglib
library file and any other required libraries to the linker, which in turn produces the executable (my_program
).
While the ECPGPlus preprocessor validates the syntax of each SQL statement, it can't validate the semantics. For example, the preprocessor confirms that an INSERT
statement is syntactically correct, but it can't confirm that the table mentioned in the INSERT
statement exists.
Behind the scenes
A client application contains a mix of C code and SQL code made up of the following elements:
- C preprocessor directives
- C declarations (variables, types, functions, ...)
- C definitions (variables, types, functions, ...)
- SQL preprocessor directives
- SQL statements
For example:
In this code fragment:
Line 1 specifies a directive to the C preprocessor.
C preprocessor directives can be interpreted or ignored. The option is controlled by a command line option (
-C PROC
) entered when you invoke ECPGPlus. In either case, ECPGPlus copies each C preprocessor directive to the output file (4) without change. Any C preprocessor directive found in the source file appears in the output file.Line 2 specifies a directive to the SQL preprocessor.
SQL preprocessor directives are interpreted by the ECPGPlus preprocessor and aren't copied to the output file.
Lines 4 through 6 contain C declarations.
C declarations are copied to the output file without change, except that each
VARCHAR
declaration is translated into an equivalentstruct
declaration.Lines 10 through 14 contain an embedded-SQL declaration section.
C variables that you refer to in SQL code are known as host variables. If you invoke the ECPGPlus preprocessor in Pro*C mode (
-C PROC
), you can refer to any C variable in a SQL statement. Otherwise you must declare each host variable in aBEGIN/END DECLARATION SECTION
pair.Lines 16 through 19 contain a SQL statement.
SQL statements are translated into calls to the ECPGPlus runtime library.
Lines 21 through 23 contain C code.
C code is copied to the output file without change.
Prefix any SQL statement with EXEC SQL
. The SQL statement extends to the next (unquoted) semicolon. For example:
When the preprocessor encounters this code fragment, it passes the C code (the first line and the last line) to the output file without translation and converts each EXEC SQL
statement into a call to an ecpglib
function. The result is similar to the following:
Installation and configuration
On Windows, ECPGPlus is installed by the EDB Postgres Advanced Server installation wizard as part of the Database Server component. On Linux, install with the edb-asxx-server-devel
RPM package, where xx
is the EDB Postgres Advanced Server version number. By default, the executable is located on Windows in:
On Linux, it's located in:
When invoking the ECPGPlus compiler, the executable must be in your search path (%PATH%
on Windows, $PATH
on Linux). For example, the following commands set the search path to include the directory that holds the ECPGPlus executable file ecpg
.
On Windows:
On Linux:
Constructing a makefile
A makefile contains a set of instructions that tell the make utility how to transform a program written in C that contains embedded SQL into a C program. To try the examples, you need:
- A C compiler (and linker)
- The make utility
- ECPGPlus preprocessor and library
- A makefile that contains instructions for ECPGPlus
The following code is an example of a makefile for the samples included in this documentation. To use the sample code, save it in a file named makefile
in the directory that contains the source code file.
The first two lines use the pg_config
program to locate the necessary header files and library directories:
The pg_config
program is shipped with EDB Postgres Advanced Server.
make knows to use the CFLAGS
variable when running the C compiler and LDFLAGS
and LDLIBS
when invoking the linker. ECPG programs must be linked against the ECPG runtime library (-lecpg
) and the libpq library (-lpq
).
The sample makefile tells make how to translate a .pgc
or a .pc
file into a C program. Two lines in the makefile specify the mode in which the source file is compiled. The first compile option is:
The first option tells make how to transform a file that ends in .pgc
(presumably, an ECPG source file) into a file that ends in .c
(a C program), using community ECPG, without the ECPGPlus enhancements. It invokes the ECPG precompiler with the -c
flag, which instructs the compiler to convert SQL code into C, using the value of the INCLUDES
variable and the name of the .pgc
file.
The second option tells make how to transform a file that ends in .pg
(an ECPG source file) into a file that ends in .c
(a C program) using the ECPGPlus extensions. It invokes the ECPG precompiler with the -c
flag, which instructs the compiler to convert SQL code to C. It also uses the -C PROC
flag, which instructs the compiler to use ECPGPlus in Pro*C-compatibility mode, using the value of the INCLUDES
variable and the name of the .pgc
file.
When you run make, pass the name of the ECPG source code file you want to compile. For example, to compile an ECPG source code file named customer_list.pgc
, use the command:
The make utility:
- Consults the makefile located in the current directory.
- Discovers that the makefile contains a rule that compiles
customer_list.pgc
into a C program (customer_list.c
). - Uses the rules built into
make
to compilecustomer_list.c
into an executable program.
ECPGPlus command line options
In the sample makefile, make includes the -C
option when invoking ECPGPlus to invoke ECPGPlus in Pro*C-compatible mode.
If you include the -C
PROC
keywords on the command line, in addition to the ECPG syntax, you can use Pro*C command line syntax. For example:
To display a complete list of the other ECPGPlus options available, in the ECPGPlus installation directory, enter:
The command line options are:
Option | Description |
---|---|
-c | Automatically generate C code from embedded SQL code. |
-C mode | Use the -C option to specify a compatibility mode:INFORMIX INFORMIX_SE PROC |
-D symbol | Define a preprocessor symbol. The -D keyword is not supported when compiling in PROC mode. Instead, use the Oracle-style ‘DEFINE=’ clause. |
-h | Parse a header file, this option includes option '-c' . |
-i | Parse system, include files as well. |
-I directory | Search directory for include files. |
-o outfile | Write the result to outfile. |
-r option | Specify runtime behavior; option can be:no_indicator - Don't use indicators, but instead use special values to represent NULL values.prepare - Prepare all statements before using them.questionmarks - Allow use of a question mark as a placeholder.usebulk - Enable bulk processing for INSERT , UPDATE , and DELETE statements that operate on host variable arrays. |
--regression | Run in regression testing mode. |
-t | Turn on autocommit of transactions. |
-l | Disable #line directives. |
--help | Display the help options. |
--version | Output version information. |
Note
If you don't specify an output file name when invoking ECPGPlus, the output file name is created by removing the .pgc
extension from the file name and appending .c
.