catalogW.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2007 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: catalogW.c (Unicode counterpart of catalog.c)
  14. *
  15. * Description: To use catalog functions to obtain information about
  16. * the database tables and columns
  17. *
  18. *
  19. ***************************************************************************
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifndef NO_WIN32
  25. #include <io.h>
  26. #include <windows.h>
  27. #include <conio.h>
  28. #endif /*NO_WIN32*/
  29. #include "infxcli.h"
  30. #define BUFFER_LEN 130
  31. #define ERRMSG_LEN 200
  32. SQLWCHAR defDsnW[] = L"odbc_demo";
  33. SQLCHAR defDsn[] = "odbc_demo";
  34. SQLINTEGER checkError (SQLRETURN rc,
  35. SQLSMALLINT handleType,
  36. SQLHANDLE handle,
  37. SQLCHAR* errmsg)
  38. {
  39. SQLRETURN retcode = SQL_SUCCESS;
  40. SQLSMALLINT errNum = 1;
  41. SQLWCHAR sqlStateW[6];
  42. SQLCHAR *sqlState;
  43. SQLINTEGER nativeError;
  44. SQLWCHAR errMsgW[ERRMSG_LEN];
  45. SQLCHAR *errMsg;
  46. SQLSMALLINT textLengthPtr;
  47. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  48. {
  49. while (retcode != SQL_NO_DATA)
  50. {
  51. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, 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. {
  59. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  60. ;
  61. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  62. + sizeof(char));
  63. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  64. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  65. + sizeof(char));
  66. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  67. }
  68. errNum++;
  69. }
  70. fprintf (stderr, "%s\n", errmsg);
  71. return 1; /* all errors on this handle have been reported */
  72. }
  73. else
  74. return 0; /* no errors to report */
  75. }
  76. int main (long argc,
  77. char* argv[])
  78. {
  79. /* Declare variables
  80. */
  81. /* Handles */
  82. SQLHDBC hdbc;
  83. SQLHENV henv;
  84. SQLHSTMT hTableStmt;
  85. SQLHSTMT hColStmt;
  86. SQLHSTMT hPriKeyStmt;
  87. /* Miscellaneous variables */
  88. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  89. SQLRETURN rc = 0;
  90. SQLINTEGER in;
  91. SQLCHAR *tableName;
  92. SQLWCHAR tableNameW[BUFFER_LEN];
  93. SQLLEN cbTableName;
  94. SQLCHAR *colName;
  95. SQLWCHAR colNameW[BUFFER_LEN];
  96. SQLLEN cbColName;
  97. SQLCHAR *priKeyName;
  98. SQLWCHAR priKeyNameW[BUFFER_LEN];
  99. SQLLEN cbPriKeyName;
  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(dsn is not explicitly passed in as arg) */
  108. if (argc != 2)
  109. {
  110. /* Use default dsnW - odbc_demo */
  111. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  112. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  113. + sizeof(SQLWCHAR));
  114. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  115. }
  116. else
  117. {
  118. /* Use specified dsnW */
  119. lenArgv1 = strlen((char *)argv[1]);
  120. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  121. + sizeof(SQLWCHAR));
  122. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  123. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  124. }
  125. /* Allocate the Environment handle */
  126. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  127. if (rc != SQL_SUCCESS)
  128. {
  129. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  130. return (1);
  131. }
  132. /* Set the ODBC version to 3.0 */
  133. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
  134. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  135. return (1);
  136. /* Allocate the connection handle */
  137. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  138. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  139. return (1);
  140. /* Establish the database connection */
  141. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  142. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  143. return (1);
  144. /* Allocate the statement handles */
  145. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hTableStmt );
  146. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  147. return (1);
  148. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hColStmt );
  149. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  150. return (1);
  151. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hPriKeyStmt );
  152. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  153. return (1);
  154. fprintf (stdout, "STEP 1 done...connected to database\n");
  155. /* STEP 2. Call SQLTables to get table names from the database
  156. ** For each table in the database, call SQLColumns to get its
  157. ** columns and SQLPrimaryKeys to get the columns which comprise
  158. ** its primary key
  159. */
  160. /* Get the names of all the tables in the database */
  161. rc = SQLTablesW (hTableStmt, NULL, 0, NULL, 0, NULL, 0, (SQLWCHAR *) L"TABLE", SQL_NTS);
  162. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLTables failed\n"))
  163. goto Exit;
  164. /* Bind the result set column # 3 - 'TABLE_NAME' */
  165. rc = SQLBindCol (hTableStmt, 3, SQL_C_WCHAR, tableNameW, BUFFER_LEN, &cbTableName);
  166. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed\n"))
  167. goto Exit;
  168. /* Fetch the data */
  169. while (1)
  170. {
  171. rc = SQLFetch (hTableStmt);
  172. if (rc == SQL_NO_DATA_FOUND)
  173. break;
  174. else if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed for table\n"))
  175. goto Exit;
  176. /* Display the table name */
  177. tableName = (SQLCHAR *) malloc (wcslen(tableNameW) + sizeof(char));
  178. wcstombs( (char *) tableName, tableNameW, wcslen(tableNameW)
  179. + sizeof(char));
  180. fprintf (stdout, "Table Name %s\n", tableName);
  181. /* For this table, get all its columns */
  182. rc = SQLColumnsW (hColStmt, NULL, 0, NULL, 0, tableNameW, SQL_NTS, NULL, SQL_NTS);
  183. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLColumns failed\n"))
  184. goto Exit;
  185. /* Bind the result set column #4 - 'COLUMN_NAME' */
  186. rc = SQLBindCol (hColStmt, 4, SQL_C_WCHAR, colNameW, BUFFER_LEN, &cbColName);
  187. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed for column\n"))
  188. goto Exit;
  189. /* Fetch and display the column names */
  190. while (1)
  191. {
  192. rc = SQLFetch (hColStmt);
  193. if (rc == SQL_NO_DATA_FOUND)
  194. break;
  195. else if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed for column\n"))
  196. goto Exit;
  197. /* Display the results */
  198. colName = (SQLCHAR *) malloc (wcslen(colNameW) + sizeof(char));
  199. wcstombs( (char *) colName, colNameW, wcslen(colNameW)
  200. + sizeof(char));
  201. fprintf (stdout, "\tColumn: %s\n", colName);
  202. }
  203. /* For this table, get all its primary key columns */
  204. rc = SQLPrimaryKeysW (hPriKeyStmt, NULL, 0, NULL, 0, tableNameW, SQL_NTS);
  205. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLPrimaryKeys failed\n"))
  206. goto Exit;
  207. /* Bind the result set column #4 - 'COLUMN_NAME' */
  208. rc = SQLBindCol (hPriKeyStmt, 4, SQL_C_WCHAR, priKeyNameW, BUFFER_LEN, &cbPriKeyName);
  209. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed for primary key\n"))
  210. goto Exit;
  211. /* Fetch and display the column names */
  212. while (1)
  213. {
  214. rc = SQLFetch (hPriKeyStmt);
  215. if (rc == SQL_NO_DATA_FOUND)
  216. break;
  217. else if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed for column\n"))
  218. goto Exit;
  219. /* Display the results */
  220. priKeyName = (SQLCHAR *) malloc (wcslen(priKeyNameW) + sizeof(char));
  221. wcstombs( (char *) priKeyName, priKeyNameW, wcslen(priKeyNameW)
  222. + sizeof(char));
  223. fprintf (stdout, "\t\t Primary Key Column: %s\n", priKeyName);
  224. }
  225. /* Close the result set cursors for columns and primary keys */
  226. rc = SQLCloseCursor (hColStmt);
  227. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for column\n"))
  228. goto Exit;
  229. rc = SQLCloseCursor (hPriKeyStmt);
  230. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for primary key\n"))
  231. goto Exit;
  232. }
  233. /* Close the result set cursor for table */
  234. rc = SQLCloseCursor (hTableStmt);
  235. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for table\n"))
  236. goto Exit;
  237. fprintf (stdout, "STEP 2 done...catalog information obtained from the database\n");
  238. Exit:
  239. /* CLEANUP: Close the statement handles
  240. ** Free the statement handles
  241. ** Disconnect from the datasource
  242. ** Free the connection and environment handles
  243. ** Exit
  244. */
  245. /* Close the statement handle */
  246. SQLFreeStmt (hTableStmt, SQL_CLOSE);
  247. SQLFreeStmt (hColStmt, SQL_CLOSE);
  248. SQLFreeStmt (hPriKeyStmt, SQL_CLOSE);
  249. /* Free the statement handle */
  250. SQLFreeHandle (SQL_HANDLE_STMT, hTableStmt);
  251. SQLFreeHandle (SQL_HANDLE_STMT, hColStmt);
  252. SQLFreeHandle (SQL_HANDLE_STMT, hPriKeyStmt);
  253. /* Disconnect from the data source */
  254. SQLDisconnect (hdbc);
  255. /* Free the environment handle and the database connection handle */
  256. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  257. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  258. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  259. in = getchar ();
  260. return (rc);
  261. }