distselW.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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: distselW.c (Unicode counterpart of distsel.c)
  14. *
  15. * Description: To select data from a column that uses a DISTINCT
  16. * datatype
  17. * -- the column selected is 'unit_price' of the 'item'
  18. * table, which is of distinct type DOLLAR based on
  19. * decimal
  20. *
  21. *
  22. ***************************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifndef NO_WIN32
  28. #include <io.h>
  29. #include <windows.h>
  30. #include <conio.h>
  31. #endif /*NO_WIN32*/
  32. #include "infxcli.h"
  33. #define BUFFER_LENW 120
  34. #define ERRMSG_LEN 200
  35. SQLWCHAR defDsnW[] = L"odbc_demo";
  36. SQLCHAR defDsn[] = "odbc_demo";
  37. SQLINTEGER checkError (SQLRETURN rc,
  38. SQLSMALLINT handleType,
  39. SQLHANDLE handle,
  40. SQLCHAR* errmsg)
  41. {
  42. SQLRETURN retcode = SQL_SUCCESS;
  43. SQLSMALLINT errNum = 1;
  44. SQLWCHAR sqlStateW[6];
  45. SQLCHAR *sqlState;
  46. SQLINTEGER nativeError;
  47. SQLWCHAR errMsgW[ERRMSG_LEN];
  48. SQLCHAR *errMsg;
  49. SQLSMALLINT textLengthPtr;
  50. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  51. {
  52. while (retcode != SQL_NO_DATA)
  53. {
  54. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, 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. {
  62. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  63. ;
  64. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  65. + sizeof(char));
  66. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  67. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  68. + sizeof(char));
  69. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  70. }
  71. errNum++;
  72. }
  73. fprintf (stderr, "%s\n", errmsg);
  74. return 1; /* all errors on this handle have been reported */
  75. }
  76. else
  77. return 0; /* no errors to report */
  78. }
  79. int main (long argc,
  80. char* argv[])
  81. {
  82. /* Declare variables
  83. */
  84. /* Handles */
  85. SQLHDBC hdbc;
  86. SQLHENV henv;
  87. SQLHSTMT hstmt;
  88. /* Miscellaneous variables */
  89. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  90. SQLRETURN rc = 0;
  91. SQLINTEGER in;
  92. SQLWCHAR verInfoBufferW[BUFFER_LENW];
  93. SQLSMALLINT verInfoLen;
  94. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT item_num, description, unit_price FROM item";
  95. SQLCHAR *description;
  96. SQLWCHAR descriptionW[BUFFER_LENW];
  97. SQLLEN item_num, cbItemNum, cbDescription, cbUnitPrice;
  98. SQLREAL unit_price;
  99. int lenArgv1;
  100. /* STEP 1. Get data source name from command line (or use default)
  101. ** Allocate the environment handle and set ODBC version
  102. ** Allocate the connection handle
  103. ** Establish the database connection
  104. ** Get version information from the database server
  105. ** -- if version < 9.x (not UDO enabled), exit with error message
  106. ** Allocate the statement handle
  107. */
  108. /* If(dsn is not explicitly passed in as arg) */
  109. if (argc != 2)
  110. {
  111. /* Use default dsnW - odbc_demo */
  112. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  113. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  114. + sizeof(SQLWCHAR) );
  115. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  116. }
  117. else
  118. {
  119. /* Use specified dsnW */
  120. lenArgv1 = strlen((char *)argv[1]);
  121. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  122. + sizeof(SQLWCHAR));
  123. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  124. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  125. }
  126. /* Allocate the Environment handle */
  127. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  128. if (rc != SQL_SUCCESS)
  129. {
  130. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  131. return (1);
  132. }
  133. /* Set the ODBC version to 3.0 */
  134. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  135. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  136. return (1);
  137. /* Allocate the connection handle */
  138. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  139. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  140. return (1);
  141. /* Establish the database connection */
  142. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  143. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!\n"))
  144. return (1);
  145. /* Get version information from the database server
  146. If version < 9.x (not UDO enabled), exit with error message */
  147. rc = SQLGetInfoW (hdbc, SQL_DBMS_VER, verInfoBufferW, BUFFER_LENW, &verInfoLen);
  148. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  149. return 1;
  150. if ((wcsncmp ((SQLWCHAR *) verInfoBufferW, L"09", 2)) < 0 )
  151. {
  152. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  153. return 1;
  154. }
  155. /* Allocate the statement handle */
  156. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  157. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  158. return (1);
  159. fprintf (stdout, "STEP 1 done...connected to database\n");
  160. /* STEP 2. Retrieve data from the 'item' table and display the results
  161. ** Close the result set cursor
  162. */
  163. /* Retrieve data from the 'item' table */
  164. fprintf (stdout, "Retrieving data from 'item' table\n\n");
  165. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  166. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  167. goto Exit;
  168. /* Bind the result set columns */
  169. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &item_num, 0, &cbItemNum);
  170. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
  171. goto Exit;
  172. rc = SQLBindCol (hstmt, 2, SQL_C_WCHAR, descriptionW, BUFFER_LENW, &cbDescription);
  173. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
  174. goto Exit;
  175. rc = SQLBindCol (hstmt, 3, SQL_C_FLOAT, &unit_price, 5, &cbUnitPrice);
  176. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 3)\n"))
  177. goto Exit;
  178. while (1)
  179. {
  180. /* Fetch the data */
  181. rc = SQLFetch (hstmt);
  182. if (rc == SQL_NO_DATA_FOUND)
  183. break;
  184. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
  185. goto Exit;
  186. /* Display the results */
  187. description = (SQLCHAR *) malloc (wcslen(descriptionW)
  188. + sizeof(char));
  189. wcstombs( (char *) description, descriptionW, wcslen(descriptionW)
  190. + sizeof(char));
  191. fprintf (stdout, "Item Num: %d, Unit_price (DISTINCT TYPE dollar): %5.2f, Description: %s\n", item_num, unit_price, description);
  192. }
  193. /* Close the result set cursor */
  194. rc = SQLCloseCursor(hstmt);
  195. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
  196. goto Exit;
  197. fprintf (stdout, "\nSTEP 2 done...data retrieved from 'item' table\n");
  198. Exit:
  199. /* CLEANUP: Close the statement handle
  200. ** Free the statement handle
  201. ** Disconnect from the datasource
  202. ** Free the connection and environment handles
  203. ** Exit
  204. */
  205. /* Close the statement handle */
  206. SQLFreeStmt (hstmt, SQL_CLOSE);
  207. /* Free the statement handle */
  208. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  209. /* Disconnect from the data source */
  210. SQLDisconnect (hdbc);
  211. /* Free the environment handle and the database connection handle */
  212. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  213. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  214. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  215. in = getchar ();
  216. return (rc);
  217. }