00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <algorithm>
00022 #include <sstream>
00023 #include <stdexcept>
00024
00025 #include "dal/recordset.h"
00026
00027 #include "dal/dalexcept.h"
00028
00029 namespace dal
00030 {
00031
00032
00036 RecordSet::RecordSet(void)
00037 throw()
00038 {
00039
00040 }
00041
00042
00046 RecordSet::~RecordSet(void)
00047 throw()
00048 {
00049
00050 }
00051
00052
00056 void
00057 RecordSet::clear(void)
00058 {
00059 mHeaders.clear();
00060 mRows.clear();
00061 }
00062
00063
00067 bool
00068 RecordSet::isEmpty(void) const
00069 {
00070 return (mRows.size() == 0);
00071 }
00072
00073
00079 unsigned int
00080 RecordSet::rows(void) const
00081 {
00082 return mRows.size();
00083 }
00084
00085
00091 unsigned int
00092 RecordSet::cols(void) const
00093 {
00094 return mHeaders.size();
00095 }
00096
00097
00101 void
00102 RecordSet::setColumnHeaders(const Row& headers)
00103 {
00104 if (mHeaders.size() > 0) {
00105 throw AlreadySetException();
00106 }
00107
00108 mHeaders = headers;
00109 }
00110
00111
00115 void
00116 RecordSet::add(const Row& row)
00117 {
00118 const unsigned int nCols = mHeaders.size();
00119
00120 if (nCols == 0) {
00121 throw RsColumnHeadersNotSet();
00122 }
00123
00124 if (row.size() != nCols) {
00125 std::ostringstream msg;
00126 msg << "row has " << row.size() << " columns; "
00127 << "expected: " << nCols << std::ends;
00128
00129 throw std::invalid_argument(msg.str());
00130 }
00131
00132 mRows.push_back(row);
00133 }
00134
00135
00139 const std::string&
00140 RecordSet::operator()(const unsigned int row,
00141 const unsigned int col) const
00142 {
00143 if ((row >= mRows.size()) || (col >= mHeaders.size())) {
00144 std::ostringstream os;
00145 os << "(" << row << ", " << col << ") is out of range; "
00146 << "max rows: " << mRows.size()
00147 << ", max cols: " << mHeaders.size() << std::ends;
00148
00149 throw std::out_of_range(os.str());
00150 }
00151
00152 return mRows[row][col];
00153 }
00154
00155
00159 const std::string&
00160 RecordSet::operator()(const unsigned int row,
00161 const std::string& name) const
00162 {
00163 if (row >= mRows.size()) {
00164 std::ostringstream os;
00165 os << "row " << row << " is out of range; "
00166 << "max rows: " << mRows.size() << std::ends;
00167
00168 throw std::out_of_range(os.str());
00169 }
00170
00171 Row::const_iterator it = std::find(mHeaders.begin(),
00172 mHeaders.end(),
00173 name);
00174 if (it == mHeaders.end()) {
00175 std::ostringstream os;
00176 os << "field " << name << " does not exist." << std::ends;
00177
00178 throw std::invalid_argument(os.str());
00179 }
00180
00181
00182 const unsigned int nCols = mHeaders.size();
00183 unsigned int i;
00184 for (i = 0; i < nCols; ++i) {
00185 if (mHeaders[i] == name) {
00186 break;
00187 }
00188 }
00189
00190 return mRows[row][i];
00191 }
00192
00193
00197 std::ostream&
00198 operator<<(std::ostream& out, const RecordSet& rhs)
00199 {
00200
00201 if (rhs.mHeaders.size() > 0) {
00202 out << "|";
00203 for (Row::const_iterator it = rhs.mHeaders.begin();
00204 it != rhs.mHeaders.end();
00205 ++it)
00206 {
00207 out << (*it) << "|";
00208 }
00209 out << std::endl << std::endl;
00210 }
00211
00212
00213 for (RecordSet::Rows::const_iterator it = rhs.mRows.begin();
00214 it != rhs.mRows.end();
00215 ++it)
00216 {
00217 out << "|";
00218 for (Row::const_iterator it2 = (*it).begin();
00219 it2 != (*it).end();
00220 ++it2)
00221 {
00222 out << (*it2) << "|";
00223 }
00224 out << std::endl;
00225 }
00226
00227 return out;
00228 }
00229
00230
00231 }