OutInOutParamBlobW.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /****************************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * (C) Copyright IBM Corporation 2010 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. * Title: OutInOutParamBlobW.c (Unicode counterpart of OutInOutParamBlob.c)
  10. *
  11. * Description: OUT/INOUT parameters demo with BLOB, INTEGER and VARCHAR data types.
  12. *
  13. *****************************************************************************************
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <wchar.h>
  19. #ifndef NO_WIN32
  20. #include <io.h>
  21. #include <windows.h>
  22. #include <conio.h>
  23. #endif /*NO_WIN32*/
  24. #include "infxcli.h"
  25. #define BUFFER_LENW 130 * sizeof(SQLWCHAR)
  26. #define ERRMSG_LEN 200
  27. SQLCHAR defDsn[] = "odbc_demo";
  28. SQLWCHAR defDsnW[] = L"odbc_demo";
  29. SQLINTEGER checkError (SQLRETURN rc,
  30. SQLSMALLINT handleType,
  31. SQLHANDLE handle,
  32. SQLCHAR* errmsg)
  33. {
  34. SQLRETURN retcode = SQL_SUCCESS;
  35. SQLSMALLINT errNum = 1;
  36. SQLWCHAR sqlStateW[6];
  37. SQLCHAR *sqlState;
  38. SQLINTEGER nativeError;
  39. SQLWCHAR errMsgW[ERRMSG_LEN];
  40. SQLCHAR *errMsg;
  41. SQLSMALLINT textLengthPtr;
  42. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  43. {
  44. while (retcode != SQL_NO_DATA)
  45. {
  46. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  47. if (retcode == SQL_INVALID_HANDLE)
  48. {
  49. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  50. return 1;
  51. }
  52. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  53. {
  54. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char));
  55. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  56. + sizeof(char));
  57. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  58. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  59. + sizeof(char));
  60. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  61. }
  62. errNum++;
  63. }
  64. fprintf (stderr, "%s\n", errmsg);
  65. return 1; /* all errors on this handle have been reported */
  66. }
  67. else
  68. return 0; /* no errors to report */
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. /* Declare variables
  73. */
  74. /* Handles */
  75. HENV henv = NULL;
  76. HDBC hdbc = NULL;
  77. HSTMT hstmt = NULL;
  78. SQLHDESC hdesc = NULL;
  79. /* Miscellaneous variables */
  80. SQLWCHAR *dsnW = L""; /*name of the DSN used for connecting to the database*/
  81. SQLRETURN rc = 0;
  82. SQLINTEGER in;
  83. int sParm1 = 123;
  84. int sParm2 = 456;
  85. int sParm3 = 789, cmpParm3 = 789;
  86. int len=0;
  87. SQLLEN cbParm1 = SQL_NTS;
  88. SQLLEN cbParm2 = SQL_NTS;
  89. SQLLEN cbParm3 = SQL_NULL_DATA;
  90. SQLLEN cbParm4 = SQL_NTS;
  91. SQLLEN cbParm5 = SQL_NULL_DATA;
  92. SQLLEN cbParm6 = SQL_NTS;
  93. SQLWCHAR scharW[20] = L"";
  94. SQLWCHAR blob_bufferW[BUFFER_LENW] = L"";
  95. int lenArgv1;
  96. /* If(dsnW is not explicitly passed in as arg) */
  97. if (argc != 2)
  98. {
  99. /* Use default dsnW - odbc_demo */
  100. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  101. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  102. + sizeof(SQLWCHAR) );
  103. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  104. }
  105. else
  106. {
  107. /* Use specified dsnW */
  108. lenArgv1 = strlen((char *)argv[1]);
  109. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  110. + sizeof(SQLWCHAR));
  111. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  112. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  113. }
  114. /* Allocate the Environment handle */
  115. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  116. if (rc != SQL_SUCCESS)
  117. {
  118. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  119. return (1);
  120. }
  121. /* Set the ODBC version to 3.0 */
  122. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
  123. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  124. return (1);
  125. /* Allocate the connection handle */
  126. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  127. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  128. return (1);
  129. /* Establish the database connection */
  130. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  131. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  132. return (1);
  133. /* Allocate the statement handles */
  134. rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt );
  135. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  136. return (1);
  137. fprintf (stdout, "STEP 1 done...connected to database\n");
  138. /*********************************************************************/
  139. /* tests involving procedures that has OUT/INOUT params and also returns single value */
  140. /*********************************************************************/
  141. /* Drop procedure */
  142. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop procedure spl_out_param_blob;", SQL_NTS);
  143. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop table tab_blob;", SQL_NTS);
  144. /* Create table with BLOB column */
  145. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"create table tab_blob(c_blob BLOB, c_int INTEGER, c_char varchar(20));", SQL_NTS);
  146. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  147. goto Exit;
  148. /* Insert one row into the table */
  149. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"insert into tab_blob values(filetoblob('insert.txt', 'c'), 10, 'blob_test');", SQL_NTS);
  150. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  151. goto Exit;
  152. /* Create procedure */
  153. rc = SQLExecDirectW(hstmt, L"CREATE PROCEDURE spl_out_param_blob(inParam int, OUT blobparam BLOB, OUT intparam int, OUT charparam varchar(20)) \n"
  154. L"returning integer; \n"
  155. L"select c_blob, c_int, c_char into blobparam, intparam, charparam from tab_blob; \n"
  156. L"return inParam; \n"
  157. L"end procedure; ",
  158. SQL_NTS);
  159. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  160. goto Exit;
  161. /* Prepare stored procedure to be executed */
  162. rc = SQLPrepareW(hstmt, (SQLWCHAR *) L"{? = call spl_out_param_blob(?, ?, ?, ?)}", SQL_NTS);
  163. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLPrepare failed\n"))
  164. goto Exit;
  165. /* Bind the required parameters */
  166. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_INTEGER, 3, 0, &sParm1, 0, &cbParm1);
  167. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter 1 failed\n"))
  168. goto Exit;
  169. rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 10, 0, &sParm2, 0, &cbParm2);
  170. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter 2 failed\n"))
  171. goto Exit;
  172. rc = SQLBindParameter(hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_BINARY, SQL_LONGVARBINARY, sizeof(blob_bufferW), 0, blob_bufferW, sizeof(blob_bufferW), &cbParm3);
  173. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter 3 failed\n"))
  174. goto Exit;
  175. rc = SQLBindParameter(hstmt, 4, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_INTEGER, 10, 0, &sParm3, 0, &cbParm4);
  176. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter 4 failed\n"))
  177. goto Exit;
  178. rc = SQLBindParameter (hstmt, 5, SQL_PARAM_OUTPUT, SQL_C_WCHAR, SQL_VARCHAR, sizeof(scharW), 0, scharW, sizeof(scharW), &cbParm6);
  179. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter 5 failed\n"))
  180. goto Exit;
  181. /* Exeute the prepared stored procedure */
  182. rc = SQLExecute(hstmt);
  183. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecute failed\n"))
  184. goto Exit;
  185. len = strlen("123456789abcdefghijklmnopqrstuvwxyz1234567890123456789012345678901234567890 ");
  186. if( (sParm2 != sParm1) || (10 != sParm3) || (wcscmp(L"blob_test", scharW)) || (cbParm3 != len) )
  187. {
  188. fprintf(stdout, "\n 1st Data compare failed!");
  189. goto Exit;
  190. }
  191. else
  192. {
  193. fprintf(stdout, "\n 1st Data compare successful");
  194. }
  195. /* Reset the parameters */
  196. rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
  197. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
  198. goto Exit;
  199. /* Reset variables */
  200. sParm1 = 0;
  201. cbParm6 = cbParm1 = SQL_NTS;
  202. cbParm3 = SQL_NULL_DATA;
  203. /*********************************************************************/
  204. /* tests involving procedures that has OUT/INOUT params no RETURN values */
  205. /*********************************************************************/
  206. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop procedure out_param_blob;", SQL_NTS);
  207. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop table tab_blob1;", SQL_NTS);
  208. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"create table tab_blob1(c_blob BLOB, c_int INTEGER, c_char varchar(20));", SQL_NTS);
  209. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  210. goto Exit;
  211. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"insert into tab_blob1 values(filetoblob('insert.txt', 'c'), 10, 'blob_test1');", SQL_NTS);
  212. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  213. goto Exit;
  214. rc = SQLExecDirectW(hstmt, L"CREATE PROCEDURE out_param_blob(OUT blobparam BLOB, OUT intparam int, OUT charparam varchar(20)) \n"
  215. L"select c_blob, c_int, c_char into blobparam, intparam, charparam from tab_blob1; \n"
  216. L"end procedure; ",
  217. SQL_NTS);
  218. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  219. goto Exit;
  220. rc = SQLPrepareW(hstmt, (SQLWCHAR *) L"{call out_param_blob(?, ?, ?)}", SQL_NTS);
  221. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  222. goto Exit;
  223. /* Bind the parameters */
  224. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY, SQL_LONGVARBINARY, sizeof(blob_bufferW), 0, blob_bufferW, sizeof(blob_bufferW), &cbParm3);
  225. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter 1 failed\n"))
  226. goto Exit;
  227. rc = SQLBindParameter(hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_INTEGER, 3, 0, &sParm1, 0, &cbParm1);
  228. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter 2 failed\n"))
  229. goto Exit;
  230. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_WCHAR, SQL_VARCHAR, sizeof(scharW), 0, scharW, sizeof(scharW), &cbParm6);
  231. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter 3 failed\n"))
  232. goto Exit;
  233. rc = SQLExecute(hstmt);
  234. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecute failed\n"))
  235. goto Exit;
  236. /* The below data has been inserted (from insert.txt file), hence verify(lenth) the OUT value against the same */
  237. len = strlen("123456789abcdefghijklmnopqrstuvwxyz1234567890123456789012345678901234567890 ");
  238. if( (10 != sParm1) || (wcscmp(L"blob_test1", scharW)) || (cbParm3 != len) )
  239. {
  240. fprintf(stdout, "\n 2nd Data compare failed!");
  241. goto Exit;
  242. }
  243. else
  244. {
  245. fprintf(stdout, "\n 2nd Data compare successful");
  246. }
  247. rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
  248. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
  249. goto Exit;
  250. /* Reset variables */
  251. sParm1 = 0;
  252. cbParm6 = cbParm1 = SQL_NTS;
  253. cbParm3 = SQL_NULL_DATA;
  254. /*********************************************************************/
  255. /* tests involving procedures that has OUT/INOUT params no RETURN value and with NULL data for BLOB */
  256. /*********************************************************************/
  257. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop procedure out_param_blob1;", SQL_NTS);
  258. SQLExecDirectW(hstmt, (SQLWCHAR *) L"drop table tab_blob2;", SQL_NTS);
  259. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"create table tab_blob2(c_blob BLOB, c_int INTEGER, c_char varchar(20));", SQL_NTS);
  260. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  261. goto Exit;
  262. rc = SQLExecDirectW(hstmt, (SQLWCHAR *) L"insert into tab_blob2 values(filetoblob('insert.txt', 'c'), 10, 'blob_test2');", SQL_NTS);
  263. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  264. goto Exit;
  265. rc = SQLExecDirectW(hstmt, L"CREATE PROCEDURE out_param_blob1(OUT blobparam BLOB, OUT intparam int, OUT charparam varchar(20)) \n"
  266. L"select c_int, c_char into intparam, charparam from tab_blob2; \n"
  267. L"let blobparam = null; \n"
  268. L"end procedure; ",
  269. SQL_NTS);
  270. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  271. goto Exit;
  272. rc = SQLPrepareW(hstmt, (SQLWCHAR *) L"{call out_param_blob1(?, ?, ?)}", SQL_NTS);
  273. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLPrepare failed\n"))
  274. goto Exit;
  275. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY, SQL_LONGVARBINARY, sizeof(blob_bufferW), 0, blob_bufferW, sizeof(blob_bufferW), &cbParm3);
  276. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter 1 failed\n"))
  277. goto Exit;
  278. rc = SQLBindParameter(hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_LONG, SQL_INTEGER, 3, 0, &sParm1, 0, &cbParm1);
  279. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter 2 failed\n"))
  280. goto Exit;
  281. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_WCHAR, SQL_VARCHAR, sizeof(scharW), 0, scharW, sizeof(scharW), &cbParm6);
  282. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter 3 failed\n"))
  283. goto Exit;
  284. rc = SQLExecute(hstmt);
  285. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecute failed\n"))
  286. goto Exit;
  287. if( (10 != sParm1) || (wcscmp(L"blob_test2", scharW)) || (cbParm3 != SQL_NULL_DATA) )
  288. {
  289. fprintf(stdout, "\n 3rd Data compare failed!");
  290. goto Exit;
  291. }
  292. else
  293. {
  294. fprintf(stdout, "\n 3rd Data compare successful");
  295. }
  296. Exit:
  297. /* CLEANUP: Close the statement handles
  298. ** Free the statement handles
  299. ** Disconnect from the datasource
  300. ** Free the connection and environment handles
  301. ** Exit
  302. */
  303. SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  304. SQLDisconnect(hdbc);
  305. SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  306. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  307. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  308. in = getchar ();
  309. return (rc);
  310. }