1 #ifndef DBALLE_DB_V7_DATAV7_H
2 #define DBALLE_DB_V7_DATAV7_H
6 #include <dballe/db/defs.h>
7 #include <dballe/db/v7/state.h>
8 #include <wreport/var.h>
22 struct IdQueryBuilder;
25 struct InsertStationVars;
36 template<
typename Traits>
67 virtual void read_attrs(
int id_data, std::function<
void(std::unique_ptr<wreport::Var>)> dest) = 0;
82 void remove_attrs(
int data_id,
const db::AttrList& attrs);
91 virtual void dump(FILE* out) = 0;
103 void print_row(
int id,
int id_station,
wreport::Varcode code,
const char* val,
const std::vector<uint8_t>& attrs);
115 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);
123 static const unsigned FLAG_NEEDS_UPDATE = 1 << 0;
124 static const unsigned FLAG_UPDATED = 1 << 1;
125 static const unsigned FLAG_NEEDS_INSERT = 1 << 2;
126 static const unsigned FLAG_INSERTED = 1 << 3;
129 bool needs_update()
const {
return flags & FLAG_NEEDS_UPDATE; }
130 bool updated()
const {
return flags & FLAG_UPDATED; }
131 bool needs_insert()
const {
return flags & FLAG_NEEDS_INSERT; }
132 bool inserted()
const {
return flags & FLAG_INSERTED; }
133 void set_needs_update() { flags |= FLAG_NEEDS_UPDATE; }
134 void set_updated() { flags = (flags & ~FLAG_NEEDS_UPDATE) | FLAG_UPDATED; }
135 void set_needs_insert() { flags |= FLAG_NEEDS_INSERT; }
136 void set_inserted() { flags = (flags & ~FLAG_NEEDS_INSERT) | FLAG_INSERTED; }
146 template<
typename state_t>
149 typename state_t::iterator cur;
153 : cur(cur), var(var) {}
161 using VarItem::VarItem;
163 bool is_new()
const {
return false; }
164 bool has_cur(
State& state)
const {
return cur != state.stationvalues.end(); }
166 void dump(FILE* out)
const;
178 :
VarItem(cur, var), levtr(levtr) {}
180 bool is_new()
const {
return levtr.is_new; }
181 bool has_cur(
State& state)
const {
return cur != state.values.end(); }
182 void fill_cur(
State& state,
const ValueDesc& desc) { cur = state.values.find(desc); }
183 void dump(FILE* out)
const;
188 stations_t::iterator station;
191 SharedContext(stations_t::iterator station) : station(station) {}
193 bool is_new()
const {
return station->second.is_new; }
198 using SharedContext::SharedContext;
216 return ValueDesc(station, v.levtr.id, datetime, v.var->
code());
220 template<
typename var_t,
typename shared_context_t>
223 typedef typename std::vector<var_t>::iterator iterator;
226 shared_context_t shared_context;
228 bool do_insert =
false;
229 bool do_update =
false;
230 std::list<var_t*> to_query;
232 template<
typename... Args>
233 InsertPlan(
State& state, Args&&... args) : state(state), shared_context(std::forward<Args>(args)...) {}
243 for (
auto i = this->begin(); i != this->end(); ++i)
245 i->fill_cur(state, shared_context.make_desc(*i));
246 if (i->has_cur(state))
continue;
247 if (shared_context.is_new() || i->is_new())
continue;
248 to_query.push_back(&*i);
256 for (
auto& var: *
this)
258 if (!var.has_cur(state))
260 var.set_needs_insert();
265 var.set_needs_update();
278 using InsertPlan::InsertPlan;
287 emplace_back(state.stationvalues.end(), var);
290 void dump(FILE* out)
const;
298 struct InsertVars :
public InsertPlan<Var, SharedDataContext>
300 using InsertPlan::InsertPlan;
302 bool has_datetime()
const
304 return not shared_context.datetime.
is_missing();
307 void set_datetime(
const Datetime& dt)
309 shared_context.datetime = dt;
314 emplace_back(state.values.end(), var, levtr);
317 void dump(FILE* out)
const;
325 static const char* table_name;
331 static const char* table_name;
void format_flags(char *dest) const
Format flags in the first 4 characters of dest.
virtual void dump(FILE *out)=0
Dump the entire contents of the table to an output stream.
Definition: v7/qbuilder.h:115
bool is_missing() const
Check if this datetime is the missing value.
Cache intermediate results during a database transaction, to avoid hitting the database multiple time...
Definition: state.h:140
void read_attrs_into_values(int id_data, Values &values)
Load attributes from the database into a Values.
virtual void write_attrs(int id_data, const Values &values)=0
Replace the attributes of a variable with those in Values.
Forward declarations for public dballe/sql names.
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: data.h:276
Definition: db/v7/transaction.h:12
void map_known_values()
Fill the cur state pointer in all variables to insert.
Definition: data.h:240
virtual void insert(dballe::db::v7::Transaction &t, typename Traits::BulkVars &vars, bulk::UpdateMode update_mode=bulk::UPDATE, bool with_attrs=false)=0
Bulk variable insert.
Workflow information about a variable listed for bulk insert/update.
Definition: data.h:173
Date and time.
Definition: types.h:158
void merge_attrs(int id_data, const Values &attrs)
Merge the given attributes with the existing attributes of the given variable:
virtual void read_attrs(int id_data, std::function< void(std::unique_ptr< wreport::Var >)> dest)=0
Load from the database all the attributes for var.
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: data.h:298
void remove_attrs(int data_id, const db::AttrList &attrs)
Remove the given attributes from the given variable, if they exist.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:203
virtual void remove_all_attrs(int id_data)=0
Remove all attributes from a variable.
Workflow information about a variable listed for bulk insert/update.
Definition: data.h:159