From 8d9d02d2e769b8b25e166f07f4ecd67a22f4e499 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 --- libodbcpp/selectcommand.cpp | 64 +++++++-------------------------------------- libodbcpp/selectcommand.h | 7 +---- 2 files changed, 10 insertions(+), 61 deletions(-) (limited to 'libodbcpp') diff --git a/libodbcpp/selectcommand.cpp b/libodbcpp/selectcommand.cpp index d53a487..9a306c3 100644 --- a/libodbcpp/selectcommand.cpp +++ b/libodbcpp/selectcommand.cpp @@ -14,11 +14,6 @@ ODBC::SelectCommand::SelectCommand(const Connection & c, const std::string & s) ODBC::SelectCommand::~SelectCommand() { - for (Columns::iterator i = columns.begin(); i != columns.end(); ++i) { - if (*i) { - delete *i; - } - } if (columns.size()) { RETCODE rc = SQLCloseCursor(hStmt); if (!SQL_SUCCEEDED(rc)) { @@ -49,7 +44,7 @@ ODBC::SelectCommand::fetch(SQLSMALLINT orientation, SQLLEN offset) if (SQL_SUCCEEDED(diagrc)) { if (!strncmp((const char*)sqlstatus, "01004", 5)) { for (Columns::iterator i = columns.begin(); i != columns.end(); ++i) { - (*i)->resize(); + boost::dynamic_pointer_cast(*i)->resize(); } return fetch(SQL_FETCH_RELATIVE, 0); } @@ -62,7 +57,7 @@ ODBC::SelectCommand::fetch(SQLSMALLINT orientation, SQLLEN offset) { bool resized = false; for (Columns::iterator i = columns.begin(); i != columns.end(); ++i) { - resized |= (*i)->resize(); + resized |= boost::dynamic_pointer_cast(*i)->resize(); } if (resized) { return fetch(SQL_FETCH_RELATIVE, 0); @@ -88,7 +83,6 @@ ODBC::SelectCommand::execute() if (colCount < 1) { throw Error("ODBC::SelectCommand::execute No result columns"); } - columns.resize(colCount); for (int col = 0; col < colCount; col++) { SQLCHAR _colName[300]; SQLSMALLINT nameLen, dp, nullable, bindType; @@ -99,19 +93,20 @@ ODBC::SelectCommand::execute() throw Error(rc, SQL_HANDLE_STMT, hStmt, "ODBC::SelectCommand::execute SQLDescribeCol for %d"); } Glib::ustring colName((const char *)_colName, nameLen); + DB::ColumnPtr ncol; switch (bindType) { case SQL_DECIMAL: case SQL_NUMERIC: case SQL_REAL: case SQL_FLOAT: case SQL_DOUBLE: - columns[col] = new FloatingPointColumn(this, colName, col); + ncol = *columns.insert(DB::ColumnPtr(new FloatingPointColumn(this, colName, col))).first; break; case SQL_SMALLINT: case SQL_INTEGER: case SQL_TINYINT: case SQL_BIGINT: - columns[col] = new SignedIntegerColumn(this, colName, col); + ncol = *columns.insert(DB::ColumnPtr(new SignedIntegerColumn(this, colName, col))).first; break; case SQL_TYPE_TIME: case SQL_INTERVAL_YEAR: @@ -127,60 +122,19 @@ ODBC::SelectCommand::execute() case SQL_INTERVAL_HOUR_TO_MINUTE: case SQL_INTERVAL_HOUR_TO_SECOND: case SQL_INTERVAL_MINUTE_TO_SECOND: - columns[col] = new IntervalColumn(this, colName, col); + ncol = *columns.insert(DB::ColumnPtr(new IntervalColumn(this, colName, col))).first; break; case SQL_TIMESTAMP: case SQL_DATETIME: case SQL_TYPE_DATE: case SQL_TYPE_TIMESTAMP: - columns[col] = new TimeStampColumn(this, colName, col); + ncol = *columns.insert(DB::ColumnPtr(new TimeStampColumn(this, colName, col))).first; break; default: - columns[col] = new CharArrayColumn(this, colName, col, bindSize); + ncol = *columns.insert(DB::ColumnPtr(new CharArrayColumn(this, colName, col, bindSize))).first; break; }; - columns[col]->bind(); + boost::dynamic_pointer_cast(ncol)->bind(); } } - -const DB::Column& -ODBC::SelectCommand::operator[](unsigned int col) const -{ - if (col > columns.size()) { - throw ODBC::Error("Column index out of range"); - } - return *columns[col]; -} - -const DB::Column& -ODBC::SelectCommand::operator[](const Glib::ustring & colName) const -{ - for (Columns::const_iterator col = columns.begin(); col != columns.end(); ++col) { - if ((*col)->name == colName) { - return **col; - } - } - throw ODBC::Error("Column does not exist"); -} - -unsigned int -ODBC::SelectCommand::getOrdinal(const Glib::ustring & colName) const -{ - unsigned int n = 0; - for (Columns::const_iterator col = columns.begin(); col != columns.end(); ++col) { - if ((*col)->name == colName) { - return n; - } - n += 1; - } - throw ODBC::Error("Column does not exist"); -} - -unsigned int -ODBC::SelectCommand::columnCount() const -{ - return columns.size(); -} - - diff --git a/libodbcpp/selectcommand.h b/libodbcpp/selectcommand.h index 81f3ea9..c728ee1 100644 --- a/libodbcpp/selectcommand.h +++ b/libodbcpp/selectcommand.h @@ -7,19 +7,14 @@ namespace ODBC { class Column; class SelectCommand : public Command, public DB::SelectCommand { - typedef std::vector Columns; public: SelectCommand (const Connection &, const std::string & sql); ~SelectCommand(); bool fetch(); void execute(); - const DB::Column & operator[](unsigned int col) const; - const DB::Column & operator[](const Glib::ustring &) const; - unsigned int columnCount() const; - unsigned int getOrdinal(const Glib::ustring &) const; + private: bool fetch(SQLSMALLINT orientation = SQL_FETCH_NEXT, SQLLEN offset = 0); - Columns columns; }; } -- cgit v1.2.3