loselectW.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2013 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: loselectW.c (Unicode counterpart of loselect.c)
  14. *
  15. * Description: To retrieve a smart large object stored in the database
  16. * -- the column retrieved is the 'advert' column of the
  17. * table 'item' (datatype - CLOB)
  18. *
  19. ***************************************************************************
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef NO_WIN32
  25. #include <io.h>
  26. #include <windows.h>
  27. #include <conio.h>
  28. #endif /*NO_WIN32*/
  29. #include "infxcli.h"
  30. #define BUFFER_LENW 120
  31. #define ERRMSG_LEN 200
  32. SQLWCHAR defDsnW[] = L"odbc_demo";
  33. SQLCHAR defDsn[] = "odbc_demo";
  34. SQLINTEGER checkError (SQLRETURN rc,
  35. SQLSMALLINT handleType,
  36. SQLHANDLE handle,
  37. SQLCHAR* errmsg)
  38. {
  39. SQLRETURN retcode = SQL_SUCCESS;
  40. SQLSMALLINT errNum = 1;
  41. SQLWCHAR sqlStateW[6];
  42. SQLCHAR *sqlState;
  43. SQLINTEGER nativeError;
  44. SQLWCHAR errMsgW[ERRMSG_LEN];
  45. SQLCHAR *errMsg;
  46. SQLSMALLINT textLengthPtr;
  47. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  48. {
  49. while (retcode != SQL_NO_DATA)
  50. {
  51. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  52. if (retcode == SQL_INVALID_HANDLE)
  53. {
  54. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  55. return 1;
  56. }
  57. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  58. {
  59. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  60. ;
  61. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  62. + sizeof(char));
  63. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  64. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  65. + sizeof(char));
  66. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  67. }
  68. errNum++;
  69. }
  70. fprintf (stderr, "%s\n", errmsg);
  71. return 1; /* all errors on this handle have been reported */
  72. }
  73. else
  74. return 0; /* no errors to report */
  75. }
  76. int main (long argc,
  77. char* argv[])
  78. {
  79. /* Declare variables
  80. */
  81. /* Handles */
  82. SQLHDBC hdbc;
  83. SQLHENV henv;
  84. SQLHSTMT hstmt;
  85. /* Smart large object file descriptor */
  86. SQLINTEGER lofd;
  87. SQLLEN lofd_valsize = 0;
  88. /* Smart large object pointer structure */
  89. SQLWCHAR* loptr_bufferW;
  90. SQLSMALLINT loptr_size;
  91. SQLLEN loptr_valsize = 0;
  92. /* Smart large object status structure */
  93. SQLWCHAR* lostat_bufferW;
  94. SQLSMALLINT lostat_size;
  95. SQLLEN lostat_valsize = 0;
  96. /* Smart large object data */
  97. SQLCHAR* lo_data;
  98. SQLLEN lo_data_valsize = 0;
  99. /* Miscellaneous variables */
  100. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  101. SQLRETURN rc = 0;
  102. SQLINTEGER in;
  103. SQLWCHAR verInfoBufferW[BUFFER_LENW];
  104. SQLSMALLINT verInfoLen;
  105. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT advert FROM item WHERE item_num = 1004";
  106. SQLINTEGER mode = LO_RDONLY;
  107. SQLINTEGER lo_size;
  108. SQLLEN cbMode = 0, cbLoSize = 0;
  109. int lenArgv1;
  110. /* STEP 1. Get data source name from command line (or use default)
  111. ** Allocate the environment handle and set ODBC version
  112. ** Allocate the connection handle
  113. ** Establish the database connection
  114. ** Get version information from the database server
  115. ** -- if version < 9.x (not UDO enabled), exit with error message
  116. ** Allocate the statement handle
  117. */
  118. if (argc != 2)
  119. {
  120. /* Use default dsnW - odbc_demo */
  121. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  122. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  123. + sizeof(SQLWCHAR) );
  124. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  125. }
  126. else
  127. {
  128. /* Use specified dsnW */
  129. lenArgv1 = strlen((char *)argv[1]);
  130. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  131. + sizeof(SQLWCHAR));
  132. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  133. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  134. }
  135. /* Allocate the Environment handle */
  136. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  137. if (rc != SQL_SUCCESS)
  138. {
  139. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  140. return (1);
  141. }
  142. /* Set the ODBC version to 3.0 */
  143. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  144. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  145. return (1);
  146. /* Allocate the connection handle */
  147. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  148. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  149. return (1);
  150. /* Establish the database connection */
  151. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  152. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!\n"))
  153. return (1);
  154. /* Get version information from the database server
  155. If version < 9.x (not UDO enabled), exit with error message */
  156. rc = SQLGetInfoW (hdbc, SQL_DBMS_VER, verInfoBufferW, BUFFER_LENW, &verInfoLen);
  157. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  158. return 1;
  159. if ((wcsncmp ((SQLWCHAR *) verInfoBufferW, L"09", 2)) < 0)
  160. {
  161. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  162. return 1;
  163. }
  164. /* Allocate the statement handle */
  165. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  166. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  167. return (1);
  168. fprintf (stdout, "STEP 1 done...connected to database\n");
  169. /* STEP 2. Select a smart-large object from the database
  170. ** -- the select statement executed is -
  171. ** "SELECT advert FROM item WHERE item_num = 1004"
  172. */
  173. /* Execute the select statement */
  174. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  175. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  176. goto Exit;
  177. fprintf (stdout, "STEP 2 done...select statement executed...smart large object retrieved from the databse\n");
  178. /* STEP 3. Get the size of the smart large object pointer structure
  179. ** Allocate a buffer to hold the structure.
  180. ** Get the smart large object pointer structure from the database
  181. ** Close the result set cursor
  182. */
  183. /* Get the size of the smart large object pointer structure */
  184. rc = SQLGetInfo (hdbc, SQL_INFX_LO_PTR_LENGTH, &loptr_size, sizeof(loptr_size), NULL);
  185. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 3 -- SQLGetInfo failed\n"))
  186. goto Exit;
  187. /* Allocate a buffer to hold the smart large object pointer structure */
  188. loptr_bufferW = malloc (loptr_size);
  189. /* Bind the smart large object pointer structure buffer allocated to the column
  190. in the result set & fetch it from the database */
  191. rc = SQLBindCol (hstmt, 1, SQL_C_BINARY, loptr_bufferW, loptr_size, &loptr_valsize);
  192. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
  193. goto Exit;
  194. rc = SQLFetch (hstmt);
  195. if (rc == SQL_NO_DATA_FOUND)
  196. {
  197. fprintf (stdout, "No Data Found\nExiting!!\n");
  198. goto Exit;
  199. }
  200. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  201. goto Exit;
  202. /* Close the result set cursor */
  203. rc = SQLCloseCursor (hstmt);
  204. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
  205. goto Exit;
  206. fprintf (stdout, "STEP 3 done...smart large object pointer structure fetched from the database\n");
  207. /* STEP 4. Use the smart large object's pointer structure to open it
  208. ** and obtain the smart large object file descriptor.
  209. ** Reset the statement parameters
  210. */
  211. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_LONG,
  212. SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  213. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  214. goto Exit;
  215. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  216. SQL_INFX_UDT_FIXED, (UDWORD)loptr_size, 0, loptr_bufferW,
  217. loptr_size, &loptr_valsize);
  218. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 2)\n"))
  219. goto Exit;
  220. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_LONG,
  221. SQL_INTEGER, (UDWORD)0, 0, &mode, sizeof(mode), &cbMode);
  222. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 3)\n"))
  223. goto Exit;
  224. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{? = call ifx_lo_open(?, ?)}", SQL_NTS);
  225. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  226. goto Exit;
  227. /* Reset the statement parameters */
  228. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  229. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLFreeStmt failed\n"))
  230. goto Exit;
  231. fprintf (stdout, "STEP 4 done...smart large object opened... file descriptor obtained\n");
  232. /* STEP 5. Get the size of the smart large object status structure
  233. ** Allocate a buffer to hold the structure.
  234. ** Get the smart large object status structure from the database
  235. ** Reset the statement parameters
  236. */
  237. /* Get the size of the smart large object status structure */
  238. rc = SQLGetInfo (hdbc, SQL_INFX_LO_STAT_LENGTH, &lostat_size,
  239. sizeof(lostat_size), NULL);
  240. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 5 -- SQLGetInfo failed\n"))
  241. goto Exit;
  242. /* Allocate a buffer to hold the smart large object status structure. */
  243. lostat_bufferW = malloc(lostat_size);
  244. /* Get the smart large object status structure from the database. */
  245. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG,
  246. SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  247. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  248. goto Exit;
  249. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT_OUTPUT,
  250. SQL_C_BINARY, SQL_INFX_UDT_FIXED, (UDWORD)lostat_size, 0,
  251. lostat_bufferW, lostat_size, &lostat_valsize);
  252. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 2)\n"))
  253. goto Exit;
  254. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_stat(?, ?)}",
  255. SQL_NTS);
  256. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  257. goto Exit;
  258. /* Reset the statement parameters */
  259. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  260. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLFreeStmt failed\n"))
  261. goto Exit;
  262. fprintf (stdout, "STEP 5 done...smart large object status structure fetched from the database\n");
  263. /* STEP 6. Use the smart large object's status structure to get the size
  264. ** of the smart large object
  265. ** Reset the statement parameters
  266. */
  267. /* Use the smart large object status structure to get the size
  268. of the smart large object. */
  269. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  270. SQL_INFX_UDT_FIXED, (UDWORD)lostat_size, 0, lostat_bufferW,
  271. lostat_size, &lostat_valsize);
  272. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
  273. goto Exit;
  274. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_LONG,
  275. SQL_BIGINT, (UDWORD)0, 0, &lo_size, sizeof(lo_size), &cbLoSize);
  276. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
  277. goto Exit;
  278. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_stat_size(?, ?)}",
  279. SQL_NTS);
  280. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  281. goto Exit;
  282. /* Reset the statement parameters */
  283. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  284. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLFreeStmt failed\n"))
  285. goto Exit;
  286. fprintf (stdout, "STEP 6 done...smart large object size = %ld bytes\n", lo_size);
  287. /* STEP 7. Allocate a buffer to hold the smart large object's data
  288. ** Read the smart large object's data using its file descriptor
  289. ** Null-terminate the last byte of the smart large-object's data
  290. ** Print out the contents of the smart large object
  291. ** Reset the statement parameters
  292. */
  293. /* Allocate a buffer to hold the smart large object's data chunks */
  294. lo_data = malloc (lo_size + 1);
  295. /* Read the smart large object's data */
  296. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG,
  297. SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  298. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed (param 1)\n"))
  299. goto Exit;
  300. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_OUTPUT, SQL_C_CHAR,
  301. SQL_CHAR, lo_size, 0, lo_data, lo_size, &lo_data_valsize);
  302. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed (param 2)\n"))
  303. goto Exit;
  304. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_read(?, ?)}", SQL_NTS);
  305. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLExecDirect failed\n"))
  306. goto Exit;
  307. /* Null-terminate the last byte of the smart large objects data */
  308. lo_data[lo_size] = '\0';
  309. /* Print the contents of the smart large object */
  310. /*
  311. lo_data = (SQLCHAR *) malloc (wcslen(lo_dataW) + sizeof(char));
  312. wcstombs( (char *) lo_data, lo_dataW, wcslen(lo_dataW)
  313. + sizeof(char));
  314. */
  315. fprintf (stdout, "Smart large object contents are.....\n\n\n%s\n\n\n", lo_data);
  316. /* Reset the statement parameters */
  317. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  318. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLFreeStmt failed\n"))
  319. goto Exit;
  320. fprintf (stdout, "STEP 7 done...smart large object read completely\n");
  321. /* STEP 8. Close the smart large object.
  322. */
  323. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG,
  324. SQL_INTEGER, (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  325. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLBindParameter failed\n"))
  326. goto Exit;
  327. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_close(?)}",
  328. SQL_NTS);
  329. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLExecDirect failed\n"))
  330. goto Exit;
  331. fprintf (stdout, "STEP 8 done...smart large object closed\n");
  332. /* STEP 9. Free the allocated buffers */
  333. free (loptr_bufferW);
  334. free (lostat_bufferW);
  335. free (lo_data);
  336. fprintf (stdout, "STEP 9 done...smart large object buffers freed\n");
  337. Exit:
  338. /* CLEANUP: Close the statement handle
  339. ** Free the statement handle
  340. ** Disconnect from the datasource
  341. ** Free the connection and environment handles
  342. ** Exit
  343. */
  344. /* Close the statement handle */
  345. SQLFreeStmt (hstmt, SQL_CLOSE);
  346. /* Free the statement handle */
  347. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  348. /* Disconnect from the data source */
  349. SQLDisconnect (hdbc);
  350. /* Free the environment handle and the database connection handle */
  351. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  352. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  353. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  354. in = getchar ();
  355. return (rc);
  356. }