I needed to perform a quick test for a particular version of Oracle in a previous project. I decided to give OTL a chance. Here is an example of some real world code using OTL. I'll try to go into the details in my later posts. Enjoy!
/**************** CTest Header File ***************************
* CTest.h
***************************************************************/
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma warning(disable : 4786)
#include <map>
#include <string>
#include <utility>
#include <vector>
#define OTL_ORA10G // Compile OTL 4.0/OCI8
// We need to include timestamp information
#define OTL_ORA_TIMESTAMP
#define OTL_STL // Turn on STL features
#define OTL_ANSI_CPP // Turn on ANSI C++ typecasts
#include "otlv4.h" // include the OTL 4.0 header file
class CTest
{
public:
CTest();
virtual ~CTest();
BOOL CheckIf9iInstalled(void);
BOOL CheckIf10gInstalled(void);
private:
otl_connect db; // connect object
};
/***************** CTest Body File **************************
* CTest.cpp
***************************************************************/
CTest::CTest()
{
otl_connect::otl_initialize(); // initialize OCI environment
}
CTest::~CTest()
{
// Determine if we need to close the database connection.
if (db.connected)
db.logoff();
// Shutdown OTL
otl_connect::otl_terminate();
}
BOOL CTest::CheckIf10gInstalled(void)
{
std::string strBanner;
BOOL dbConnected = FALSE;
BOOL retVal = FALSE;
try
{
// This is an assignment, not a comparison in the if statement
// If someone else has logged in, we don't want to repeat the log in.
if (!(dbConnected = (BOOL) db.connected))
{
db.rlogon("admin/P@ssw0rd1@XE");
}
// Declare a new stream
otl_stream i(50,
"select banner from v$version where banner like '%Oracle%10g%'",
db
);
if (i.good())
{
if (!i.eof())
{
i >> strBanner;
if (std::string::npos != strBanner.find("10.2.0.1.0", 0, 10))
retVal = TRUE;
}
else
retVal = FALSE;
}
}
catch(otl_exception& p) // intercept OTL exceptions
{
strBanner = (char*) &p.msg;
strBanner += "\n";
strBanner += p.stm_text;
::AfxMessageBox(strBanner.c_str(), MB_ICONSTOP);
retVal = -1;
}
// Leave the connection in the same state we found it.
if (!dbConnected)
db.logoff(); // disconnect from Oracle
return retVal;
}
BOOL CTest::CheckIf9iInstalled(void)
{
std::string strBanner;
BOOL dbConnected = FALSE;
BOOL retVal = FALSE;
try
{
// This is an assignment, not a comparison in the if statement
// If someone else has logged in, we don't want to repeat the log in.
if (!(dbConnected = (BOOL) db.connected))
{
db.rlogon("admin/P@ssw0rd1@XE");
}
// Declare a new stream
otl_stream i(50,
"select banner from v$version where banner like '%Oracle9i%'",
db
);
if (i.good())
{
if (!i.eof())
{
i >> strBanner;
if (std::string::npos != strBanner.find("9.2.0.3.0", 0, 9))
retVal = TRUE;
}
else
retVal = FALSE;
}
}
catch(otl_exception& p) // intercept OTL exceptions
{
strBanner = (char*) &p.msg;
strBanner += "\n";
strBanner += p.stm_text;
::AfxMessageBox(strBanner.c_str(), MB_ICONSTOP);
retVal = -1;
}
// Leave the connection in the same state we found it.
if (!dbConnected)
db.logoff(); // disconnect from Oracle
return retVal;
}I found that using OTL only added about 100K to my project. Not bad. I was able to avoid using any COM or ADO. The performance is impressive.