From f45b9b322ae87a593a0603a66ee25cca0b566679 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 29 Nov 2013 23:54:29 +0000 Subject: Refactor to set up columns in a const way by calling execute() early, before fetch() --- project2/sql/sqlRows.cpp | 18 ++++++++---------- project2/sql/sqlRows.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/project2/sql/sqlRows.cpp b/project2/sql/sqlRows.cpp index f723269..6570a60 100644 --- a/project2/sql/sqlRows.cpp +++ b/project2/sql/sqlRows.cpp @@ -34,16 +34,16 @@ SqlRows::loadComplete(const CommonObjects * co) SqlRows::SqlState::SqlState(SelectPtr s) : query(s) { + query->execute(); + fields.resize(query->columnCount()); + for (unsigned int c = 0; c < query->columnCount(); c++) { + columns.insert(new Column(c, (*query)[c].name)); + } } const Columns & SqlRows::SqlState::getColumns() const { - if (columns.empty()) { - for (unsigned int c = 0; c < query->columnCount(); c++) { - columns.insert(new Column(c, (*query)[c].name)); - } - } return columns; } @@ -51,13 +51,11 @@ void SqlRows::execute(const Glib::ustring & filter, const RowProcessorCallback & rp, ExecContext * ec) const { unsigned int offset = 0; - SqlState ss(SelectPtr(db->getReadonly().newSelectCommand(sqlCommand.getSqlFor(filter)))); - sqlCommand.bindParams(ec, ss.query.get(), offset); + auto select = SelectPtr(db->getReadonly().newSelectCommand(sqlCommand.getSqlFor(filter))); + sqlCommand.bindParams(ec, select.get(), offset); + SqlState ss(select); while (ss.query->fetch()) { HandleAsVariableType h; - if (ss.fields.empty()) { - ss.fields.resize(ss.query->columnCount()); - } for (unsigned int c = 0; c < ss.query->columnCount(); c++) { const DB::Column & col = (*ss.query)[c]; col.apply(h); diff --git a/project2/sql/sqlRows.h b/project2/sql/sqlRows.h index 6f78dcd..46f3972 100644 --- a/project2/sql/sqlRows.h +++ b/project2/sql/sqlRows.h @@ -29,7 +29,7 @@ class SqlRows : public RowSet { SqlState(SelectPtr query); const Columns & getColumns() const; SelectPtr query; - mutable Columns columns; + Columns columns; friend class SqlRows; }; const RdbmsDataSource * db; -- cgit v1.2.3