Error Handling v10

ECPGPlus provides two methods to detect and handle errors in embedded SQL code:

  • A client application can examine the sqlca data structure for error messages, and supply customized error handling for your client application.
  • A client application can include EXEC SQL WHENEVER directives to instruct the ECPGPlus compiler to add error-handling code.

Error Handling with sqlca

sqlca (SQL communications area) is a global variable used by ecpglib to communicate information from the server to the client application. After executing a SQL statement (for example, an INSERT or SELECT statement) you can inspect the contents of sqlca to determine if the statement has completed successfully or if the statement has failed.

sqlca has the following structure:

struct
{
    char sqlcaid[8];
    long sqlabc;
    long sqlcode;
    struct
    {
        int sqlerrml;
        char sqlerrmc[SQLERRMC_LEN];
    } sqlerrm;
    char sqlerrp[8];
    long sqlerrd[6];
    char sqlwarn[8];
    char sqlstate[5];

} sqlca;

Use the following directive to implement sqlca functionality:

EXEC SQL INCLUDE sqlca;

If you include the ecpg directive, you do not need to #include the sqlca.h file in the client application's header declaration.

The Advanced Server sqlca structure contains the following members:

sqlcaid

sqlcaid contains the string: "SQLCA".

sqlabc

sqlabc contains the size of the sqlca structure.

sqlcode

The sqlcode member has been deprecated with SQL 92; Advanced Server supports sqlcode for backward compatibility, but you should use the sqlstate member when writing new code.

sqlcode is an integer value; a positive sqlcode value indicates that the client application has encountered a harmless processing condition, while a negative value indicates a warning or error.

If a statement processes without error, sqlcode will contain a value of 0. If the client application encounters an error (or warning) during a statement's execution, sqlcode will contain the last code returned.

The SQL standard defines only a positive value of 100, which indicates that he most recent SQL statement processed returned/affected no rows. Since the SQL standard does not define other sqlcode values, please be aware that the values assigned to each condition may vary from database to database.

sqlerrm is a structure embedded within sqlca, composed of two members:

sqlerrml

sqlerrml contains the length of the error message currently stored in sqlerrmc.

sqlerrmc

sqlerrmc contains the null-terminated message text associated with the code stored in sqlstate. If a message exceeds 149 characters in length, ecpglib will truncate the error message.

sqlerrp

sqlerrp contains the string "NOT SET".

sqlerrd is an array that contains six elements:

  • sqlerrd[1] contains the OID of the processed row (if applicable).

  • sqlerrd[2] contains the number of processed or returned rows.

  • sqlerrd[0], sqlerrd[3], sqlerrd[4] and sqlerrd[5] are unused.

sqlwarn is an array that contains 8 characters:

  • sqlwarn[0] contains a value of 'W' if any other element within sqlwarn is set to 'W'.

  • sqlwarn[1] contains a value of 'W' if a data value was truncated when it was stored in a host variable.

  • sqlwarn[2] contains a value of 'W' if the client application encounters a non-fatal warning.

  • sqlwarn[3], sqlwarn[4], sqlwarn[5], sqlwarn[6], and sqlwarn[7] are unused.

sqlstate

sqlstate is a 5 character array that contains a SQL-compliant status code after the execution of a statement from the client application. If a statement processes without error, sqlstate will contain a value of 00000. Please note that sqlstate is not a null-terminated string.

sqlstate codes are assigned in a hierarchical scheme:

  • The first two characters of sqlstate indicate the general class of the condition.
  • The last three characters of sqlstate indicate a specific status within the class.

If the client application encounters multiple errors (or warnings) during an SQL statement's execution sqlstate will contain the last code returned.

The following table lists the sqlstate and sqlcode values, as well as the symbolic name and error description for the related condition:

sqlstatesqlcode (Deprecated)Symbolic NameDescription
YE001-12ECPG_OUT_OF_MEMORYVirtual memory is exhausted.
YE002-200ECPG_UNSUPPORTEDThe preprocessor has generated an unrecognized item. Could indicate incompatibility between the preprocessor and the library.
07001, or 07002-201ECPG_TOO_MANY_ARGUMENTSThe program specifies more variables than the command expects.
07001, or 07002-202ECPG_TOO_FEW_ARGUMENTSThe program specified fewer variables than the command expects.
21000-203ECPG_TOO_MANY_MATCHESThe SQL command has returned multiple rows, but the statement was prepared to receive a single row.
42804-204ECPG_INT_FORMATThe host variable (defined in the C code) is of type INT, and the selected data is of a type that cannot be converted into an INT. ecpglib uses the strtol() function to convert string values into numeric form.
42804-205ECPG_UINT_FORMATThe host variable (defined in the C code) is an unsigned INT, and the selected data is of a type that cannot be converted into an unsigned INT. ecpglib uses the strtoul() function to convert string values into numeric form.
42804-206ECPG_FLOAT_FORMATThe host variable (defined in the C code) is of type FLOAT, and the selected data is of a type that cannot be converted into an FLOAT. ecpglib uses the strtod() function to convert string values into numeric form.
42804-211ECPG_CONVERT_BOOLThe host variable (defined in the C code) is of type BOOL, and the selected data cannot be stored in a BOOL.
YE002-2-1ECPG_EMPTYThe statement sent to the server was empty.
22002-213ECPG_MISSING_INDICATORA NULL indicator variable has not been supplied for the NULL value returned by the server (the client application has received an unexpected NULL value).
42804-214ECPG_NO_ARRAYThe server has returned an array, and the corresponding host variable is not capable of storing an array.
42804-215ECPG_DATA_NOT_ARRAYThe server has returned a value that is not an array into a host variable that expects an array value.
08003-220ECPG_NO_CONNThe client application has attempted to use a non-existent connection.
YE002-221ECPG_NOT_CONNThe client application has attempted to use an allocated, but closed connection.
26000-230ECPG_INVALID_STMTThe statement has not been prepared.
33000-240ECPG_UNKNOWN_DESCRIPTORThe specified descriptor is not found.
07009-241ECPG_INVALID_DESCRIPTOR_INDEXThe descriptor index is out-of-range.
YE002-242ECPG_UNKNOWN_DESCRIPTOR_ITEMThe client application has requested an invalid descriptor item (internal error).
07006-243ECPG_VAR_NOT_NUMERICA dynamic statement has returned a numeric value for a non-numeric host variable.
07006-244ECPG_VAR_NOT_CHARA dynamic SQL statement has returned a CHAR value, and the host variable is not a CHAR.
-400ECPG_PGSQLThe server has returned an error message; the resulting message contains the error text.
08007-401ECPG_TRANSThe server cannot start, commit or rollback the specified transaction.
08001-402ECPG_CONNECTThe client application's attempt to connect to the database has failed.
02000100ECPG_NOT_FOUNDThe last command retrieved or processed no rows, or you have reached the end of a cursor.

EXEC SQL WHENEVER

Use the EXEC SQL WHENEVER directive to implement simple error handling for client applications compiled with ECPGPlus. The syntax of the directive is:

EXEC SQL WHENEVER <condition> <action>;

This directive instructs the ECPG compiler to insert error-handling code into your program.

The code instructs the client application that it should perform a specified action if the client application detects a given condition. The condition may be one of the following:

SQLERROR

A SQLERROR condition exists when sqlca.sqlcode is less than zero.

SQLWARNING

A SQLWARNING condition exists when sqlca.sqlwarn[0] contains a 'W'.

NOT FOUND

A NOT FOUND condition exists when sqlca.sqlcode is ECPG_NOT_FOUND (when a query returns no data).

You can specify that the client application perform one of the following actions if it encounters one of the previous conditions:

CONTINUE

Specify CONTINUE to instruct the client application to continue processing, ignoring the current condition. CONTINUE is the default action.

DO CONTINUE

An action of DO CONTINUE will generate a CONTINUE statement in the emitted C code that if it encounters the condition, skips the rest of the code in the loop and continues with the next iteration. You can only use it within a loop.

GOTO label

or

GO TO label

Use a C goto statement to jump to the specified label.

SQLPRINT

Print an error message to stderr (standard error), using the sqlprint() function. The sqlprint() function prints sql error, followed by the contents of sqlca.sqlerrm.sqlerrmc.

STOP

Call exit(1) to signal an error, and terminate the program.

DO BREAK

Execute the C break statement. Use this action in loops, or switch statements.

CALL name(args)

or

DO name(args)

Invoke the C function specified by the name parameter, using the parameters specified in the args parameter.

Example:

The following code fragment prints a message if the client application encounters a warning, and aborts the application if it encounters an error:

EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR STOP;
Note

The ECPGPlus compiler processes your program from top to bottom, even though the client application may not execute from top to bottom. The compiler directive is applied to each line in order, and remains in effect until the compiler encounters another directive. If the control of the flow within your program is not top-to-bottom, you should consider adding error-handling directives to any parts of the program that may be inadvertently missed during compilation.