IODA Bundle
Select.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 /// @author Piotr Kuchta
12 /// @author Simon Smart
13 /// @date April 2010
14 
15 #include "eckit/io/DataHandle.h"
16 
18 #include "odc/Select.h"
19 #include "odc/SelectIterator.h"
22 #include "odc/sql/TODATable.h"
23 
24 using namespace eckit;
25 
26 namespace odc {
27 
28 //----------------------------------------------------------------------------------------------------------------------
29 
30 // TODO: Select should BE a SelectIterator, not posess one.
31 
32 Select::Select(const std::string& selectStatement, bool manageOwnBuffer) :
33  selectStatement_(selectStatement),
34  session_(std::unique_ptr<eckit::sql::SQLOutput>(new sql::SQLSelectOutput(manageOwnBuffer)),
35  std::unique_ptr<eckit::sql::SQLOutputConfig>(new odc::sql::SQLOutputConfig)),
36  initted_(false),
37  it_(nullptr) {}
38 
39 
40 Select::Select(const std::string& selectStatement, DataHandle& dh, bool /* manageOwnBuffer */) :
41  Select(selectStatement, true) {
42 
43  dh.openForRead();
44  eckit::sql::SQLDatabase& db(session_.currentDatabase());
45  db.addImplicitTable(new odc::sql::ODATable(db, dh));
46 }
47 
48 
49 Select::Select(const std::string& selectStatement, const std::string& path, bool /* manageOwnBuffer */) :
50  Select(selectStatement) {
51 
53  ownDH_->openForRead();
54  eckit::sql::SQLDatabase& db(session_.currentDatabase());
55  db.addImplicitTable(new odc::sql::ODATable(db, *ownDH_));
56 }
57 
58 Select::Select(const std::string &selectStatement, const char *path, bool manageOwnBuffer) :
59  Select(selectStatement, std::string(path), manageOwnBuffer) {}
60 
61 
63 
64 eckit::sql::SQLDatabase& Select::database() {
65  return session_.currentDatabase();
66 }
67 
68 
69 SelectIterator* Select::createSelectIterator(const std::string& sql) {
70 
71  sql::SQLSelectOutput* output = dynamic_cast<sql::SQLSelectOutput*>(&session_.output());
72  ASSERT(output);
73 
74  return new SelectIterator(sql, session_, *output);
75 }
76 
77 const Select::iterator Select::end() { return iterator(nullptr); }
78 
79 // This is horrible, but the TextReader, and any stream based iteraton, can only
80 // iterate once, so we MUST NOT create two iterators if begin() is called twice.
82 {
83  if (!initted_) {
85  ASSERT(it);
86  it->next();
87  it_ = iterator(it);
88  initted_ = true;
89  }
90  return it_;
91 }
92 
93 
94 #ifdef SWIGPYTHON
96 #endif
97 
98 //----------------------------------------------------------------------------------------------------------------------
99 
100 } // namespace odc
static eckit::DataHandle * openForRead(const std::string &)
eckit::sql::SQLDatabase & database()
Definition: Select.cc:64
const iterator end()
Definition: Select.cc:77
bool initted_
Definition: Select.h:75
std::string selectStatement_
Definition: Select.h:68
iterator it_
Definition: Select.h:76
std::unique_ptr< eckit::DataHandle > ownDH_
Definition: Select.h:64
Select(const std::string &selectStatement="", bool manageOwnBuffer=true)
Definition: Select.cc:32
virtual ~Select()
Definition: Select.cc:62
eckit::sql::SQLSession session_
Definition: Select.h:71
iterator begin()
Definition: Select.cc:81
IteratorProxy< SelectIterator, Select, const double > iterator
Definition: Select.h:36
SelectIterator * createSelectIterator(const std::string &)
Definition: Select.cc:69
Definition: ColumnInfo.h:23
Definition: encode.cc:30