diff options
-rw-r--r-- | libdbpp/column.h | 2 | ||||
-rw-r--r-- | libdbpp/selectcommand.cpp | 48 | ||||
-rw-r--r-- | libdbpp/selectcommand.h | 25 |
3 files changed, 69 insertions, 6 deletions
diff --git a/libdbpp/column.h b/libdbpp/column.h index 80bec8c..5ca0be1 100644 --- a/libdbpp/column.h +++ b/libdbpp/column.h @@ -3,6 +3,7 @@ #include <glibmm/ustring.h> #include <boost/date_time/posix_time/posix_time_types.hpp> +#include <boost/shared_ptr.hpp> namespace DB { class HandleField { @@ -27,6 +28,7 @@ namespace DB { const unsigned int colNo; const Glib::ustring name; }; + typedef boost::shared_ptr<Column> ColumnPtr; } #endif diff --git a/libdbpp/selectcommand.cpp b/libdbpp/selectcommand.cpp index 662ced4..6cb8759 100644 --- a/libdbpp/selectcommand.cpp +++ b/libdbpp/selectcommand.cpp @@ -1,4 +1,20 @@ #include "selectcommand.h" +#include "error.h" + +namespace DB { + class ColumnIndexOutOfRange : public Error { + public: + ColumnIndexOutOfRange(unsigned int n) : colNo(n) { } + + const unsigned int colNo; + }; + class ColumnDoesNotExist : public Error { + public: + ColumnDoesNotExist(const Glib::ustring & n) : colName(n) { } + + const Glib::ustring colName; + }; +}; DB::SelectCommand::SelectCommand(const std::string & sql) : DB::Command(sql) @@ -9,3 +25,35 @@ DB::SelectCommand::~SelectCommand() { } +const DB::Column& +DB::SelectCommand::operator[](unsigned int n) const +{ + if (n < columns.size()) { + return **columns.get<0>().find(n); + } + throw ColumnIndexOutOfRange(n); +} + +const DB::Column& +DB::SelectCommand::operator[](const Glib::ustring & n) const +{ + typedef Columns::nth_index<1>::type CbyName; + CbyName::iterator i = columns.get<1>().find(n); + if (i != columns.get<1>().end()) { + return **i; + } + throw ColumnDoesNotExist(n); +} + +unsigned int +DB::SelectCommand::getOrdinal(const Glib::ustring & n) const +{ + return operator[](n).colNo; +} + +unsigned int +DB::SelectCommand::columnCount() const +{ + return columns.size(); +} + diff --git a/libdbpp/selectcommand.h b/libdbpp/selectcommand.h index 943e724..c23ad81 100644 --- a/libdbpp/selectcommand.h +++ b/libdbpp/selectcommand.h @@ -2,6 +2,11 @@ #define DB_SELECTCOMMAND_H #include "command.h" +#include "column.h" +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/indexed_by.hpp> +#include <boost/multi_index/ordered_index.hpp> +#include <boost/multi_index/member.hpp> namespace DB { class Column; @@ -9,12 +14,20 @@ namespace DB { public: SelectCommand(const std::string & sql); ~SelectCommand(); - virtual bool fetch() = 0; - virtual void execute() = 0; - virtual const Column & operator[](unsigned int col) const = 0; - virtual const Column & operator[](const Glib::ustring &) const = 0; - virtual unsigned int columnCount() const = 0; - virtual unsigned int getOrdinal(const Glib::ustring &) const = 0; + virtual bool fetch() = 0; + virtual void execute() = 0; + const Column & operator[](unsigned int col) const; + const Column & operator[](const Glib::ustring &) const; + unsigned int columnCount() const; + unsigned int getOrdinal(const Glib::ustring &) const; + + typedef boost::multi_index_container<ColumnPtr, boost::multi_index::indexed_by< + boost::multi_index::ordered_unique<boost::multi_index::member<DB::Column, const unsigned int, &DB::Column::colNo>>, + boost::multi_index::ordered_unique<boost::multi_index::member<DB::Column, const Glib::ustring, &DB::Column::name>> + >> Columns; + + protected: + Columns columns; }; } |