libdballe  7.19
data.h
1 #ifndef DBALLE_DB_V7_DATAV7_H
2 #define DBALLE_DB_V7_DATAV7_H
3 
4 #include <dballe/core/defs.h>
5 #include <dballe/sql/fwd.h>
6 #include <dballe/db/defs.h>
7 #include <dballe/db/v7/state.h>
8 #include <wreport/var.h>
9 #include <memory>
10 #include <vector>
11 #include <list>
12 #include <cstdio>
13 
14 namespace dballe {
15 struct Record;
16 struct Values;
17 
18 namespace db {
19 namespace v7 {
20 struct Transaction;
21 struct IdQueryBuilder;
22 
23 namespace bulk {
24 struct InsertStationVars;
25 struct InsertVars;
26 
27 enum UpdateMode {
28  UPDATE,
29  IGNORE,
30  ERROR,
31 };
32 
33 }
34 
35 template<typename Traits>
37 {
38 protected:
42  void read_attrs_into_values(int id_data, Values& values);
43 
47  virtual void write_attrs(int id_data, const Values& values) = 0;
48 
52  virtual void remove_all_attrs(int id_data) = 0;
53 
54 public:
55  virtual ~DataCommon() {}
56 
66  virtual void read_attrs(int id_data, std::function<void(std::unique_ptr<wreport::Var>)> dest) = 0;
67 
76  void merge_attrs(int id_data, const Values& attrs);
77 
81  void remove_attrs(int data_id, const db::AttrList& attrs);
82 
84  virtual void insert(dballe::db::v7::Transaction& t, typename Traits::BulkVars& vars, bulk::UpdateMode update_mode=bulk::UPDATE, bool with_attrs=false) = 0;
85 
87  virtual void remove(const v7::IdQueryBuilder& qb) = 0;
88 
90  virtual void dump(FILE* out) = 0;
91 };
92 
93 
95 {
96  unsigned count = 0;
97  FILE* out;
98 
99  StationDataDumper(FILE* out);
100 
101  void print_head();
102  void print_row(int id, int id_station, wreport::Varcode code, const char* val, const std::vector<uint8_t>& attrs);
103  void print_tail();
104 };
105 
107 {
108  unsigned count = 0;
109  FILE* out;
110 
111  DataDumper(FILE* out);
112 
113  void print_head();
114  void print_row(int id, int id_station, int id_levtr, const Datetime& dt, wreport::Varcode code, const char* val, const std::vector<uint8_t>& attrs);
115  void print_tail();
116 };
117 
118 namespace bulk {
119 
120 struct Item
121 {
122  static const unsigned FLAG_NEEDS_UPDATE = 1 << 0;
123  static const unsigned FLAG_UPDATED = 1 << 1;
124  static const unsigned FLAG_NEEDS_INSERT = 1 << 2;
125  static const unsigned FLAG_INSERTED = 1 << 3;
126  unsigned flags = 0;
127 
128  bool needs_update() const { return flags & FLAG_NEEDS_UPDATE; }
129  bool updated() const { return flags & FLAG_UPDATED; }
130  bool needs_insert() const { return flags & FLAG_NEEDS_INSERT; }
131  bool inserted() const { return flags & FLAG_INSERTED; }
132  void set_needs_update() { flags |= FLAG_NEEDS_UPDATE; }
133  void set_updated() { flags = (flags & ~FLAG_NEEDS_UPDATE) | FLAG_UPDATED; }
134  void set_needs_insert() { flags |= FLAG_NEEDS_INSERT; }
135  void set_inserted() { flags = (flags & ~FLAG_NEEDS_INSERT) | FLAG_INSERTED; }
136 
142  void format_flags(char* dest) const;
143 };
144 
145 template<typename state_t>
146 struct VarItem : public Item
147 {
148  typename state_t::iterator cur;
149  const wreport::Var* var;
150 
151  VarItem(typename state_t::iterator cur, const wreport::Var* var)
152  : cur(cur), var(var) {}
153 };
154 
158 struct StationVar : public VarItem<stationvalues_t>
159 {
160  using VarItem::VarItem;
161 
162  bool is_new() const { return false; }
163  bool has_cur(State& state) const { return cur != state.stationvalues.end(); }
164  void fill_cur(State& state, const StationValueDesc& desc) { cur = state.stationvalues.find(desc); }
165  void dump(FILE* out) const;
166 };
167 
168 
172 struct Var : public VarItem<values_t>
173 {
174  LevTrState levtr;
175 
176  Var(values_t::iterator cur, const wreport::Var* var, const LevTrState& levtr)
177  : VarItem(cur, var), levtr(levtr) {}
178 
179  bool is_new() const { return levtr.is_new; }
180  bool has_cur(State& state) const { return cur != state.values.end(); }
181  void fill_cur(State& state, const ValueDesc& desc) { cur = state.values.find(desc); }
182  void dump(FILE* out) const;
183 };
184 
186 {
187  stations_t::iterator station;
188 
189  SharedContext() {}
190  SharedContext(stations_t::iterator station) : station(station) {}
191 
192  bool is_new() const { return station->second.is_new; }
193 };
194 
196 {
197  using SharedContext::SharedContext;
198 
199  StationValueDesc make_desc(StationVar& v) const
200  {
201  return StationValueDesc(station, v.var->code());
202  }
203 };
204 
206 {
207  Datetime datetime;
208 
209  SharedDataContext() {}
210  SharedDataContext(stations_t::iterator station) : SharedContext(station) {}
211  SharedDataContext(stations_t::iterator station, const Datetime& datetime) : SharedContext(station), datetime(datetime) {}
212 
213  ValueDesc make_desc(Var& v) const
214  {
215  return ValueDesc(station, v.levtr.id, datetime, v.var->code());
216  }
217 };
218 
219 template<typename var_t, typename shared_context_t>
220 struct InsertPlan : public std::vector<var_t>
221 {
222  typedef typename std::vector<var_t>::iterator iterator;
223 
224  State& state;
225  shared_context_t shared_context;
226 
227  bool do_insert = false;
228  bool do_update = false;
229  std::list<var_t*> to_query;
230 
231  template<typename... Args>
232  InsertPlan(State& state, Args&&... args) : state(state), shared_context(std::forward<Args>(args)...) {}
233 
240  {
241  to_query.clear();
242  for (auto i = this->begin(); i != this->end(); ++i)
243  {
244  i->fill_cur(state, shared_context.make_desc(*i));
245  if (i->has_cur(state)) continue;
246  if (shared_context.is_new() || i->is_new()) continue;
247  to_query.push_back(&*i);
248  }
249  }
250 
251  void compute_plan()
252  {
253  do_insert = false;
254  do_update = false;
255  for (auto& var: *this)
256  {
257  if (!var.has_cur(state))
258  {
259  var.set_needs_insert();
260  do_insert = true;
261  }
262  else
263  {
264  var.set_needs_update();
265  do_update = true;
266  }
267  }
268  }
269 };
270 
275 struct InsertStationVars : public InsertPlan<StationVar, SharedStationContext>
276 {
277  using InsertPlan::InsertPlan;
278 
279  StationValueDesc make_desc(iterator& i) const
280  {
281  return StationValueDesc(shared_context.station, i->var->code());
282  }
283 
284  void add(const wreport::Var* var)
285  {
286  emplace_back(state.stationvalues.end(), var);
287  }
288 
289  void dump(FILE* out) const;
290 };
291 
292 
297 struct InsertVars : public InsertPlan<Var, SharedDataContext>
298 {
299  using InsertPlan::InsertPlan;
300 
301  bool has_datetime() const
302  {
303  return not shared_context.datetime.is_missing();
304  }
305 
306  void set_datetime(const Datetime& dt)
307  {
308  shared_context.datetime = dt;
309  }
310 
311  void add(const wreport::Var* var, const LevTrState& levtr)
312  {
313  emplace_back(state.values.end(), var, levtr);
314  }
315 
316  void dump(FILE* out) const;
317 };
318 
319 }
320 
322 {
324  static const char* table_name;
325 };
326 
328 {
329  typedef bulk::InsertVars BulkVars;
330  static const char* table_name;
331 };
332 
333 extern template class DataCommon<StationDataTraits>;
334 extern template class DataCommon<DataTraits>;
335 
338 
339 }
340 }
341 }
342 #endif
Definition: data.h:106
Definition: v7/qbuilder.h:115
Cache intermediate results during a database transaction, to avoid hitting the database multiple time...
Definition: state.h:140
Definition: data.h:36
Definition: data.h:146
Forward declarations for public dballe/sql names.
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: state.h:86
Definition: data.h:120
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: data.h:275
uint16_t Varcode
Definition: data.h:220
Definition: db/v7/transaction.h:12
void map_known_values()
Fill the cur state pointer in all variables to insert.
Definition: data.h:239
Definition: state.h:77
Definition: data.h:321
Workflow information about a variable listed for bulk insert/update.
Definition: data.h:172
Varcode code() const
Date and time.
Definition: types.h:158
Definition: data.h:327
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: data.h:297
Definition: state.h:109
Common definitions.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:202
Workflow information about a variable listed for bulk insert/update.
Definition: data.h:158