diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-04-29 20:43:50 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-04-29 20:43:50 +0100 |
commit | 8d9d02d2e769b8b25e166f07f4ecd67a22f4e499 (patch) | |
tree | c23db7820aa1472733b6a712c5458b771446ef72 /libodbcpp/selectcommand.cpp | |
parent | Ignore Vim swap files (diff) | |
download | libdbpp-odbc-8d9d02d2e769b8b25e166f07f4ecd67a22f4e499.tar.bz2 libdbpp-odbc-8d9d02d2e769b8b25e166f07f4ecd67a22f4e499.tar.xz libdbpp-odbc-8d9d02d2e769b8b25e166f07f4ecd67a22f4e499.zip |
Centralize all the column reference storage logic
Diffstat (limited to 'libodbcpp/selectcommand.cpp')
-rw-r--r-- | libodbcpp/selectcommand.cpp | 64 |
1 files changed, 9 insertions, 55 deletions
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<Column>(*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<Column>(*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<Column>(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(); -} - - |