rcupdate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2013 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: rcupdate.c
  14. *
  15. * Description: To update a row and collection column
  16. * To update the row, we update one of the elements of
  17. * the row and then update the entire row on the database
  18. * To update the list, we add an element to the list and
  19. * then update the list on the database
  20. *
  21. ***************************************************************************
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifndef NO_WIN32
  27. #include <io.h>
  28. #include <windows.h>
  29. #include <conio.h>
  30. #endif /*NO_WIN32*/
  31. #include "infxcli.h"
  32. #define BUFFER_SIZE 25
  33. #define ERRMSG_LEN 200
  34. SQLCHAR defDsn[] = "odbc_demo";
  35. SQLINTEGER checkError (SQLRETURN rc,
  36. SQLSMALLINT handleType,
  37. SQLHANDLE handle,
  38. SQLCHAR* errmsg)
  39. {
  40. SQLRETURN retcode = SQL_SUCCESS;
  41. SQLSMALLINT errNum = 1;
  42. SQLCHAR sqlState[6];
  43. SQLINTEGER nativeError;
  44. SQLCHAR errMsg[ERRMSG_LEN];
  45. SQLSMALLINT textLengthPtr;
  46. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  47. {
  48. while (retcode != SQL_NO_DATA)
  49. {
  50. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, 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. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  58. errNum++;
  59. }
  60. fprintf (stderr, "%s\n", errmsg);
  61. return 1; /* all errors on this handle have been reported */
  62. }
  63. else
  64. return 0; /* no errors to report */
  65. }
  66. int main (long argc,
  67. char* argv[] )
  68. {
  69. /* Declare variables
  70. */
  71. /* Handles */
  72. SQLHDBC hdbc;
  73. SQLHENV henv;
  74. SQLHSTMT hstmt;
  75. HINFX_RC hrow;
  76. HINFX_RC hlist;
  77. /* Miscellaneous variables */
  78. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  79. SQLRETURN rc = 0;
  80. SQLINTEGER in;
  81. SQLCHAR verInfoBuffer[BUFFER_SIZE];
  82. SQLSMALLINT verInfoLen;
  83. SQLLEN data_size = SQL_NTS;
  84. SQLSMALLINT position, jump;
  85. SQLLEN cbHrow = 0, cbHlist = 0, cbPosition = 0, cbJump = 0;
  86. SQLCHAR new_address[BUFFER_SIZE] = "295 Holly Street";
  87. SQLLEN new_address_size = SQL_NTS;
  88. SQLCHAR new_date[BUFFER_SIZE] = "1995-11-26";
  89. SQLLEN new_date_size = SQL_NTS;
  90. SQLCHAR* selectStmt = (SQLCHAR *) "SELECT address, contact_dates FROM customer WHERE cust_num = 109";
  91. SQLCHAR* updateStmt = (SQLCHAR *) "UPDATE customer SET address = ?, contact_dates = ? WHERE cust_num = 109";
  92. /* STEP 1. Get data source name from command line (or use default)
  93. ** Allocate environment handle and set ODBC version
  94. ** Allocate connection handle
  95. ** Establish the database connection
  96. ** Get version information from the database server
  97. ** -- if version < 9.x (not UDO enabled), exit with error message
  98. ** Allocate the statement handle
  99. */
  100. /* If(dsn is not explicitly passed in as arg) */
  101. if (argc != 2)
  102. {
  103. /* Use default dsn - odbc_demo */
  104. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  105. strcpy ((char *)dsn, (char *)defDsn);
  106. }
  107. else
  108. {
  109. /* Use specified dsn */
  110. strcpy ((char *)dsn, (char *)argv[1]);
  111. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  112. }
  113. /* Allocate the Environment handle */
  114. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  115. if (rc != SQL_SUCCESS)
  116. {
  117. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  118. return (1);
  119. }
  120. /* Set the ODBC version to 3.0 */
  121. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  122. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  123. return (1);
  124. /* Allocate the connection handle */
  125. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  126. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  127. return (1);
  128. /* Establish the database connection */
  129. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  130. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\n"))
  131. return (1);
  132. /* Get version information from the database server
  133. If version < 9.x (not UDO enabled), exit with error message */
  134. rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_SIZE, &verInfoLen);
  135. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  136. return 1;
  137. if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0)
  138. {
  139. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  140. return 1;
  141. }
  142. /* Allocate the statement handle */
  143. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  144. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  145. return (1);
  146. fprintf (stdout, "STEP 1 done...connected to database\n");
  147. /* STEP 2. Allocate an unfixed row handle
  148. ** Allocate an unfixed collection (list) handle
  149. ** Reset the statement parameters
  150. */
  151. /* Allocate an unfixed row handle */
  152. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  153. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0,
  154. &hrow, sizeof(HINFX_RC), &cbHrow);
  155. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 1) failed\n"))
  156. goto Exit;
  157. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  158. SQL_CHAR, 0, 0, (SQLCHAR *) "row", 0, &data_size);
  159. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 2) failed\n"))
  160. goto Exit;
  161. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{? = call ifx_rc_create(?)}", SQL_NTS);
  162. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  163. goto Exit;
  164. /* Reset the statement parameters */
  165. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  166. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
  167. goto Exit;
  168. /* Allocate an unfixed list handle */
  169. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  170. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0,
  171. &hlist, sizeof(HINFX_RC), &cbHlist);
  172. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 1) failed\n"))
  173. goto Exit;
  174. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  175. SQL_CHAR, 0, 0, (SQLCHAR *) "list", 0, &data_size);
  176. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 2) failed\n"))
  177. goto Exit;
  178. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{? = call ifx_rc_create(?)}", SQL_NTS);
  179. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  180. goto Exit;
  181. /* Reset the statement parameters */
  182. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  183. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
  184. goto Exit;
  185. fprintf (stdout, "STEP 2 done...unfixed row and list handles allocated\n");
  186. /* STEP 3. Bind the result set columns for the row and list handle
  187. ** Retrieve the row data from the database into the buffer allocated
  188. ** Reset the statement parameters
  189. */
  190. /* Bind result set columns for the row and list handle */
  191. rc = SQLBindCol (hstmt, 1, SQL_C_BINARY, hrow, sizeof(HINFX_RC), NULL);
  192. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
  193. goto Exit;
  194. rc = SQLBindCol (hstmt, 2, SQL_C_BINARY, hlist, sizeof(HINFX_RC), NULL);
  195. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
  196. goto Exit;
  197. /* Retrieve the row data from the database into the buffer allocated */
  198. rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
  199. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  200. goto Exit;
  201. rc = SQLFetch (hstmt);
  202. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  203. goto Exit;
  204. /* Close the result set cursor */
  205. rc = SQLCloseCursor (hstmt);
  206. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
  207. goto Exit;
  208. fprintf (stdout, "STEP 3 done...data retrieved from database into row buffer\n");
  209. /* STEP 4. Update the elements of the row buffer allocated
  210. ** -- the element of the row being updated is element
  211. ** no. 1 - 'address1'
  212. ** Reset the statement parameters
  213. */
  214. position = SQL_INFX_RC_ABSOLUTE;
  215. jump = 1;
  216. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  217. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  218. sizeof(HINFX_RC), &cbHrow);
  219. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  220. goto Exit;
  221. rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  222. SQL_CHAR, BUFFER_SIZE, 0, new_address, 0, &new_address_size);
  223. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  224. goto Exit;
  225. rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_SHORT,
  226. SQL_SMALLINT, 0, 0, &position, 0, &cbPosition);
  227. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  228. goto Exit;
  229. rc = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT,
  230. SQL_SMALLINT, 0, 0, &jump, 0, &cbJump);
  231. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  232. goto Exit;
  233. rc = SQLExecDirect(hstmt, (SQLCHAR *) "{call ifx_rc_update(?, ?, ?, ?)}", SQL_NTS);
  234. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  235. goto Exit;
  236. fprintf (stdout, "STEP 4 done...row element updated\n");
  237. /* STEP 5. Insert a new element into the list buffer allocated
  238. ** Reset the statement parameters
  239. */
  240. position = SQL_INFX_RC_LAST;
  241. jump = 0;
  242. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  243. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  244. sizeof(HINFX_RC), &cbHlist);
  245. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  246. goto Exit;
  247. rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  248. SQL_CHAR, BUFFER_SIZE, 0, new_date, 0, &new_date_size);
  249. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  250. goto Exit;
  251. rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_SHORT,
  252. SQL_SMALLINT, 0, 0, &position, 0, &cbPosition);
  253. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  254. goto Exit;
  255. rc = SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT,
  256. SQL_SMALLINT, 0, 0, &jump, 0, &cbJump);
  257. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  258. goto Exit;
  259. rc = SQLExecDirect(hstmt, (SQLCHAR *) "{call ifx_rc_insert(?, ?, ?, ?)}", SQL_NTS);
  260. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  261. goto Exit;
  262. fprintf (stdout, "STEP 5 done...list element updated\n");
  263. /* STEP 6. Update the database with the new row data
  264. */
  265. /* Update the database with the new row and list data */
  266. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  267. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  268. sizeof(HINFX_RC), &cbHrow);
  269. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed\n"))
  270. goto Exit;
  271. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  272. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  273. sizeof(HINFX_RC), &cbHlist);
  274. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed\n"))
  275. goto Exit;
  276. rc = SQLExecDirect (hstmt, updateStmt, SQL_NTS);
  277. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  278. goto Exit;
  279. fprintf (stdout, "STEP 6 done...database row updated\n");
  280. /* STEP 7. Free the row and list handles
  281. */
  282. /* Free the row handle */
  283. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  284. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  285. sizeof(HINFX_RC), &cbHrow);
  286. rc = SQLExecDirect(hstmt, (SQLCHAR *) "{call ifx_rc_free(?)}", SQL_NTS);
  287. /* Free the list handle */
  288. rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  289. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  290. sizeof(HINFX_RC), &cbHlist);
  291. rc = SQLExecDirect(hstmt, (SQLCHAR *) "{call ifx_rc_free(?)}", SQL_NTS);
  292. fprintf (stdout, "STEP 7 done...row and list handles freed\n");
  293. Exit:
  294. /* CLEANUP: Close the statement handle
  295. ** Free the statement handle
  296. ** Disconnect from the datasource
  297. ** Free the connection and environment handles
  298. ** Exit
  299. */
  300. /* Close the statement handle */
  301. SQLFreeStmt (hstmt, SQL_CLOSE);
  302. /* Free the statement handle */
  303. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  304. /* Disconnect from the data source */
  305. SQLDisconnect (hdbc);
  306. /* Free the environment handle and the database connection handle */
  307. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  308. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  309. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  310. in = getchar ();
  311. return (rc);
  312. }