libdballe  6.8
db.h
Go to the documentation of this file.
1 /*
2  * dballe/db - Archive for point-based meteorological data
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_H
23 #define DBA_DB_H
24 
25 #include <dballe/core/defs.h>
26 #include <wreport/varinfo.h>
27 #include <wreport/var.h>
28 #include <vector>
29 #include <string>
30 #include <map>
31 #include <memory>
32 
43 /* Import the attributes. */
44 #define DBA_IMPORT_ATTRS 1
45 /* Attempt to merge pseudoana extra information into the existing ones. */
46 #define DBA_IMPORT_FULL_PSEUDOANA 2
47 /* Message data will overwrite existing values; otherwise, trying to insert
48  * existing data will cause an error. */
49 #define DBA_IMPORT_OVERWRITE 8
50 
52 namespace dballe {
53 struct Record;
54 struct Msg;
55 struct Msgs;
56 struct MsgConsumer;
57 struct DB;
58 
59 namespace db {
60 struct Connection;
61 
65 typedef enum {
66  V5 = 0,
67  V6 = 1,
68  MEM = 2,
69  MESSAGES = 3,
70 } Format;
71 
75 typedef std::vector<wreport::Varcode> AttrList;
76 
77 class Cursor
78 {
79 public:
80  virtual ~Cursor();
81 
83  virtual DB& get_db() const = 0;
84 
92  virtual int remaining() const = 0;
93 
100  virtual bool next() = 0;
101 
103  virtual void discard_rest() = 0;
104 
111  virtual void to_record(Record& rec) = 0;
112 
114  virtual int get_station_id() const = 0;
115 
117  virtual double get_lat() const = 0;
118 
120  virtual double get_lon() const = 0;
121 
123  virtual const char* get_ident(const char* def=0) const = 0;
124 
126  virtual const char* get_rep_memo(const char* def=0) const = 0;
127 
129  virtual Level get_level() const = 0;
130 
132  virtual Trange get_trange() const = 0;
133 
135  virtual void get_datetime(int (&dt)[6]) const = 0;
136 
138  virtual wreport::Varcode get_varcode() const = 0;
139 
141  virtual wreport::Var get_var() const = 0;
142 
147  virtual int attr_reference_id() const = 0;
148 
152  virtual unsigned query_attrs(const AttrList& qcs, Record& attrs) = 0;
153 
160  virtual void attr_insert(const Record& attrs) = 0;
161 
169  virtual void attr_remove(const AttrList& qcs) = 0;
170 
176  virtual unsigned test_iterate(FILE* dump=0);
177 };
178 
179 }
180 
181 class DB
182 {
183 public:
184  virtual ~DB();
185 
186  static db::Format get_default_format();
187  static void set_default_format(db::Format format);
188 
202  static std::auto_ptr<DB> connect(const char* dsn, const char* user, const char* password);
203 
210  static std::auto_ptr<DB> connect_from_file(const char* pathname);
211 
222  static std::auto_ptr<DB> connect_from_url(const char* url);
223 
227  static std::auto_ptr<DB> connect_memory(const std::string& arg = std::string());
228 
235  static std::auto_ptr<DB> connect_test();
236 
245  static bool is_url(const char* str);
246 
248  virtual db::Format format() const = 0;
249 
256  virtual void disappear() = 0;
257 
269  virtual void reset(const char* repinfo_file = 0) = 0;
270 
288  virtual void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated) = 0;
289 
293  virtual std::map<std::string, int> get_repinfo_priorities() = 0;
294 
313  virtual void insert(const Record& rec, bool can_replace, bool station_can_add) = 0;
314 
318  virtual int last_station_id() const = 0;
319 
327  virtual void remove(const Record& rec) = 0;
328 
335  virtual void remove_all() = 0;
336 
346  virtual void vacuum() = 0;
347 
361  virtual std::auto_ptr<db::Cursor> query_stations(const Record& query) = 0;
362 
375  virtual std::auto_ptr<db::Cursor> query_data(const Record& rec) = 0;
376 
389  virtual std::auto_ptr<db::Cursor> query_summary(const Record& rec) = 0;
390 
406  virtual unsigned query_attrs(int reference_id, wreport::Varcode id_var, const db::AttrList& qcs, Record& attrs) = 0;
407 
418  virtual void attr_insert(wreport::Varcode id_var, const Record& attrs) = 0;
419 
432  virtual void attr_insert(int reference_id, wreport::Varcode id_var, const Record& attrs) = 0;
433 
446  virtual void attr_remove(int reference_id, wreport::Varcode id_var, const db::AttrList& qcs) = 0;
447 
462  virtual void import_msg(const Msg& msg, const char* repmemo, int flags) = 0;
463 
478  virtual void import_msgs(const Msgs& msgs, const char* repmemo, int flags);
479 
489  virtual void export_msgs(const Record& query, MsgConsumer& cons) = 0;
490 
494  virtual void dump(FILE* out) = 0;
495 
497  static const char* default_repinfo_file();
498 
499 protected:
500  static std::auto_ptr<DB> instantiate_db(std::auto_ptr<db::Connection>& conn);
501 };
502 
503 }
504 
505 /* vim:set ts=4 sw=4: */
506 #endif
virtual std::auto_ptr< db::Cursor > query_stations(const Record &query)=0
Start a query on the station variables archive.
virtual unsigned query_attrs(const AttrList &qcs, Record &attrs)=0
Query attributes for the current variable.
static std::auto_ptr< DB > connect_memory(const std::string &arg=std::string())
Create an in-memory database.
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 void attr_insert(const Record &attrs)=0
Insert/overwrite new attributes for the current variable.
virtual std::auto_ptr< db::Cursor > query_data(const Record &rec)=0
Query the database.
virtual const char * get_rep_memo(const char *def=0) const =0
Get the report name.
virtual void import_msg(const Msg &msg, const char *repmemo, int flags)=0
Import a Msg message into the DB-All.e database.
Storage for related physical data.
Definition: msg.h:119
virtual int attr_reference_id() const =0
Return an integer value that can be used to refer to the current variable for attribute access...
static std::auto_ptr< DB > connect_from_url(const char *url)
Create from an url-like specification, that can be:
virtual double get_lon() const =0
Get the station longitude.
Definition: db.h:77
static const char * default_repinfo_file()
Return the default repinfo file pathname.
virtual Level get_level() const =0
Get the level.
virtual db::Format format() const =0
Return the format of this DB.
virtual std::map< std::string, int > get_repinfo_priorities()=0
Get a mapping between rep_memo and their priorities.
Definition: defs.h:113
virtual DB & get_db() const =0
Get the database that created this cursor.
Common definitions.
virtual int get_station_id() const =0
Get the station identifier.
virtual unsigned query_attrs(int reference_id, wreport::Varcode id_var, const db::AttrList &qcs, Record &attrs)=0
Query attributes.
virtual void dump(FILE *out)=0
Dump the entire contents of the database to an output stream.
virtual void attr_insert(wreport::Varcode id_var, const Record &attrs)=0
Insert new attributes into the database, reusing the reference IDs stored by the last insert...
DB-All.E record.
Definition: record.h:102
Definition: cmdline.h:34
Definition: defs.h:54
virtual void attr_remove(int reference_id, wreport::Varcode id_var, const db::AttrList &qcs)=0
Delete QC data for the variable `var' in record `rec' (coming from a previous dba_query) ...
virtual Trange get_trange() const =0
Get the level.
virtual double get_lat() const =0
Get the station latitude.
virtual wreport::Varcode get_varcode() const =0
Get the variable code.
virtual void attr_remove(const AttrList &qcs)=0
Delete attributes for the current variable.
virtual void get_datetime(int(&dt)[6]) const =0
Get the datetime.
virtual unsigned test_iterate(FILE *dump=0)
Iterate the cursor until the end, returning the number of items.
virtual void discard_rest()=0
Discard the results that have not been read yet.
virtual void export_msgs(const Record &query, MsgConsumer &cons)=0
Perform the query in `query', and return the results as a NULL-terminated array of dba_msg...
virtual void import_msgs(const Msgs &msgs, const char *repmemo, int flags)
Import Msgs 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 int last_station_id() const =0
Return the station id for the last data that was inserted.
virtual const char * get_ident(const char *def=0) const =0
Get the station identifier, or NULL if missing.
virtual void reset(const char *repinfo_file=0)=0
Reset the database, removing all existing Db-All.e tables and re-creating them empty.
virtual wreport::Var get_var() const =0
Get the variable.
Definition: db.h:181
Dynamic array of dba_msg.
Definition: msgs.h:38
virtual void to_record(Record &rec)=0
Fill in a record with the contents of a dba_db_cursor.
virtual std::auto_ptr< db::Cursor > query_summary(const Record &rec)=0
Query a summary of what the result would be for a query.
Consumer interface used to stream messages as they are produced.
Definition: msg.h:478
virtual bool next()=0
Get a new item from the results of a query.
virtual void vacuum()=0
Perform database cleanup operations.
static std::auto_ptr< DB > connect_test()
Start a test session with DB-All.e.
virtual int remaining() const =0
Get the number of rows still to be fetched.
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.
virtual void insert(const Record &rec, bool can_replace, bool station_can_add)=0
Insert a record into the database.
static std::auto_ptr< DB > connect_from_file(const char *pathname)
Create from a SQLite file pathname.
static std::auto_ptr< DB > connect(const char *dsn, const char *user, const char *password)
Start a session with DB-All.e.