libdballe  7.29
datav6.h
Go to the documentation of this file.
1 #ifndef DBALLE_DB_V6_DATAV6_H
2 #define DBALLE_DB_V6_DATAV6_H
3 
10 #include <dballe/core/defs.h>
11 #include <dballe/sql/fwd.h>
12 #include <wreport/var.h>
13 #include <memory>
14 #include <vector>
15 #include <cstdio>
16 
17 namespace dballe {
18 struct Record;
19 
20 namespace db {
21 namespace v6 {
22 struct QueryBuilder;
23 
24 namespace bulk {
25 struct InsertV6;
26 }
27 
31 struct DataV6
32 {
33 public:
34  enum UpdateMode {
35  UPDATE,
36  IGNORE,
37  ERROR,
38  };
39 
40  virtual ~DataV6();
41 
43  virtual void insert(dballe::sql::Transaction& t, bulk::InsertV6& vars, UpdateMode update_mode=UPDATE) = 0;
44 
46  virtual void remove(const v6::QueryBuilder& qb) = 0;
47 
49  virtual void dump(FILE* out) = 0;
50 };
51 
52 
53 namespace bulk {
54 
55 struct Item
56 {
57  static const unsigned FLAG_NEEDS_UPDATE = 1 << 0;
58  static const unsigned FLAG_UPDATED = 1 << 1;
59  static const unsigned FLAG_NEEDS_INSERT = 1 << 2;
60  static const unsigned FLAG_INSERTED = 1 << 3;
61  unsigned flags = 0;
62 
63  bool needs_update() const { return flags & FLAG_NEEDS_UPDATE; }
64  bool updated() const { return flags & FLAG_UPDATED; }
65  bool needs_insert() const { return flags & FLAG_NEEDS_INSERT; }
66  bool inserted() const { return flags & FLAG_INSERTED; }
67  void set_needs_update() { flags |= FLAG_NEEDS_UPDATE; }
68  void set_updated() { flags = (flags & ~FLAG_NEEDS_UPDATE) | FLAG_UPDATED; }
69  void set_needs_insert() { flags |= FLAG_NEEDS_INSERT; }
70  void set_inserted() { flags = (flags & ~FLAG_NEEDS_INSERT) | FLAG_INSERTED; }
71 
77  void format_flags(char* dest) const;
78 };
79 
83 struct VarV6 : public Item
84 {
85  int id_levtr;
86  int id_data;
87  const wreport::Var* var;
88 
89  VarV6(const wreport::Var* var, int id_levtr=-1, int id_data=-1)
90  : id_levtr(id_levtr), id_data(id_data), var(var)
91  {
92  }
93  bool operator<(const VarV6& v) const
94  {
95  if (int d = id_levtr - v.id_levtr) return d < 0;
96  return var->code() < v.var->code();
97  }
98 
99  void dump(FILE* out) const;
100 };
101 
102 
107 struct InsertV6 : public std::vector<VarV6>
108 {
109  int id_station;
110  int id_report;
111  Datetime datetime;
112 
113  void add(const wreport::Var* var, int id_levtr)
114  {
115  emplace_back(var, id_levtr);
116  }
117 
118  void dump(FILE* out) const;
119 };
120 
126 {
127  InsertV6& vars;
128  InsertV6::iterator iter;
129  bool do_insert = false;
130  bool do_update = false;
131 
132  AnnotateVarsV6(InsertV6& vars);
133 
134  bool annotate(int id_data, int id_levtr, wreport::Varcode code, const char* value);
135  void annotate_end();
136 
137  void dump(FILE* out) const;
138 };
139 
140 }
141 
142 
143 
144 }
145 }
146 }
147 
148 #endif
149 
Forward declarations for public dballe/sql names.
Helper class for annotating InsertV6 variables with the current status of the database.
Definition: datav6.h:125
Workflow information about a variable listed for bulk insert/update.
Definition: datav6.h:83
uint16_t Varcode
virtual void dump(FILE *out)=0
Dump the entire contents of the table to an output stream.
A RAII transaction interface.
Definition: sql.h:133
Varcode code() const
Date and time.
Definition: types.h:158
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: datav6.h:107
void format_flags(char *dest) const
Format flags in the first 4 characters of dest.
Build SQL queries for V6 databases.
Definition: v6/qbuilder.h:14
Common definitions.
Definition: datav6.h:55
Precompiled query to manipulate the data table.
Definition: datav6.h:31
virtual void insert(dballe::sql::Transaction &t, bulk::InsertV6 &vars, UpdateMode update_mode=UPDATE)=0
Bulk variable insert.