libdballe  7.6
db.h
Go to the documentation of this file.
1 #ifndef DBALLE_DB_H
2 #define DBALLE_DB_H
3 
4 #include <dballe/core/defs.h>
5 #include <dballe/db/defs.h>
6 #include <wreport/varinfo.h>
7 #include <wreport/var.h>
8 #include <vector>
9 #include <string>
10 #include <map>
11 #include <memory>
12 
19 namespace dballe {
20 struct Record;
21 struct Query;
22 struct Values;
23 struct StationValues;
24 struct DataValues;
25 struct Message;
26 struct Messages;
27 struct DB;
28 
29 namespace db {
30 struct Connection;
31 struct ODBCConnection;
32 
36 typedef std::vector<wreport::Varcode> AttrList;
37 
39 struct Cursor
40 {
41  virtual ~Cursor();
42 
44  virtual DB& get_db() const = 0;
45 
53  virtual int remaining() const = 0;
54 
61  virtual bool next() = 0;
62 
64  virtual void discard_rest() = 0;
65 
72  virtual void to_record(Record& rec) = 0;
73 
75  virtual int get_station_id() const = 0;
76 
78  virtual double get_lat() const = 0;
79 
81  virtual double get_lon() const = 0;
82 
84  virtual const char* get_ident(const char* def=0) const = 0;
85 
87  virtual const char* get_rep_memo() const = 0;
88 
94  virtual unsigned test_iterate(FILE* dump=0);
95 };
96 
98 struct CursorStation : public Cursor
99 {
100 };
101 
103 struct CursorValue : public Cursor
104 {
106  virtual wreport::Varcode get_varcode() const = 0;
107 
109  virtual wreport::Var get_var() const = 0;
110 
115  virtual int attr_reference_id() const = 0;
116 };
117 
120 {
121 };
122 
124 struct CursorData : public CursorValue
125 {
127  virtual Level get_level() const = 0;
128 
130  virtual Trange get_trange() const = 0;
131 
133  virtual Datetime get_datetime() const = 0;
134 };
135 
137 struct CursorSummary : public Cursor
138 {
140  virtual Level get_level() const = 0;
141 
143  virtual Trange get_trange() const = 0;
144 
146  virtual wreport::Varcode get_varcode() const = 0;
147 
149  virtual DatetimeRange get_datetimerange() const = 0;
150 
152  virtual size_t get_count() const = 0;
153 };
154 
155 
156 #if 0
157 
160  virtual void query_attrs(std::function<void(std::unique_ptr<wreport::Var>&&)> dest) = 0;
161 
168  virtual void attr_insert(const Values& attrs) = 0;
169 
177  virtual void attr_remove(const AttrList& qcs) = 0;
178 #endif
179 
180 }
181 
182 class DB
183 {
184 public:
185  virtual ~DB();
186 
187  static db::Format get_default_format();
188  static void set_default_format(db::Format format);
189 
203  static std::unique_ptr<DB> connect(const char* dsn, const char* user, const char* password);
204 
211  static std::unique_ptr<DB> connect_from_file(const char* pathname);
212 
223  static std::unique_ptr<DB> connect_from_url(const char* url);
224 
228  static std::unique_ptr<DB> connect_memory(const std::string& arg = std::string());
229 
236  static std::unique_ptr<DB> connect_test();
237 
241  static std::unique_ptr<DB> create(std::unique_ptr<db::Connection> conn);
242 
251  static bool is_url(const char* str);
252 
254  virtual db::Format format() const = 0;
255 
262  virtual void disappear() = 0;
263 
275  virtual void reset(const char* repinfo_file=0) = 0;
276 
294  virtual void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated) = 0;
295 
299  virtual std::map<std::string, int> get_repinfo_priorities() = 0;
300 
301 public:
318  virtual void insert_station_data(StationValues& vals, bool can_replace, bool station_can_add) = 0;
319 
336  virtual void insert_data(DataValues& vals, bool can_replace, bool station_can_add) = 0;
337 
345  virtual void remove_station_data(const Query& query) = 0;
346 
354  virtual void remove(const Query& rec) = 0;
355 
362  virtual void remove_all() = 0;
363 
373  virtual void vacuum() = 0;
374 
388  virtual std::unique_ptr<db::CursorStation> query_stations(const Query& query) = 0;
389 
402  virtual std::unique_ptr<db::CursorStationData> query_station_data(const Query& query) = 0;
403 
416  virtual std::unique_ptr<db::CursorData> query_data(const Query& query) = 0;
417 
430  virtual std::unique_ptr<db::CursorSummary> query_summary(const Query& query) = 0;
431 
440  virtual void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
441 
450  virtual void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) = 0;
451 
460  virtual void attr_insert_station(int data_id, const Values& attrs) = 0;
461 
470  virtual void attr_insert_data(int data_id, const Values& attrs) = 0;
471 
481  virtual void attr_remove_station(int data_id, const db::AttrList& qcs) = 0;
482 
492  virtual void attr_remove_data(int data_id, const db::AttrList& qcs) = 0;
493 
506  virtual bool is_station_variable(int data_id, wreport::Varcode varcode) = 0;
507 
522  virtual void import_msg(const Message& msg, const char* repmemo, int flags) = 0;
523 
538  virtual void import_msgs(const Messages& msgs, const char* repmemo, int flags);
539 
552  virtual bool export_msgs(const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest) = 0;
553 
557  virtual void dump(FILE* out) = 0;
558 
560  static const char* default_repinfo_file();
561 };
562 
563 }
564 
565 /* vim:set ts=4 sw=4: */
566 #endif
virtual bool next()=0
Get a new item from the results of a query.
static std::unique_ptr< DB > connect(const char *dsn, const char *user, const char *password)
Start a session with DB-All.e.
Common interface for cursors iterating over station or data values.
Definition: db.h:103
virtual void remove_all()=0
Remove all data from the database.
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:137
virtual void import_msg(const Message &msg, const char *repmemo, int flags)=0
Import a Message into the DB-All.e database.
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:119
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.
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.
Definition: values.h:163
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:565
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
virtual void attr_insert_data(int data_id, const Values &attrs)=0
Insert new attributes on a data value.
A bulletin that has been decoded and physically interpreted.
Definition: message.h:25
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
virtual void dump(FILE *out)=0
Dump the entire contents of the database to an output stream.
virtual void remove_station_data(const Query &query)=0
Remove data from 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.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
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 void attr_remove_data(int data_id, const db::AttrList &qcs)=0
Delete attributes from a data value.
virtual Trange get_trange() const =0
Get the time range.
Common interface for all kinds of cursors.
Definition: db.h:39
Vertical level or layer.
Definition: types.h:515
virtual double get_lat() const =0
Get the station latitude.
Cursor iterating over stations.
Definition: db.h:98
Cursor iterating over data values.
Definition: db.h:124
virtual int remaining() const =0
Get the number of rows still to be fetched.
virtual DatetimeRange get_datetimerange() const =0
Get the datetime range.
Range of datetimes.
Definition: types.h:255
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.
virtual void attr_remove_station(int data_id, const db::AttrList &qcs)=0
Delete attributes from a station value.
virtual std::unique_ptr< db::CursorStation > query_stations(const Query &query)=0
Start a query on the station variables archive.
virtual void import_msgs(const Messages &msgs, const char *repmemo, int flags)
Import Messages into the DB-All.e database.
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.
Ordered collection of messages.
Definition: message.h:64
static std::unique_ptr< DB > create(std::unique_ptr< db::Connection > conn)
Create a database from an open Connection.
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:182
virtual Trange get_trange() const =0
Get the time range.
Query used to filter DB-All.e data.
Definition: query.h:14
Date and time.
Definition: types.h:147
static std::unique_ptr< DB > connect_from_file(const char *pathname)
Create from a SQLite file pathname.
Definition: values.h:142
static std::unique_ptr< DB > connect_from_url(const char *url)
Create from an url-like specification, that can be:
virtual wreport::Varcode get_varcode() const =0
Get the variable code.
virtual void insert_station_data(StationValues &vals, bool can_replace, bool station_can_add)=0
Insert station values into the database.
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.
virtual void attr_insert_station(int data_id, const Values &attrs)=0
Insert new attributes on a station value.
Common definitions.
virtual Level get_level() const =0
Get the level.
virtual void insert_data(DataValues &vals, bool can_replace, bool station_can_add)=0
Insert data values into 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.
Definition: values.h:106
virtual Datetime get_datetime() const =0
Get the datetime.
virtual double get_lon() const =0
Get the station longitude.
virtual bool export_msgs(const Query &query, std::function< bool(std::unique_ptr< Message > &&)> dest)=0
Perform the query in `query', and send the results to dest.
static std::unique_ptr< DB > connect_test()
Start a test session with DB-All.e.
virtual void discard_rest()=0
Discard the results that have not been read yet.