libdballe  7.7
sql/datav6.h
Go to the documentation of this file.
1 /*
2  * db/sql/datav6 - interface to the V6 data table
3  *
4  * Copyright (C) 2005--2015 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBALLE_DB_SQL_DATAV6_H
23 #define DBALLE_DB_SQL_DATAV6_H
24 
31 #include <dballe/core/defs.h>
32 #include <wreport/var.h>
33 #include <memory>
34 #include <vector>
35 #include <cstdio>
36 
37 namespace dballe {
38 struct Record;
39 
40 namespace db {
41 struct Connection;
42 struct Transaction;
43 
44 namespace v6 {
45 struct QueryBuilder;
46 }
47 
48 namespace sql {
49 
50 namespace bulk {
51 struct InsertV6;
52 }
53 
57 struct DataV6
58 {
59 public:
60  enum UpdateMode {
61  UPDATE,
62  IGNORE,
63  ERROR,
64  };
65 
66  virtual ~DataV6();
67 
69  virtual void insert(Transaction& t, bulk::InsertV6& vars, UpdateMode update_mode=UPDATE) = 0;
70 
72  virtual void remove(const v6::QueryBuilder& qb) = 0;
73 
75  virtual void dump(FILE* out) = 0;
76 };
77 
78 
79 namespace bulk {
80 
81 struct Item
82 {
83  static const unsigned FLAG_NEEDS_UPDATE = 1 << 0;
84  static const unsigned FLAG_UPDATED = 1 << 1;
85  static const unsigned FLAG_NEEDS_INSERT = 1 << 2;
86  static const unsigned FLAG_INSERTED = 1 << 3;
87  unsigned flags = 0;
88 
89  bool needs_update() const { return flags & FLAG_NEEDS_UPDATE; }
90  bool updated() const { return flags & FLAG_UPDATED; }
91  bool needs_insert() const { return flags & FLAG_NEEDS_INSERT; }
92  bool inserted() const { return flags & FLAG_INSERTED; }
93  void set_needs_update() { flags |= FLAG_NEEDS_UPDATE; }
94  void set_updated() { flags = (flags & ~FLAG_NEEDS_UPDATE) | FLAG_UPDATED; }
95  void set_needs_insert() { flags |= FLAG_NEEDS_INSERT; }
96  void set_inserted() { flags = (flags & ~FLAG_NEEDS_INSERT) | FLAG_INSERTED; }
97 
103  void format_flags(char* dest) const;
104 };
105 
109 struct VarV6 : public Item
110 {
111  int id_levtr;
112  int id_data;
113  const wreport::Var* var;
114 
115  VarV6(const wreport::Var* var, int id_levtr=-1, int id_data=-1)
116  : id_levtr(id_levtr), id_data(id_data), var(var)
117  {
118  }
119  bool operator<(const VarV6& v) const
120  {
121  if (int d = id_levtr - v.id_levtr) return d < 0;
122  return var->code() < v.var->code();
123  }
124 
125  void dump(FILE* out) const;
126 };
127 
128 
133 struct InsertV6 : public std::vector<VarV6>
134 {
135  int id_station;
136  int id_report;
137  Datetime datetime;
138 
139  void add(const wreport::Var* var, int id_levtr)
140  {
141  emplace_back(var, id_levtr);
142  }
143 
144  void dump(FILE* out) const;
145 };
146 
152 {
153  InsertV6& vars;
154  InsertV6::iterator iter;
155  bool do_insert = false;
156  bool do_update = false;
157 
158  AnnotateVarsV6(InsertV6& vars);
159 
160  bool annotate(int id_data, int id_levtr, wreport::Varcode code, const char* value);
161  void annotate_end();
162 
163  void dump(FILE* out) const;
164 };
165 
166 }
167 
168 
169 
170 }
171 }
172 }
173 
174 #endif
175 
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Helper class for annotating InsertV6 variables with the current status of the database.
Definition: sql/datav6.h:151
Definition: sql/datav6.h:81
A RAII transaction interface.
Definition: sql.h:134
Input for a bulk insert of a lot of variables sharing the same context information.
Definition: sql/datav6.h:133
Workflow information about a variable listed for bulk insert/update.
Definition: sql/datav6.h:109
Date and time.
Definition: types.h:147
Build SQL queries for V6 databases.
Definition: qbuilder.h:15
Common definitions.
Precompiled query to manipulate the data table.
Definition: sql/datav6.h:57