loinfo.c 18 KB

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