This section describes how to use ecpg.
The preprocessor is called ecpg. After installation it resides in the PostgreSQL bin/ directory.
The ecpg library is called
libecpg.a or libecpg.so. Additionally, the library uses the
libpq library for communication to
the PostgreSQL server. You
will have to link your program using -lecpg -lpq
.
The library has some methods that are "hidden" but may prove useful.
ECPGdebug(int on, FILE *stream)
turns on debug logging
if called with the first argument non-zero. Debug logging
is done on stream. Most
SQL statement log their
arguments and results.
The most important function , ECPGdo
, logs all SQL statements with both the expanded
string, i.e. the string with all the input variables
inserted, and the result from the PostgreSQL server. This can be very
useful when searching for errors in your SQL statements.
ECPGstatus()
This method
returns TRUE if we are connected to a database and FALSE if
not.
To detect errors from the PostgreSQL server, include a line like:
exec sql include sqlca;
in the include section of your file. This will define a
struct and a variable with the name sqlca
as follows:
struct sqlca { char sqlcaid[8]; long sqlabc; long sqlcode; struct { int sqlerrml; char sqlerrmc[70]; } sqlerrm; char sqlerrp[8]; long sqlerrd[6]; /* 0: empty */ /* 1: OID of processed tuple if applicable */ /* 2: number of rows processed in an INSERT, UPDATE */ /* or DELETE statement */ /* 3: empty */ /* 4: empty */ /* 5: empty */ char sqlwarn[8]; /* 0: set to 'W' if at least one other is 'W' */ /* 1: if 'W' at least one character string */ /* value was truncated when it was */ /* stored into a host variable. */ /* 2: empty */ /* 3: empty */ /* 4: empty */ /* 5: empty */ /* 6: empty */ /* 7: empty */ char sqlext[8]; } sqlca;
If an no error occurred in the last SQL statement. sqlca.sqlcode
will be 0 (ECPG_NO_ERROR). If
sqlca.sqlcode
is less that zero,
this is a serious error, like the database definition does not
match the query. If it is greater than zero, it is a normal
error like the table did not contain the requested row.
sqlca.sqlerrm.sqlerrmc
will
contain a string that describes the error. The string ends with
the line number in the source file.
These are the errors that can occur:
Should not normally occur. This indicates your virtual memory is exhausted.
Should not normally occur. This indicates the preprocessor has generated something that the library does not know about. Perhaps you are running incompatible versions of the preprocessor and the library.
This means that PostgreSQL has returned more arguments than we have matching variables. Perhaps you have forgotten a couple of the host variables in the INTO :var1,:var2-list.
This means that PostgreSQL has returned fewer arguments than we have host variables. Perhaps you have too many host variables in the INTO :var1,:var2-list.
This means the query has returned several rows but the variables specified are not arrays. The SELECT command was not unique.
This means the host variable is of type int and the field in the PostgreSQL database is of another
type and contains a value that cannot be interpreted as
an int. The library uses
strtol()
for this
conversion.
This means the host variable is of type unsigned int and the field in the
PostgreSQL database is
of another type and contains a value that cannot be
interpreted as an unsigned int. The
library uses strtoul()
for
this conversion.
This means the host variable is of type float and the field in the PostgreSQL database is of another
type and contains a value that cannot be interpreted as a
float. The library uses
strtod()
for this
conversion.
This means the host variable is of type bool and the field in the PostgreSQL database is neither 't' nor 'f'.
PostgreSQL returned PGRES_EMPTY_QUERY, probably because the query indeed was empty.
PostgreSQL returned ECPG_MISSING_INDICATOR because a NULL was returned and no NULL indicator variable was supplied.
PostgreSQL returned ECPG_NO_ARRAY because an ordinary variable was used in a place that requires an array.
PostgreSQL returned ECPG_DATA_NOT_ARRAY because the database returned an ordinary variable in a place that requires array value.
The program tried to access a connection that does not exist.
The program tried to access a connection that does exist but is not open.
The statement you are trying to use has not been prepared.
The descriptor specified was not found. The statement you are trying to use has not been prepared.
The descriptor index specified was out of range.
The descriptor specified was not found. The statement you are trying to use has not been prepared.
The database returned a numeric value and the variable was not numeric.
The database returned a non-numeric value and the variable was numeric.
Some PostgreSQL error. The message contains the error message from the PostgreSQL backend.
PostgreSQL signaled that we cannot start, commit or rollback the transaction.
The connect to the database did not work.
This is a "normal" error that tells you that what you are querying cannot be found or you are at the end of the cursor.