diff options
author | randomdan <randomdan@localhost> | 2013-11-29 23:54:29 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2013-11-29 23:54:29 +0000 |
commit | 58542e385af2f96c3ef4c432568ce22c5e35a8a0 (patch) | |
tree | db0d7328475e4ac47933b75dfcb746850434046f | |
parent | Fix a few more includes (diff) | |
download | project2-58542e385af2f96c3ef4c432568ce22c5e35a8a0.tar.bz2 project2-58542e385af2f96c3ef4c432568ce22c5e35a8a0.tar.xz project2-58542e385af2f96c3ef4c432568ce22c5e35a8a0.zip |
Refactor to set up columns in a const way by calling execute() early, before fetch()
-rw-r--r-- | project2/sql/sqlRows.cpp | 18 | ||||
-rw-r--r-- | 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; |