odbcutils.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. void returncode_check (SQLHDBC handle,
  2. SQLHSTMT hstmt,
  3. int rc,
  4. char *str);
  5. static void rc_to_str (int rc,
  6. char *str);
  7. /***************************************************************************
  8. *
  9. * server_connect -- connect to server
  10. *
  11. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  12. *
  13. * Purpose:
  14. * This function establishes a connection to the server.
  15. *
  16. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  17. *
  18. * Parameters:
  19. * datasource <Input> == (char *) ODBC datasource name.
  20. * henv <Input> == (SQLHENV *) Pointer to the ODBC environment.
  21. * SQLHDBC <Input> == (SQLHDBC *) The ODBC connection handle.
  22. *
  23. * RETURN <Output> == (void)
  24. *
  25. ****************************************************************************/
  26. void server_connect (
  27. SQLCHAR *datasource,
  28. SQLHENV *henv,
  29. SQLHDBC *hdbc
  30. )
  31. {
  32. int rc;
  33. SDWORD dbms_err;
  34. SWORD length;
  35. unsigned char err_msg[SQL_MAX_MESSAGE_LENGTH];
  36. unsigned char state[6];
  37. /* Load the connectivity variables... */
  38. printf ("Connecting to datasource %s ...\n", datasource);
  39. /* Allocate memory for the ODBC environment handle and initialize
  40. the ODBC environment. */
  41. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, henv);
  42. if (rc != SQL_SUCCESS)
  43. {
  44. printf ("SQLAllocHandle failed with %d\n", rc);
  45. exit (1);
  46. }
  47. /* Set the ODBC version to 3.0 */
  48. rc = SQLSetEnvAttr (*henv, SQL_ATTR_ODBC_VERSION,
  49. (SQLPOINTER) SQL_OV_ODBC3, 0);
  50. if (rc != SQL_SUCCESS)
  51. {
  52. printf ("SQLSetEnvAttr failed with %d\n", rc);
  53. SQLGetDiagRec (SQL_HANDLE_ENV, *henv, 1, state,
  54. &dbms_err, err_msg,
  55. SQL_MAX_MESSAGE_LENGTH - 1, &length);
  56. printf ("DBMS error code:%d, SQL state: %s, message: %s\n",
  57. dbms_err, state, err_msg);
  58. exit (1);
  59. }
  60. /* Allocate memory for a ODBC connection handle within
  61. the environment just established. */
  62. rc = SQLAllocHandle (SQL_HANDLE_DBC, *henv, hdbc);
  63. if (rc != SQL_SUCCESS)
  64. {
  65. printf ("SQLAllocConnect failed with %d\n", rc);
  66. SQLGetDiagRec (SQL_HANDLE_ENV, *henv, 1, state,
  67. &dbms_err, err_msg,
  68. SQL_MAX_MESSAGE_LENGTH - 1, &length);
  69. printf ("DBMS error code:%d, SQL state: %s, message: %s\n",
  70. dbms_err, state, err_msg);
  71. exit (1);
  72. }
  73. /* Establish the database connection */
  74. rc = SQLConnect (*hdbc, datasource, SQL_NTS,
  75. (SQLCHAR *) "", SQL_NTS, /* username */
  76. (SQLCHAR *) "", SQL_NTS); /* password */
  77. returncode_check (*hdbc, (SQLHSTMT)NULL, rc, "SQLConnect");
  78. return;
  79. }
  80. /***************************************************************************
  81. *
  82. * returncode_check -- checks the return code for SQL_SUCCESS
  83. *
  84. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  85. *
  86. * Purpose:
  87. * This program expands the return error code and displays
  88. * the results if the return code is not SQL_SUCCESS.
  89. *
  90. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  91. *
  92. * Parameters:
  93. * hdbc <Input> == (SQLHDBC) The ODBC connection handle.
  94. * hstmt <Input> == (SQLHSTMT) The ODBC SQL statement handle.
  95. * rc <Input> == (int) The error return code.
  96. * str <Input> == (char *) The ODBC function the error occurred on.
  97. *
  98. * RETURN <Output> == (void)
  99. *
  100. ****************************************************************************/
  101. void returncode_check (
  102. SQLHDBC hdbc,
  103. SQLHSTMT hstmt,
  104. int rc,
  105. char *str
  106. )
  107. {
  108. SDWORD dbms_err = 0;
  109. SWORD length;
  110. unsigned char err_msg[SQL_MAX_MESSAGE_LENGTH];
  111. unsigned char state[6];
  112. char rc_str[80];
  113. if (rc != SQL_SUCCESS)
  114. {
  115. /* Convert the return code (rc) to its character string
  116. equivalent (rc_str)*/
  117. rc_to_str (rc, rc_str);
  118. /* The SQLError function returns the SQL state, the native DBMS
  119. error code, and a pointer to the associated native DBMS error
  120. message. */
  121. if (hdbc != NULL)
  122. SQLGetDiagRec (SQL_HANDLE_DBC, hdbc, 1, state, &dbms_err,
  123. err_msg, SQL_MAX_MESSAGE_LENGTH - 1, &length);
  124. else
  125. SQLGetDiagRec (SQL_HANDLE_STMT, hstmt, 1, state, &dbms_err,
  126. err_msg, SQL_MAX_MESSAGE_LENGTH - 1, &length);
  127. printf ("%s ERROR (%s): DBMS code:%d, SQL state: %s, message: \n%s\n",
  128. str, rc_str, dbms_err, state, err_msg);
  129. if (rc != SQL_SUCCESS_WITH_INFO)
  130. {
  131. /* Only fatal out if more severe than SQL_SUCCESS_WITH_INFO */
  132. if (NULL != hstmt)
  133. {
  134. /* Try to release locks, & other resources */
  135. SQLFreeStmt (hstmt, SQL_DROP); /* Release the SQL statment handle */
  136. /* and free all resources associated */
  137. /* with it. */
  138. }
  139. SQLDisconnect (hdbc); /* Close the connection */
  140. SQLFreeConnect (hdbc); /* Free the connection handle */
  141. exit (1);
  142. }
  143. }
  144. }
  145. /***************************************************************************
  146. *
  147. * rc_to_str -- converts ODBC return code to string
  148. *
  149. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  150. *
  151. * Purpose:
  152. * The ODBC return code integer is converted to its string equivalent.
  153. *
  154. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  155. *
  156. * Parameters:
  157. * rc <Input> == (int) The ODBC return code.
  158. * str <Input> == (char *) A pointer to the error code string.
  159. *
  160. * RETURN <Output> == (void)
  161. *
  162. ****************************************************************************/
  163. static void rc_to_str (
  164. int rc,
  165. char *str
  166. )
  167. {
  168. /* Expand the error code (rc) to its string (str) */
  169. switch (rc)
  170. {
  171. case SQL_SUCCESS:
  172. strcpy (str, "SQL_SUCCESS");
  173. break;
  174. case SQL_SUCCESS_WITH_INFO:
  175. strcpy (str, "SQL_SUCCESS_WITH_INFO");
  176. break;
  177. case SQL_NO_DATA_FOUND:
  178. strcpy (str, "SQL_NO_DATA_FOUND");
  179. break;
  180. case SQL_NEED_DATA:
  181. strcpy (str, "SQL_NEED_DATA");
  182. break;
  183. case SQL_STILL_EXECUTING:
  184. strcpy (str, "SQL_STILL_EXECUTING");
  185. break;
  186. case SQL_ERROR:
  187. strcpy (str, "SQL_ERROR");
  188. break;
  189. case SQL_INVALID_HANDLE:
  190. strcpy (str, "SQL_INVALID_HANDLE");
  191. break;
  192. default:
  193. strcpy (str, "UNKNOWN RETURN CODE!!");
  194. break;
  195. }
  196. }