OutInOutParamBlob.c 15 KB

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