Guide to the getdiag.ec file

The annotations in this section primarily describe the exception-handling statements.

=======================================================================
1. #include <stdio.h>
2. EXEC SQL define FNAME_LEN 15;
3. EXEC SQL define LNAME_LEN 15;
4. int4 sqlstate_err();
5. extern char statement[20];
6. main()
7. {
8.       EXEC SQL BEGIN DECLARE SECTION;
9.       char fname[ FNAME_LEN + 1 ];
10.       char lname[ LNAME_LEN + 1 ];
11.       EXEC SQL END DECLARE SECTION;
12.       EXEC SQL whenever sqlerror CALL whenexp_chk;
13.       EXEC SQL whenever sqlwarning CALL whenexp_chk;
14.       printf("GETDIAG Sample ESQL program running.\n\n");
15.       strcpy (statement, "CONNECT stmt");
16.       EXEC SQL connect to 'stores7';
17.       strcpy (statement, "DECLARE stmt");
18.       EXEC SQL declare democursor cursor for
19.          select fname, lname
20.          into :fname, :lname;
21.          from customer
22.          where lname < 'C';
23.       strcpy (statement, "OPEN stmt");
24.       EXEC SQL open democursor;
25.       strcpy (statement, "FETCH stmt");
26.       for (;;)
27.       {
28.          EXEC SQL fetch democursor;
29.          if(sqlstate_err() == 100)
30.             break;
31.          printf("%s %s\n", fname, lname);
32.       }
33.       strcpy (statement, "CLOSE stmt");
34.       EXEC SQL close democursor;
=======================================================================

Line 4

Line 4 declares an external global variable to hold the name of the most-recently executed SQL statement. The exception-handling functions use this information (see Lines 169 - 213).

Lines 12 and 13

The WHENEVER SQLERROR statement tells the Informix® ESQL/C preprocessor to add code to the program to call the whenexp_chk() function whenever an SQL statement generates an error. The WHENEVER SQLWARNING statement tells the Informix® ESQL/C preprocessor to add code to the program to call the whenexp_chk() function whenever an SQL statement generates a warning. The whenexp_chk() function is in the exp_chk.ec file, which line 40 includes.

Line 15

The strcpy() function copies the string "CONNECT stmt" to the global statement variable. If an error occurs, the whenexp_chk() function uses this variable to print the name of the statement that caused the failure.

Lines 17, 23, 25, and 33

These lines copy the name of the current SQL statement into the statement variable before the DECLARE, OPEN, FETCH, and CLOSE statements execute. This action enables the whenexp_chk() function to identify the statement that failed if an error occurs.

=======================================================================
36.       strcpy (statement, "FREE stmt");
37.       EXEC SQL free democursor;
38.       strcpy (statement, "DISCONNECT stmt");
39.       EXEC SQL disconnect current;
40.       printf("\nGETDIAG Sample Program Over.\n");
41. }        /* End of main routine */
42. EXEC SQL include exp_chk.ec;
=======================================================================

Lines 35 and 37

These lines copy the name of the current SQL statement into the statement variable before the FREE and DISCONNECT statements execute. The whenexp_chk() function uses the statement variable to identify the statement that failed if an error occurs.

Line 41

The whenexp_chk() function examines the SQLSTATE status variable to determine the outcome of an SQL statement. Because several demonstration programs use the whenexp_chk() function with the WHENEVER statement for exception handling, the whenexp_chk() function and its supporting functions are placed in a separate source file, exp_chk.ec. The getdiag program must include this file with the Informix® ESQL/C include directive because the exception-handling functions use Informix® ESQL/C statements.
Tip: Consider putting functions such as whenexp_chk() into a library and include this library on the command line when you compile the Informix® ESQL/C program.