blkinsrt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2010 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: 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 ERRMSG_LEN 200
  33. #define NUM_OLD_ORDERS 9
  34. #define NUM_NEW_ORDERS 9
  35. #define NUM_ORDERS 18
  36. SQLCHAR defDsn[] = "odbc_demo";
  37. typedef struct
  38. {
  39. /* data values */
  40. SQLINTEGER order_num;
  41. SQLLEN cbOrderNum;
  42. SQLINTEGER cust_num;
  43. SQLLEN cbCustNum;
  44. SQLINTEGER item_num;
  45. SQLLEN cbItemNum;
  46. SQLCHAR order_date[BUFFER_LEN];
  47. SQLLEN cbOrderDate;
  48. SQLINTEGER quantity;
  49. SQLLEN cbQuantity;
  50. } orderStruct;
  51. SQLINTEGER checkError (SQLRETURN rc,
  52. SQLSMALLINT handleType,
  53. SQLHANDLE handle,
  54. SQLCHAR* errmsg)
  55. {
  56. SQLRETURN retcode = SQL_SUCCESS;
  57. SQLSMALLINT errNum = 1;
  58. SQLCHAR sqlState[6];
  59. SQLINTEGER nativeError;
  60. SQLCHAR errMsg[ERRMSG_LEN];
  61. SQLSMALLINT textLengthPtr;
  62. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  63. {
  64. while (retcode != SQL_NO_DATA)
  65. {
  66. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  67. if (retcode == SQL_INVALID_HANDLE)
  68. {
  69. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  70. return 1;
  71. }
  72. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  73. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  74. errNum++;
  75. }
  76. fprintf (stderr, "%s\n", errmsg);
  77. return 1; /* all errors on this handle have been reported */
  78. }
  79. else
  80. return 0; /* no errors to report */
  81. }
  82. int main (long argc,
  83. char* argv[])
  84. {
  85. /* Declare variables
  86. */
  87. /* Handles */
  88. SQLHDBC hdbc;
  89. SQLHENV henv;
  90. SQLHSTMT hstmt;
  91. /* Miscellaneous variables */
  92. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  93. SQLRETURN rc = 0;
  94. SQLINTEGER i, in;
  95. SQLCHAR * selectStmt = (UCHAR *) "SELECT order_num, cust_num, item_num, order_date, quantity FROM orders";
  96. orderStruct orders[NUM_ORDERS]; /*has elements for the current rowset,and
  97. for the new rows being inserted*/
  98. SQLLEN bindOffset = 0;
  99. /* STEP 1. Get data source name from command line (or use default)
  100. ** Allocate the environment handle and set ODBC version
  101. ** Allocate the connection handle
  102. ** Establish the database connection
  103. ** Allocate the statement handle
  104. */
  105. /* If(dsn is not explicitly passed in as arg) */
  106. if (argc != 2)
  107. {
  108. /* Use default dsn - odbc_demo */
  109. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  110. strcpy ((char *)dsn, (char *)defDsn);
  111. }
  112. else
  113. {
  114. /* Use specified dsn */
  115. strcpy ((char *)dsn, (char *)argv[1]);
  116. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  117. }
  118. /* Allocate the Environment handle */
  119. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  120. if (rc != SQL_SUCCESS)
  121. {
  122. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  123. return (1);
  124. }
  125. /* Set the ODBC version to 3.0 */
  126. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
  127. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  128. return (1);
  129. /* Allocate the connection handle */
  130. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  131. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  132. return (1);
  133. /* Connect to the database */
  134. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  135. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  136. return (1);
  137. rc = SQLSetConnectAttr(hdbc, SQL_INFX_ATTR_LENGTHINCHARFORDIAGRECW, (void *)SQL_TRUE, SQL_IS_UINTEGER);
  138. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Setting LENGTH IN CHAR FOR DIAG RECW \nExiting!!"))
  139. return (1);
  140. /* Allocate the statement handle */
  141. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  142. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  143. return (1);
  144. fprintf (stdout, "STEP 1 done...connected to database\n");
  145. /* STEP 2 Set the following statement attributes
  146. ** -- SQL_ATTR_ROW_BIND_TYPE to the size of the structure
  147. ** 'orderStruct', so that the binding orientation of SQLFetch
  148. ** is set to row-wise binding
  149. ** SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound
  150. ** SQL_ATTR_ROW_BIND_OFFSET_PTR to the address of an integer
  151. ** that contains the offset added to pointers to change
  152. ** binding of column data
  153. ** SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK
  154. **
  155. */
  156. /* Set the statement attribute SQL_ATTR_ROW_BIND_TYPE */
  157. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER) sizeof(orderStruct), 0);
  158. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_TYPE\n"))
  159. goto Exit;
  160. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  161. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_OLD_ORDERS, 0);
  162. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  163. goto Exit;
  164. /* Set the statement attribute SQL_ATTR_ROW_BIND_OFFSET_PTR to the binding offset */
  165. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_OFFSET_PTR, (SQLPOINTER) &bindOffset, 0);
  166. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_OFFSET_PTR\n"))
  167. goto Exit;
  168. /* Set the statement attribute SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK */
  169. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_LOCK, 0);
  170. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CONCURRENCY\n"))
  171. goto Exit;
  172. fprintf (stdout, "STEP 2 done...Statement attributes set\n");
  173. /* STEP 3. Retrieve existing data in table 'orders'
  174. ** Bind the result set columns
  175. ** Display the results
  176. */
  177. /* Bind the result set columns */
  178. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &orders[0].order_num, 0, &orders[0].cbOrderNum);
  179. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
  180. goto Exit;
  181. rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &orders[0].cust_num, 0, &orders[0].cbCustNum);
  182. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
  183. goto Exit;
  184. rc = SQLBindCol (hstmt, 3, SQL_C_LONG, &orders[0].item_num, 0, &orders[0].cbItemNum);
  185. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 3)\n"))
  186. goto Exit;
  187. rc = SQLBindCol (hstmt, 4, SQL_C_CHAR, &orders[0].order_date, BUFFER_LEN, &orders[0].cbOrderDate);
  188. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 4)\n"))
  189. goto Exit;
  190. rc = SQLBindCol (hstmt, 5, SQL_C_LONG, &orders[0].quantity, 0, &orders[0].cbQuantity);
  191. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 5)\n"))
  192. goto Exit;
  193. /* Execute the SELECT statement */
  194. rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
  195. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  196. goto Exit;
  197. /* Fetch and display the results */
  198. rc = SQLFetch (hstmt);
  199. if (rc == SQL_NO_DATA_FOUND)
  200. {
  201. fprintf (stdout, "NO DATA FOUND!!\n Exiting!!");
  202. goto Exit;
  203. }
  204. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  205. goto Exit;
  206. fprintf(stdout, "Retrieved rows from 'orders' table\n\n\n");
  207. for (i=0; i<NUM_OLD_ORDERS; i++)
  208. {
  209. /* Display the results */
  210. fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
  211. orders[i].order_num, orders[i].cust_num, orders[i].item_num, orders[i].order_date, orders[i].quantity);
  212. }
  213. fprintf (stdout, "STEP 3 done...Existing data retrieved from table 'orders'\n");
  214. /* STEP 4. Set the values of the orders array for the new rows to be inserted
  215. */
  216. fprintf(stdout, "New values for data in the 'orders' table\n\n\n");
  217. for (i=0; i<NUM_NEW_ORDERS; i++)
  218. {
  219. orders[NUM_OLD_ORDERS+i].order_num = NUM_OLD_ORDERS+i+1;
  220. orders[NUM_OLD_ORDERS+i].cust_num = 101+(i%100);
  221. orders[NUM_OLD_ORDERS+i].item_num = 1001+(i%4);
  222. strcpy ((char *) orders[NUM_OLD_ORDERS+i].order_date, "1999-03-05");
  223. orders[NUM_OLD_ORDERS+i].quantity = i;
  224. orders[NUM_OLD_ORDERS+i].cbOrderNum = 0;
  225. orders[NUM_OLD_ORDERS+i].cbCustNum = 0;
  226. orders[NUM_OLD_ORDERS+i].cbItemNum = 0;
  227. orders[NUM_OLD_ORDERS+i].cbOrderDate = SQL_NTS;
  228. orders[NUM_OLD_ORDERS+i].cbQuantity = 0;
  229. }
  230. for (i=0; i<NUM_NEW_ORDERS; i++)
  231. {
  232. /* Display the values */
  233. fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
  234. orders[NUM_OLD_ORDERS+i].order_num, orders[NUM_OLD_ORDERS+i].cust_num, orders[NUM_OLD_ORDERS+i].item_num, orders[NUM_OLD_ORDERS+i].order_date, orders[NUM_OLD_ORDERS+i].quantity);
  235. }
  236. fprintf (stdout, "STEP 4 done...Values of new rows set in orders array\n");
  237. /* STEP 5. Change the bind offset
  238. ** Call SQLBulkOperations to insert the new rows
  239. */
  240. /* Change the bid offset */
  241. bindOffset = NUM_OLD_ORDERS*sizeof(orderStruct);
  242. /* Call SQLBulkOperations */
  243. rc = SQLBulkOperations (hstmt, SQL_ADD);
  244. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBulkOperations\n"))
  245. goto Exit;
  246. fprintf (stdout, "STEP 5 done...New rows inserted in table 'orders'\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. }