rcselectW.c 18 KB

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