libdballe  6.8
mem/cursor.h
1 /*
2  * db/mem/cursor - iterate results of queries on mem databases
3  *
4  * Copyright (C) 2013 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_DB_MEM_CURSOR_H
23 #define DBA_DB_MEM_CURSOR_H
24 
25 #include <dballe/memdb/memdb.h>
26 #include <dballe/memdb/results.h>
27 #include <dballe/db/db.h>
28 #include <iosfwd>
29 
30 namespace dballe {
31 struct DB;
32 struct Record;
33 
34 namespace memdb {
35 template<typename T> class ValueStorage;
36 }
37 
38 namespace db {
39 
40 namespace mem {
41 struct DB;
42 
47 typedef std::vector<wreport::Varcode> AttrList;
48 
53 class Cursor : public db::Cursor
54 {
55 protected:
58 
60  const unsigned int modifiers;
61 
63  size_t count;
64 
65  const memdb::Station* cur_station;
66  const memdb::Value* cur_value;
67  const wreport::Var* cur_var;
68 
69 public:
70  virtual ~Cursor();
71 
72  virtual dballe::DB& get_db() const;
73 
75  int remaining() const;
76 
83  virtual bool next() = 0;
84 
86  virtual void discard_rest() = 0;
87 
91  unsigned query_attrs(const AttrList& qcs, Record& attrs);
92 
93  virtual int get_station_id() const;
94  virtual double get_lat() const;
95  virtual double get_lon() const;
96  virtual const char* get_ident(const char* def=0) const;
97  virtual const char* get_rep_memo(const char* def=0) const;
98  virtual Level get_level() const;
99  virtual Trange get_trange() const;
100  virtual void get_datetime(int (&dt)[6]) const;
101  virtual wreport::Varcode get_varcode() const;
102  virtual wreport::Var get_var() const;
103 
104  virtual int attr_reference_id() const;
105 
106 #if 0
107 
112  virtual unsigned test_iterate(FILE* dump=0) = 0;
113 #endif
114  static std::auto_ptr<db::Cursor> createStations(mem::DB& db, unsigned modifiers, memdb::Results<memdb::Station>& res);
115  static std::auto_ptr<db::Cursor> createStationData(mem::DB& db, unsigned modifiers, memdb::Results<memdb::StationValue>& res);
116  static std::auto_ptr<db::Cursor> createData(mem::DB& db, unsigned modifiers, memdb::Results<memdb::Value>& res);
117  static std::auto_ptr<db::Cursor> createDataBest(mem::DB& db, unsigned modifiers, memdb::Results<memdb::Value>& res);
118  static std::auto_ptr<db::Cursor> createSummary(mem::DB& db, unsigned modifiers, memdb::Results<memdb::Value>& res);
119 
120 protected:
129  Cursor(mem::DB& db, unsigned modifiers);
130 
131  void to_record_station(Record& rec);
132  void to_record_levtr(Record& rec);
133  void to_record_varcode(Record& rec);
134  void to_record_value(Record& rec);
135 
137  void add_station_info(Record& rec);
138 };
139 
140 namespace cursor {
141 
147 {
148  const memdb::ValueStorage<memdb::Value>& values;
149  size_t idx;
150 
151  DataBestKey(const memdb::ValueStorage<memdb::Value>& values, size_t idx)
152  : values(values), idx(idx) {}
153 
154  const memdb::Value& value() const;
155 
156  bool operator<(const DataBestKey& o) const;
157 };
158 
159 std::ostream& operator<<(std::ostream& out, const DataBestKey& k);
160 
161 }
162 
163 } // namespace v6
164 } // namespace db
165 } // namespace dballe
166 
167 /* vim:set ts=4 sw=4: */
168 #endif
virtual void get_datetime(int(&dt)[6]) const
Get the datetime.
virtual void discard_rest()=0
Discard the results that have not been read yet.
Wrapper around a Value index that compares so that all values from which the best report should be se...
Definition: mem/cursor.h:146
mem::DB & db
Database to operate on.
Definition: mem/cursor.h:57
Definition: db.h:77
size_t count
Number of results still to be fetched.
Definition: mem/cursor.h:63
Definition: defs.h:113
virtual const char * get_ident(const char *def=0) const
Get the station identifier, or NULL if missing.
virtual int get_station_id() const
Get the station identifier.
const unsigned int modifiers
Modifier flags to enable special query behaviours.
Definition: mem/cursor.h:60
virtual double get_lon() const
Get the station longitude.
virtual wreport::Var get_var() const
Get the variable.
DB-All.E record.
Definition: record.h:102
Definition: cmdline.h:34
virtual wreport::Varcode get_varcode() const
Get the variable code.
void add_station_info(Record &rec)
Query extra station info and add it to rec.
Cursor(mem::DB &db, unsigned modifiers)
Create a query cursor.
Station information.
Definition: value.h:43
Definition: defs.h:54
Functions used to connect to DB-All.e and insert, query and delete data.
Station information.
Definition: memdb/station.h:43
Structure used to build and execute a query, and to iterate through the results.
Definition: mem/cursor.h:53
virtual const char * get_rep_memo(const char *def=0) const
Get the report name.
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
Definition: db.h:181
virtual dballe::DB & get_db() const
Get the database that created this cursor.
virtual int attr_reference_id() const
Return an integer value that can be used to refer to the current variable for attribute access...
int remaining() const
Get the number of rows still to be fetched.
Definition: mem/cursor.h:35
unsigned query_attrs(const AttrList &qcs, Record &attrs)
Query attributes for the current variable.
virtual Trange get_trange() const
Get the level.
Definition: levtr.h:33
virtual double get_lat() const
Get the station latitude.
virtual bool next()=0
Get a new item from the results of a query.
DB-ALLe database connection.
Definition: mem/db.h:52
virtual Level get_level() const
Get the level.