IODA Bundle
TODATableIterator.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "eckit/sql/SQLColumn.h"
12 #include "eckit/exception/Exceptions.h"
13 
14 #include "odc/csv/TextReader.h"
16 #include "odc/Reader.h"
17 #include "odc/sql/TODATable.h"
19 
20 
21 namespace odc {
22 namespace sql {
23 
24 //----------------------------------------------------------------------------------------------------------------------
25 
26 // TODO: It is not appropriate for parent_.oda() to be const_cast<>-ed.
27 // Not entirely clear how to resolve this. But there is no reason for us intrinsically
28 // to be modifying the parent. Perhaps we should take a copy of somethnig (oda, dh?)
29 
30 template <typename READER>
32  const std::vector<std::reference_wrapper<const eckit::sql::SQLColumn>>& columns,
33  std::function<void(eckit::sql::SQLTableIterator&)> metadataUpdateCallback) :
34  parent_(parent),
35  it_(const_cast<READER&>(parent_.oda()).begin()),
36  end_(parent_.oda().end()),
37  columns_(columns),
38  metadataUpdateCallback_(metadataUpdateCallback),
39  firstRow_(true) {
40 
41  if (it_ != end_) updateMetaData();
42 }
43 
44 template <typename READER>
46  it_ = const_cast<READER&>(parent_.oda()).begin();
47  end_ = parent_.oda().end();
48  firstRow_ = true;
49 }
50 
51 template <typename READER>
53 
54 template <typename READER>
56 
57  // We don't need to increment pointer on first row. begin() just called.
58 
59  if (firstRow_) {
60  firstRow_ = false;
61  } else {
62  ++it_;
63  }
64 
65  if (it_ == end_) return false;
66 
67  if (it_->isNewDataset()) {
68  // TODO: Need to update the column pointers in the SQLSelect. AARGH.
69  updateMetaData();
70  metadataUpdateCallback_(*this);
71  }
72 
73  return true;
74 }
75 
76 
77 template <typename READER>
79 
80  const core::MetaData& md = it_->columns();
81 
82  columnOffsets_.clear();
83  columnDoublesSizes_.clear();
84  for (const eckit::sql::SQLColumn& col : columns_) {
85 
86  if (!md.hasColumn(col.name())) {
87  throw eckit::UserError("Column \"" + col.name() + "\" not found in table, but required in SQL request", Here());
88  }
89 
90  size_t idx = md.columnIndex(col.name());
91  columnOffsets_.push_back(it_->dataOffset(idx));
92  columnDoublesSizes_.push_back(it_->dataSizeDoubles(idx));
93  }
94 }
95 
96 template <typename READER>
97 std::vector<size_t> TODATableIterator<READER>::columnOffsets() const {
98  ASSERT(columnOffsets_.size() == columns_.size());
99  return columnOffsets_;
100 }
101 
102 template <typename READER>
103 std::vector<size_t> TODATableIterator<READER>::doublesDataSizes() const {
104  ASSERT(columnDoublesSizes_.size() == columns_.size());
105  return columnDoublesSizes_;
106 }
107 
108 template <typename READER>
109 const double* TODATableIterator<READER>::data() const {
110  return it_->data();
111 }
112 
113 // Explicit instantiation
114 
115 template class TODATableIterator<Reader>;
116 template class TODATableIterator<TextReader>;
117 
118 //----------------------------------------------------------------------------------------------------------------------
119 
120 } // namespace sql
121 } // namespace odc
void oda
size_t columnIndex(const std::string &) const
Definition: MetaData.cc:95
bool hasColumn(const std::string &) const
Definition: MetaData.cc:87
virtual std::vector< size_t > columnOffsets() const override
virtual const double * data() const override
TODATableIterator(const TODATable< READER > &parent, const std::vector< std::reference_wrapper< const eckit::sql::SQLColumn >> &columns, std::function< void(eckit::sql::SQLTableIterator &)> metadataUpdateCallback)
virtual void rewind() override
virtual bool next() override
virtual std::vector< size_t > doublesDataSizes() const override
Definition: ColumnInfo.h:23