rcselect.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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: rcselect.c
  14. *
  15. * Description: To retrieve row and collection data from the database
  16. * and display it. This example also illustrates the fact
  17. * that the same client functions can use row and
  18. * collection handles interchangeably
  19. *
  20. * The row/collection columns retrieved are
  21. * -- column 'address' of table 'customer' (datatype - ROW)
  22. * -- column 'contact_dates' of table 'customer'
  23. * (datatype - LIST)
  24. *
  25. ***************************************************************************
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifndef NO_WIN32
  31. #include <io.h>
  32. #include <windows.h>
  33. #include <conio.h>
  34. #endif /*NO_WIN32*/
  35. #include "infxcli.h"
  36. #define BUFFER_LEN 25
  37. #define ERRMSG_LEN 200
  38. SQLCHAR defDsn[] = "odbc_demo";
  39. SQLINTEGER checkError (SQLRETURN rc,
  40. SQLSMALLINT handleType,
  41. SQLHANDLE handle,
  42. SQLCHAR* errmsg)
  43. {
  44. SQLRETURN retcode = SQL_SUCCESS;
  45. SQLSMALLINT errNum = 1;
  46. SQLCHAR sqlState[6];
  47. SQLINTEGER nativeError;
  48. SQLCHAR errMsg[ERRMSG_LEN];
  49. SQLSMALLINT textLengthPtr;
  50. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  51. {
  52. while (retcode != SQL_NO_DATA)
  53. {
  54. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  55. if (retcode == SQL_INVALID_HANDLE)
  56. {
  57. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  58. return 1;
  59. }
  60. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  61. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  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. /*
  71. ** Executes the given select statement and assumes the results will be
  72. ** either rows or collections. The 'hrc' parameter may reference either
  73. ** a row or a collection. Rows and collection handles may often be used
  74. ** interchangeably.
  75. **
  76. ** Each row of the select statement will be fetched into the given row or
  77. ** collection handle. Then each field of the row or collection will be
  78. ** individually converted into a character buffer and displayed.
  79. **
  80. ** This function returns 0 if an error occurs, else returns 1
  81. **
  82. */
  83. SQLINTEGER do_select (SQLHDBC hdbc,
  84. SQLCHAR* select_str,
  85. HINFX_RC hrc)
  86. {
  87. SQLHSTMT hRCStmt;
  88. SQLHSTMT hSelectStmt;
  89. SQLRETURN rc = 0;
  90. SQLSMALLINT index, rownum;
  91. SQLSMALLINT position = SQL_INFX_RC_ABSOLUTE;
  92. SQLSMALLINT jump;
  93. SQLCHAR fname[BUFFER_LEN];
  94. SQLCHAR lname[BUFFER_LEN];
  95. SQLCHAR rc_data[BUFFER_LEN];
  96. SQLLEN cbFname = 0, cbLname = 0, cbHrc = 0;
  97. SQLLEN cbPosition = 0, cbJump = 0, cbRCData = 0;
  98. /* STEP A. Allocate the statement handles for the select statement and
  99. ** the statement used to retrieve the row/collection data
  100. */
  101. /* Allocate the statement handle */
  102. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hRCStmt );
  103. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step A -- Statement Handle Allocation failed for row/collection statement\nExiting!!"))
  104. return 0;
  105. /* Allocate the statement handle */
  106. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hSelectStmt );
  107. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step A -- Statement Handle Allocation failed for select statement\nExiting!!"))
  108. return 0;
  109. fprintf (stdout, "STEP A done...statement handles allocated\n");
  110. /* STEP B. Execut the select statement
  111. ** Bind the result set columns -
  112. ** -- col1 = fname
  113. ** col2 = lname
  114. ** col3 = row/collection data
  115. */
  116. /* Execute the select statement */
  117. rc = SQLExecDirect (hSelectStmt, select_str, SQL_NTS);
  118. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step B -- SQLExecDirect failed\n"))
  119. return 0;
  120. /* Bind the result set columns */
  121. rc = SQLBindCol (hSelectStmt, 1, SQL_C_CHAR, (SQLPOINTER) fname, BUFFER_LEN, &cbFname);
  122. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step B -- SQLBindCol failed for column 'fname'\n"))
  123. return 0;
  124. rc = SQLBindCol (hSelectStmt, 2, SQL_C_CHAR, (SQLPOINTER) lname, BUFFER_LEN, &cbLname);
  125. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step B -- SQLBindCol failed for column 'lname'\n"))
  126. return 0;
  127. rc = SQLBindCol (hSelectStmt, 3, SQL_C_BINARY, (SQLPOINTER) hrc, sizeof(HINFX_RC), &cbHrc);
  128. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step B -- SQLBindCol failed for row/collection column\n"))
  129. return 0;
  130. fprintf (stdout, "STEP B done...select statement executed and result set columns bound\n");
  131. /* STEP C. Retrieve the results
  132. */
  133. for (rownum = 1;; rownum++)
  134. {
  135. rc = SQLFetch (hSelectStmt);
  136. if (rc == SQL_NO_DATA_FOUND)
  137. {
  138. fprintf (stdout, "No data found...\n");
  139. break;
  140. }
  141. else if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step C -- SQLFetch failed\n"))
  142. return 0;
  143. fprintf(stdout, "Retrieving row number %d:\n\tfname -- %s\n\tlname -- %s\n\tRow/Collection Data --\n", rownum, fname, lname);
  144. /* For each row in the result set, display each field of the
  145. retrieved row/collection */
  146. for (index = 1;; index++)
  147. {
  148. strcpy((char *) rc_data, "<null>");
  149. /* Each value in the local row/collection will be fetched into a
  150. * character buffer and displayed using fprintf().
  151. */
  152. rc = SQLBindParameter (hRCStmt, 1, SQL_PARAM_OUTPUT, SQL_C_CHAR,
  153. SQL_CHAR, 0, 0, rc_data, BUFFER_LEN, &cbRCData);
  154. if (checkError (rc, SQL_HANDLE_STMT, hRCStmt, (SQLCHAR *) "Error in Step C -- SQLBindParameter failed (param 1)\n"))
  155. return 0;
  156. rc = SQLBindParameter (hRCStmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  157. SQL_INFX_RC_COLLECTION, sizeof(HINFX_RC), 0, hrc,
  158. sizeof(HINFX_RC), &cbHrc);
  159. if (checkError (rc, SQL_HANDLE_STMT, hRCStmt, (SQLCHAR *) "Error in Step C -- SQLBindParameter failed (param 2)\n"))
  160. return 0;
  161. rc = SQLBindParameter (hRCStmt, 3, SQL_PARAM_INPUT, SQL_C_SHORT,
  162. SQL_SMALLINT, 0, 0, &position, 0, &cbPosition);
  163. if (checkError (rc, SQL_HANDLE_STMT, hRCStmt, (SQLCHAR *) "Error in Step C -- SQLBindParameter failed (param 3)\n"))
  164. return 0;
  165. jump = index;
  166. rc = SQLBindParameter (hRCStmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT,
  167. SQL_SMALLINT, 0, 0, &jump, 0, &cbJump);
  168. if (checkError (rc, SQL_HANDLE_STMT, hRCStmt, (SQLCHAR *) "Error in Step C -- SQLBindParameter failed (param 4)\n"))
  169. return 0;
  170. rc = SQLExecDirect (hRCStmt, (SQLCHAR *) "{ ? = call ifx_rc_fetch( ?, ?, ? ) }", SQL_NTS);
  171. if (rc == SQL_NO_DATA_FOUND)
  172. {
  173. break;
  174. }
  175. else if (checkError (rc, SQL_HANDLE_STMT, hRCStmt, (SQLCHAR *) "Error in Step C -- SQLExecDirect failed\n"))
  176. return 0;
  177. /* Display retrieved row */
  178. fprintf(stdout, "\t\t%d: %s\n", index, rc_data);
  179. }
  180. }
  181. fprintf (stdout, "STEP C done...results retrieved\n");
  182. /* Free the statement handles */
  183. SQLFreeHandle (SQL_HANDLE_STMT, hSelectStmt);
  184. SQLFreeHandle (SQL_HANDLE_STMT, hRCStmt);
  185. return 1; /* no error */
  186. }
  187. /*
  188. * This function allocates the row and collection buffers, passes
  189. * them to the do_select() function, along with an appropriate select
  190. * statement and then frees all allocated handles.
  191. */
  192. int main (long argc,
  193. char* argv[])
  194. {
  195. /* Declare variables
  196. */
  197. /* Handles */
  198. SQLHDBC hdbc;
  199. SQLHENV henv;
  200. SQLHSTMT hstmt;
  201. HINFX_RC hrow, hlist;
  202. /* Miscellaneous variables */
  203. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  204. SQLRETURN rc = 0;
  205. SQLINTEGER in;
  206. SQLCHAR verInfoBuffer[BUFFER_LEN];
  207. SQLSMALLINT verInfoLen;
  208. SQLLEN data_size = SQL_NTS;
  209. SQLCHAR* listSelectStmt = (SQLCHAR *) "SELECT fname, lname, contact_dates FROM customer";
  210. SQLCHAR* rowSelectStmt = (SQLCHAR *) "SELECT fname, lname, address FROM customer";
  211. SQLLEN cbHlist = 0, cbHrow = 0;
  212. /* STEP 1. Get data source name from command line (or use default)
  213. ** Allocate environment handle and set ODBC version
  214. ** Allocate connection handle
  215. ** Establish the database connection
  216. ** Get version information from the database server
  217. ** -- if version < 9.x (not UDO enabled), exit with error message
  218. ** Allocate the statement handle
  219. */
  220. /* If(dsn is not explicitly passed in as arg) */
  221. if (argc != 2)
  222. {
  223. /* Use default dsn - odbc_demo */
  224. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  225. strcpy ((char *)dsn, (char *)defDsn);
  226. }
  227. else
  228. {
  229. /* Use specified dsn */
  230. strcpy ((char *)dsn, (char *)argv[1]);
  231. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  232. }
  233. /* Allocate the Environment handle */
  234. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  235. if (rc != SQL_SUCCESS)
  236. {
  237. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  238. return (1);
  239. }
  240. /* Set the ODBC version to 3.0 */
  241. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  242. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  243. return (1);
  244. /* Allocate the connection handle */
  245. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  246. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  247. return (1);
  248. /* Establish the database connection */
  249. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  250. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\n"))
  251. return (1);
  252. /* Get version information from the database server
  253. If version < 9.x (not UDO enabled), exit with error message */
  254. rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN, &verInfoLen);
  255. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  256. return 1;
  257. if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0 )
  258. {
  259. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  260. return 1;
  261. }
  262. /* Allocate the statement handle */
  263. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  264. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  265. return (1);
  266. fprintf (stdout, "STEP 1 done...connected to database\n");
  267. /* STEP 2. Allocate an unfixed collection handle
  268. ** Retrieve database rows containing a list
  269. ** Reset the statement parameters
  270. */
  271. /* Allocate an unfixed list handle */
  272. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  273. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0,
  274. &hlist, sizeof(HINFX_RC), &cbHlist);
  275. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 1) failed\n"))
  276. goto Exit;
  277. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  278. SQL_CHAR, 0, 0, (SQLCHAR *) "list", 0, &data_size);
  279. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 2) failed\n"))
  280. goto Exit;
  281. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{? = call ifx_rc_create(?)}", SQL_NTS);
  282. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  283. goto Exit;
  284. /* Retrieve databse rows containing a list */
  285. if (!do_select (hdbc, listSelectStmt, hlist))
  286. goto Exit;
  287. /* Reset the statement parameters */
  288. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  289. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
  290. goto Exit;
  291. fprintf (stdout, "STEP 2 done...list data retrieved\n");
  292. fprintf (stdout,"\nHit <Enter> to continue...");
  293. in = getchar ();
  294. /* STEP 3. Allocate an unfixed row handle
  295. ** Retrieve database rows containing a row
  296. ** Reset the statement parameters
  297. */
  298. /* Allocate an unfixed row handle */
  299. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  300. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0,
  301. &hrow, sizeof(HINFX_RC), &cbHrow);
  302. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 1) failed\n"))
  303. goto Exit;
  304. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  305. SQL_CHAR, 0, 0, (SQLCHAR *) "row", 0, &data_size);
  306. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 2) failed\n"))
  307. goto Exit;
  308. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{? = call ifx_rc_create(?)}", SQL_NTS);
  309. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  310. goto Exit;
  311. /* Retrieve databse rows containing a row */
  312. if (!do_select (hdbc, rowSelectStmt, hrow))
  313. goto Exit;
  314. /* Reset the statement parameters */
  315. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  316. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
  317. goto Exit;
  318. fprintf (stdout, "STEP 3 done...row data retrieved\n");
  319. /* STEP 4. Free the row and list handles
  320. */
  321. /* Free the row handle */
  322. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  323. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  324. sizeof(HINFX_RC), &cbHrow);
  325. rc = SQLExecDirect(hstmt, (SQLCHAR *)"{call ifx_rc_free(?)}", SQL_NTS);
  326. /* Free the list handle */
  327. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  328. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  329. sizeof(HINFX_RC), &cbHlist);
  330. rc = SQLExecDirect(hstmt, (SQLCHAR *)"{call ifx_rc_free(?)}", SQL_NTS);
  331. fprintf (stdout, "STEP 4 done...row and list handles freed\n");
  332. Exit:
  333. /* CLEANUP: Close the statement handle
  334. ** Free the statement handle
  335. ** Disconnect from the datasource
  336. ** Free the connection and environment handles
  337. ** Exit
  338. */
  339. /* Close the statement handle */
  340. SQLFreeStmt (hstmt, SQL_CLOSE);
  341. /* Free the statement handle */
  342. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  343. /* Disconnect from the data source */
  344. SQLDisconnect (hdbc);
  345. /* Free the environment handle and the database connection handle */
  346. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  347. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  348. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  349. in = getchar ();
  350. return (rc);
  351. }