123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <wchar.h>
- #include <errno.h>
- #ifndef NO_WIN32
- #include <io.h>
- #include <windows.h>
- #include <conio.h>
- #endif
- #include <fcntl.h>
- #include "infxcli.h"
- #define BUFFER_LEN 25
- #define BUFFER_LENW BUFFER_LEN * sizeof(SQLWCHAR)
- #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));
- if (sqlState == NULL)
- {
- return -1;
- }
- wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
- + sizeof(char));
- errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
- if (errMsg == NULL)
- {
- return -1;
- }
- 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;
-
- SQLINTEGER lofd;
- SQLLEN lofd_valsize = 0;
-
- SQLWCHAR* loptr_bufferW;
- SQLSMALLINT loptr_size;
- SQLLEN loptr_valsize = 0;
-
- SQLWCHAR* lospec_bufferW;
- SQLSMALLINT lospec_size;
- SQLLEN lospec_valsize = 0;
-
- SQLWCHAR* write_bufferW;
- off_t write_size;
- SQLLEN write_valsize = 0;
- size_t status;
- struct stat statbuf;
- int fd;
-
- SQLWCHAR *dsnW;
- SQLRETURN rc = 0;
- SQLINTEGER in;
- SQLWCHAR verInfoBufferW[BUFFER_LENW];
- SQLSMALLINT verInfoLen;
- SQLCHAR* lo_file_name = (SQLCHAR *) "advert.txt";
- SQLWCHAR colnameW[BUFFER_LEN] = L"item.advert";
- SQLLEN colname_size = SQL_NTS;
- SQLINTEGER mode = LO_RDWR;
- SQLLEN cbMode = 0;
- SQLWCHAR* insertStmtW = (SQLWCHAR *) L"INSERT INTO item VALUES (1005, 'Helmet', 235, 'Each', '39.95', ?)";
- int lenArgv1;
-
-
- if (argc != 2)
- {
-
- fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
- dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
- + sizeof(SQLWCHAR) );
- if (dsnW == NULL)
- {
- fprintf(stdout, "Failed to allocate memory for DSN\n");
- goto Exit;
- }
- wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
- }
- else
- {
-
- lenArgv1 = strlen((char *)argv[1]);
- dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
- + sizeof(SQLWCHAR));
- if (dsnW == NULL)
- {
- fprintf(stdout, "Failed to allocate memory for DSN\n");
- goto Exit;
- }
- 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!!\n");
- 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!!\n"))
- 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!!\n"))
- 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\n"))
- return (1);
-
- rc = SQLGetInfoW (hdbc, SQL_DBMS_VER, verInfoBufferW, BUFFER_LENW, &verInfoLen);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
- return 1;
- if ((wcsncmp ((SQLWCHAR *) verInfoBufferW, L"09", 2)) < 0 )
- {
- fprintf (stdout, "\n** This test can only be run against database server -- version 9 or higher **\n");
- 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 = SQLGetInfo (hdbc, SQL_INFX_LO_SPEC_LENGTH, &lospec_size,
- sizeof(lospec_size), NULL);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLGetInfo failed\n"))
- goto Exit;
-
- lospec_bufferW = malloc (lospec_size);
- if (lospec_bufferW == NULL)
- {
- fprintf(stdout, "Failed to allocate memory for the smart large object specification buffer \n");
- goto Exit;
- }
-
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
- SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0,
- lospec_bufferW, lospec_size, &lospec_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter failed\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_def_create_spec(?)}", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
- goto Exit;
- fprintf (stdout, "STEP 2 done...default smart large object specification structure created\n");
-
-
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_CHAR,
- BUFFER_LEN, 0, colnameW, BUFFER_LENW, &colname_size);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed (param 1)\n"))
- goto Exit;
- lospec_valsize = lospec_size;
- rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
- SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0, lospec_bufferW,
- lospec_size, &lospec_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed (param 2)\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_col_info(?, ?)}", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
- goto Exit;
- fprintf(stdout, "STEP 3 done...smart large object specification structure initialised\n");
-
-
- rc = SQLGetInfo (hdbc, SQL_INFX_LO_PTR_LENGTH, &loptr_size, sizeof(loptr_size), NULL);
- if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 4 -- SQLGetInfo failed\n"))
- goto Exit;
-
- loptr_bufferW = malloc (loptr_size);
- if (loptr_bufferW == NULL)
- {
- fprintf(stdout, "Failed to allocate memory for the smart large object pointer structure buffer \n");
- goto Exit;
- }
- fprintf (stdout, "STEP 4 done...smart large object pointer structure allocated\n");
-
-
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
- (UDWORD)lospec_size, 0, lospec_bufferW, lospec_size, &lospec_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
- goto Exit;
- rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
- (UDWORD)0, 0, &mode, sizeof(mode), &cbMode);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 2)\n"))
- goto Exit;
- loptr_valsize = loptr_size;
- rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
- (UDWORD)loptr_size, 0, loptr_bufferW, loptr_size, &loptr_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 3)\n"))
- goto Exit;
- rc = SQLBindParameter (hstmt, 4, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER,
- (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 4)\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_create(?, ?, ?, ?)}", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLFreeStmt failed\n"))
- goto Exit;
- fprintf (stdout, "STEP 5 done...smart large object created\n");
-
-
- if (stat( (char *)lo_file_name,&statbuf) == -1)
- {
- fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
- exit(1);
- }
- write_size = statbuf.st_size;
-
- write_bufferW = malloc (write_size + 1);
- if (write_bufferW == NULL)
- {
- fprintf(stdout, "Failed to allocate memory for the smart large object data buffer \n");
- goto Exit;
- }
-
- fd = open ((char *) lo_file_name, O_RDONLY);
- if (fd == -1)
- {
- fprintf (stdout, "Error %d creating file descriptor for %s\n", errno, lo_file_name);
- exit(1);
- }
- status = read (fd, write_bufferW, write_size);
- if (status < 0)
- {
- fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
- }
- if (close(fd) < 0)
- {
- fprintf (stdout, "Error %d closing the file %s\n", errno, lo_file_name);
- exit(1);
- }
- write_bufferW[write_size] = L'\0';
- write_valsize = write_size;
-
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
- (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
- goto Exit;
- rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
- (UDWORD)write_size, 0, write_bufferW, write_size, &write_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 2)\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_write(?, ?)}", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLFreeStmt failed\n"))
- goto Exit;
- fprintf (stdout, "STEP 6 done...data written to new smart large object\n");
-
-
- loptr_valsize = loptr_size;
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
- (UDWORD)loptr_size, 0, loptr_bufferW, loptr_size, &loptr_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, insertStmtW, SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLExecDirect failed\n"))
- goto Exit;
-
- rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLFreeStmt failed\n"))
- goto Exit;
- fprintf (stdout, "STEP 7 done...smart large object inserted into the database\n");
-
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
- (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLBindParameter failed\n"))
- goto Exit;
- rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{call ifx_lo_close(?)}", SQL_NTS);
- if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLExecDirect failed\n"))
- goto Exit;
- fprintf (stdout, "STEP 8 done...smart large object closed\n");
-
- if (lospec_bufferW)
- free (lospec_bufferW);
- if (loptr_bufferW)
- free (loptr_bufferW);
- if (write_bufferW)
- free (write_bufferW);
- fprintf (stdout, "STEP 9 done...smart large object buffers freed\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);
- }
|