libdballe  7.21
db.h
Go to the documentation of this file.
1 #ifndef DBALLE_DB_H
2 #define DBALLE_DB_H
3 
4 #include <dballe/transaction.h>
5 #include <dballe/core/defs.h>
6 #include <dballe/db/defs.h>
7 #include <dballe/sql/fwd.h>
8 #include <wreport/varinfo.h>
9 #include <wreport/var.h>
10 #include <vector>
11 #include <string>
12 #include <map>
13 #include <memory>
14 
21 namespace dballe {
22 struct Record;
23 struct Query;
24 struct Values;
25 struct StationValues;
26 struct DataValues;
27 struct Message;
28 struct Messages;
29 struct DB;
30 
31 namespace db {
32 
34 std::string format_format(Format format);
35 
36 
38 struct Cursor
39 {
40  virtual ~Cursor();
41 
43  virtual DB& get_db() const = 0;
44 
52  virtual int remaining() const = 0;
53 
60  virtual bool next() = 0;
61 
63  virtual void discard_rest() = 0;
64 
71  virtual void to_record(Record& rec) = 0;
72 
74  virtual int get_station_id() const = 0;
75 
77  virtual double get_lat() const = 0;
78 
80  virtual double get_lon() const = 0;
81 
83  virtual const char* get_ident(const char* def=0) const = 0;
84 
86  virtual const char* get_rep_memo() const = 0;
87 
93  virtual unsigned test_iterate(FILE* dump=0);
94 };
95 
97 struct CursorStation : public Cursor
98 {
99 };
100 
102 struct CursorValue : public Cursor
103 {
105  virtual wreport::Varcode get_varcode() const = 0;
106 
108  virtual wreport::Var get_var() const = 0;
109 
114  virtual int attr_reference_id() const = 0;
115 };
116 
119 {
120 };
121 
123 struct CursorData : public CursorValue
124 {
126  virtual Level get_level() const = 0;
127 
129  virtual Trange get_trange() const = 0;
130 
132  virtual Datetime get_datetime() const = 0;
133 };
134 
136 struct CursorSummary : public Cursor
137 {
139  virtual Level get_level() const = 0;
140 
142  virtual Trange get_trange() const = 0;
143 
145  virtual wreport::Varcode get_varcode() const = 0;
146 
148  virtual DatetimeRange get_datetimerange() const = 0;
149 
151  virtual size_t get_count() const = 0;
152 };
153 
154 }
155 
156 class DB
157 {
158 public:
159  virtual ~DB();
160 
161  static db::Format get_default_format();
162  static void set_default_format(db::Format format);
163 
170  static std::unique_ptr<DB> connect_from_file(const char* pathname);
171 
179  static std::unique_ptr<DB> connect_from_url(const char* url);
180 
184  static std::unique_ptr<DB> connect_memory(const std::string& arg = std::string());
185 
192  static std::unique_ptr<DB> connect_test();
193 
197  static std::unique_ptr<DB> create(std::unique_ptr<sql::Connection> conn);
198 
207  static bool is_url(const char* str);
208 
210  virtual db::Format format() const = 0;
211 
218  virtual void disappear() = 0;
219 
231  virtual void reset(const char* repinfo_file=0) = 0;
232 
250  virtual void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated) = 0;
251 
255  virtual std::map<std::string, int> get_repinfo_priorities() = 0;
256 
257 public:
262  virtual std::unique_ptr<dballe::Transaction> transaction() = 0;
263 
280  void insert_station_data(StationValues& vals, bool can_replace, bool station_can_add);
281 
300  virtual void insert_station_data(dballe::Transaction& transaction, StationValues& vals, bool can_replace, bool station_can_add) = 0;
301 
318  void insert_data(DataValues& vals, bool can_replace, bool station_can_add);
319 
338  virtual void insert_data(dballe::Transaction& transaction, DataValues& vals, bool can_replace, bool station_can_add) = 0;
339 
347  void remove_station_data(const Query& query);
348 
358  virtual void remove_station_data(dballe::Transaction& transaction, const Query& query) = 0;
359 
367  void remove(const Query& query);
368 
378  virtual void remove(dballe::Transaction& transaction, const Query& rec) = 0;
379 
386  void remove_all();
387 
397  virtual void remove_all(dballe::Transaction& transaction) = 0;
398 
408  virtual void vacuum() = 0;
409 
423  virtual std::unique_ptr<db::CursorStation> query_stations(const Query& query) = 0;
424 
437  virtual std::unique_ptr<db::CursorStationData> query_station_data(const Query& query) = 0;
438 
451  virtual std::unique_ptr<db::CursorData> query_data(const Query& query) = 0;
452 
465  virtual std::unique_ptr<db::CursorSummary> query_summary(const Query& query) = 0;
466 
475  virtual void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
476 
485  virtual void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
486 
495  void attr_insert_station(int data_id, const Values& attrs);
496 
507  virtual void attr_insert_station(dballe::Transaction& transaction, int data_id, const Values& attrs) = 0;
508 
517  void attr_insert_data(int data_id, const Values& attrs);
518 
529  virtual void attr_insert_data(dballe::Transaction& transaction, int data_id, const Values& attrs) = 0;
530 
540  void attr_remove_station(int data_id, const db::AttrList& attrs);
541 
553  virtual void attr_remove_station(dballe::Transaction& transaction, int data_id, const db::AttrList& attrs) = 0;
554 
564  void attr_remove_data(int data_id, const db::AttrList& attrs);
565 
577  virtual void attr_remove_data(dballe::Transaction& transaction, int data_id, const db::AttrList& attrs) = 0;
578 
591  virtual bool is_station_variable(int data_id, wreport::Varcode varcode) = 0;
592 
605  void import_msg(const Message& msg, const char* repmemo, int flags);
606 
621  virtual void import_msg(dballe::Transaction& transaction, const Message& msg, const char* repmemo, int flags) = 0;
622 
635  void import_msgs(const Messages& msgs, const char* repmemo, int flags);
636 
651  virtual void import_msgs(dballe::Transaction& transaction, const Messages& msgs, const char* repmemo, int flags);
652 
665  bool export_msgs(const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest);
666 
681  virtual bool export_msgs(dballe::Transaction& transaction, const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest) = 0;
682 
686  virtual void dump(FILE* out) = 0;
687 
689  virtual void print_info(FILE* out);
690 
692  static const char* default_repinfo_file();
693 };
694 
695 }
696 #endif
virtual bool next()=0
Get a new item from the results of a query.
Common interface for cursors iterating over station or data values.
Definition: db.h:102
Cursor iterating over summary entries.
Definition: db.h:136
A RAII transaction interface.
Definition: transaction.h:15
Cursor iterating over station data values.
Definition: db.h:118
virtual const char * get_rep_memo() const =0
Get the report name.
A set of measured values.
Definition: values.h:303
virtual DB & get_db() const =0
Get the database that created this cursor.
virtual void to_record(Record &rec)=0
Fill in a record with the contents of a dba_db_cursor.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:587
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
A bulletin that has been decoded and physically interpreted.
Definition: message.h:28
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
Forward declarations for public dballe/sql names.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Common interface for all kinds of cursors.
Definition: db.h:38
Vertical level or layer.
Definition: types.h:532
virtual double get_lat() const =0
Get the station latitude.
Cursor iterating over stations.
Definition: db.h:97
Cursor iterating over data values.
Definition: db.h:123
virtual int remaining() const =0
Get the number of rows still to be fetched.
uint16_t Varcode
Range of datetimes.
Definition: types.h:272
virtual int get_station_id() const =0
Get the station identifier.
Ordered collection of messages.
Definition: message.h:67
Definition: db.h:156
std::string format_format(Format format)
Format a db::Format value to a string.
Query used to filter DB-All.e data.
Definition: query.h:14
Date and time.
Definition: types.h:158
A set of station values.
Definition: values.h:273
virtual const char * get_ident(const char *def=0) const =0
Get the station identifier, or NULL if missing.
Common definitions.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:202
virtual double get_lon() const =0
Get the station longitude.
virtual void discard_rest()=0
Discard the results that have not been read yet.