Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

OracleResultSet.cpp

Go to the documentation of this file.
00001 #include "../include/OracleResultSet.h"
00002 #include "../include/OracleResultSetMetaData.h"
00003 #include "../include/OracleDatabaseLayer.h"
00004 #include "../include/DatabaseLayerException.h"
00005 #include "../include/DatabaseErrorCodes.h"
00006 
00007   // ctor
00008 OracleResultSet::OracleResultSet(oracle::occi::ResultSet* pResultSet, bool bManageStatement /*= false*/)
00009 {
00010   try
00011   {
00012     m_pResultSet = pResultSet;
00013     m_bManageStatement = bManageStatement;
00014 
00015     std::vector<oracle::occi::MetaData> m_MetaData = m_pResultSet->getColumnListMetaData();
00016     std::vector<oracle::occi::MetaData>::iterator start = m_MetaData.begin();
00017     std::vector<oracle::occi::MetaData>::iterator stop = m_MetaData.end();
00018     int nCounter = 1;
00019     while (start != stop)
00020     {
00021       wxString strField = ConvertFromUnicodeStream((*start).getString(oracle::occi::MetaData::ATTR_NAME).c_str());
00022       m_FieldLookupMap[strField] = nCounter;
00023       start++;
00024       nCounter++;
00025     }
00026   }
00027   catch (oracle::occi::SQLException& e)
00028   {
00029     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00030     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00031     ThrowDatabaseException();
00032   }
00033 }
00034 
00035 // dtor
00036 OracleResultSet::~OracleResultSet()
00037 {
00038   Close();
00039 }
00040 
00041 bool OracleResultSet::Next()
00042 {
00043   try
00044   {
00045     return (m_pResultSet->next());
00046   }
00047   catch (oracle::occi::SQLException& e)
00048   {
00049     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00050     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00051     ThrowDatabaseException();
00052   }
00053   return false;
00054 }
00055 
00056 void OracleResultSet::Close()
00057 {
00058   try
00059   {
00060     oracle::occi::Statement* pStatement = m_pResultSet->getStatement();
00061 
00062     if (pStatement)
00063     {
00064       pStatement->closeResultSet(m_pResultSet);
00065 
00066       if (m_bManageStatement)
00067       {
00068         oracle::occi::Connection* pDatabase = pStatement->getConnection();
00069         if (pDatabase)
00070           pDatabase->terminateStatement(pStatement);
00071       }
00072     }
00073   }
00074   catch (oracle::occi::SQLException& e)
00075   {
00076     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00077     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00078     ThrowDatabaseException();
00079   }
00080 }
00081   
00082 // get field
00083 int OracleResultSet::GetResultInt(int nField)
00084 {
00085   int nValue = -1;
00086   try
00087   {
00088     nValue = m_pResultSet->getInt(nField);
00089   }
00090   catch (oracle::occi::SQLException& e)
00091   {
00092     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00093     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00094     ThrowDatabaseException();
00095   }
00096   return nValue;
00097 }
00098 
00099 wxString OracleResultSet::GetResultString(int nField)
00100 {
00101   wxString strValue = wxEmptyString;
00102   try
00103   {
00104     strValue = ConvertFromUnicodeStream(m_pResultSet->getString(nField).c_str());
00105   }
00106   catch (oracle::occi::SQLException& e)
00107   {
00108     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00109     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00110     ThrowDatabaseException();
00111   }
00112   return strValue;
00113 }
00114 
00115 long OracleResultSet::GetResultLong(int nField)
00116 {
00117   long lValue = -1L;
00118   try
00119   {
00120     lValue = m_pResultSet->getInt(nField);
00121   }
00122   catch (oracle::occi::SQLException& e)
00123   {
00124     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00125     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00126     ThrowDatabaseException();
00127   }
00128   return lValue;
00129 }
00130 
00131 bool OracleResultSet::GetResultBool(int nField)
00132 {
00133   bool bValue = false;
00134   try
00135   {
00136     bValue = (m_pResultSet->getInt(nField) != 0);
00137   }
00138   catch (oracle::occi::SQLException& e)
00139   {
00140     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00141     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00142     ThrowDatabaseException();
00143   }
00144   return bValue;
00145 }
00146 
00147 wxDateTime OracleResultSet::GetResultDate(int nField)
00148 {
00149   wxDateTime date = wxInvalidDateTime;
00150   try
00151   {
00152     //int nType = m_MetaData[nField-1].getInt(MetaData::ATTR_DATA_TYPE);
00153     //if (nType = OCCI_SQLT_DAT)
00154     //{
00155     //}
00156       
00157     oracle::occi::Date oracleDate = m_pResultSet->getDate(nField);
00158     int year;
00159     unsigned int month, day, hour, min, sec;
00160     oracleDate.getDate(year, month, day, hour, min, sec);
00161     date.Set(day, wxDateTime::Month(month), year, hour, min, sec);
00162   }
00163   catch (oracle::occi::SQLException& e)
00164   {
00165     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00166     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00167     ThrowDatabaseException();
00168   }
00169   return date;
00170 }
00171 
00172 void* OracleResultSet::GetResultBlob(int nField, wxMemoryBuffer& Buffer)
00173 {
00174   try
00175   {
00176     oracle::occi::Blob blob = m_pResultSet->getBlob(nField);
00177     blob.open(oracle::occi::OCCI_LOB_READONLY);
00178     int nLength = blob.length();
00179     unsigned char* pBuffer = (unsigned char*)(Buffer.GetWriteBuf(nLength));
00180     int nLengthRead = blob.read(nLength, pBuffer, nLength);
00181   }
00182   catch (oracle::occi::SQLException& e)
00183   {
00184     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00185     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00186     ThrowDatabaseException();
00187   }
00188   return Buffer.GetData();
00189 }
00190 
00191 double OracleResultSet::GetResultDouble(int nField)
00192 {
00193   double dblValue = -1;
00194   try
00195   {
00196     dblValue = m_pResultSet->getDouble(nField);
00197   }
00198   catch (oracle::occi::SQLException& e)
00199   {
00200     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00201     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00202     ThrowDatabaseException();
00203   }
00204   return dblValue;
00205 }
00206 
00207 bool OracleResultSet::IsFieldNull(int nField)
00208 {
00209   try
00210   {
00211     return m_pResultSet->isNull(nField);
00212   }
00213   catch (oracle::occi::SQLException& e)
00214   {
00215     SetErrorCode(OracleDatabaseLayer::TranslateErrorCode(e.getErrorCode()));
00216     SetErrorMessage(ConvertFromUnicodeStream(e.getMessage().c_str()));
00217     ThrowDatabaseException();
00218   }
00219   return true;
00220 }
00221 
00222 int OracleResultSet::LookupField(const wxString& strField)
00223 {
00224   StringToIntMap::iterator SearchIterator = m_FieldLookupMap.find(strField);
00225   if (SearchIterator == m_FieldLookupMap.end())
00226   {
00227     wxString msg(_("Field '") + strField + _("' not found in the resultset"));
00228 #ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
00229     DatabaseLayerException error(DATABASE_LAYER_FIELD_NOT_IN_RESULTSET, msg);
00230     throw error;
00231 #else
00232     wxLogError(msg);
00233 #endif
00234     return -1;
00235   }
00236   else
00237   {
00238     return (*SearchIterator).second;
00239   }
00240 }
00241 
00242 ResultSetMetaData* OracleResultSet::GetMetaData()
00243 {
00244   return new OracleResultSetMetaData(m_MetaData);
00245 }
00246 

Generated on Sat May 13 17:31:34 2006 for databaselayer by  doxygen 1.4.1