catalog.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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: 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. SQLCHAR defDsn[] = "odbc_demo";
  33. SQLINTEGER checkError (SQLRETURN rc,
  34. SQLSMALLINT handleType,
  35. SQLHANDLE handle,
  36. SQLCHAR* errmsg)
  37. {
  38. SQLRETURN retcode = SQL_SUCCESS;
  39. SQLSMALLINT errNum = 1;
  40. SQLCHAR sqlState[6];
  41. SQLINTEGER nativeError;
  42. SQLCHAR errMsg[ERRMSG_LEN];
  43. SQLSMALLINT textLengthPtr;
  44. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  45. {
  46. while (retcode != SQL_NO_DATA)
  47. {
  48. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  49. if (retcode == SQL_INVALID_HANDLE)
  50. {
  51. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  52. return 1;
  53. }
  54. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  55. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  56. errNum++;
  57. }
  58. fprintf (stderr, "%s\n", errmsg);
  59. return 1; /* all errors on this handle have been reported */
  60. }
  61. else
  62. return 0; /* no errors to report */
  63. }
  64. int main (long argc,
  65. char* argv[])
  66. {
  67. /* Declare variables
  68. */
  69. /* Handles */
  70. SQLHDBC hdbc;
  71. SQLHENV henv;
  72. SQLHSTMT hTableStmt;
  73. SQLHSTMT hColStmt;
  74. SQLHSTMT hPriKeyStmt;
  75. /* Miscellaneous variables */
  76. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  77. SQLRETURN rc = 0;
  78. SQLINTEGER in;
  79. SQLCHAR tableName[BUFFER_LEN];
  80. SQLLEN cbTableName;
  81. SQLCHAR colName[BUFFER_LEN];
  82. SQLLEN cbColName;
  83. SQLCHAR priKeyName[BUFFER_LEN];
  84. SQLLEN cbPriKeyName;
  85. /* STEP 1. Get data source name from command line (or use default)
  86. ** Allocate the environment handle and set ODBC version
  87. ** Allocate the connection handle
  88. ** Establish the database connection
  89. ** Allocate the statement handle
  90. */
  91. /* If(dsn is not explicitly passed in as arg) */
  92. if (argc != 2)
  93. {
  94. /* Use default dsn - odbc_demo */
  95. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  96. strcpy ((char *)dsn, (char *)defDsn);
  97. }
  98. else
  99. {
  100. /* Use specified dsn */
  101. strcpy ((char *)dsn, (char *)argv[1]);
  102. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  103. }
  104. /* Allocate the Environment handle */
  105. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  106. if (rc != SQL_SUCCESS)
  107. {
  108. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  109. return (1);
  110. }
  111. /* Set the ODBC version to 3.0 */
  112. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
  113. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  114. return (1);
  115. /* Allocate the connection handle */
  116. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  117. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  118. return (1);
  119. /* Establish the database connection */
  120. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  121. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  122. return (1);
  123. /* Allocate the statement handles */
  124. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hTableStmt );
  125. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  126. return (1);
  127. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hColStmt );
  128. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  129. return (1);
  130. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hPriKeyStmt );
  131. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  132. return (1);
  133. fprintf (stdout, "STEP 1 done...connected to database\n");
  134. /* STEP 2. Call SQLTables to get table names from the database
  135. ** For each table in the database, call SQLColumns to get its
  136. ** columns and SQLPrimaryKeys to get the columns which comprise
  137. ** its primary key
  138. */
  139. /* Get the names of all the tables in the database */
  140. rc = SQLTables (hTableStmt, NULL, 0, NULL, 0, NULL, 0, (SQLCHAR *) "TABLE", SQL_NTS);
  141. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLTables failed\n"))
  142. goto Exit;
  143. /* Bind the result set column # 3 - 'TABLE_NAME' */
  144. rc = SQLBindCol (hTableStmt, 3, SQL_C_CHAR, tableName, BUFFER_LEN, &cbTableName);
  145. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed\n"))
  146. goto Exit;
  147. /* Fetch the data */
  148. while (1)
  149. {
  150. rc = SQLFetch (hTableStmt);
  151. if (rc == SQL_NO_DATA_FOUND)
  152. break;
  153. else if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed for table\n"))
  154. goto Exit;
  155. /* Display the table name */
  156. fprintf (stdout, "Table Name %s\n", tableName);
  157. /* For this table, get all its columns */
  158. rc = SQLColumns (hColStmt, NULL, 0, NULL, 0, tableName, SQL_NTS, NULL, SQL_NTS);
  159. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLColumns failed\n"))
  160. goto Exit;
  161. /* Bind the result set column #4 - 'COLUMN_NAME' */
  162. rc = SQLBindCol (hColStmt, 4, SQL_C_CHAR, colName, BUFFER_LEN, &cbColName);
  163. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed for column\n"))
  164. goto Exit;
  165. /* Fetch and display the column names */
  166. while (1)
  167. {
  168. rc = SQLFetch (hColStmt);
  169. if (rc == SQL_NO_DATA_FOUND)
  170. break;
  171. else if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed for column\n"))
  172. goto Exit;
  173. /* Display the results */
  174. fprintf (stdout, "\tColumn: %s\n", colName);
  175. }
  176. /* For this table, get all its primary key columns */
  177. rc = SQLPrimaryKeys (hPriKeyStmt, NULL, 0, NULL, 0, tableName, SQL_NTS);
  178. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLPrimaryKeys failed\n"))
  179. goto Exit;
  180. /* Bind the result set column #4 - 'COLUMN_NAME' */
  181. rc = SQLBindCol (hPriKeyStmt, 4, SQL_C_CHAR, priKeyName, BUFFER_LEN, &cbPriKeyName);
  182. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed for primary key\n"))
  183. goto Exit;
  184. /* Fetch and display the column names */
  185. while (1)
  186. {
  187. rc = SQLFetch (hPriKeyStmt);
  188. if (rc == SQL_NO_DATA_FOUND)
  189. break;
  190. else if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed for column\n"))
  191. goto Exit;
  192. /* Display the results */
  193. fprintf (stdout, "\t\t Primary Key Column: %s\n", priKeyName);
  194. }
  195. /* Close the result set cursors for columns and primary keys */
  196. rc = SQLCloseCursor (hColStmt);
  197. if (checkError (rc, SQL_HANDLE_STMT, hColStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for column\n"))
  198. goto Exit;
  199. rc = SQLCloseCursor (hPriKeyStmt);
  200. if (checkError (rc, SQL_HANDLE_STMT, hPriKeyStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for primary key\n"))
  201. goto Exit;
  202. }
  203. /* Close the result set cursor for table */
  204. rc = SQLCloseCursor (hTableStmt);
  205. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed for table\n"))
  206. goto Exit;
  207. fprintf (stdout, "STEP 2 done...catalog information obtained from the database\n");
  208. Exit:
  209. /* CLEANUP: Close the statement handles
  210. ** Free the statement handles
  211. ** Disconnect from the datasource
  212. ** Free the connection and environment handles
  213. ** Exit
  214. */
  215. /* Close the statement handle */
  216. SQLFreeStmt (hTableStmt, SQL_CLOSE);
  217. SQLFreeStmt (hColStmt, SQL_CLOSE);
  218. SQLFreeStmt (hPriKeyStmt, SQL_CLOSE);
  219. /* Free the statement handle */
  220. SQLFreeHandle (SQL_HANDLE_STMT, hTableStmt);
  221. SQLFreeHandle (SQL_HANDLE_STMT, hColStmt);
  222. SQLFreeHandle (SQL_HANDLE_STMT, hPriKeyStmt);
  223. /* Disconnect from the data source */
  224. SQLDisconnect (hdbc);
  225. /* Free the environment handle and the database connection handle */
  226. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  227. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  228. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  229. in = getchar ();
  230. return (rc);
  231. }