transact.c 12 KB

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