diff --git a/uppdev/odbctest/init b/uppdev/odbctest/init new file mode 100644 index 000000000..78a3537b2 --- /dev/null +++ b/uppdev/odbctest/init @@ -0,0 +1,5 @@ +#ifndef _odbctest_icpp_init_stub +#define _odbctest_icpp_init_stub +#include "Core/init" +#include "ODBC/init" +#endif diff --git a/uppdev/odbctest/odbctest.cpp b/uppdev/odbctest/odbctest.cpp new file mode 100644 index 000000000..d85dbd551 --- /dev/null +++ b/uppdev/odbctest/odbctest.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + +#define MAXDSN 25 +#define MAXUID 25 +#define MAXAUTHSTR 25 +#define MAXBUFLEN 255 +#define BUFFERSIZE 450 + +using namespace Upp; + +void ProcessLogMessages(HDBC hdbc, HSTMT hstmt, char *logstring) +{ + SQLCHAR SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH]; + SQLINTEGER NativeError; + SQLSMALLINT MsgLen; + if(SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 1, SqlState, &NativeError, + Msg, sizeof(Msg), &MsgLen) != SQL_NO_DATA) { + DDUMP((char *)SqlState); + DDUMP((char *)Msg); + } + LOG(logstring); +} + + +CONSOLE_APP_MAIN +{ + HENV henv = SQL_NULL_HENV; + HDBC hdbc1 = SQL_NULL_HDBC; + HSTMT hstmt1 = SQL_NULL_HSTMT; + char logstring[MAXBUFLEN] = ""; + + RETCODE retcode; + SWORD cntr; + //SQLGetData variables. + UCHAR Data[BUFFERSIZE]; + SDWORD cbBatch = (SDWORD)sizeof(Data)-1; + SDWORD cbBinSize; + // Clear data array. + for(cntr = 0; cntr < BUFFERSIZE; cntr++) + Data[cntr] = 0x00; + // Allocate the ODBC environment and save handle. + retcode = SQLAllocEnv (&henv); + + SQLAllocConnect(henv, &hdbc1); + + char tmpStr[1024]; + char maxStr[1024]; +// strcpy(tmpStr, "Provider=SQL Server Native Client 10.0;Data Source=QUAD\\SQLEXPRESS;Trusted_Connection=Yes;"); + strcpy(tmpStr, "DRIVER=SQL Server Native Client 10.0;SERVER=QUAD\\SQLEXPRESS;DATABASE=Holly;UID=test;PWD=koblih;"); +// strcpy(tmpStr, "DRIVER=SQL Server Native Client 10.0;SERVER=QUAD\\SQLEXPRESS;Trusted_Connection=Yes;"); + memset(maxStr, 0x00, sizeof(maxStr)); + SQLSMALLINT returnSize=0; + retcode = ::SQLDriverConnect(hdbc1, NULL, (SQLCHAR *)tmpStr, strlen(tmpStr), (SQLCHAR *)maxStr, sizeof(maxStr), &returnSize, + SQL_DRIVER_NOPROMPT ); + DDUMP(retcode); +/* + // Allocate ODBC connection and connect. + retcode = SQLAllocConnect(henv, &hdbc1); + // Make the connection, then print the information messages. + retcode = SQLConnect(hdbc1, szDSN, (SWORD)strlen((char *)szDSN), + szUID, (SWORD)strlen((char *)szUID), + szAuthStr, + (SWORD)strlen((char *)szAuthStr));*/ + if ( (retcode != SQL_SUCCESS) && + (retcode != SQL_SUCCESS_WITH_INFO) ) { + ProcessLogMessages(hdbc1, hstmt1, + "SQLConnect() Failed\n\n"); + return; + } + else { + ProcessLogMessages(hdbc1, hstmt1, + "\nConnect Successful\n\n"); + } + // Allocate the statement handle. + retcode = SQLAllocStmt(hdbc1,&hstmt1); + // Execute the SELECT statement. + retcode = SQLExecDirect(hstmt1, (SQLCHAR *)"SELECT TEXT FROM SUBJECT_ATTR", SQL_NTS); + if ( (retcode != SQL_SUCCESS) && + (retcode != SQL_SUCCESS_WITH_INFO) ) { + ProcessLogMessages(hdbc1, hstmt1, + "SQLExecDirect hstmt1 Failed\n\n"); + return; + } + // Get first row. + for(;;) { + retcode = SQLFetch(hstmt1); + if(retcode == SQL_NO_DATA) + break; + if ( (retcode != SQL_SUCCESS) && + (retcode != SQL_SUCCESS_WITH_INFO) ) { + ProcessLogMessages(hdbc1, hstmt1, + "SQLFetch hstmt1 Failed\n\n"); + return; + } + + SQLLEN len; + byte buffer[1024]; + SQLGetData(hstmt1, 1, SQL_C_CHAR, buffer, 1024, &len); + if(len > 0) + DDUMP(String(buffer, len)); + else + LOG("NULL"); + + // Get the SQL_LONG column. CbBatch has size of data chunk + // the buffer can handle. Call SQLGetData until + // SQL_NO_DATA_FOUND. cbBinSize on each call has the + // amount of data left to transfer. +/* cntr = 1; + do { + retcode = SQLGetData(hstmt1, // hstmt + 1, // ipar + SQL_C_BINARY, // fCType + Data, // rgbValue + cbBatch, // cbValueMax + &cbBinSize); // pcbValue + printf("GetData iteration %d, pcbValue = %d\n", cntr++, cbBinSize); + if ( (retcode != SQL_SUCCESS) && + (retcode != SQL_SUCCESS_WITH_INFO) && + retcode != SQL_NO_DATA_FOUND ){ + ProcessLogMessages(hdbc1, hstmt1, "SQLGetData hstmt1 Failed\n\n"); + return; + } + } while (retcode != SQL_NO_DATA_FOUND); +*/ /* Clean up. */ + } + LOG("OK"); + SQLFreeStmt(hstmt1, SQL_DROP); + SQLDisconnect(hdbc1); + SQLFreeConnect (hdbc1); + SQLFreeEnv(henv); + return; +} diff --git a/uppdev/odbctest/odbctest.upp b/uppdev/odbctest/odbctest.upp new file mode 100644 index 000000000..4a43b00b2 --- /dev/null +++ b/uppdev/odbctest/odbctest.upp @@ -0,0 +1,13 @@ +uses + Core, + ODBC; + +library + odbc32.lib; + +file + odbctest.cpp; + +mainconfig + "" = ""; +