libdballe 9.6
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/fwd.h>
5#include <dballe/core/cursor.h>
6#include <dballe/db.h>
7#include <dballe/db/fwd.h>
8#include <dballe/db/defs.h>
9#include <dballe/msg/fwd.h>
10#include <dballe/sql/fwd.h>
11#include <wreport/varinfo.h>
12#include <wreport/var.h>
13#include <string>
14#include <memory>
15#include <functional>
16
23namespace dballe {
24
25namespace impl {
26
29{
30 DBImportOptions() = default;
31 DBImportOptions(const DBImportOptions& o) = default;
32 DBImportOptions(DBImportOptions&& o) = default;
33 DBImportOptions& operator=(const DBImportOptions&) = default;
34 DBImportOptions& operator=(DBImportOptions&&) = default;
35};
36
39{
40 DBInsertOptions() = default;
41 DBInsertOptions(const DBInsertOptions& o) = default;
42 DBInsertOptions(DBInsertOptions&& o) = default;
43 DBInsertOptions& operator=(const DBInsertOptions&) = default;
44 DBInsertOptions& operator=(DBInsertOptions&&) = default;
45};
46
47}
48
49namespace db {
50
52std::string format_format(Format format);
53
55Format format_parse(const std::string& str);
56
57
59{
61 virtual void remove() = 0;
62
68 virtual unsigned test_iterate(FILE* dump=0) = 0;
69};
70
72{
74 virtual void remove() = 0;
75
77 virtual std::shared_ptr<db::Transaction> get_transaction() const = 0;
78
83 virtual int attr_reference_id() const = 0;
84
88 virtual void query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest, bool force_read=false) = 0;
89
91 virtual void insert_attrs(const Values& attrs);
92
94 virtual void remove_attrs(const db::AttrList& attrs);
95
101 virtual unsigned test_iterate(FILE* dump=0) = 0;
102};
103
105{
107 virtual void remove() = 0;
108
110 virtual std::shared_ptr<db::Transaction> get_transaction() const = 0;
111
116 virtual int attr_reference_id() const = 0;
117
121 virtual void query_attrs(std::function<void(std::unique_ptr<wreport::Var>)> dest, bool force_read=false) = 0;
122
124 virtual void insert_attrs(const Values& attrs);
125
127 virtual void remove_attrs(const db::AttrList& attrs);
128
134 virtual unsigned test_iterate(FILE* dump=0) = 0;
135};
136
138{
140 virtual void remove() = 0;
141
147 virtual unsigned test_iterate(FILE* dump=0) = 0;
148};
149
150
152{
153public:
154 virtual ~Transaction() {}
155
163 virtual void clear_cached_state() = 0;
164
173 virtual void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)> dest) = 0;
174
183 virtual void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)> dest) = 0;
184
193 virtual void attr_insert_station(int data_id, const Values& attrs) = 0;
194
203 virtual void attr_insert_data(int data_id, const Values& attrs) = 0;
204
214 virtual void attr_remove_station(int data_id, const db::AttrList& attrs) = 0;
215
225 virtual void attr_remove_data(int data_id, const db::AttrList& attrs) = 0;
226
244 virtual void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated) = 0;
245
249 virtual void dump(FILE* out) = 0;
250};
251
252class DB: public dballe::DB
253{
254public:
255 static db::Format get_default_format();
256 static void set_default_format(db::Format format);
257
264 static std::shared_ptr<DB> connect_from_file(const char* pathname);
265
269 static std::shared_ptr<DB> connect_memory();
270
274 static std::shared_ptr<DB> create(std::shared_ptr<sql::Connection> conn);
275
284 static bool is_url(const char* str);
285
287 virtual db::Format format() const = 0;
288
295 virtual void disappear() = 0;
296
308 virtual void reset(const char* repinfo_file=0) = 0;
309
314 virtual std::shared_ptr<dballe::db::Transaction> test_transaction(bool readonly=false) = 0;
315
325 virtual void vacuum() = 0;
326
335 virtual void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest);
336
345 virtual void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest);
346
355 void attr_insert_station(int data_id, const Values& attrs);
356
365 void attr_insert_data(int data_id, const Values& attrs);
366
376 void attr_remove_station(int data_id, const db::AttrList& attrs);
377
387 void attr_remove_data(int data_id, const db::AttrList& attrs);
388
392 void dump(FILE* out);
393
395 virtual void print_info(FILE* out);
396
398 static const char* default_repinfo_file();
399
401 inline static std::unique_ptr<db::DB> downcast(std::unique_ptr<dballe::DB> db)
402 {
403 db::DB* res = dynamic_cast<db::DB*>(db.get());
404 if (!res) throw std::runtime_error("Attempted to downcast the wrong kind of DB");
405 db.release();
406 return std::unique_ptr<db::DB>(res);
407 }
408
410 inline static std::shared_ptr<db::DB> downcast(std::shared_ptr<dballe::DB> db)
411 {
412 auto res = std::dynamic_pointer_cast<db::DB>(db);
413 if (!res) throw std::runtime_error("Attempted to downcast the wrong kind of DB");
414 return res;
415 }
416};
417
418}
419}
420#endif
Options controlling how messages are imported in the database.
Definition: db.h:65
Options controlling how values are inserted in the database.
Definition: db.h:123
Definition: db.h:297
Definition: db.h:150
Definition: db/db.h:253
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.
virtual void disappear()=0
Remove all our traces from the database, if applicable.
static std::unique_ptr< db::DB > downcast(std::unique_ptr< dballe::DB > db)
Downcast a unique_ptr pointer.
Definition: db/db.h:401
virtual void print_info(FILE *out)
Print informations about the database to the given output stream.
static bool is_url(const char *str)
Return TRUE if the string looks like a DB URL.
virtual void attr_query_data(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)
Query attributes on a data value.
static std::shared_ptr< DB > connect_from_file(const char *pathname)
Create from a SQLite file pathname.
static std::shared_ptr< DB > create(std::shared_ptr< sql::Connection > conn)
Create a database from an open Connection.
static std::shared_ptr< DB > connect_memory()
Create an in-memory database.
void attr_remove_data(int data_id, const db::AttrList &attrs)
Delete attributes from a data value.
virtual void reset(const char *repinfo_file=0)=0
Reset the database, removing all existing Db-All.e tables and re-creating them empty.
static std::shared_ptr< db::DB > downcast(std::shared_ptr< dballe::DB > db)
Downcast a shared_ptr pointer.
Definition: db/db.h:410
void attr_insert_data(int data_id, const Values &attrs)
Insert new attributes on a data value.
void attr_remove_station(int data_id, const db::AttrList &attrs)
Delete attributes from a station value.
static const char * default_repinfo_file()
Return the default repinfo file pathname.
void attr_insert_station(int data_id, const Values &attrs)
Insert new attributes on a station value.
virtual void attr_query_station(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest)
Query attributes on a station value.
void dump(FILE *out)
Dump the entire contents of the database to an output stream.
virtual void vacuum()=0
Perform database cleanup operations.
virtual db::Format format() const =0
Return the format of this DB.
Definition: db/db.h:152
virtual void attr_remove_station(int data_id, const db::AttrList &attrs)=0
Delete attributes from a station value.
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 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 clear_cached_state()=0
Clear state information cached during the transaction.
virtual void dump(FILE *out)=0
Dump the entire contents of the database to an output stream.
virtual void attr_remove_data(int data_id, const db::AttrList &attrs)=0
Delete attributes from a data value.
virtual void attr_insert_data(int data_id, const Values &attrs)=0
Insert new attributes on a data value.
virtual void attr_insert_station(int data_id, const Values &attrs)=0
Insert new attributes on a station value.
virtual void attr_query_data(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> dest)=0
Query attributes on a data value.
std::string format_format(Format format)
Format a db::Format value to a string.
Format format_parse(const std::string &str)
Parse a formatted db::Format value.
Forward declarations for public dballe/sql names.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:177
Definition: db/db.h:105
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.
virtual std::shared_ptr< db::Transaction > get_transaction() const =0
Get the database that created this cursor.
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
virtual void remove_attrs(const db::AttrList &attrs)
Remove attributes for the current variable.
virtual void remove()=0
Remove this datum.
virtual void insert_attrs(const Values &attrs)
Insert/update attributes for the current variable.
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: db/db.h:72
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.
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
virtual std::shared_ptr< db::Transaction > get_transaction() const =0
Get the database that created this cursor.
virtual void remove()=0
Remove this datum.
virtual int attr_reference_id() const =0
Return an integer value that can be used to refer to the current variable for attribute access.
virtual void remove_attrs(const db::AttrList &attrs)
Remove attributes for the current variable.
virtual void insert_attrs(const Values &attrs)
Insert/update attributes for the current variable.
Definition: db/db.h:59
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
virtual void remove()=0
Remove this station and all its data.
Definition: db/db.h:138
virtual void remove()=0
Remove all data summarised by this entry.
virtual unsigned test_iterate(FILE *dump=0)=0
Iterate the cursor until the end, returning the number of items.
Cursor iterating over data values.
Definition: core/cursor.h:49
Cursor iterating over station data values.
Definition: core/cursor.h:31
Cursor iterating over stations.
Definition: core/cursor.h:13
Cursor iterating over summary entries.
Definition: core/cursor.h:67
DBImportOptions with public constructor and copy, safe to use in dballe code but not accessible from ...
Definition: db/db.h:29
DBInsertOptions with public constructor and copy, safe to use in dballe code but not accessible from ...
Definition: db/db.h:39