libdballe  9.14
db/db.h
Go to the documentation of this file.
1 #ifndef DBALLE_DB_DB_H
2 #define DBALLE_DB_DB_H
3 
4 #include <dballe/core/cursor.h>
5 #include <dballe/db.h>
6 #include <dballe/db/defs.h>
7 #include <dballe/db/fwd.h>
8 #include <dballe/fwd.h>
9 #include <dballe/msg/fwd.h>
10 #include <dballe/sql/fwd.h>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <wreport/var.h>
15 #include <wreport/varinfo.h>
16 
23 namespace dballe {
24 
25 namespace impl {
26 
30 {
31  DBImportOptions() = default;
32  DBImportOptions(const DBImportOptions& o) = default;
33  DBImportOptions(DBImportOptions&& o) = default;
34  DBImportOptions& operator=(const DBImportOptions&) = default;
35  DBImportOptions& operator=(DBImportOptions&&) = default;
36 };
37 
41 {
42  DBInsertOptions() = default;
43  DBInsertOptions(const DBInsertOptions& o) = default;
44  DBInsertOptions(DBInsertOptions&& o) = default;
45  DBInsertOptions& operator=(const DBInsertOptions&) = default;
46  DBInsertOptions& operator=(DBInsertOptions&&) = default;
47 };
48 
49 } // namespace impl
50 
51 namespace db {
52 
54 std::string format_format(Format format);
55 
57 Format format_parse(const std::string& str);
58 
60 {
62  virtual void remove() = 0;
63 
69  virtual unsigned test_iterate(FILE* dump = 0) = 0;
70 };
71 
73 {
75  virtual void remove() = 0;
76 
78  virtual std::shared_ptr<db::Transaction> get_transaction() const = 0;
79 
84  virtual int attr_reference_id() const = 0;
85 
89  virtual void
90  query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest,
91  bool force_read = false) = 0;
92 
94  virtual void insert_attrs(const Values& attrs);
95 
97  virtual void remove_attrs(const db::AttrList& attrs);
98 
104  virtual unsigned test_iterate(FILE* dump = 0) = 0;
105 };
106 
108 {
110  virtual void remove() = 0;
111 
113  virtual std::shared_ptr<db::Transaction> get_transaction() const = 0;
114 
119  virtual int attr_reference_id() const = 0;
120 
124  virtual void
125  query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest,
126  bool force_read = false) = 0;
127 
129  virtual void insert_attrs(const Values& attrs);
130 
132  virtual void remove_attrs(const db::AttrList& attrs);
133 
139  virtual unsigned test_iterate(FILE* dump = 0) = 0;
140 };
141 
143 {
145  virtual void remove() = 0;
146 
152  virtual unsigned test_iterate(FILE* dump = 0) = 0;
153 };
154 
156 {
157 public:
158  virtual ~Transaction() {}
159 
167  virtual void clear_cached_state() = 0;
168 
178  virtual void attr_query_station(
179  int data_id,
180  std::function<void(std::unique_ptr<wreport::Var>)> dest) = 0;
181 
191  virtual void attr_query_data(
192  int data_id,
193  std::function<void(std::unique_ptr<wreport::Var>)> dest) = 0;
194 
204  virtual void attr_insert_station(int data_id, const Values& attrs) = 0;
205 
215  virtual void attr_insert_data(int data_id, const Values& attrs) = 0;
216 
227  virtual void attr_remove_station(int data_id,
228  const db::AttrList& attrs) = 0;
229 
240  virtual void attr_remove_data(int data_id, const db::AttrList& attrs) = 0;
241 
259  virtual void update_repinfo(const char* repinfo_file, int* added,
260  int* deleted, int* updated) = 0;
261 
265  virtual void dump(FILE* out) = 0;
266 };
267 
268 class DB : public dballe::DB
269 {
270 public:
271  static db::Format get_default_format();
272  static void set_default_format(db::Format format);
273 
280  static std::shared_ptr<DB> connect_from_file(const char* pathname);
281 
285  static std::shared_ptr<DB> connect_memory();
286 
290  static std::shared_ptr<DB> create(std::shared_ptr<sql::Connection> conn);
291 
300  static bool is_url(const char* str);
301 
303  virtual db::Format format() const = 0;
304 
311  virtual void disappear() = 0;
312 
324  virtual void reset(const char* repinfo_file = 0) = 0;
325 
330  virtual std::shared_ptr<dballe::db::Transaction>
331  test_transaction(bool readonly = false) = 0;
332 
343  virtual void vacuum() = 0;
344 
354  virtual void attr_query_station(
355  int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest);
356 
366  virtual void
367  attr_query_data(int data_id,
368  std::function<void(std::unique_ptr<wreport::Var>)>&& dest);
369 
379  void attr_insert_station(int data_id, const Values& attrs);
380 
390  void attr_insert_data(int data_id, const Values& attrs);
391 
402  void attr_remove_station(int data_id, const db::AttrList& attrs);
403 
414  void attr_remove_data(int data_id, const db::AttrList& attrs);
415 
419  void dump(FILE* out);
420 
422  virtual void print_info(FILE* out);
423 
425  static const char* default_repinfo_file();
426 
428  inline static std::unique_ptr<db::DB>
429  downcast(std::unique_ptr<dballe::DB> db)
430  {
431  db::DB* res = dynamic_cast<db::DB*>(db.get());
432  if (!res)
433  throw std::runtime_error(
434  "Attempted to downcast the wrong kind of DB");
435  db.release();
436  return std::unique_ptr<db::DB>(res);
437  }
438 
440  inline static std::shared_ptr<db::DB>
441  downcast(std::shared_ptr<dballe::DB> db)
442  {
443  auto res = std::dynamic_pointer_cast<db::DB>(db);
444  if (!res)
445  throw std::runtime_error(
446  "Attempted to downcast the wrong kind of DB");
447  return res;
448  }
449 };
450 
451 } // namespace db
452 } // namespace dballe
453 #endif
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
virtual void attr_query_station(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> dest)=0
Query attributes on a station value.
Forward declarations for public dballe/sql names.
virtual void insert_attrs(const Values &attrs)
Insert/update attributes for the current variable.
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
Options controlling how messages are imported in the database.
Definition: db.h:64
virtual void attr_query_station(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)
Query attributes on a station value.
Definition: db/db.h:142
Definition: db.h:149
virtual void attr_insert_station(int data_id, const Values &attrs)=0
Insert new attributes on a station value.
virtual void disappear()=0
Remove all our traces from the database, if applicable.
void attr_insert_data(int data_id, const Values &attrs)
Insert new attributes on a data value.
Definition: db/db.h:72
virtual void insert_attrs(const Values &attrs)
Insert/update attributes for the current variable.
virtual void remove_attrs(const db::AttrList &attrs)
Remove attributes for the current variable.
static std::shared_ptr< DB > connect_from_file(const char *pathname)
Create from a SQLite file pathname.
void attr_remove_station(int data_id, const db::AttrList &attrs)
Delete attributes from a station value.
virtual void query_attrs(std::function< void(std::unique_ptr< wreport::Var >)> dest, bool force_read=false)=0
Query/return the attributes for the current value of this cursor.
DBInsertOptions with public constructor and copy, safe to use in dballe code but not accessible from ...
Definition: db/db.h:40
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
static const char * default_repinfo_file()
Return the default repinfo file pathname.
static std::shared_ptr< DB > create(std::shared_ptr< sql::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.
Cursor iterating over station data values.
Definition: core/cursor.h:32
void attr_insert_station(int data_id, const Values &attrs)
Insert new attributes on a station value.
virtual int attr_reference_id() const =0
Return an integer value that can be used to refer to the current variable for attribute access...
Definition: cmdline.h:18
static std::shared_ptr< db::DB > downcast(std::shared_ptr< dballe::DB > db)
Downcast a shared_ptr pointer.
Definition: db/db.h:441
static bool is_url(const char *str)
Return TRUE if the string looks like a DB URL.
void attr_remove_data(int data_id, const db::AttrList &attrs)
Delete attributes from a data value.
virtual std::shared_ptr< db::Transaction > get_transaction() const =0
Get the database that created this cursor.
Options controlling how values are inserted in the database.
Definition: db.h:122
Definition: db/db.h:268
virtual void dump(FILE *out)=0
Dump the entire contents of the database to an output stream.
virtual std::shared_ptr< dballe::db::Transaction > test_transaction(bool readonly=false)=0
Same as transaction(), but the resulting transaction will throw an exception if commit is called...
Definition: db/db.h:59
void dump(FILE *out)
Dump the entire contents of the database to an output stream.
Definition: db/db.h:107
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
virtual void attr_query_data(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)
Query attributes on a data value.
virtual std::shared_ptr< db::Transaction > get_transaction() const =0
Get the database that created this cursor.
virtual void remove_attrs(const db::AttrList &attrs)
Remove attributes for the current variable.
virtual void query_attrs(std::function< void(std::unique_ptr< wreport::Var >)> dest, bool force_read=false)=0
Query/return the attributes for the current value of this cursor.
Definition: db/db.h:155
static std::unique_ptr< db::DB > downcast(std::unique_ptr< dballe::DB > db)
Downcast a unique_ptr pointer.
Definition: db/db.h:429
Definition: db.h:307
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 stations.
Definition: core/cursor.h:12
DBImportOptions with public constructor and copy, safe to use in dballe code but not accessible from ...
Definition: db/db.h:29
Cursor iterating over data values.
Definition: core/cursor.h:52
Cursor iterating over summary entries.
Definition: core/cursor.h:72
virtual void clear_cached_state()=0
Clear state information cached during the transaction.
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 &attrs)=0
Delete attributes from a data value.
virtual db::Format format() const =0
Return the format of this DB.
static std::shared_ptr< DB > connect_memory()
Create an in-memory database.
virtual void attr_remove_station(int data_id, const db::AttrList &attrs)=0
Delete attributes from a station value.
virtual void vacuum()=0
Perform database cleanup operations.
virtual void print_info(FILE *out)
Print informations about the database to the given output stream.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:215
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 attr_insert_data(int data_id, const Values &attrs)=0
Insert new attributes on a data value.