dbdropW.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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: dbdropW.c (Unicode counterpart of dbdrop.c)
  14. *
  15. * Description: To drop the sample database (created by dbCreate.c)
  16. * Before running this program, make sure that all
  17. * connections to the sample database have been closed.
  18. * To successfully drop the sample database, you must
  19. * have DBA permissions or be running this as user
  20. * 'informix'
  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 70
  34. #define ERRMSG_LEN 200
  35. SQLWCHAR defDsnW[] = L"odbc_demo";
  36. SQLCHAR defDsn[] = "odbc_demo";
  37. SQLINTEGER checkError (SQLRETURN rc,
  38. SQLSMALLINT handleType,
  39. SQLHANDLE handle,
  40. SQLCHAR* errmsg)
  41. {
  42. SQLRETURN retcode = SQL_SUCCESS;
  43. SQLSMALLINT errNum = 1;
  44. SQLWCHAR sqlStateW[6];
  45. SQLCHAR *sqlState;
  46. SQLINTEGER nativeError;
  47. SQLWCHAR errMsgW[ERRMSG_LEN];
  48. SQLCHAR *errMsg;
  49. SQLSMALLINT textLengthPtr;
  50. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  51. {
  52. while (retcode != SQL_NO_DATA)
  53. {
  54. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  55. if (retcode == SQL_INVALID_HANDLE)
  56. {
  57. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  58. return 1;
  59. }
  60. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  61. {
  62. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  63. ;
  64. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  65. + sizeof(char));
  66. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  67. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  68. + sizeof(char));
  69. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  70. }
  71. errNum++;
  72. }
  73. fprintf (stderr, "%s\n", errmsg);
  74. return 1; /* all errors on this handle have been reported */
  75. }
  76. else
  77. return 0; /* no errors to report */
  78. }
  79. int main (long argc,
  80. char* argv[])
  81. {
  82. /* Declare variables
  83. */
  84. /* Handles */
  85. SQLHDBC hdbc;
  86. SQLHENV henv;
  87. SQLHSTMT hstmt;
  88. /* Miscellaneous variables */
  89. SQLCHAR *dsn; /*name of the DSN used for connecting to the database */
  90. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  91. SQLRETURN rc = 0;
  92. SQLINTEGER in;
  93. SQLWCHAR connStrInW[BUFFER_LEN];
  94. SQLWCHAR connStrOutW[BUFFER_LEN];
  95. SQLSMALLINT connStrOutLen;
  96. SQLWCHAR* dropDBStmtW = (SQLWCHAR *) L"DROP DATABASE odbc_demodb";
  97. int lenArgv1;
  98. /* STEP 1. Get data source name from command line (or use default)
  99. ** Allocate the environment handle and set ODBC version
  100. ** Allocate the connection handle
  101. ** Establish the database connection
  102. ** Allocate the statement handle
  103. */
  104. /* If(dsnW is not explicitly passed in as arg) */
  105. if (argc != 2)
  106. {
  107. /* Use default dsnW - odbc_demo */
  108. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  109. dsn = (SQLCHAR *) malloc (sizeof(defDsn) + sizeof(char));
  110. strcpy( (char *)dsn, (char *)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. dsn = (SQLCHAR *) malloc (lenArgv1 + sizeof(char));
  120. strcpy((char *)dsn, (char *)argv[1]);
  121. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  122. + sizeof(SQLWCHAR));
  123. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  124. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  125. }
  126. /* Allocate the Environment handle */
  127. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  128. if (rc != SQL_SUCCESS)
  129. {
  130. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  131. return (1);
  132. }
  133. /* Set the ODBC version to 3.0 */
  134. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  135. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR * ) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  136. return (1);
  137. /* Allocate the connection handle */
  138. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  139. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR * ) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  140. return (1);
  141. /* Establish the database connection */
  142. #ifndef _WINNT
  143. wsprintf((SQLWCHAR *) connStrInW, "DSN=%s;connectdatabase=NO", dsn);
  144. #else
  145. swprintf((SQLWCHAR *) connStrInW, L"DSN=%s;connectdatabase=NO", dsnW);
  146. #endif
  147. rc = SQLDriverConnectW (hdbc, NULL, connStrInW, SQL_NTS, connStrOutW, BUFFER_LEN, &connStrOutLen, SQL_DRIVER_NOPROMPT);
  148. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR * ) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  149. return (1);
  150. /* Allocate the statement handle */
  151. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  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. Drop the sample database -- odbc_demodb */
  156. rc = SQLExecDirectW (hstmt, dropDBStmtW, SQL_NTS);
  157. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR * ) "Error in Step 2 -- SQLExecDirect failed\n"))
  158. goto Exit;
  159. fprintf(stdout, "STEP 2 done...Sample Database Dropped\n");
  160. Exit:
  161. /* CLEANUP: Close the statement handle
  162. ** Free the statement handle
  163. ** Disconnect from the datasource
  164. ** Free the connection and environment handles
  165. ** Exit
  166. */
  167. /* Close the statement handle */
  168. SQLFreeStmt (hstmt, SQL_CLOSE);
  169. /* Free the statement handle */
  170. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  171. /* Disconnect from the data source */
  172. SQLDisconnect (hdbc);
  173. /* Free the environment handle and the database connection handle */
  174. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  175. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  176. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  177. in = getchar ();
  178. return (rc);
  179. }