diff options
Diffstat (limited to 'src/dal/recordset.cpp')
-rw-r--r-- | src/dal/recordset.cpp | 185 |
1 files changed, 105 insertions, 80 deletions
diff --git a/src/dal/recordset.cpp b/src/dal/recordset.cpp index fdaf6614..25862d3d 100644 --- a/src/dal/recordset.cpp +++ b/src/dal/recordset.cpp @@ -35,7 +35,7 @@ namespace dal /** * Default constructor. */ -Record::Record(void) +RecordSet::RecordSet(void) throw() { // NOOP @@ -45,7 +45,7 @@ Record::Record(void) /** * Destructor. */ -Record::~Record(void) +RecordSet::~RecordSet(void) throw() { // NOOP @@ -53,144 +53,169 @@ Record::~Record(void) /** - * Add a new field. + * Remove all the Records. */ void -Record::addField(const std::string& name, - const std::string& value) +RecordSet::clear(void) throw() { - mFields.insert(Fields::value_type(name, value)); + mHeaders.clear(); + mRows.clear(); } /** - * Add a new field. + * Get the number of rows. + * + * @return the number of rows. */ -void -Record::addField(const std::string& name, - const double value) +unsigned int +RecordSet::rows(void) throw() { - // convert the number into a string. - std::ostringstream os; - os << value << std::ends; - - mFields.insert(Fields::value_type(name, os.str())); + return mRows.size(); } /** - * Get the field value. + * Get the number of columns. + * + * @return the number of columns. */ -const std::string& -Record::get(const std::string& name) const - throw(std::invalid_argument) +unsigned int +RecordSet::cols(void) + throw() { - return getAsString(name); + return mHeaders.size(); } /** - * Get the field value as string. + * Set the column headers. */ -const std::string& -Record::getAsString(const std::string& name) const - throw(std::invalid_argument) +void +RecordSet::setColumnHeaders(const Row& headers) + throw(AlreadySetException) { - Fields::const_iterator it = mFields.find(name); - - if (it == mFields.end()) { - std::ostringstream msg; - msg << "unknown field name: " << name << std::ends; - - throw std::invalid_argument(msg.str()); + if (mHeaders.size() > 0) { + throw AlreadySetException(); } - return it->second; + mHeaders = headers; } /** - * Get the field value as a number. + * Add a new row. */ -const double -Record::getAsNumber(const std::string& name) const +void +RecordSet::add(const Row& row) throw(std::invalid_argument) { - std::istringstream is(getAsString(name)); - double doubleValue; - - // convert the string to a number. - is >> doubleValue; + if (row.size() != mHeaders.size()) { + throw std::invalid_argument( + "the new row does not have the required number of columns."); + } - return doubleValue; + mRows.push_back(row); } /** - * Default constructor. + * Operator() */ -RecordSet::RecordSet(void) - throw() +const std::string& +RecordSet::operator()(const unsigned int row, + const unsigned int col) const + throw(std::out_of_range) { - // NOOP + if ((row >= mRows.size()) || (col >= mHeaders.size())) { + std::ostringstream os; + os << "(" << row << ", " << col << ") is out of range; " + << "max rows: " << mRows.size() + << ", max cols: " << mHeaders.size() << std::ends; + + throw std::out_of_range(os.str()); + } + + return mRows[row][col]; } /** - * Destructor. + * Operator() */ -RecordSet::~RecordSet(void) - throw() +const std::string& +RecordSet::operator()(const unsigned int row, + const std::string& name) const + throw(std::out_of_range, + std::invalid_argument) { - Rows::iterator it = mRows.begin(); - Rows::iterator it_end = mRows.end(); + if (row >= mRows.size()) { + std::ostringstream os; + os << "row " << row << " is out of range; " + << "max rows: " << mRows.size() << std::ends; - for(; it != it_end; ++it) { - delete *it; + throw std::out_of_range(os.str()); } -} + Row::const_iterator it = std::find(mHeaders.begin(), + mHeaders.end(), + name); + if (it == mHeaders.end()) { + std::ostringstream os; + os << "field " << name << " does not exist." << std::ends; -/** - * Add a new Record. - */ -void -RecordSet::addRecord(const Record* record) - throw() -{ - mRows.push_back(record); -} + throw std::invalid_argument(os.str()); + } + // find the field index. + const int nCols = mHeaders.size(); + int i; + for (i = 0; i < nCols; ++i) { + if (mHeaders[i] == name) { + break; + } + } -/** - * Get all the rows. - */ -const Rows& -RecordSet::getRows(void) const - throw() -{ - return mRows; + return mRows[row][i]; } /** - * Get a particular row. + * Operator<< */ -const Record& -RecordSet::getRow(const unsigned int row) const - throw(std::out_of_range) +std::ostream& +operator<<(std::ostream& out, const RecordSet& rhs) { - if (row >= mRows.size()) { - std::ostringstream msg; - msg << "index out of range: " << row << "; " - << "num. of records: " << mRows.size() << std::ends; + using namespace tmw::dal; + + // print the field names first. + if (rhs.mHeaders.size() > 0) { + Row::const_iterator it = rhs.mHeaders.begin(); + out << "[ " << (*it); + it++; + for (; it != rhs.mHeaders.end(); ++it) { + out << " | " << (*it); + } + out << " ]" << std::endl; + } - throw std::out_of_range(msg.str()); + // and then print every line. + for (RecordSet::Rows::const_iterator it = rhs.mRows.begin(); + it != rhs.mRows.end(); + ++it) + { + Row::const_iterator it2 = (*it).begin(); + out << "[ " << (*it2); + it2++; + for (; it2 != (*it).end(); ++it2) { + out << " | " << (*it2); + } + out << " ]" << std::endl; } - return *(mRows[row]); + return out; } |