loselect.c 16 KB

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