From 48a325d6aa729f4e6d808d83d5cd415dc00b935f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 29 Apr 2015 20:43:50 +0100 Subject: Centralize all the column reference storage logic --- libdbpp/column.h | 2 ++ libdbpp/selectcommand.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 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 #include +#include namespace DB { class HandleField { @@ -27,6 +28,7 @@ namespace DB { const unsigned int colNo; const Glib::ustring name; }; + typedef boost::shared_ptr 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 +#include +#include +#include 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>, + boost::multi_index::ordered_unique> + >> Columns; + + protected: + Columns columns; }; } -- cgit v1.2.3