22 #ifndef odc_IteratorProxy_H
23 #define odc_IteratorProxy_H
28 #include "eckit/sql/SQLTypedefs.h"
30 namespace eckit {
class PathName; }
31 namespace eckit {
class DataHandle; }
40 struct ODBStopIteration :
public std::exception {
41 const char* what()
const throw() {
return "end of data"; }
44 struct ODBIndexError :
public std::exception {
45 const char* what()
const throw() {
return "index out of range"; }
48 extern "C" void python_api_start()
57 namespace core {
class MetaData; }
63 template <
typename ITERATOR,
typename O,
typename DATA>
66 template <
typename ITERATOR,
typename O,
typename DATA,
typename ITERATOR_PROXY>
77 DATA*
data() {
return ((*it_).iter_)->data(); }
78 DATA&
data(
size_t i) {
return ((*it_).iter_)->data(
i); }
80 const DATA*
data()
const {
return ((*it_).iter_)->data(); }
81 const DATA&
data(
size_t i)
const {
return ((*it_).iter_)->data(
i); }
87 const char *s =
reinterpret_cast<const char *
>(&
data(
i));
88 return std::string(s, ::strnlen(s, maxlen));
98 bool isMissing(
size_t i) {
return ((*it_).iter_)->columns()[
i]->missingValue() == (*it_)->data(
i); }
99 double missingValue(
size_t i) {
return ((*it_).iter_)->columns()[
i]->missingValue(); }
102 {
return (*((*it_).iter_)).setColumn(index,
name,
type); }
105 {
return ((*it_).iter_)->setBitfieldColumn(index,
name,
type, b); }
112 const std::map<std::string, std::string>&
properties()
const {
return it_->iter_->properties(); }
114 template <
typename T>
unsigned long pass1(T b,
const T e) {
return ((*it_).iter_)->pass1(b, e); }
121 template <
typename ITERATOR,
typename O = Reader,
typename DATA =
double>
173 size_t __len__() {
return iter_->columns().size(); }
175 PyObject* getitem(
const char* s)
179 return getitem(
iter_->columns().columnIndex(
name));
181 throw ODBIndexError();
184 PyObject* getitem(
int i)
186 Column& column = *
iter_->columns()[
i];
188 if (
d == column.missingValue())
191 switch (column.type()) {
194 const char *s =
reinterpret_cast<const char *
>(&
d);
196 for (; j <
sizeof(double) && s[j]; ++j)
198 return PyString_FromStringAndSize(s, j);
204 typedef unsigned long B;
205 char buf[
sizeof(B) + 1];
209 B mask = 1 << (
sizeof(B) - 1);
210 for(
size_t j = 0; j <
sizeof(B); ++j)
212 *s++ = (n & mask) ?
'1' :
'0';
216 return PyString_FromStringAndSize(buf,
sizeof(B) + 1);
218 default:
return PyFloat_FromDouble(
d);
222 PyObject* __getitem__(PyObject*
i)
225 if (PyTuple_Check(
i))
227 Py_ssize_t n = PyTuple_Size(
i);
228 PyObject* l = PyTuple_New(n);
229 for(
int j (0); j < n; ++j)
231 PyObject* o = PyTuple_GetItem(
i, j);
232 PyTuple_SetItem(l, j, __getitem__(o));
238 Py_ssize_t n (PyList_Size(
i));
239 PyObject* l (PyTuple_New(n));
240 for(ssize_t j (0); j < n; ++j)
242 PyObject* o (PyList_GetItem(
i, j));
243 PyTuple_SetItem(l, j, __getitem__(o));
247 if (PyString_Check(
i))
250 return getitem(PyString_AsString(
i));
252 if (PySlice_Check(
i))
255 return getslice((PySliceObject*)
i);
258 long li = PyLong_AsLong(
i);
262 PyObject* getslice(PySliceObject* slice)
265 Py_ssize_t start = 0, stop = 0, step = 0, slicelength = 0;
266 PySlice_GetIndicesEx(slice, __len__(), &start, &stop, &step, &slicelength);
268 return getslice(start, stop, step, slicelength);
271 PyObject* getslice(Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step, Py_ssize_t slicelength)
273 ssize_t maxIndex = __len__();
274 if (start < 0 || start > maxIndex)
throw ODBIndexError();
275 if (stop < 0 || stop > maxIndex)
throw ODBIndexError();
279 size_t outputSize = 0;
280 for (
int index = start; (step > 0) ? (index < stop) : (index > stop); index += step)
282 PyObject* l = PyList_New(outputSize);
285 for (
int index = start; (step > 0) ? (index < stop) : (index > stop); index += step)
287 ASSERT(outIndex < outputSize);
288 PyList_SetItem(l, outIndex++, getitem(index));
298 throw ODBStopIteration();
310 for (
size_t i = 0;
i < it.
iter_->columns().size(); ++
i)
311 o << it.
iter_->data(
i) <<
"\t";
const Row * operator->() const
bool operator==(const IteratorProxy &other)
IteratorProxy & operator=(const IteratorProxy &other)
IteratorProxy(const IteratorProxy &other)
IteratorProxy & operator++()
bool operator!=(const IteratorProxy &)
IteratorProxy(ITERATOR *iter)
friend std::ostream & operator<<(std::ostream &o, const IteratorProxy &it)
Row_< ITERATOR, O, DATA, IteratorProxy > Row
friend class MetaDataReaderIterator
const Row & operator*() const
void missingValue(size_t index, double v)
DATA & operator[](size_t i)
double missingValue(size_t i)
size_t dataSizeDoubles(size_t i) const
void setNumberOfColumns(size_t n)
const std::map< std::string, std::string > & properties() const
unsigned long pass1(T b, const T e)
std::string string(int i)
const DATA * data() const
const core::MetaData & columns(const core::MetaData &md)
const core::MetaData & columns() const
const DATA & data(size_t i) const
size_t dataOffset(size_t i) const
int setColumn(size_t index, const std::string &name, api::ColumnType type)
int setBitfieldColumn(size_t index, const std::string &name, api::ColumnType type, eckit::sql::BitfieldDef b)