transactW.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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: transactW.c (Unicode counterpart of transact.c)
  14. *
  15. * Description: To use explicit transactions using SQLEndTran
  16. *
  17. *
  18. ***************************************************************************
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifndef NO_WIN32
  24. #include <io.h>
  25. #include <windows.h>
  26. #include <conio.h>
  27. #endif /*NO_WIN32*/
  28. #include "infxcli.h"
  29. #define BUFFER_LEN 10
  30. #define ERRMSG_LEN 200
  31. SQLWCHAR defDsnW[] = L"odbc_demo";
  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. SQLWCHAR sqlStateW[6];
  41. SQLCHAR *sqlState;
  42. SQLINTEGER nativeError;
  43. SQLWCHAR errMsgW[ERRMSG_LEN];
  44. SQLCHAR *errMsg;
  45. SQLSMALLINT textLengthPtr;
  46. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  47. {
  48. while (retcode != SQL_NO_DATA)
  49. {
  50. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, 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. {
  58. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  59. ;
  60. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  61. + sizeof(char));
  62. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  63. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  64. + sizeof(char));
  65. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  66. }
  67. errNum++;
  68. }
  69. fprintf (stderr, "%s\n", errmsg);
  70. return 1; /* all errors on this handle have been reported */
  71. }
  72. else
  73. return 0; /* no errors to report */
  74. }
  75. int main (long argc,
  76. char* argv[])
  77. {
  78. /* Declare variables
  79. */
  80. /* Handles */
  81. SQLHDBC hdbc;
  82. SQLHENV henv;
  83. SQLHSTMT hstmt;
  84. /* Miscellaneous variables */
  85. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  86. SQLRETURN rc = 0;
  87. SQLINTEGER in;
  88. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT order_num, quantity FROM orders";
  89. SQLWCHAR* updateStmtW = (SQLWCHAR *) L"UPDATE orders SET quantity = quantity*2";
  90. SQLLEN order_num, quantity, cbOrderNum = 0, cbQuantity = 0;
  91. int lenArgv1;
  92. /* STEP 1. Get data source name from command line (or use default)
  93. ** Allocate the environment handle and set ODBC version
  94. ** Allocate the connection handle
  95. ** Establish the database connection
  96. ** Allocate the statement handle
  97. */
  98. /* If(dsnW is not explicitly passed in as arg) */
  99. if (argc != 2)
  100. {
  101. /* Use default dsnW - odbc_demo */
  102. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  103. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  104. + sizeof(SQLWCHAR) );
  105. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  106. }
  107. else
  108. {
  109. /* Use specified dsnW */
  110. lenArgv1 = strlen((char *)argv[1]);
  111. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  112. + sizeof(SQLWCHAR));
  113. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  114. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  115. }
  116. /* Allocate the Environment handle */
  117. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  118. if (rc != SQL_SUCCESS)
  119. {
  120. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  121. return (1);
  122. }
  123. /* Set the ODBC version to 3.0 */
  124. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  125. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  126. return (1);
  127. /* Allocate the connection handle */
  128. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  129. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  130. return (1);
  131. /* Establish the database connection */
  132. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  133. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  134. return (1);
  135. /* Allocate the statement handle */
  136. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  137. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  138. return (1);
  139. fprintf (stdout, "STEP 1 done...connected to database\n");
  140. /* STEP 2. Set the connection attribute SQL_ATTR_AUTOCOMMIT to
  141. ** SQL_AUTOCOMMIT_OFF to disable auto=commit
  142. ** Retrieve data from the 'orders' table and display the results
  143. ** Close the result set cursor
  144. */
  145. /* Set the connection attribute SQL_ATTR_AUTOCOMMIT to SQL_AUTOCOMMIT_OFF */
  146. rc = SQLSetConnectAttr (hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, (SQLINTEGER) NULL);
  147. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLSetConnectAttr failed\n"))
  148. goto Exit;
  149. /* Retrieve data from the 'orders' table */
  150. fprintf (stdout, "Retrieving data from 'orders' table\n\n");
  151. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  152. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  153. goto Exit;
  154. /* Bind the result set columns */
  155. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 0, &cbOrderNum);
  156. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
  157. goto Exit;
  158. rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &quantity, 5, &cbQuantity);
  159. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
  160. goto Exit;
  161. /* Fetch the data */
  162. while (1)
  163. {
  164. rc = SQLFetch (hstmt);
  165. if (rc == SQL_NO_DATA_FOUND)
  166. break;
  167. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
  168. goto Exit;
  169. /* Display the results */
  170. fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
  171. }
  172. /* Close the result set cursor */
  173. rc = SQLCloseCursor(hstmt);
  174. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
  175. goto Exit;
  176. fprintf (stdout, "\nSTEP 2 done...auto commit turned off & original data retrieved from the database\n");
  177. /* STEP 3. Update the quantity field of the 'orders' table
  178. ** (set quantity = quantity*2)
  179. ** Rollback the transaction
  180. ** Retrieve data from the 'orders' table and display the results
  181. ** -- data should be the same as in step 2
  182. ** Close the result set cursor
  183. */
  184. /* Execute the UPDATE statement */
  185. rc = SQLExecDirectW (hstmt, updateStmtW, SQL_NTS);
  186. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for update stmt\n"))
  187. goto Exit;
  188. /* Call SQLEndTran with SQL_ROLLBACK to rollback the transaction */
  189. rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK);
  190. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 3 -- SQLEndTran failed\n"))
  191. goto Exit;
  192. /* Retrieve data from the 'orders' table */
  193. fprintf (stdout, "Retrieving data from 'orders' table after rollback\n\n");
  194. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  195. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for select stmt\n"))
  196. goto Exit;
  197. /* Bind the result set columns */
  198. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 0, &cbOrderNum);
  199. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
  200. goto Exit;
  201. rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &quantity, 5, &cbQuantity);
  202. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
  203. goto Exit;
  204. /* Fetch the data */
  205. while (1)
  206. {
  207. rc = SQLFetch (hstmt);
  208. if (rc == SQL_NO_DATA_FOUND)
  209. break;
  210. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  211. goto Exit;
  212. /* Display the results */
  213. fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
  214. }
  215. /* Close the result set cursor */
  216. rc = SQLCloseCursor(hstmt);
  217. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
  218. goto Exit;
  219. fprintf (stdout, "\nSTEP 3 done...transaction successfully rolled back...\n\t...data retrieved should be same as in step 2\n");
  220. fprintf (stdout,"\n\nHit <Enter> to continue...\n\n");
  221. in = getchar ();
  222. /* STEP 4. Update the quantity field of the 'orders' table
  223. ** (set quantity = quantity*2)
  224. ** Commit the transaction
  225. ** Retrieve data from the 'orders' table and display the results
  226. ** -- data should be updated
  227. ** Close the result set cursor
  228. */
  229. /* Execute the UPDATE statement */
  230. rc = SQLExecDirectW (hstmt, updateStmtW, SQL_NTS);
  231. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed for update stmt\n"))
  232. goto Exit;
  233. /* Call SQLEndTran with SQL_COMMIT to commit the transaction */
  234. rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  235. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 4 -- SQLEndTran failed\n"))
  236. goto Exit;
  237. /* Retrieve data from the 'orders' table */
  238. fprintf (stdout, "Retrieving data from 'orders' table after commit\n\n");
  239. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  240. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed for select stmt\n"))
  241. goto Exit;
  242. /* Bind the result set columns */
  243. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 0, &cbOrderNum);
  244. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindCol failed (column 1)\n"))
  245. goto Exit;
  246. rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &quantity, 5, &cbQuantity);
  247. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindCol failed (column 2)\n"))
  248. goto Exit;
  249. /* Fetch the data */
  250. while (1)
  251. {
  252. rc = SQLFetch (hstmt);
  253. if (rc == SQL_NO_DATA_FOUND)
  254. break;
  255. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLFetch failed\n"))
  256. goto Exit;
  257. /* Display the results */
  258. fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
  259. }
  260. /* Close the result set cursor */
  261. rc = SQLCloseCursor(hstmt);
  262. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLCloseCursor failed\n"))
  263. goto Exit;
  264. fprintf (stdout, "\n\nSTEP 4 done...transaction successfully committed...\n\t...data retrieved should be updated data\n");
  265. Exit:
  266. /* CLEANUP: Close the statement handle
  267. ** Free the statement handle
  268. ** Disconnect from the datasource
  269. ** Free the connection and environment handles
  270. ** Exit
  271. */
  272. /* Close the statement handle */
  273. SQLFreeStmt (hstmt, SQL_CLOSE);
  274. /* Free the statement handle */
  275. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  276. /* Disconnect from the data source */
  277. SQLDisconnect (hdbc);
  278. /* Free the environment handle and the database connection handle */
  279. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  280. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  281. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  282. in = getchar ();
  283. return (rc);
  284. }