rcupdateW.c 16 KB

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