blkinsrtW.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * (C) 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: blkinsrtW.c (Unicode counterpart of blkinsrt.c)
  14. *
  15. * Description: To insert multiple rows into a database table using
  16. * SQLBulkOperations
  17. * -- the rows are inserted into the 'orders' table
  18. *
  19. *
  20. ***************************************************************************
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #ifndef NO_WIN32
  26. #include <io.h>
  27. #include <windows.h>
  28. #include <conio.h>
  29. #endif /*NO_WIN32*/
  30. #include "infxcli.h"
  31. #define BUFFER_LEN 15
  32. #define BUFFER_LENW BUFFER_LEN * sizeof(SQLWCHAR)
  33. #define ERRMSG_LEN 200
  34. #define NUM_OLD_ORDERS 9
  35. #define NUM_NEW_ORDERS 9
  36. #define NUM_ORDERS 18
  37. SQLWCHAR defDsnW[] = L"odbc_demo";
  38. SQLCHAR defDsn[] = "odbc_demo";
  39. typedef struct
  40. {
  41. /* data values */
  42. SQLLEN order_num;
  43. SQLLEN cbOrderNum;
  44. SQLLEN cust_num;
  45. SQLLEN cbCustNum;
  46. SQLLEN item_num;
  47. SQLLEN cbItemNum;
  48. SQLWCHAR order_dateW[BUFFER_LEN];
  49. SQLLEN cbOrderDate;
  50. SQLLEN quantity;
  51. SQLLEN cbQuantity;
  52. } orderStruct;
  53. SQLINTEGER checkError (SQLRETURN rc,
  54. SQLSMALLINT handleType,
  55. SQLHANDLE handle,
  56. SQLCHAR* errmsg)
  57. {
  58. SQLRETURN retcode = SQL_SUCCESS;
  59. SQLSMALLINT errNum = 1;
  60. SQLWCHAR sqlStateW[6];
  61. SQLCHAR *sqlState;
  62. SQLINTEGER nativeError;
  63. SQLWCHAR errMsgW[ERRMSG_LEN];
  64. SQLCHAR *errMsg;
  65. SQLSMALLINT textLengthPtr;
  66. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  67. {
  68. while (retcode != SQL_NO_DATA)
  69. {
  70. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  71. if (retcode == SQL_INVALID_HANDLE)
  72. {
  73. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  74. return 1;
  75. }
  76. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  77. {
  78. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  79. ;
  80. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  81. + sizeof(char));
  82. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  83. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  84. + sizeof(char));
  85. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  86. }
  87. errNum++;
  88. }
  89. fprintf (stderr, "%s\n", errmsg);
  90. return 1; /* all errors on this handle have been reported */
  91. }
  92. else
  93. return 0; /* no errors to report */
  94. }
  95. int main (long argc,
  96. char* argv[])
  97. {
  98. /* Declare variables
  99. */
  100. /* Handles */
  101. SQLHDBC hdbc;
  102. SQLHENV henv;
  103. SQLHSTMT hstmt;
  104. /* Miscellaneous variables */
  105. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  106. SQLRETURN rc = 0;
  107. SQLINTEGER i, in;
  108. SQLCHAR *order_date;
  109. SQLWCHAR * selectStmtW = L"SELECT order_num, cust_num, item_num, order_date, quantity FROM orders";
  110. orderStruct orders[NUM_ORDERS]; /*has elements for the current rowset,and
  111. for the new rows being inserted*/
  112. SQLLEN bindOffset = 0;
  113. int lenArgv1;
  114. /* STEP 1. Get data source name from command line (or use default)
  115. ** Allocate the environment handle and set ODBC version
  116. ** Allocate the connection handle
  117. ** Establish the database connection
  118. ** Allocate the statement handle
  119. */
  120. /* If(dsnW is not explicitly passed in as arg) */
  121. if (argc != 2)
  122. {
  123. /* Use default dsnW - odbc_demo */
  124. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  125. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  126. + sizeof(SQLWCHAR) );
  127. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  128. }
  129. else
  130. {
  131. /* Use specified dsnW */
  132. lenArgv1 = strlen((char *)argv[1]);
  133. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  134. + sizeof(SQLWCHAR));
  135. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  136. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  137. }
  138. /* Allocate the Environment handle */
  139. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  140. if (rc != SQL_SUCCESS)
  141. {
  142. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  143. return (1);
  144. }
  145. /* Set the ODBC version to 3.0 */
  146. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
  147. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  148. return (1);
  149. /* Allocate the connection handle */
  150. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  151. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  152. return (1);
  153. /* Connect to the database */
  154. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  155. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  156. return (1);
  157. /* Allocate the statement handle */
  158. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  159. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  160. return (1);
  161. fprintf (stdout, "STEP 1 done...connected to database\n");
  162. /* STEP 2 Set the following statement attributes
  163. ** -- SQL_ATTR_ROW_BIND_TYPE to the size of the structure
  164. ** 'orderStruct', so that the binding orientation of SQLFetch
  165. ** is set to row-wise binding
  166. ** SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound
  167. ** SQL_ATTR_ROW_BIND_OFFSET_PTR to the address of an integer
  168. ** that contains the offset added to pointers to change
  169. ** binding of column data
  170. ** SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK
  171. **
  172. */
  173. /* Set the statement attribute SQL_ATTR_ROW_BIND_TYPE */
  174. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER) sizeof(orderStruct), 0);
  175. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_TYPE\n"))
  176. goto Exit;
  177. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  178. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_OLD_ORDERS, 0);
  179. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  180. goto Exit;
  181. /* Set the statement attribute SQL_ATTR_ROW_BIND_OFFSET_PTR to the binding offset */
  182. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_OFFSET_PTR, (SQLPOINTER) &bindOffset, 0);
  183. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_OFFSET_PTR\n"))
  184. goto Exit;
  185. /* Set the statement attribute SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK */
  186. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_LOCK, 0);
  187. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CONCURRENCY\n"))
  188. goto Exit;
  189. fprintf (stdout, "STEP 2 done...Statement attributes set\n");
  190. /* STEP 3. Retrieve existing data in table 'orders'
  191. ** Bind the result set columns
  192. ** Display the results
  193. */
  194. /* Bind the result set columns */
  195. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &orders[0].order_num, 0, &orders[0].cbOrderNum);
  196. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
  197. goto Exit;
  198. rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &orders[0].cust_num, 0, &orders[0].cbCustNum);
  199. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
  200. goto Exit;
  201. rc = SQLBindCol (hstmt, 3, SQL_C_LONG, &orders[0].item_num, 0, &orders[0].cbItemNum);
  202. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 3)\n"))
  203. goto Exit;
  204. rc = SQLBindCol (hstmt, 4, SQL_C_WCHAR, &orders[0].order_dateW, BUFFER_LENW, &orders[0].cbOrderDate);
  205. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 4)\n"))
  206. goto Exit;
  207. rc = SQLBindCol (hstmt, 5, SQL_C_LONG, &orders[0].quantity, 0, &orders[0].cbQuantity);
  208. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 5)\n"))
  209. goto Exit;
  210. /* Execute the SELECT statement */
  211. rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
  212. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  213. goto Exit;
  214. /* Fetch and display the results */
  215. rc = SQLFetch (hstmt);
  216. if (rc == SQL_NO_DATA_FOUND)
  217. {
  218. fprintf (stdout, "NO DATA FOUND!!\n Exiting!!");
  219. goto Exit;
  220. }
  221. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  222. goto Exit;
  223. fprintf(stdout, "Retrieved rows from 'orders' table\n\n\n");
  224. for (i=0; i<NUM_OLD_ORDERS; i++)
  225. {
  226. /* Display the results */
  227. order_date = (SQLCHAR *) malloc (wcslen(orders[i].order_dateW)
  228. + sizeof(char));
  229. wcstombs( (char *) order_date, orders[i].order_dateW,
  230. wcslen(orders[i].order_dateW) + sizeof(char));
  231. fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
  232. orders[i].order_num, orders[i].cust_num, orders[i].item_num, order_date, orders[i].quantity);
  233. }
  234. fprintf (stdout, "STEP 3 done...Existing data retrieved from table 'orders'\n");
  235. /* STEP 4. Set the values of the orders array for the new rows to be inserted
  236. */
  237. fprintf(stdout, "New values for data in the 'orders' table\n\n\n");
  238. for (i=0; i<NUM_NEW_ORDERS; i++)
  239. {
  240. orders[NUM_OLD_ORDERS+i].order_num = NUM_OLD_ORDERS+i+1;
  241. orders[NUM_OLD_ORDERS+i].cust_num = 101+(i%100);
  242. orders[NUM_OLD_ORDERS+i].item_num = 1001+(i%4);
  243. wcscpy ((SQLWCHAR *) orders[NUM_OLD_ORDERS+i].order_dateW, L"1999-03-05");
  244. orders[NUM_OLD_ORDERS+i].quantity = i;
  245. orders[NUM_OLD_ORDERS+i].cbOrderNum = 0;
  246. orders[NUM_OLD_ORDERS+i].cbCustNum = 0;
  247. orders[NUM_OLD_ORDERS+i].cbItemNum = 0;
  248. orders[NUM_OLD_ORDERS+i].cbOrderDate = SQL_NTS;
  249. orders[NUM_OLD_ORDERS+i].cbQuantity = 0;
  250. }
  251. for (i=0; i<NUM_NEW_ORDERS; i++)
  252. {
  253. order_date = (SQLCHAR *) malloc (wcslen(orders[NUM_OLD_ORDERS+i].order_dateW)
  254. + sizeof(char));
  255. wcstombs( (char *) order_date, orders[NUM_OLD_ORDERS+i].order_dateW,
  256. wcslen(orders[i].order_dateW) + sizeof(char));
  257. /* Display the values */
  258. fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
  259. orders[NUM_OLD_ORDERS+i].order_num, orders[NUM_OLD_ORDERS+i].cust_num, orders[NUM_OLD_ORDERS+i].item_num, order_date, orders[NUM_OLD_ORDERS+i].quantity);
  260. }
  261. fprintf (stdout, "STEP 4 done...Values of new rows set in orders array\n");
  262. /* STEP 5. Change the bind offset
  263. ** Call SQLBulkOperations to insert the new rows
  264. */
  265. /* Change the bid offset */
  266. bindOffset = NUM_OLD_ORDERS*sizeof(orderStruct);
  267. /* Call SQLBulkOperations */
  268. rc = SQLBulkOperations (hstmt, SQL_ADD);
  269. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBulkOperations\n"))
  270. goto Exit;
  271. fprintf (stdout, "STEP 5 done...New rows inserted in table 'orders'\n");
  272. Exit:
  273. /* CLEANUP: Close the statement handle
  274. ** Free the statement handle
  275. ** Disconnect from the datasource
  276. ** Free the connection and environment handles
  277. ** Exit
  278. */
  279. /* Close the statement handle */
  280. SQLFreeStmt (hstmt, SQL_CLOSE);
  281. /* Free the statement handle */
  282. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  283. /* Disconnect from the data source */
  284. SQLDisconnect (hdbc);
  285. /* Free the environment handle and the database connection handle */
  286. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  287. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  288. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  289. in = getchar ();
  290. return (rc);
  291. }