libdballe  7.29
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/core/values.h>
7 #include <dballe/db/defs.h>
8 #include <dballe/sql/fwd.h>
9 #include <wreport/varinfo.h>
10 #include <wreport/var.h>
11 #include <vector>
12 #include <string>
13 #include <map>
14 #include <memory>
15 #include <functional>
16 
23 namespace dballe {
24 struct Record;
25 struct Query;
26 struct Values;
27 struct StationValues;
28 struct DataValues;
29 struct Message;
30 struct Messages;
31 struct DB;
32 
33 namespace db {
34 
36 std::string format_format(Format format);
37 
39 Format format_parse(const std::string& str);
40 
41 
43 struct Cursor
44 {
45  virtual ~Cursor();
46 
48  virtual DB& get_db() const = 0;
49 
57  virtual int remaining() const = 0;
58 
65  virtual bool next() = 0;
66 
68  virtual void discard_rest() = 0;
69 
76  virtual void to_record(Record& rec) = 0;
77 
81  virtual Station get_station() const = 0;
82 
84  virtual int get_station_id() const = 0;
85 
87  virtual double get_lat() const = 0;
88 
90  virtual double get_lon() const = 0;
91 
93  virtual const char* get_ident(const char* def=0) const = 0;
94 
96  virtual const char* get_rep_memo() const = 0;
97 
103  virtual unsigned test_iterate(FILE* dump=0);
104 };
105 
107 struct CursorStation : public Cursor
108 {
109 };
110 
112 struct CursorValue : public Cursor
113 {
115  virtual wreport::Varcode get_varcode() const = 0;
116 
118  virtual wreport::Var get_var() const = 0;
119 
124  virtual int attr_reference_id() const = 0;
125 };
126 
129 {
130 };
131 
133 struct CursorData : public CursorValue
134 {
136  virtual Level get_level() const = 0;
137 
139  virtual Trange get_trange() const = 0;
140 
142  virtual Datetime get_datetime() const = 0;
143 };
144 
146 struct CursorSummary : public Cursor
147 {
149  virtual Level get_level() const = 0;
150 
152  virtual Trange get_trange() const = 0;
153 
155  virtual wreport::Varcode get_varcode() const = 0;
156 
158  virtual DatetimeRange get_datetimerange() const = 0;
159 
161  virtual size_t get_count() const = 0;
162 };
163 
164 }
165 
166 class DB
167 {
168 public:
169  virtual ~DB();
170 
171  static db::Format get_default_format();
172  static void set_default_format(db::Format format);
173 
180  static std::unique_ptr<DB> connect_from_file(const char* pathname);
181 
189  static std::unique_ptr<DB> connect_from_url(const char* url);
190 
194  static std::unique_ptr<DB> connect_memory(const std::string& arg = std::string());
195 
202  static std::unique_ptr<DB> connect_test();
203 
207  static std::unique_ptr<DB> create(std::unique_ptr<sql::Connection> conn);
208 
217  static bool is_url(const char* str);
218 
220  virtual db::Format format() const = 0;
221 
228  virtual void disappear() = 0;
229 
241  virtual void reset(const char* repinfo_file=0) = 0;
242 
260  virtual void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated) = 0;
261 
265  virtual std::map<std::string, int> get_repinfo_priorities() = 0;
266 
267 public:
272  virtual std::unique_ptr<dballe::Transaction> transaction() = 0;
273 
290  void insert_station_data(StationValues& vals, bool can_replace, bool station_can_add);
291 
310  virtual void insert_station_data(dballe::Transaction& transaction, StationValues& vals, bool can_replace, bool station_can_add) = 0;
311 
328  void insert_data(DataValues& vals, bool can_replace, bool station_can_add);
329 
348  virtual void insert_data(dballe::Transaction& transaction, DataValues& vals, bool can_replace, bool station_can_add) = 0;
349 
357  void remove_station_data(const Query& query);
358 
368  virtual void remove_station_data(dballe::Transaction& transaction, const Query& query) = 0;
369 
377  void remove(const Query& query);
378 
388  virtual void remove(dballe::Transaction& transaction, const Query& rec) = 0;
389 
396  void remove_all();
397 
407  virtual void remove_all(dballe::Transaction& transaction) = 0;
408 
418  virtual void vacuum() = 0;
419 
433  virtual std::unique_ptr<db::CursorStation> query_stations(const Query& query) = 0;
434 
447  virtual std::unique_ptr<db::CursorStationData> query_station_data(const Query& query) = 0;
448 
461  virtual std::unique_ptr<db::CursorData> query_data(const Query& query) = 0;
462 
475  virtual std::unique_ptr<db::CursorSummary> query_summary(const Query& query) = 0;
476 
485  virtual void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
486 
495  virtual void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
496 
505  void attr_insert_station(int data_id, const Values& attrs);
506 
517  virtual void attr_insert_station(dballe::Transaction& transaction, int data_id, const Values& attrs) = 0;
518 
527  void attr_insert_data(int data_id, const Values& attrs);
528 
539  virtual void attr_insert_data(dballe::Transaction& transaction, int data_id, const Values& attrs) = 0;
540 
550  void attr_remove_station(int data_id, const db::AttrList& attrs);
551 
563  virtual void attr_remove_station(dballe::Transaction& transaction, int data_id, const db::AttrList& attrs) = 0;
564 
574  void attr_remove_data(int data_id, const db::AttrList& attrs);
575 
587  virtual void attr_remove_data(dballe::Transaction& transaction, int data_id, const db::AttrList& attrs) = 0;
588 
601  virtual bool is_station_variable(int data_id, wreport::Varcode varcode) = 0;
602 
615  void import_msg(const Message& msg, const char* repmemo, int flags);
616 
631  virtual void import_msg(dballe::Transaction& transaction, const Message& msg, const char* repmemo, int flags) = 0;
632 
645  void import_msgs(const Messages& msgs, const char* repmemo, int flags);
646 
661  virtual void import_msgs(dballe::Transaction& transaction, const Messages& msgs, const char* repmemo, int flags);
662 
675  bool export_msgs(const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest);
676 
691  virtual bool export_msgs(dballe::Transaction& transaction, const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest) = 0;
692 
696  virtual void dump(FILE* out) = 0;
697 
699  virtual void print_info(FILE* out);
700 
702  static const char* default_repinfo_file();
703 };
704 
705 }
706 #endif
virtual void print_info(FILE *out)
Print informations about the database to the given output stream.
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:112
virtual void disappear()=0
Remove all our traces from the database, if applicable.
virtual wreport::Var get_var() const =0
Get the variable.
Cursor iterating over summary entries.
Definition: db.h:146
void import_msg(const Message &msg, const char *repmemo, int flags)
Import a Message into the DB-All.e database.
A RAII transaction interface.
Definition: transaction.h:15
virtual int attr_reference_id() const =0
Return an integer value that can be used to refer to the current variable for attribute access...
Cursor iterating over station data values.
Definition: db.h:128
Information about a station.
Definition: values.h:21
virtual std::unique_ptr< db::CursorStationData > query_station_data(const Query &query)=0
Query the station variables in the database.
virtual std::unique_ptr< db::CursorSummary > query_summary(const Query &query)=0
Query a summary of what the result would be for a query.
void insert_data(DataValues &vals, bool can_replace, bool station_can_add)
Insert data values into the database.
void import_msgs(const Messages &msgs, const char *repmemo, int flags)
Import Messages into the DB-All.e database.
void attr_insert_data(int data_id, const Values &attrs)
Insert new attributes on a data value.
static const char * default_repinfo_file()
Return the default repinfo file pathname.
virtual db::Format format() const =0
Return the format of this DB.
virtual const char * get_rep_memo() const =0
Get the report name.
A set of measured values.
Definition: values.h:304
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.
virtual wreport::Varcode get_varcode() const =0
Get the variable code.
virtual std::map< std::string, int > get_repinfo_priorities()=0
Get a mapping between rep_memo and their priorities.
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:17
virtual void dump(FILE *out)=0
Dump the entire contents of the database to an output stream.
void insert_station_data(StationValues &vals, bool can_replace, bool station_can_add)
Insert station values into the database.
virtual bool is_station_variable(int data_id, wreport::Varcode varcode)=0
Check if this varcode and data_id correspond to a station variable.
Forward declarations for public dballe/sql names.
virtual void attr_query_data(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)=0
Query attributes on a data value.
virtual Trange get_trange() const =0
Get the time range.
Common interface for all kinds of cursors.
Definition: db.h:43
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:107
Cursor iterating over data values.
Definition: db.h:133
virtual int remaining() const =0
Get the number of rows still to be fetched.
virtual DatetimeRange get_datetimerange() const =0
Get the datetime range.
uint16_t Varcode
Range of datetimes.
Definition: types.h:272
void remove_station_data(const Query &query)
Remove data from the database.
virtual void attr_query_station(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)=0
Query attributes on a station value.
virtual size_t get_count() const =0
Get the count of elements.
virtual int get_station_id() const =0
Get the station identifier.
static std::unique_ptr< DB > create(std::unique_ptr< sql::Connection > conn)
Create a database from an open Connection.
virtual std::unique_ptr< db::CursorStation > query_stations(const Query &query)=0
Start a query on the station variables archive.
static bool is_url(const char *str)
Return TRUE if the string looks like a DB URL.
virtual Level get_level() const =0
Get the level.
void attr_insert_station(int data_id, const Values &attrs)
Insert new attributes on a station value.
Ordered collection of messages.
Definition: message.h:67
virtual void reset(const char *repinfo_file=0)=0
Reset the database, removing all existing Db-All.e tables and re-creating them empty.
Definition: db.h:166
virtual Trange get_trange() const =0
Get the time range.
void attr_remove_data(int data_id, const db::AttrList &attrs)
Delete attributes from a data value.
virtual std::unique_ptr< dballe::Transaction > transaction()=0
Begin a transaction on this database, and return a Transaction object that can be used to commit it...
Query used to filter DB-All.e data.
Definition: query.h:14
Date and time.
Definition: types.h:158
void attr_remove_station(int data_id, const db::AttrList &attrs)
Delete attributes from a station value.
bool export_msgs(const Query &query, std::function< bool(std::unique_ptr< Message > &&)> dest)
Perform the query in `query&#39;, and send the results to dest.
static std::unique_ptr< DB > connect_from_file(const char *pathname)
Create from a SQLite file pathname.
A set of station values.
Definition: values.h:274
static std::unique_ptr< DB > connect_from_url(const char *url)
Create from an url-like specification, as described in doc/fapi_connect.md.
virtual wreport::Varcode get_varcode() const =0
Get the variable code.
virtual void vacuum()=0
Perform database cleanup operations.
virtual std::unique_ptr< db::CursorData > query_data(const Query &query)=0
Query the database.
virtual const char * get_ident(const char *def=0) const =0
Get the station identifier, or NULL if missing.
Common definitions.
Structures used as input to database insert functions.
virtual Level get_level() const =0
Get the level.
void remove_all()
Remove all data from the database.
virtual void update_repinfo(const char *repinfo_file, int *added, int *deleted, int *updated)=0
Update the repinfo table in the database, with the data found in the given file.
static std::unique_ptr< DB > connect_memory(const std::string &arg=std::string())
Create an in-memory database.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:203
virtual Datetime get_datetime() const =0
Get the datetime.
virtual double get_lon() const =0
Get the station longitude.
static std::unique_ptr< DB > connect_test()
Start a test session with DB-All.e.
virtual Station get_station() const =0
Get the whole station data in a single call.
virtual void discard_rest()=0
Discard the results that have not been read yet.