Codwiz51's Wiki

RSS

Navigation







Quick Search
»
Advanced Search »

A Quick Example Of OTL

RSS
Modified on 2011/03/30 09:55 by codewiz51 Categorized as Oracle XE, OTL
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 ***************************
# 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::~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&amp; p) // intercept OTL exceptions
    { 
     strBanner = (char*) &amp;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&amp; p) // intercept OTL exceptions
    { 
     strBanner = (char*) &amp;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.

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.