123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef NO_WIN32
- #include <io.h>
- #include <windows.h>
- #include <conio.h>
- #endif
- #include "infxcli.h"
- #define BUFFER_LEN 15
- #define ERRMSG_LEN 200
- #define NUM_OLD_ORDERS 9
- #define NUM_NEW_ORDERS 9
- #define NUM_ORDERS 18
- SQLCHAR defDsn[] = "odbc_demo";
- typedef struct
- {
-
- SQLINTEGER order_num;
- SQLLEN cbOrderNum;
- SQLINTEGER cust_num;
- SQLLEN cbCustNum;
- SQLINTEGER item_num;
- SQLLEN cbItemNum;
- SQLCHAR order_date[BUFFER_LEN];
- SQLLEN cbOrderDate;
- SQLINTEGER quantity;
- SQLLEN cbQuantity;
- } orderStruct;
- SQLINTEGER checkError (SQLRETURN rc,
- SQLSMALLINT handleType,
- SQLHANDLE handle,
- SQLCHAR* errmsg)
- {
- SQLRETURN retcode = SQL_SUCCESS;
- SQLSMALLINT errNum = 1;
- SQLCHAR sqlState[6];
- SQLINTEGER nativeError;
- SQLCHAR errMsg[ERRMSG_LEN];
- SQLSMALLINT textLengthPtr;
-
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- while (retcode != SQL_NO_DATA)
- {
- retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
- if (retcode == SQL_INVALID_HANDLE)
- {
- fprintf (stderr, "checkError function was called with an invalid handle!!\n");
- return 1;
- }
- if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
- fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
- errNum++;
- }
- fprintf (stderr, "%s\n", errmsg);
- return 1;
- }
- else
- return 0;
- }
- int main (long argc,
- char* argv[])
- {
-
-
- SQLHDBC hdbc;
- SQLHENV henv;
- SQLHSTMT hstmt;
-
- SQLCHAR dsn[20];
- SQLRETURN rc = 0;
- SQLINTEGER i, in;
-
- SQLCHAR * selectStmt = (UCHAR *) "SELECT order_num, cust_num, item_num, order_date, quantity FROM orders";
- orderStruct orders[NUM_ORDERS];
- SQLLEN bindOffset = 0;
-
-
- if (argc != 2)
- {
-
- fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
- strcpy ((char *)dsn, (char *)defDsn);
- }
- else
- {
-
- strcpy ((char *)dsn, (char *)argv[1]);
- fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
- }
-
-
- rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
- if (rc != SQL_SUCCESS)
- {
- fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
- return (1);
- }
-
- rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
- if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
- return (1);
-
- rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
- if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
- return (1);
-
- rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
- return (1);
- rc = SQLSetConnectAttr(hdbc, SQL_INFX_ATTR_LENGTHINCHARFORDIAGRECW, (void *)SQL_TRUE, SQL_IS_UINTEGER);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Setting LENGTH IN CHAR FOR DIAG RECW \nExiting!!"))
- return (1);
-
- rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
- return (1);
- fprintf (stdout, "STEP 1 done...connected to database\n");
-
-
- rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER) sizeof(orderStruct), 0);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_TYPE\n"))
- goto Exit;
-
- rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_OLD_ORDERS, 0);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
- goto Exit;
-
- rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_BIND_OFFSET_PTR, (SQLPOINTER) &bindOffset, 0);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_BIND_OFFSET_PTR\n"))
- goto Exit;
-
- rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_LOCK, 0);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CONCURRENCY\n"))
- goto Exit;
- fprintf (stdout, "STEP 2 done...Statement attributes set\n");
-
-
- rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &orders[0].order_num, 0, &orders[0].cbOrderNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &orders[0].cust_num, 0, &orders[0].cbCustNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 3, SQL_C_LONG, &orders[0].item_num, 0, &orders[0].cbItemNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 3)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 4, SQL_C_CHAR, &orders[0].order_date, BUFFER_LEN, &orders[0].cbOrderDate);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 4)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 5, SQL_C_LONG, &orders[0].quantity, 0, &orders[0].cbQuantity);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 5)\n"))
- goto Exit;
-
- rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFetch (hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- {
- fprintf (stdout, "NO DATA FOUND!!\n Exiting!!");
- goto Exit;
- }
- else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
- goto Exit;
- fprintf(stdout, "Retrieved rows from 'orders' table\n\n\n");
- for (i=0; i<NUM_OLD_ORDERS; i++)
- {
-
- fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
- orders[i].order_num, orders[i].cust_num, orders[i].item_num, orders[i].order_date, orders[i].quantity);
- }
- fprintf (stdout, "STEP 3 done...Existing data retrieved from table 'orders'\n");
-
-
-
- fprintf(stdout, "New values for data in the 'orders' table\n\n\n");
- for (i=0; i<NUM_NEW_ORDERS; i++)
- {
- orders[NUM_OLD_ORDERS+i].order_num = NUM_OLD_ORDERS+i+1;
- orders[NUM_OLD_ORDERS+i].cust_num = 101+(i%100);
- orders[NUM_OLD_ORDERS+i].item_num = 1001+(i%4);
- strcpy ((char *) orders[NUM_OLD_ORDERS+i].order_date, "1999-03-05");
- orders[NUM_OLD_ORDERS+i].quantity = i;
- orders[NUM_OLD_ORDERS+i].cbOrderNum = 0;
- orders[NUM_OLD_ORDERS+i].cbCustNum = 0;
- orders[NUM_OLD_ORDERS+i].cbItemNum = 0;
- orders[NUM_OLD_ORDERS+i].cbOrderDate = SQL_NTS;
- orders[NUM_OLD_ORDERS+i].cbQuantity = 0;
- }
- for (i=0; i<NUM_NEW_ORDERS; i++)
- {
-
- fprintf (stdout, "Order Num: %d Cust Num: %d Item Num: %d Order Date %s Quantity: %d\n",
- 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);
- }
- fprintf (stdout, "STEP 4 done...Values of new rows set in orders array\n");
-
-
-
- bindOffset = NUM_OLD_ORDERS*sizeof(orderStruct);
-
- rc = SQLBulkOperations (hstmt, SQL_ADD);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBulkOperations\n"))
- goto Exit;
- fprintf (stdout, "STEP 5 done...New rows inserted in table 'orders'\n");
- Exit:
-
-
- SQLFreeStmt (hstmt, SQL_CLOSE);
-
- SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
-
- SQLDisconnect (hdbc);
-
- SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
- SQLFreeHandle (SQL_HANDLE_ENV, henv);
- fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
- in = getchar ();
- return (rc);
- }
|