exp_chk.ec 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. EXEC SQL define SUCCESS 0;
  2. EXEC SQL define WARNING 1;
  3. EXEC SQL define NODATA 100;
  4. EXEC SQL define RTERROR -1;
  5. char statement[80];
  6. /*
  7. * The sqlstate_err() function checks the SQLSTATE status variable to see
  8. * if an error or warning has occurred following an SQL statement.
  9. */
  10. int4 sqlstate_err()
  11. {
  12. int4 err_code = RTERROR;
  13. if(SQLSTATE[0] == '0') /* trap '00', '01', '02' */
  14. {
  15. switch(SQLSTATE[1])
  16. {
  17. case '0': /* success - return 0 */
  18. err_code = SUCCESS;
  19. break;
  20. case '1': /* warning - return 1 */
  21. err_code = WARNING;
  22. break;
  23. case '2': /* end of data - return 100 */
  24. err_code = NODATA;
  25. break;
  26. default: /* error - return SQLCODE */
  27. break;
  28. }
  29. }
  30. return(err_code);
  31. }
  32. /*
  33. * The disp_sqlstate_err() function executes the GET DIAGNOSTICS
  34. * statement and prints the detail for each exception that is returned.
  35. */
  36. void disp_sqlstate_err()
  37. {
  38. mint j;
  39. EXEC SQL BEGIN DECLARE SECTION;
  40. mint exception_count;
  41. char overflow[2];
  42. mint exception_num=1;
  43. char class_id[255];
  44. char subclass_id[255];
  45. char message[255];
  46. mint messlen;
  47. char sqlstate_code[6];
  48. mint i;
  49. EXEC SQL END DECLARE SECTION;
  50. printf("---------------------------------");
  51. printf("-------------------------\n");
  52. printf("SQLSTATE: %s\n",SQLSTATE);
  53. printf("SQLCODE: %d\n", SQLCODE);
  54. printf("\n");
  55. EXEC SQL get diagnostics :exception_count = NUMBER,
  56. :overflow = MORE;
  57. printf("EXCEPTIONS: Number=%d\t", exception_count);
  58. printf("More? %s\n", overflow);
  59. for (i = 1; i <= exception_count; i++)
  60. {
  61. EXEC SQL get diagnostics exception :i
  62. :sqlstate_code = RETURNED_SQLSTATE,
  63. :class_id = CLASS_ORIGIN, :subclass_id = SUBCLASS_ORIGIN,
  64. :message = MESSAGE_TEXT, :messlen = MESSAGE_LENGTH;
  65. printf("- - - - - - - - - - - - - - - - - - - -\n");
  66. printf("EXCEPTION %d: SQLSTATE=%s\n", i,
  67. sqlstate_code);
  68. message[messlen-1] = '\0';
  69. printf("MESSAGE TEXT: %s\n", message);
  70. j = byleng(class_id, stleng(class_id));
  71. class_id[j] = '\0';
  72. printf("CLASS ORIGIN: %s\n",class_id);
  73. j = byleng(subclass_id, stleng(subclass_id));
  74. subclass_id[j] = '\0';
  75. printf("SUBCLASS ORIGIN: %s\n",subclass_id);
  76. }
  77. printf("---------------------------------");
  78. printf("-------------------------\n");
  79. }
  80. void disp_error(stmt)
  81. char *stmt;
  82. {
  83. printf("\n********Error encountered in %s********\n",
  84. stmt);
  85. disp_sqlstate_err();
  86. }
  87. void disp_warning(stmt)
  88. char *stmt;
  89. {
  90. printf("\n********Warning encountered in %s********\n",
  91. stmt);
  92. disp_sqlstate_err();
  93. }
  94. void disp_exception(stmt, sqlerr_code, warn_flg)
  95. char *stmt;
  96. int4 sqlerr_code;
  97. mint warn_flg;
  98. {
  99. switch (sqlerr_code)
  100. {
  101. case SUCCESS:
  102. case NODATA:
  103. break;
  104. case WARNING:
  105. if(warn_flg)
  106. disp_warning(stmt);
  107. break;
  108. case RTERROR:
  109. disp_error(stmt);
  110. break;
  111. default:
  112. printf("\n********INVALID EXCEPTION STATE for %s********\n",
  113. stmt);
  114. break;
  115. }
  116. }
  117. /*
  118. * The exp_chk() function calls sqlstate_err() to check the SQLSTATE
  119. * status variable to see if an error or warning has occurred following
  120. * an SQL statement. If either condition has occurred, exp_chk()
  121. * calls disp_sqlstate_err() to print the detailed error information.
  122. *
  123. * This function handles exceptions as follows:
  124. * runtime errors - call exit(1)
  125. * warnings - continue execution, returning "1"
  126. * success - continue execution, returning "0"
  127. * Not Found - continue execution, returning "100"
  128. */
  129. int4 exp_chk(stmt, warn_flg)
  130. char *stmt;
  131. mint warn_flg;
  132. {
  133. int4 sqlerr_code = SUCCESS;
  134. sqlerr_code = sqlstate_err();
  135. disp_exception(stmt, sqlerr_code, warn_flg);
  136. if(sqlerr_code == RTERROR) /* Exception is a runtime error */
  137. {
  138. /* Exit the program after examining the error */
  139. printf("********Program terminated*******\n\n");
  140. exit(1);
  141. }
  142. else /* Exception is "success", "Not Found",*/
  143. return(sqlerr_code); /* or "warning" */
  144. }
  145. /*
  146. * The exp_chk2() function calls sqlstate_err() to check the SQLSTATE
  147. * status variable to see if an error or warning has occurred following
  148. * an SQL statement. If either condition has occurred, exp_chk2()
  149. * calls disp_exception() to print the detailed error information.
  150. *
  151. * This function handles exceptions as follows:
  152. * runtime errors - continue execution, returning SQLCODE (<0)
  153. * warnings - continue execution, returning one (1)
  154. * success - continue execution, returning zero (0)
  155. * Not Found - continue execution, returning 100
  156. */
  157. int4 exp_chk2(stmt, warn_flg)
  158. char *stmt;
  159. mint warn_flg;
  160. {
  161. int4 sqlerr_code = SUCCESS;
  162. int4 sqlcode;
  163. sqlcode = SQLCODE; /* save SQLCODE in case of error */
  164. sqlerr_code = sqlstate_err();
  165. disp_exception(stmt, sqlerr_code, warn_flg);
  166. if(sqlerr_code == RTERROR) /* if runtime error, return SQLCODE */
  167. sqlerr_code = sqlcode;
  168. return(sqlerr_code);
  169. }
  170. /*
  171. * The whenexp_chk() function calls sqlstate_err() to check the SQLSTATE
  172. * status variable to see if an error or warning has occurred following
  173. * an SQL statement. If either condition has occurred, whenerr_chk()
  174. * calls disp_sqlstate_err() to print the detailed error information.
  175. *
  176. * This function is expected to be used with the WHENEVER SQLERROR
  177. * statement: it executes an exit(1) when it encounters a negative
  178. * error code. It also assumes the presence of the "statement" global
  179. * variable, set by the calling program to the name of the statement
  180. * encountering the error.
  181. */
  182. whenexp_chk()
  183. {
  184. int4 sqlerr_code = SUCCESS;
  185. mint disp = 0;
  186. sqlerr_code = sqlstate_err();
  187. if(sqlerr_code == WARNING)
  188. {
  189. disp = 1;
  190. printf("\n********Warning encountered in %s********\n",
  191. statement);
  192. }
  193. else
  194. if(sqlerr_code == RTERROR)
  195. {
  196. printf("\n********Error encountered in %s********\n",
  197. statement);
  198. disp = 1;
  199. }
  200. if(disp)
  201. disp_sqlstate_err();
  202. if(sqlerr_code == RTERROR)
  203. {
  204. /* Exit the program after examining the error */
  205. printf("********Program terminated*******\n\n");
  206. exit(1);
  207. }
  208. else
  209. {
  210. if(sqlerr_code == WARNING)
  211. printf("\n********Program execution continues********\n\n");
  212. return(sqlerr_code);
  213. }
  214. }
  215. /*
  216. * The 'ignore206' error handling routine ignores the -206 error
  217. * described below:
  218. *
  219. * ----------------------------------------------------------------------------
  220. * -206 The specified table <table-name> is not in the database.
  221. *
  222. * The database server cannot find a table or view specified in the statement.
  223. * The table or view might have been renamed or dropped from the database.
  224. *
  225. * Check the names of tables and views in the statement. If the names are
  226. * spelled as you intended, check that you are using the database you want.
  227. * To find the names of all tables in the database, query the systables table.
  228. * To find the names of all views, query the sysviews table.
  229. * ----------------------------------------------------------------------------
  230. */
  231. ignore206 ()
  232. {
  233. int4 sqlerr_code = SUCCESS;
  234. if (SQLCODE != -206)
  235. sqlerr_code = whenexp_chk();
  236. return(sqlerr_code);
  237. }