6.6. For the Developer

This section explain how ecpg works internally. It contains valuable information to help users understand how to use ecpg.

6.6.1. The Preprocessor

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.

When it sees an EXEC SQL statement, it intervenes and changes it. The EXEC SQL statement can be one of these:

Declare sections

Declare sections begin with:

exec sql begin declare section;

and end with:

exec sql end declare section;

In this section only variable declarations are allowed. Every variable declared within this section is stored in a list of variables indexed by name together with its corresponding type.

In particular the definition of a structure or union also must be listed inside a declare section. Otherwise ecpg cannot handle these types since it does not know the definition.

The declaration is also echoed to the file to make it a normal C variable.

The special types VARCHAR and VARCHAR2 are converted into a named struct for every variable. A declaration like:

VARCHAR var[180];

is converted into:

struct varchar_var { int len; char arr[180]; } var;

Include statements

An include statement looks like:

exec sql include filename;

Note that this is NOT the same as:

#include <filename.h>

Instead the file specified is parsed by ecpg so the contents of the file are included in the resulting C code. This way you are able to specify EXEC SQL commands in an include file.

Connect statement

A connect statement looks like:

exec sql connect to connection target;

It creates a connection to the specified database.

The connection target can be specified in the following ways:

There are also different ways to specify the user name:

Finally, the userid and password may be a constant text, a character variable, or a character string.

Disconnect statements

A disconnect statement looks like:

exec sql disconnect [connection target];
         

It closes the connection to the specified database.

The connection target can be specified in the following ways:

Open cursor statement

An open cursor statement looks like:

exec sql open cursor;

and is not copied to the output. Instead, the cursor's DECLARE command is used because it opens the cursor as well.

Commit statement

A commit statement looks like:

exec sql commit;

Rollback statement

A rollback statement looks like:

exec sql rollback;

Other statements

Other SQL statements are used by starting with exec sql and ending with ;. Everything in between is treated as an SQL statement and parsed for variable substitution.

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 declare section. Depending on whether the variable is being use for input or output, a pointer to the variable is output to allow access by the function.

For every variable that is part of the SQL query, the function gets other arguments:

6.6.2. A Complete Example

Here is a complete example describing the output of the preprocessor of a file foo.pgc:

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 in this manual is added for readability and not something the preprocessor does.)

6.6.3. The Library

The most important function in the library is ECPGdo. It takes a variable number of arguments. Hopefully there are no computers that limit the number of variables that can be accepted by a varargs() function. This can easily add up to 50 or so arguments.

The arguments are:

In the default mode, queries are committed only when exec sql commit is issued. Ecpg also supports auto-commit of transactions via the -t command-line option or via the exec sql set autocommit to on statement. In autocommit mode, each query is automatically committed unless it is inside an explicit transaction block. This mode can be explicitly turned off using exec sql set autocommit to off.