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

O3DBIRecordSet.cpp

Go to the documentation of this file.
00001 
00011 #include "O3DBIRecordSet.h"
00012 #include "O3DBIException.h"
00013 #include "O3DBIError.h"
00014 #include <algorithm>            // Std lib algorithms
00015 
00016 // ----------------------------------------------------------------------------
00017 // O3DBIRecordSet construction/destruction
00018 
00019 O3DBIRecordSet::O3DBIRecordSet()
00020 {
00021 
00022 }
00023 
00024 O3DBIRecordSet::O3DBIRecordSet(const O3DBIRecordSet& records)
00025 {
00026     _records = records._records;
00027 }
00028 
00029 O3DBIRecordSet::O3DBIRecordSet(TRecordSetConstIter& first,
00030                                TRecordSetConstIter& last)
00031 {
00032     _records.reserve(last - first);
00033     std::copy(first, last, _records.begin());   
00034 }
00035 
00036 O3DBIRecordSet::~O3DBIRecordSet()
00037 {
00038 
00039 }
00040 
00041 // ----------------------------------------------------------------------------
00042 // O3DBIRecordSet operations
00043 
00044 bool O3DBIRecordSet::operator==(const O3DBIRecordSet& other) const
00045 {
00046     if (_records.size() != other._records.size())
00047         return false;
00048     return std::equal(_records.begin(), _records.end(),
00049         other._records.begin());
00050 }
00051 
00052 TRecordIndex O3DBIRecordSet::InsertRecord(const O3DBIRecord& record)
00053 {
00054     // The first inserted record appoints the size (number of fields) of all
00055     // subsequent records!
00056     if (_records.size() > 0 &&
00057         _records[0].GetFieldCount() != record.GetFieldCount())
00058         throw new O3DBIException(O3DBI_INVALID_NUMOFFIELDS,
00059         O3DBIErrorTxt::pchInvalidNumOfFields, _T("O3DBIRecordSet::InsertRecord"),
00060         this);
00061     _records.push_back(record);
00062     return (_records.size() - 1);
00063 }
00064 
00065 O3DBIRecord O3DBIRecordSet::GetRecord(TRecordIndex index) const
00066 {
00067     if (index >= _records.size())
00068         throw new O3DBIException(O3DBI_INDEX_OUTOFBOUNDS,
00069         O3DBIErrorTxt::pchIndexOutOfBounds, _T("O3DBIRecordSet::GetRecord"),
00070         this);
00071     return _records[index];
00072 }
00073 
00074 const O3DBIRecord& O3DBIRecordSet::operator[](TRecordIndex index) const
00075 {
00076     if (index >= _records.size())
00077         throw new O3DBIException(O3DBI_INDEX_OUTOFBOUNDS,
00078         O3DBIErrorTxt::pchIndexOutOfBounds, _T("O3DBIRecordSet::operator[]"),
00079         this);
00080     return _records[index];
00081 }
00082 
00083 TO3DBIResult O3DBIRecordSet::RemoveRecord(const O3DBIRecord& record)
00084 {
00085     TRecordSetIter iter = std::find(_records.begin(), _records.end(), record);
00086     if (iter != _records.end())
00087     {
00088         _records.erase(iter);
00089         return OSUCCESS;
00090     } else {
00091         return OFAILED;
00092     }
00093 }
00094 
00095 TO3DBIResult O3DBIRecordSet::RemoveRecord(TRecordIndex index)
00096 {
00097     if (index >= _records.size())
00098         throw new O3DBIException(O3DBI_INDEX_OUTOFBOUNDS,
00099         O3DBIErrorTxt::pchIndexOutOfBounds, _T("O3DBIRecordSet::RemoveRecord"),
00100         this);
00101     return RemoveRecord((*this)[index]);
00102 }
00103 
00104 TRecordSet& O3DBIRecordSet::GetRecords()
00105 {
00106     return _records;
00107 }
00108 
00109 TRecordSetSize O3DBIRecordSet::GetRecordCount() const
00110 {
00111     return _records.size();
00112 }
00113 
00114 void O3DBIRecordSet::CancelModified()
00115 {
00116     TRecordSetIter iter = _records.begin();
00117     while (iter != _records.end())
00118     {
00119         (*iter).CancelModified();
00120         ++iter;
00121     }
00122 }
00123 
00124 bool O3DBIRecordSet::IsModified() const
00125 {
00126     TRecordSetConstIter iter = _records.begin();
00127     while (iter != _records.end() && ! (*iter).IsModified())
00128         ++iter;
00129     return (iter != _records.end());
00130 }
00131 
00132 TO3DBIString O3DBIRecordSet::ToString() const
00133 {
00134     _TCHAR buffer[512];
00135     int p;
00136     p = ::_stprintf(buffer, _T("O3DBIRecordSet at: 0x%x\n"), this);
00137     if (_records.size() == 0)
00138     {
00139         ::_tcscpy(buffer + p, _T("\t<contains no records>\n"));
00140         return TO3DBIString(buffer);
00141     } else {
00142         ::_stprintf(buffer + p, _T("O3DBIRecordSet contains %d records"),
00143             GetRecordCount());
00144     }
00145     TO3DBIString str(buffer);
00146     TRecordSetConstIter iter = _records.begin();
00147     while (iter != _records.end())
00148     {
00149         str += (*iter).ToString();
00150         ++iter;
00151     }
00152     return str;
00153 }
00154 
00155 // ----------------------------------------------------------------------------
00156 // O3DBIObject unit test 
00157 
00158 #ifdef __TEST__
00159 bool O3DBIRecordSet::TestMe()
00160 {
00161     STDOUT << _T("Entering test of O3DBIRecordSet >>>") << LF;
00162     try
00163     {
00164         O3DBIRecordSet recordset;
00165 
00166         O3DBIDataField field1(_T("EMPNO"), 12);
00167         O3DBIDataField field2(_T("EMPNAME"), _T("Scott Tiger"));
00168         O3DBIDataField field3(_T("SALARY"), 2400.0);
00169 
00170         // Fill a record with three fields
00171         O3DBIRecord record;
00172         record.InsertField(field1);
00173         record.InsertField(field2);
00174         record.InsertField(field3);
00175 
00176         // Fill a record set with 100 records
00177         for (int i = 0; i <= 100; i++)
00178         {
00179             recordset.InsertRecord(record);
00180         }
00181         STDOUT << recordset.ToString().c_str() << LF;
00182     }
00183     catch (O3DBIException* pEx)
00184     {
00185         PRINT_EXCEPTION(pEx);
00186         delete pEx;
00187     }
00188     STDOUT << _T("<<< Test of O3DBIRecordSet finished!") << LF << LF;
00189     return true;
00190 }
00191 #endif



SourceForge.net Logo Generated on Sun Jan 23 11:36:34 2005 for Oracle Object Oriented Database Interface (O3DBI) by  doxygen 1.3.9.1