123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- #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 10
- #define ERRMSG_LEN 200
- SQLWCHAR defDsnW[] = L"odbc_demo";
- SQLCHAR defDsn[] = "odbc_demo";
- SQLINTEGER checkError (SQLRETURN rc,
- SQLSMALLINT handleType,
- SQLHANDLE handle,
- SQLCHAR* errmsg)
- {
- SQLRETURN retcode = SQL_SUCCESS;
- SQLSMALLINT errNum = 1;
- SQLWCHAR sqlStateW[6];
- SQLCHAR *sqlState;
- SQLINTEGER nativeError;
- SQLWCHAR errMsgW[ERRMSG_LEN];
- SQLCHAR *errMsg;
- SQLSMALLINT textLengthPtr;
- if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
- {
- while (retcode != SQL_NO_DATA)
- {
- retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, 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))
- {
- sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
- ;
- wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
- + sizeof(char));
- errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
- wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
- + sizeof(char));
- 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;
-
- SQLWCHAR *dsnW;
- SQLRETURN rc = 0;
- SQLINTEGER in;
- SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT order_num, quantity FROM orders";
- SQLWCHAR* updateStmtW = (SQLWCHAR *) L"UPDATE orders SET quantity = quantity*2";
- SQLLEN order_num, quantity, cbOrderNum = 0, cbQuantity = 0;
- int lenArgv1;
-
-
-
- if (argc != 2)
- {
-
- fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
- dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
- + sizeof(SQLWCHAR) );
- wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
- }
- else
- {
-
- lenArgv1 = strlen((char *)argv[1]);
- dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
- + sizeof(SQLWCHAR));
- mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
- fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
- }
-
-
- 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 = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\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 = SQLSetConnectAttr (hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, (SQLINTEGER) NULL);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLSetConnectAttr failed\n"))
- goto Exit;
-
- fprintf (stdout, "Retrieving data from 'orders' table\n\n");
- rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 0, &cbOrderNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 1)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &quantity, 5, &cbQuantity);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindCol failed (column 2)\n"))
- goto Exit;
-
- while (1)
- {
- rc = SQLFetch (hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- break;
- else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFetch failed\n"))
- goto Exit;
-
- fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
- }
-
- rc = SQLCloseCursor(hstmt);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLCloseCursor failed\n"))
- goto Exit;
- fprintf (stdout, "\nSTEP 2 done...auto commit turned off & original data retrieved from the database\n");
-
-
- rc = SQLExecDirectW (hstmt, updateStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for update stmt\n"))
- goto Exit;
-
- rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 3 -- SQLEndTran failed\n"))
- goto Exit;
-
- fprintf (stdout, "Retrieving data from 'orders' table after rollback\n\n");
- rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for select stmt\n"))
- goto Exit;
-
- rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 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, &quantity, 5, &cbQuantity);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
- goto Exit;
-
- while (1)
- {
- rc = SQLFetch (hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- break;
- else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
- goto Exit;
-
- fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
- }
-
- rc = SQLCloseCursor(hstmt);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
- goto Exit;
- fprintf (stdout, "\nSTEP 3 done...transaction successfully rolled back...\n\t...data retrieved should be same as in step 2\n");
- fprintf (stdout,"\n\nHit <Enter> to continue...\n\n");
- in = getchar ();
-
-
- rc = SQLExecDirectW (hstmt, updateStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed for update stmt\n"))
- goto Exit;
-
- rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 4 -- SQLEndTran failed\n"))
- goto Exit;
-
- fprintf (stdout, "Retrieving data from 'orders' table after commit\n\n");
- rc = SQLExecDirectW (hstmt, selectStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed for select stmt\n"))
- goto Exit;
-
- rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &order_num, 0, &cbOrderNum);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindCol failed (column 1)\n"))
- goto Exit;
- rc = SQLBindCol (hstmt, 2, SQL_C_LONG, &quantity, 5, &cbQuantity);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindCol failed (column 2)\n"))
- goto Exit;
-
- while (1)
- {
- rc = SQLFetch (hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- break;
- else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLFetch failed\n"))
- goto Exit;
-
- fprintf (stdout, "Order Num: %d, Quantity: %d\n", order_num, quantity);
- }
-
- rc = SQLCloseCursor(hstmt);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLCloseCursor failed\n"))
- goto Exit;
- fprintf (stdout, "\n\nSTEP 4 done...transaction successfully committed...\n\t...data retrieved should be updated data\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);
- }
|