rsetinfoW.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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: rsetinfoW.c (Unicode counterpart of 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. SQLWCHAR defDsnW[] = L"odbc_demo";
  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. SQLWCHAR sqlStateW[6];
  44. SQLCHAR *sqlState;
  45. SQLINTEGER nativeError;
  46. SQLWCHAR errMsgW[ERRMSG_LEN];
  47. SQLCHAR *errMsg;
  48. SQLSMALLINT textLengthPtr;
  49. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  50. {
  51. while (retcode != SQL_NO_DATA)
  52. {
  53. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  54. if (retcode == SQL_INVALID_HANDLE)
  55. {
  56. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  57. return 1;
  58. }
  59. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  60. {
  61. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char));
  62. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  63. + sizeof(char));
  64. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  65. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  66. + sizeof(char));
  67. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  68. }
  69. errNum++;
  70. }
  71. fprintf (stderr, "%s\n", errmsg);
  72. return 1; /* all errors on this handle have been reported */
  73. }
  74. else
  75. return 0; /* no errors to report */
  76. }
  77. int main (long argc,
  78. char* argv[])
  79. {
  80. /* Declare variables
  81. */
  82. /* Handles */
  83. SQLHDBC hdbc;
  84. SQLHENV henv;
  85. SQLHSTMT hstmt;
  86. /* Miscellaneous variables */
  87. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  88. SQLRETURN rc = 0;
  89. SQLUINTEGER in;
  90. SQLSMALLINT colCount;
  91. SQLSMALLINT colNum;
  92. SQLCHAR *colName;
  93. SQLWCHAR colNameW[BUFFER_LEN];
  94. SQLSMALLINT colNameLength = 0;
  95. SQLSMALLINT colDataType;
  96. SQLUINTEGER colSize;
  97. SQLSMALLINT colDecimalDigits;
  98. SQLSMALLINT colNullable;
  99. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"select * from orders";
  100. int lenArgv1;
  101. /* STEP 1. Get data source name from command line (or use default)
  102. ** Allocate the environment handle and set ODBC version
  103. ** Allocate the connection handle
  104. ** Establish the database connection
  105. ** Allocate the statement handle
  106. */
  107. if (argc != 2)
  108. {
  109. /* Use default dsnW - odbc_demo */
  110. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  111. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  112. + sizeof(SQLWCHAR) );
  113. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  114. }
  115. else
  116. {
  117. /* Use specified dsnW */
  118. lenArgv1 = strlen((char *)argv[1]);
  119. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  120. + sizeof(SQLWCHAR));
  121. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  122. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  123. }
  124. /* Allocate the Environment handle */
  125. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  126. if (rc != SQL_SUCCESS)
  127. {
  128. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  129. return (1);
  130. }
  131. /* Set the ODBC version to 3.0 */
  132. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  133. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  134. return (1);
  135. /* Allocate the connection handle */
  136. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  137. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  138. return (1);
  139. /* Establish the database connection */
  140. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  141. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  142. return (1);
  143. /* Allocate the statement handle */
  144. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  145. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  146. return (1);
  147. fprintf (stdout, "STEP 1 done...connected to database\n");
  148. /* STEP 2. Execute the query
  149. ** Retrieve the results
  150. */
  151. /* Execute the query */
  152. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  153. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  154. goto Exit;
  155. /* Get the result set */
  156. rc = SQLFetch (hstmt);
  157. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
  158. goto Exit;
  159. fprintf (stdout, "STEP 2 done...query executed and results retrieved\n");
  160. /* STEP 3. Get the following information about the result set
  161. ** -- Number of columns
  162. ** -- For each column, get its name, datatype, size
  163. ** and nullability
  164. */
  165. rc = SQLNumResultCols (hstmt, &colCount);
  166. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLNumResultColumns failed\n"))
  167. goto Exit;
  168. fprintf (stdout, "Number of columns in the result set = %d\nResult Set columns are...\n\n", colCount);
  169. for (colNum = 1; colNum <= colCount; colNum++)
  170. {
  171. rc = SQLDescribeColW (hstmt, colNum, colNameW, BUFFER_LEN, &colNameLength, &colDataType, &colSize, &colDecimalDigits, &colNullable);
  172. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLDescribeCol failed\n"))
  173. goto Exit;
  174. colName = (SQLCHAR *) malloc (wcslen(colNameW) + sizeof(char));
  175. wcstombs( (char *) colName, colNameW, wcslen(colNameW)
  176. + sizeof(char));
  177. fprintf (stdout, "Column Name: %s;\t", colName);
  178. /* Find the columns datatype and print it */
  179. switch (colDataType)
  180. {
  181. case SQL_CHAR:
  182. fprintf (stdout, "Data type: SQL_CHAR;\t");
  183. break;
  184. case SQL_NUMERIC:
  185. fprintf (stdout, "Data type: SQL_NUMERIC;\t");
  186. break;
  187. case SQL_DECIMAL:
  188. fprintf (stdout, "Data type: SQL_DECIMAL;\t");
  189. break;
  190. case SQL_INTEGER:
  191. fprintf (stdout, "Data type: SQL_INTEGER; \t");
  192. break;
  193. case SQL_SMALLINT:
  194. fprintf (stdout, "Data type: SQL_SMALLINT;\t");
  195. break;
  196. case SQL_FLOAT:
  197. fprintf (stdout, "Data type: SQL_FLOAT;\t");
  198. break;
  199. case SQL_REAL:
  200. fprintf (stdout, "Data type: SQL_REAL;\t");
  201. break;
  202. case SQL_DOUBLE:
  203. fprintf (stdout, "Data type: SQL_DOUBLE;\t");
  204. break;
  205. case SQL_DATETIME:
  206. fprintf (stdout, "Data type: SQL_DATETIME;\t");
  207. break;
  208. case SQL_VARCHAR:
  209. fprintf (stdout, "Data type: SQL_VARCHAR;\t");
  210. break;
  211. case SQL_TYPE_DATE:
  212. fprintf (stdout, "Data type: SQL_TYPE_DATE;\t");
  213. break;
  214. case SQL_TYPE_TIME:
  215. fprintf (stdout, "Data type: SQL_TYPE_TIME;\t");
  216. break;
  217. case SQL_TYPE_TIMESTAMP:
  218. fprintf (stdout, "Data type: SQL_TYPE_TIMESTAMP;\t");
  219. break;
  220. case SQL_INTERVAL:
  221. fprintf (stdout, "Data type: SQL_INTERVAL;\t");
  222. break;
  223. case SQL_TIMESTAMP:
  224. fprintf (stdout, "Data type: SQL_TIMESTAMP;\t");
  225. break;
  226. case SQL_LONGVARCHAR:
  227. fprintf (stdout, "Data type: SQL_LONGVARCHAR;\t");
  228. break;
  229. case SQL_BINARY:
  230. fprintf (stdout, "Data type: SQL_BINARY;\t");
  231. break;
  232. case SQL_VARBINARY:
  233. fprintf (stdout, "Data type: SQL_VARBINARY;\t");
  234. break;
  235. case SQL_LONGVARBINARY:
  236. fprintf (stdout, "Data type: SQL_LONGVARBINARY;\t");
  237. break;
  238. case SQL_BIGINT:
  239. fprintf (stdout, "Data type: SQL_BIGINT;\t");
  240. break;
  241. case SQL_TINYINT:
  242. fprintf (stdout, "Data type: SQL_TINYINT;\t");
  243. break;
  244. case SQL_BIT:
  245. fprintf (stdout, "Data type: SQL_BIT;\t");
  246. break;
  247. case SQL_UNKNOWN_TYPE:
  248. default:
  249. fprintf (stdout, "Data type: UNKNOWN;\t");
  250. }
  251. /* format the rest of the columns information */
  252. fprintf (stdout, "Column Size: %d;\t", colSize);
  253. switch (colNullable)
  254. {
  255. case SQL_NO_NULLS:
  256. fprintf (stdout, "Allows NULLS: NO\n");
  257. break;
  258. case SQL_NULLABLE:
  259. fprintf (stdout, "Allows NULLS: YES\n");
  260. break;
  261. case SQL_NULLABLE_UNKNOWN:
  262. fprintf (stdout, "Allows NULLS: UNKNOWN\n");
  263. break;
  264. }
  265. }
  266. fprintf (stdout, "\n\nSTEP 3 done...result set information displayed\n");
  267. Exit:
  268. /* CLEANUP: Close the statement handle
  269. ** Free the statement handle
  270. ** Disconnect from the datasource
  271. ** Free the connection and environment handles
  272. ** Exit
  273. */
  274. /* Close the statement handle */
  275. SQLFreeStmt (hstmt, SQL_CLOSE);
  276. /* Free the statement handle */
  277. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  278. /* Disconnect from the data source */
  279. SQLDisconnect (hdbc);
  280. /* Free the environment handle and the database connection handle */
  281. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  282. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  283. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  284. in = getchar ();
  285. return (rc);
  286. }