distsel.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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: 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_LEN 25
  34. #define ERRMSG_LEN 200
  35. SQLCHAR defDsn[] = "odbc_demo";
  36. SQLINTEGER checkError (SQLRETURN rc,
  37. SQLSMALLINT handleType,
  38. SQLHANDLE handle,
  39. SQLCHAR* errmsg)
  40. {
  41. SQLRETURN retcode = SQL_SUCCESS;
  42. SQLSMALLINT errNum = 1;
  43. SQLCHAR sqlState[6];
  44. SQLINTEGER nativeError;
  45. SQLCHAR errMsg[ERRMSG_LEN];
  46. SQLSMALLINT textLengthPtr;
  47. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  48. {
  49. while (retcode != SQL_NO_DATA)
  50. {
  51. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  52. if (retcode == SQL_INVALID_HANDLE)
  53. {
  54. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  55. return 1;
  56. }
  57. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  58. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  59. errNum++;
  60. }
  61. fprintf (stderr, "%s\n", errmsg);
  62. return 1; /* all errors on this handle have been reported */
  63. }
  64. else
  65. return 0; /* no errors to report */
  66. }
  67. int main (long argc,
  68. char* argv[])
  69. {
  70. /* Declare variables
  71. */
  72. /* Handles */
  73. SQLHDBC hdbc;
  74. SQLHENV henv;
  75. SQLHSTMT hstmt;
  76. /* Miscellaneous variables */
  77. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  78. SQLRETURN rc = 0;
  79. SQLINTEGER in;
  80. SQLCHAR verInfoBuffer[BUFFER_LEN];
  81. SQLSMALLINT verInfoLen;
  82. SQLCHAR* selectStmt = (SQLCHAR *) "SELECT item_num, description, unit_price FROM item";
  83. SQLCHAR description[BUFFER_LEN];
  84. SQLLEN item_num, cbItemNum, cbDescription, cbUnitPrice;
  85. SQLREAL unit_price;
  86. /* STEP 1. Get data source name from command line (or use default)
  87. ** Allocate the environment handle and set ODBC version
  88. ** Allocate the connection handle
  89. ** Establish the database connection
  90. ** Get version information from the database server
  91. ** -- if version < 9.x (not UDO enabled), exit with error message
  92. ** Allocate the statement handle
  93. */
  94. /* If(dsn is not explicitly passed in as arg) */
  95. if (argc != 2)
  96. {
  97. /* Use default dsn - odbc_demo */
  98. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  99. strcpy ((char *)dsn, (char *)defDsn);
  100. }
  101. else
  102. {
  103. /* Use specified dsn */
  104. strcpy ((char *)dsn, (char *)argv[1]);
  105. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  106. }
  107. /* Allocate the Environment handle */
  108. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  109. if (rc != SQL_SUCCESS)
  110. {
  111. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  112. return (1);
  113. }
  114. /* Set the ODBC version to 3.0 */
  115. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  116. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  117. return (1);
  118. /* Allocate the connection handle */
  119. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  120. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  121. return (1);
  122. /* Establish the database connection */
  123. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  124. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!\n"))
  125. return (1);
  126. /* Get version information from the database server
  127. If version < 9.x (not UDO enabled), exit with error message */
  128. rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN, &verInfoLen);
  129. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  130. return 1;
  131. if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0 )
  132. {
  133. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  134. return 1;
  135. }
  136. /* Allocate the statement handle */
  137. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  138. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  139. return (1);
  140. fprintf (stdout, "STEP 1 done...connected to database\n");
  141. /* STEP 2. Retrieve data from the 'item' table and display the results
  142. ** Close the result set cursor
  143. */
  144. /* Retrieve data from the 'item' table */
  145. fprintf (stdout, "Retrieving data from 'item' table\n\n");
  146. rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
  147. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  148. goto Exit;
  149. /* Bind the result set columns */
  150. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &item_num, 0, &cbItemNum);
  151. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
  152. goto Exit;
  153. rc = SQLBindCol (hstmt, 2, SQL_C_CHAR, description, BUFFER_LEN, &cbDescription);
  154. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
  155. goto Exit;
  156. rc = SQLBindCol (hstmt, 3, SQL_C_FLOAT, &unit_price, 5, &cbUnitPrice);
  157. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 3)\n"))
  158. goto Exit;
  159. while (1)
  160. {
  161. /* Fetch the data */
  162. rc = SQLFetch (hstmt);
  163. if (rc == SQL_NO_DATA_FOUND)
  164. break;
  165. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
  166. goto Exit;
  167. /* Display the results */
  168. fprintf (stdout, "Item Num: %d, Unit_price (DISTINCT TYPE dollar): %5.2f, Description: %s\n", item_num, unit_price, description);
  169. }
  170. /* Close the result set cursor */
  171. rc = SQLCloseCursor(hstmt);
  172. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
  173. goto Exit;
  174. fprintf (stdout, "\nSTEP 2 done...data retrieved from 'item' table\n");
  175. Exit:
  176. /* CLEANUP: Close the statement handle
  177. ** Free the statement handle
  178. ** Disconnect from the datasource
  179. ** Free the connection and environment handles
  180. ** Exit
  181. */
  182. /* Close the statement handle */
  183. SQLFreeStmt (hstmt, SQL_CLOSE);
  184. /* Free the statement handle */
  185. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  186. /* Disconnect from the data source */
  187. SQLDisconnect (hdbc);
  188. /* Free the environment handle and the database connection handle */
  189. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  190. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  191. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  192. in = getchar ();
  193. return (rc);
  194. }