posupdtW.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2007 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: posupdtW.c (Unicode counterpart of posupdt.c)
  14. *
  15. * Description: To use SQLSetPos for positional updates to a result set
  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 BUFFER_LENW BUFFER_LEN * sizeof(SQLWCHAR)
  31. #define ERRMSG_LEN 200
  32. #define NUM_ROWS 5
  33. SQLWCHAR defDsnW[] = L"odbc_demo";
  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. SQLWCHAR sqlStateW[6];
  43. SQLCHAR *sqlState;
  44. SQLINTEGER nativeError;
  45. SQLWCHAR errMsgW[ERRMSG_LEN];
  46. SQLCHAR *errMsg;
  47. SQLSMALLINT textLengthPtr;
  48. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  49. {
  50. while (retcode != SQL_NO_DATA)
  51. {
  52. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  53. if (retcode == SQL_INVALID_HANDLE)
  54. {
  55. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  56. return 1;
  57. }
  58. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  59. {
  60. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  61. ;
  62. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  63. + sizeof(char));
  64. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  65. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  66. + sizeof(char));
  67. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  68. }
  69. errNum++;
  70. }
  71. fprintf (stderr, "%s\n", errmsg);
  72. return 1; /* all errors on this handle have been reported */
  73. }
  74. else
  75. return 0; /* no errors to report */
  76. }
  77. int main (long argc,
  78. char* argv[])
  79. {
  80. /* Declare variables
  81. */
  82. /* Handles */
  83. SQLHDBC hdbc;
  84. SQLHENV henv;
  85. SQLHSTMT hstmt;
  86. SQLHSTMT hNewStmt;
  87. /* Miscellaneous variables */
  88. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  89. SQLRETURN rc = 0;
  90. SQLINTEGER in;
  91. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT cust_num, fname, lname FROM customer";
  92. SQLWCHAR* newLnameW = (SQLWCHAR *) L"Miller";
  93. SQLWCHAR fnameW[NUM_ROWS][BUFFER_LEN], lnameW[NUM_ROWS][BUFFER_LEN];
  94. SQLWCHAR newfnameW[NUM_ROWS][BUFFER_LEN], newlnameW[NUM_ROWS][BUFFER_LEN];
  95. SQLCHAR *fname, *lname;
  96. SQLCHAR *newfname, *newlname;
  97. SQLINTEGER cust_num[NUM_ROWS];
  98. SQLLEN cbCustNum[NUM_ROWS], cbFname[NUM_ROWS], cbLname[NUM_ROWS];
  99. SQLINTEGER newcust_num[NUM_ROWS];
  100. SQLLEN newcbCustNum[NUM_ROWS], newcbFname[NUM_ROWS], newcbLname[NUM_ROWS];
  101. SQLUSMALLINT i, statusArray[NUM_ROWS];
  102. int lenArgv1;
  103. /* STEP 1. Get data source name from command line (or use default)
  104. ** Allocate the environment handle and set ODBC version
  105. ** Allocate the connection handle
  106. ** Establish the database connection
  107. ** Set the connection attribute to enable scrollable cursors
  108. ** Allocate the statement handles
  109. */
  110. /* If(dsn is not explicitly passed in as arg) */
  111. if (argc != 2)
  112. {
  113. /* Use default dsnW - odbc_demo */
  114. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  115. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  116. + sizeof(SQLWCHAR) );
  117. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  118. }
  119. else
  120. {
  121. /* Use specified dsnW */
  122. lenArgv1 = strlen((char *)argv[1]);
  123. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  124. + sizeof(SQLWCHAR));
  125. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  126. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  127. }
  128. /* Allocate the Environment handle */
  129. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  130. if (rc != SQL_SUCCESS)
  131. {
  132. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  133. return (1);
  134. }
  135. /* Set the ODBC version to 3.0 */
  136. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  137. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  138. return (1);
  139. /* Allocate the connection handle */
  140. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  141. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  142. return (1);
  143. /* Establish the database connection */
  144. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  145. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  146. return (1);
  147. /* Set the connection attribute to enable scrollable cursors */
  148. rc = SQLSetConnectAttr (hdbc, SQL_INFX_ATTR_ENABLE_SCROLL_CURSORS, (SQLPOINTER) SQL_TRUE, (SQLINTEGER) NULL);
  149. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Set Connection Attribute failed\nExiting!!"))
  150. return (1);
  151. /* Allocate the statement handles */
  152. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  153. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  154. return (1);
  155. fprintf (stdout, "STEP 1 done...connected to database\n");
  156. /* STEP 2 Set the following statement attributes
  157. ** -- SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound
  158. ** SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK
  159. ** SQL_ATTR_CURSOR_TYPE to SQL_CURSOR_STATIC
  160. ** SQL_ATTR_ROW_STATUS_PTR tot he address of the status array
  161. */
  162. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  163. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_ROWS, 0);
  164. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  165. goto Exit;
  166. /* Set the statement attribute SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK */
  167. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_LOCK, 0);
  168. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CONCURRENCY\n"))
  169. goto Exit;
  170. /* Set the statement attribute SQL_ATTR_CURSOR_TYPE to SQL_CURSOR_STATIC */
  171. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_STATIC, 0);
  172. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CURSOR_TYPE\n"))
  173. goto Exit;
  174. /* Set the statement attribute SQL_ATTR_ROW_STATUS_PTR to the status array */
  175. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_STATUS_PTR, statusArray, 0);
  176. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_STATUS_PTR\n"))
  177. goto Exit;
  178. fprintf (stdout, "STEP 2 done...Statement attributes set\n");
  179. /* STEP 3. Retrieve data from the 'customer' table
  180. ** Display the results
  181. */
  182. /* Retrieve data from the 'customer' table */
  183. fprintf (stdout, "Retrieving data from 'customer' table\n\n");
  184. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  185. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  186. goto Exit;
  187. /* Bind the result set columns */
  188. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &cust_num[0], 0, &cbCustNum[0]);
  189. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
  190. goto Exit;
  191. rc = SQLBindCol (hstmt, 2, SQL_C_WCHAR, fnameW[0],BUFFER_LENW, &cbFname[0]);
  192. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
  193. goto Exit;
  194. rc = SQLBindCol (hstmt, 3, SQL_C_WCHAR, lnameW[0], BUFFER_LENW, &cbLname[0]);
  195. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 3)\n"))
  196. goto Exit;
  197. /* Fetch the results */
  198. rc = SQLFetch (hstmt);
  199. if (rc == SQL_NO_DATA_FOUND)
  200. fprintf (stdout, "NO DATA FOUND!!\n EXITING!!");
  201. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  202. goto Exit;
  203. for (i=0; i<NUM_ROWS; i++)
  204. {
  205. /* Display the results */
  206. fname = (SQLCHAR *) malloc (wcslen(fnameW[i]) + sizeof(char));
  207. wcstombs( (char *) fname, fnameW[i],
  208. wcslen(fnameW[i]) + sizeof(char));
  209. lname = (SQLCHAR *) malloc (wcslen(lnameW[i]) + sizeof(char));
  210. wcstombs( (char *) lname, lnameW[i],
  211. wcslen(lnameW[i]) + sizeof(char));
  212. fprintf (stdout, "Cust Num: %d, Fname: %s, Lname: %s\n", cust_num[i], fname, lname);
  213. }
  214. fprintf (stdout, "\nSTEP 3 done...original data retrieved from 'customer' table\n");
  215. /* STEP 4. Update the 'lname' field of the 'customer' table for cust_num = 102
  216. ** Call SQLSetPos to update the data in the table
  217. ** Call SQLEndTran to commit the transaction
  218. */
  219. /* Update the value in the 'lname' array where cust_num = 102 to 'Miller' */
  220. for (i=0; i< NUM_ROWS; i++)
  221. {
  222. if (cust_num[i] == 102)
  223. {
  224. wcscpy ((SQLWCHAR*) lnameW[i], (SQLWCHAR*) newLnameW);
  225. cbLname[i] = wcslen(newLnameW) * sizeof(SQLWCHAR);
  226. /* Call SQLSetPos to update the data */
  227. rc = SQLSetPos (hstmt, (SQLUSMALLINT) (i+1), (SQLUSMALLINT) SQL_UPDATE, (SQLUSMALLINT) SQL_LOCK_NO_CHANGE);
  228. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLSetPos\n"))
  229. goto Exit;
  230. break;
  231. }
  232. }
  233. /* Commit the transaction */
  234. rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  235. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLEndtran\n"))
  236. goto Exit;
  237. fprintf (stdout, "STEP 4 done...row updated using SQLSetPos\n");
  238. /* STEP 5. Retrieve updated data from the 'customer' table
  239. ** Display the results
  240. */
  241. /* Retrieve updated data from the 'customer' table */
  242. fprintf (stdout, "Retrieving data from 'customer' table\n\n");
  243. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hNewStmt );
  244. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  245. return (1);
  246. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  247. rc = SQLSetStmtAttr (hNewStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_ROWS, 0);
  248. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  249. goto Exit;
  250. rc = SQLExecDirectW (hNewStmt, selectStmtW, SQL_NTS);
  251. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  252. goto Exit;
  253. /* Bind the result set columns */
  254. rc = SQLBindCol (hNewStmt, 1, SQL_C_LONG, &newcust_num[0], 0, &newcbCustNum[0]);
  255. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 1)\n"))
  256. goto Exit;
  257. rc = SQLBindCol (hNewStmt, 2, SQL_C_WCHAR, newfnameW[0], BUFFER_LENW, &newcbFname[0]);
  258. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 2)\n"))
  259. goto Exit;
  260. rc = SQLBindCol (hNewStmt, 3, SQL_C_WCHAR, newlnameW[0], BUFFER_LENW, &newcbLname[0]);
  261. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 3)\n"))
  262. goto Exit;
  263. /* Fetch the results */
  264. rc = SQLFetch (hNewStmt);
  265. if (rc == SQL_NO_DATA_FOUND)
  266. fprintf (stdout, "NO DATA FOUND!!\n EXITING!!");
  267. else if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLFetch failed\n"))
  268. goto Exit;
  269. for (i=0; i<NUM_ROWS; i++)
  270. {
  271. /* Display the results */
  272. newfname = (SQLCHAR *) malloc (wcslen(newfnameW[i]) + sizeof(char));
  273. wcstombs( (char *) newfname, newfnameW[i],
  274. wcslen(newfnameW[i]) + sizeof(char));
  275. newlname = (SQLCHAR *) malloc (wcslen(newlnameW[i]) + sizeof(char));
  276. wcstombs( (char *) newlname, newlnameW[i],
  277. wcslen(newlnameW[i]) + sizeof(char));
  278. fprintf (stdout, "Cust Num: %d, Fname: %s, Lname: %s\n", newcust_num[i], newfname, newlname);
  279. }
  280. fprintf (stdout, "\nSTEP 5 done...updated data retrieved from 'customer' table\n");
  281. Exit:
  282. /* CLEANUP: Close the statement handle
  283. ** Free the statement handle
  284. ** Disconnect from the datasource
  285. ** Free the connection and environment handles
  286. ** Exit
  287. */
  288. /* Close the statement handle */
  289. SQLFreeStmt (hstmt, SQL_CLOSE);
  290. SQLFreeStmt (hNewStmt, SQL_CLOSE);
  291. /* Free the statement handle */
  292. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  293. SQLFreeHandle (SQL_HANDLE_STMT, hNewStmt);
  294. /* Disconnect from the data source */
  295. SQLDisconnect (hdbc);
  296. /* Free the environment handle and the database connection handle */
  297. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  298. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  299. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  300. in = getchar ();
  301. return (rc);
  302. }