rsetinfo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * (C) Copyright IBM Corporation 1997, 2004 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: rsetinfo.c
  14. *
  15. * Description: To obtain information about the result set returned by
  16. * a query and display additional information about the
  17. * data returned. The information displayed is -
  18. * 1. Number of columns in the result set
  19. * 2. Name, datatype, size and nullability of each column
  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 20
  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. /* Miscellaneous variables */
  76. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  77. SQLRETURN rc = 0;
  78. SQLUINTEGER in;
  79. SQLSMALLINT colCount;
  80. SQLSMALLINT colNum;
  81. SQLCHAR colName[BUFFER_LEN];
  82. SQLSMALLINT colNameLength = 0;
  83. SQLSMALLINT colDataType;
  84. SQLULEN colSize;
  85. SQLSMALLINT colDecimalDigits;
  86. SQLSMALLINT colNullable;
  87. SQLCHAR* selectStmt = (SQLCHAR *) "select * from orders";
  88. /* STEP 1. Get data source name from command line (or use default)
  89. ** Allocate the environment handle and set ODBC version
  90. ** Allocate the connection handle
  91. ** Establish the database connection
  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!!");
  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!!"))
  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!!"))
  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!!"))
  125. return (1);
  126. /* Allocate the statement handle */
  127. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  128. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  129. return (1);
  130. fprintf (stdout, "STEP 1 done...connected to database\n");
  131. /* STEP 2. Execute the query
  132. ** Retrieve the results
  133. */
  134. /* Execute the query */
  135. rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
  136. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  137. goto Exit;
  138. /* Get the result set */
  139. rc = SQLFetch (hstmt);
  140. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
  141. goto Exit;
  142. fprintf (stdout, "STEP 2 done...query executed and results retrieved\n");
  143. /* STEP 3. Get the following information about the result set
  144. ** -- Number of columns
  145. ** -- For each column, get its name, datatype, size
  146. ** and nullability
  147. */
  148. rc = SQLNumResultCols (hstmt, &colCount);
  149. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLNumResultColumns failed\n"))
  150. goto Exit;
  151. fprintf (stdout, "Number of columns in the result set = %d\nResult Set columns are...\n\n", colCount);
  152. for (colNum = 1; colNum <= colCount; colNum++)
  153. {
  154. rc = SQLDescribeCol (hstmt, colNum, colName, BUFFER_LEN, &colNameLength, &colDataType, &colSize, &colDecimalDigits, &colNullable);
  155. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLDescribeCol failed\n"))
  156. goto Exit;
  157. fprintf (stdout, "Column Name: %s;\t", colName);
  158. /* Find the columns datatype and print it */
  159. switch (colDataType)
  160. {
  161. case SQL_CHAR:
  162. fprintf (stdout, "Data type: SQL_CHAR;\t");
  163. break;
  164. case SQL_NUMERIC:
  165. fprintf (stdout, "Data type: SQL_NUMERIC;\t");
  166. break;
  167. case SQL_DECIMAL:
  168. fprintf (stdout, "Data type: SQL_DECIMAL;\t");
  169. break;
  170. case SQL_INTEGER:
  171. fprintf (stdout, "Data type: SQL_INTEGER; \t");
  172. break;
  173. case SQL_SMALLINT:
  174. fprintf (stdout, "Data type: SQL_SMALLINT;\t");
  175. break;
  176. case SQL_FLOAT:
  177. fprintf (stdout, "Data type: SQL_FLOAT;\t");
  178. break;
  179. case SQL_REAL:
  180. fprintf (stdout, "Data type: SQL_REAL;\t");
  181. break;
  182. case SQL_DOUBLE:
  183. fprintf (stdout, "Data type: SQL_DOUBLE;\t");
  184. break;
  185. case SQL_DATETIME:
  186. fprintf (stdout, "Data type: SQL_DATETIME;\t");
  187. break;
  188. case SQL_VARCHAR:
  189. fprintf (stdout, "Data type: SQL_VARCHAR;\t");
  190. break;
  191. case SQL_TYPE_DATE:
  192. fprintf (stdout, "Data type: SQL_TYPE_DATE;\t");
  193. break;
  194. case SQL_TYPE_TIME:
  195. fprintf (stdout, "Data type: SQL_TYPE_TIME;\t");
  196. break;
  197. case SQL_TYPE_TIMESTAMP:
  198. fprintf (stdout, "Data type: SQL_TYPE_TIMESTAMP;\t");
  199. break;
  200. case SQL_INTERVAL:
  201. fprintf (stdout, "Data type: SQL_INTERVAL;\t");
  202. break;
  203. case SQL_TIMESTAMP:
  204. fprintf (stdout, "Data type: SQL_TIMESTAMP;\t");
  205. break;
  206. case SQL_LONGVARCHAR:
  207. fprintf (stdout, "Data type: SQL_LONGVARCHAR;\t");
  208. break;
  209. case SQL_BINARY:
  210. fprintf (stdout, "Data type: SQL_BINARY;\t");
  211. break;
  212. case SQL_VARBINARY:
  213. fprintf (stdout, "Data type: SQL_VARBINARY;\t");
  214. break;
  215. case SQL_LONGVARBINARY:
  216. fprintf (stdout, "Data type: SQL_LONGVARBINARY;\t");
  217. break;
  218. case SQL_BIGINT:
  219. fprintf (stdout, "Data type: SQL_BIGINT;\t");
  220. break;
  221. case SQL_TINYINT:
  222. fprintf (stdout, "Data type: SQL_TINYINT;\t");
  223. break;
  224. case SQL_BIT:
  225. fprintf (stdout, "Data type: SQL_BIT;\t");
  226. break;
  227. case SQL_UNKNOWN_TYPE:
  228. default:
  229. fprintf (stdout, "Data type: UNKNOWN;\t");
  230. }
  231. /* format the rest of the columns information */
  232. fprintf (stdout, "Column Size: %d;\t", colSize);
  233. switch (colNullable)
  234. {
  235. case SQL_NO_NULLS:
  236. fprintf (stdout, "Allows NULLS: NO\n");
  237. break;
  238. case SQL_NULLABLE:
  239. fprintf (stdout, "Allows NULLS: YES\n");
  240. break;
  241. case SQL_NULLABLE_UNKNOWN:
  242. fprintf (stdout, "Allows NULLS: UNKNOWN\n");
  243. break;
  244. }
  245. }
  246. fprintf (stdout, "\n\nSTEP 3 done...result set information displayed\n");
  247. Exit:
  248. /* CLEANUP: Close the statement handle
  249. ** Free the statement handle
  250. ** Disconnect from the datasource
  251. ** Free the connection and environment handles
  252. ** Exit
  253. */
  254. /* Close the statement handle */
  255. SQLFreeStmt (hstmt, SQL_CLOSE);
  256. /* Free the statement handle */
  257. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  258. /* Disconnect from the data source */
  259. SQLDisconnect (hdbc);
  260. /* Free the environment handle and the database connection handle */
  261. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  262. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  263. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  264. in = getchar ();
  265. return (rc);
  266. }