procW.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * (C) Copyright IBM Corporation 1997, 2004 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: procW.c (Unicode counterpart of proc.c)
  14. *
  15. * Description: To execute a stored procedure returning multiple results
  16. *
  17. * The stored procedure executed is -
  18. * PROCEDURE multiReturnProc (i integer)
  19. * RETURNING integer;
  20. * define j integer;
  21. * for j in (1 to 5)
  22. * return i*j with resume;
  23. * end for;
  24. * END PROCEDURE
  25. *
  26. * Because the Informix ODBC driver version 3.3 does not
  27. * yet suport output parameters for stored procedures,
  28. * the way to get return values from a procedure is to
  29. * use SQLBindCol and SQLFetch. This works whether the
  30. * procedure returns a single value or multiple values.
  31. *
  32. *
  33. ***************************************************************************
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #ifndef NO_WIN32
  39. #include <io.h>
  40. #include <windows.h>
  41. #include <conio.h>
  42. #endif /*NO_WIN32*/
  43. #include "infxcli.h"
  44. #define ERRMSG_LEN 200
  45. SQLWCHAR defDsnW[] = L"odbc_demo";
  46. SQLCHAR defDsn[] = "odbc_demo";
  47. SQLINTEGER checkError (SQLRETURN rc,
  48. SQLSMALLINT handleType,
  49. SQLHANDLE handle,
  50. SQLCHAR* errmsg)
  51. {
  52. SQLRETURN retcode = SQL_SUCCESS;
  53. SQLSMALLINT errNum = 1;
  54. SQLWCHAR sqlStateW[6];
  55. SQLCHAR *sqlState;
  56. SQLINTEGER nativeError;
  57. SQLWCHAR errMsgW[ERRMSG_LEN];
  58. SQLCHAR *errMsg;
  59. SQLSMALLINT textLengthPtr;
  60. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  61. {
  62. while (retcode != SQL_NO_DATA)
  63. {
  64. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  65. if (retcode == SQL_INVALID_HANDLE)
  66. {
  67. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  68. return 1;
  69. }
  70. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  71. {
  72. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  73. ;
  74. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  75. + sizeof(char));
  76. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  77. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  78. + sizeof(char));
  79. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  80. }
  81. errNum++;
  82. }
  83. fprintf (stderr, "%s\n", errmsg);
  84. return 1; /* all errors on this handle have been reported */
  85. }
  86. else
  87. return 0; /* no errors to report */
  88. }
  89. int main (long argc,
  90. char* argv[])
  91. {
  92. /* Declare variables
  93. */
  94. /* Handles */
  95. SQLHDBC hdbc;
  96. SQLHENV henv;
  97. SQLHSTMT hstmt;
  98. /* Miscellaneous variables */
  99. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  100. SQLRETURN rc = 0;
  101. SQLINTEGER in;
  102. SQLWCHAR* createProcStmtW = (SQLWCHAR *) L"CREATE PROCEDURE \
  103. multiReturnProc (i integer)\
  104. RETURNING integer;\
  105. define j integer;\
  106. for j in (1 to 5)\
  107. return i*j with resume;\
  108. end for;\
  109. END PROCEDURE;";
  110. SQLWCHAR* dropProcStmtW = (SQLWCHAR *) L"DROP PROCEDURE multiReturnProc";
  111. SQLSMALLINT inputParam = 2;
  112. SQLINTEGER retVal = 7, cbRetVal = 0, cbInputParam = 0;
  113. int lenArgv1;
  114. /* STEP 1. Get data source name from command line (or use default)
  115. ** Allocate the environment handle and set ODBC version
  116. ** Allocate the connection handle
  117. ** Establish the database connection
  118. ** Allocate the statement handle
  119. */
  120. /* If(dsn is not explicitly passed in as arg) */
  121. if (argc != 2)
  122. {
  123. /* Use default dsnW - odbc_demo */
  124. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  125. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  126. + sizeof(SQLWCHAR) );
  127. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  128. }
  129. else
  130. {
  131. /* Use specified dsnW */
  132. lenArgv1 = strlen((char *)argv[1]);
  133. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  134. + sizeof(SQLWCHAR));
  135. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  136. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  137. }
  138. /* Allocate the Environment handle */
  139. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  140. if (rc != SQL_SUCCESS)
  141. {
  142. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  143. return (1);
  144. }
  145. /* Set the ODBC version to 3.0 */
  146. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  147. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  148. return (1);
  149. /* Allocate the connection handle */
  150. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  151. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  152. return (1);
  153. /* Establish the database connection */
  154. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  155. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  156. return (1);
  157. /* Allocate the statement handle */
  158. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  159. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  160. return (1);
  161. fprintf (stdout, "STEP 1 done...connected to database\n");
  162. /* STEP 2. Create the stored procedure in the database
  163. */
  164. /* Execute the SQL statement to create the stored procedure*/
  165. rc = SQLExecDirectW (hstmt, createProcStmtW, SQL_NTS);
  166. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  167. goto Exit;
  168. fprintf (stdout, "STEP 2 done...stored procedure created\nExecuting procedure...\n\n");
  169. /* STEP 3. Bind the input parameter
  170. ** Execute the procedure
  171. ** Bind the result set column (return values)
  172. ** Fetch the results
  173. ** Display the results
  174. ** Close the result set cursor
  175. */
  176. /* Bind the input parameter */
  177. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &inputParam, 0, &cbInputParam);
  178. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed\n"))
  179. return (1);
  180. /* Execute the procedure */
  181. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call multiReturnProc (?)}", SQL_NTS);
  182. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  183. goto Exit;
  184. /* Bind the result set column */
  185. rc = SQLBindCol (hstmt, 1, SQL_C_SLONG, (SQLPOINTER)&retVal, 0, &cbRetVal);
  186. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
  187. goto Exit;
  188. /* Fetch and display the results */
  189. while (1)
  190. {
  191. rc = SQLFetch (hstmt);
  192. if (rc == SQL_NO_DATA_FOUND)
  193. break;
  194. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  195. goto Exit;
  196. /* Display the results */
  197. fprintf (stdout, "Procedure returned %d\n", retVal);
  198. }
  199. /* Close the result set cursor */
  200. rc = SQLCloseCursor (hstmt);
  201. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
  202. goto Exit;
  203. fprintf (stdout, "\nSTEP 3 done...stored procedure executed\n");
  204. Exit:
  205. /* CLEANUP: Drop the stored procedure from the database
  206. ** Close the statement handle
  207. ** Free the statement handle
  208. ** Disconnect from the datasource
  209. ** Free the connection and environment handles
  210. ** Exit
  211. */
  212. /* Drop the stored procedure from the database */
  213. SQLExecDirectW (hstmt, dropProcStmtW, SQL_NTS);
  214. /* Close the statement handle */
  215. SQLFreeStmt (hstmt, SQL_CLOSE);
  216. /* Free the statement handle */
  217. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  218. /* Disconnect from the data source */
  219. SQLDisconnect (hdbc);
  220. /* Free the environment handle and the database connection handle */
  221. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  222. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  223. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  224. in = getchar ();
  225. return (rc);
  226. }