libdballe  6.8
v6/cursor.h
Go to the documentation of this file.
1 /*
2  * db/v6/cursor - manage select queries
3  *
4  * Copyright (C) 2005--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 
28 #ifndef DBA_DB_V6_CURSOR_H
29 #define DBA_DB_V6_CURSOR_H
30 
32 #include <dballe/db/db.h>
33 #include <wreport/varinfo.h>
34 #include <sqltypes.h>
35 #include <cstddef>
36 #include <vector>
37 
38 namespace dballe {
39 struct DB;
40 struct Record;
41 
42 namespace db {
43 struct Statement;
44 
45 namespace v6 {
46 struct DB;
47 
52 typedef std::vector<wreport::Varcode> AttrList;
53 
58 struct Cursor : public db::Cursor
59 {
61  struct SQLRecord
62  {
63  DBALLE_SQL_C_SINT_TYPE out_lat;
64  DBALLE_SQL_C_SINT_TYPE out_lon;
65  char out_ident[64]; SQLLEN out_ident_ind;
66  wreport::Varcode out_varcode;
67  SQL_TIMESTAMP_STRUCT out_datetime;
68  char out_value[255];
69  DBALLE_SQL_C_SINT_TYPE out_rep_cod;
70  DBALLE_SQL_C_SINT_TYPE out_ana_id;
71  DBALLE_SQL_C_SINT_TYPE out_id_ltr;
72  DBALLE_SQL_C_SINT_TYPE out_id_data;
73  int priority;
74 
82  };
83 
86 
88  const unsigned int modifiers;
89 
91  DBALLE_SQL_C_SINT_TYPE count;
92 
95 
96 
97  virtual ~Cursor();
98 
99  virtual dballe::DB& get_db() const;
100 
108  int remaining() const;
109 
111  virtual void query(const Record& query) = 0;
112 
119  virtual bool next() = 0;
120 
122  virtual void discard_rest() = 0;
123 
127  unsigned query_attrs(const AttrList& qcs, Record& attrs);
128  virtual void attr_insert(const Record& attrs);
129  virtual void attr_remove(const AttrList& qcs);
130 
131  virtual int get_station_id() const;
132  virtual double get_lat() const;
133  virtual double get_lon() const;
134  virtual const char* get_ident(const char* def=0) const;
135  virtual const char* get_rep_memo(const char* def=0) const;
136  virtual Level get_level() const;
137  virtual Trange get_trange() const;
138  virtual void get_datetime(int (&dt)[6]) const;
139  virtual wreport::Varcode get_varcode() const;
140  virtual wreport::Var get_var() const;
141 
142  virtual int attr_reference_id() const;
143 
149  virtual unsigned test_iterate(FILE* dump=0) = 0;
150 
151 protected:
160  Cursor(v6::DB& db, unsigned int modifiers);
161 
162  void to_record_pseudoana(Record& rec);
163  void to_record_repinfo(Record& rec);
164  void to_record_ltr(Record& rec);
165  void to_record_datetime(Record& rec);
166  void to_record_varcode(Record& rec);
167 
168  int query_stations(db::Statement& stm, const Record& rec);
169  int query_data(db::Statement& stm, const Record& rec);
170 
172  void add_station_info(Record& rec);
173 
174 #if 0
175 
181  int raw_query(db::Statement& stm, const Record& rec);
182 #endif
183 
193  //int getcount(const Record& query);
194 };
195 
196 class CursorLinear : public Cursor
197 {
198 public:
199  virtual ~CursorLinear();
200 
201 protected:
204 
205  CursorLinear(DB& db, unsigned int modifiers);
206 
207  virtual void discard_rest();
208  virtual bool next();
209 
210  friend class dballe::db::v6::DB;
211 };
212 
214 {
216  virtual void query(const Record& rec);
217  virtual void to_record(Record& rec);
218  virtual unsigned test_iterate(FILE* dump=0);
219 
220 protected:
221  CursorStations(DB& db, unsigned int modifiers)
222  : CursorLinear(db, modifiers) {}
223 
224  friend class dballe::db::v6::DB;
225 };
226 
227 struct CursorData : public CursorLinear
228 {
230  virtual void query(const Record& rec);
231  virtual void to_record(Record& rec);
232  virtual unsigned test_iterate(FILE* dump=0);
233 
235  //int query_count(const Record& rec);
236 
237 protected:
238  CursorData(DB& db, unsigned int modifiers)
239  : CursorLinear(db, modifiers) {}
240 
241  friend class dballe::db::v6::DB;
242 };
243 
245 {
246 public:
247  SQL_TIMESTAMP_STRUCT out_datetime_max;
248 
250  virtual void query(const Record& rec);
251  virtual void to_record(Record& rec);
252  virtual unsigned test_iterate(FILE* dump=0);
253 
254 protected:
255  CursorSummary(DB& db, unsigned int modifiers)
256  : CursorLinear(db, modifiers) {}
257 
258  friend class dballe::db::v6::DB;
259 };
260 
262 {
264  virtual void query(const Record& rec);
265  virtual void to_record(Record& rec);
266  virtual unsigned test_iterate(FILE* dump=0);
267 
268 protected:
269  CursorDataIDs(DB& db, unsigned int modifiers)
270  : CursorLinear(db, modifiers) {}
271 
272  friend class dballe::db::v6::DB;
273 };
274 
275 class CursorBest : public Cursor
276 {
277 public:
278  virtual ~CursorBest();
279 
280  virtual void query(const Record& rec);
281  virtual void to_record(Record& rec);
282  virtual unsigned test_iterate(FILE* dump=0);
283 
284 protected:
285  FILE* results;
286 
287  CursorBest(DB& db, unsigned int modifiers);
288 
289  // Save all cursor results to a temp file, filtered to keep the best values
290  // only
291  int buffer_results(db::Statement& stm);
292 
293  virtual void discard_rest();
294  virtual bool next();
295 
296  friend class dballe::db::v6::DB;
297 };
298 
299 } // namespace v6
300 } // namespace db
301 } // namespace dballe
302 
303 /* vim:set ts=4 sw=4: */
304 #endif
virtual dballe::DB & get_db() const
Get the database that created this cursor.
virtual void query(const Record &rec)
Query station info.
void add_station_info(Record &rec)
Query extra station info and add it to rec.
virtual void to_record(Record &rec)
Fill in a record with the contents of a dba_db_cursor.
virtual int attr_reference_id() const
Return an integer value that can be used to refer to the current variable for attribute access...
virtual void get_datetime(int(&dt)[6]) const
Get the datetime.
db::Statement * stm
ODBC statement to use for the query.
Definition: v6/cursor.h:203
virtual bool next()
Get a new item from the results of a query.
Definition: v6/cursor.h:213
virtual void query(const Record &query)=0
Perform the query.
virtual void query(const Record &rec)
Query the data IDs only, to use to delete things.
Definition: db.h:77
virtual void discard_rest()
Discard the results that have not been read yet.
Structure used to build and execute a query, and to iterate through the results.
Definition: v6/cursor.h:58
virtual wreport::Var get_var() const
Get the variable.
Definition: defs.h:113
The ODBC specification is imperfect with regards to integer sizes on 64bit platforms, and different ODBC drivers are currently interpreting it differently.
virtual void to_record(Record &rec)
Fill in a record with the contents of a dba_db_cursor.
virtual const char * get_ident(const char *def=0) const
Get the station identifier, or NULL if missing.
DB-All.E record.
Definition: record.h:102
int remaining() const
Get the number of rows still to be fetched.
Definition: cmdline.h:34
Definition: v6/cursor.h:196
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
virtual double get_lon() const
Get the station longitude.
virtual double get_lat() const
Get the station latitude.
virtual void to_record(Record &rec)
Fill in a record with the contents of a dba_db_cursor.
DBALLE_SQL_C_SINT_TYPE count
Number of results still to be fetched.
Definition: v6/cursor.h:91
Cursor(v6::DB &db, unsigned int modifiers)
Create a query cursor.
virtual void attr_insert(const Record &attrs)
Insert/overwrite new attributes for the current variable.
Definition: defs.h:54
Functions used to connect to DB-All.e and insert, query and delete data.
virtual void discard_rest()
Discard the results that have not been read yet.
bool querybest_fields_are_the_same(const SQLRecord &r)
Checks true if ana_id, id_ltr, datetime and varcode are the same in both records. ...
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
virtual void query(const Record &rec)
Query stats about all possible context combinations.
virtual void to_record(Record &rec)
Fill in a record with the contents of a dba_db_cursor.
v6::DB & db
Database to operate on.
Definition: v6/cursor.h:85
CursorData(DB &db, unsigned int modifiers)
Query count of items (only for stations and data)
Definition: v6/cursor.h:238
ODBC statement.
Definition: internals.h:220
virtual bool next()
Get a new item from the results of a query.
virtual const char * get_rep_memo(const char *def=0) const
Get the report name.
Definition: db.h:181
SQLRecord sqlrec
Results written by fetch.
Definition: v6/cursor.h:94
virtual void discard_rest()=0
Discard the results that have not been read yet.
virtual Trange get_trange() const
Get the level.
const unsigned int modifiers
Modifier flags to enable special query behaviours.
Definition: v6/cursor.h:88
Definition: v6/cursor.h:261
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
Definition: v6/cursor.h:275
virtual Level get_level() const
Get the level.
virtual void query(const Record &rec)
Query data.
DB-ALLe database connection.
Definition: v6/db.h:87
virtual bool next()=0
Get a new item from the results of a query.
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
Query results from SQL output.
Definition: v6/cursor.h:61
virtual wreport::Varcode get_varcode() const
Get the variable code.
virtual int get_station_id() const
Get the station identifier.
Definition: v6/cursor.h:227
virtual void query(const Record &rec)
Perform the query.
virtual void attr_remove(const AttrList &qcs)
Delete attributes for the current variable.
Definition: v6/cursor.h:244
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
unsigned query_attrs(const AttrList &qcs, Record &attrs)
Query attributes for the current variable.
virtual void to_record(Record &rec)
Fill in a record with the contents of a dba_db_cursor.