libdballe  7.6
v6/db.h
Go to the documentation of this file.
1 /*
2  * dballe/v6/db - Archive for point-based meteorological data, db layout version 6
3  *
4  * Copyright (C) 2005--2014 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 DBA_DB_V6_H
23 #define DBA_DB_V6_H
24 
25 #include <dballe/db/db.h>
26 #include <dballe/db/trace.h>
27 #include <wreport/varinfo.h>
28 #include <string>
29 #include <vector>
30 #include <memory>
31 
42 #define DBA_DB_WANT_COORDS (1 << 0)
43 
44 #define DBA_DB_WANT_IDENT (1 << 1)
45 
46 #define DBA_DB_WANT_LEVEL (1 << 2)
47 
48 #define DBA_DB_WANT_TIMERANGE (1 << 3)
49 
50 #define DBA_DB_WANT_DATETIME (1 << 4)
51 
52 #define DBA_DB_WANT_VAR_NAME (1 << 5)
53 
54 #define DBA_DB_WANT_VAR_VALUE (1 << 6)
55 
56 #define DBA_DB_WANT_REPCOD (1 << 7)
57 
58 #define DBA_DB_WANT_ANA_ID (1 << 8)
59 
60 #define DBA_DB_WANT_CONTEXT_ID (1 << 9)
61 
62 namespace dballe {
63 struct Station;
64 struct StationValues;
65 struct DataValues;
66 
67 namespace db {
68 struct Connection;
69 struct Statement;
70 struct Sequence;
71 
72 namespace sql {
73 struct Driver;
74 struct Repinfo;
75 struct Station;
76 struct LevTr;
77 struct LevTrCache;
78 struct DataV6;
79 struct AttrV6;
80 }
81 
82 namespace v6 {
83 
87 class DB : public dballe::DB
88 {
89 public:
95  bool explain_queries = false;
96 
97 protected:
100 
122  void init_after_connect();
123 
124  DB(std::unique_ptr<Connection> conn);
125 
126  /*
127  * Lookup, insert or replace data in station taking the values from
128  * rec.
129  *
130  * If rec did not contain ana_id, it will be set by this function.
131  *
132  * @param rec
133  * The record with the station information
134  * @param can_add
135  * If true we can insert new stations in the database, if false we
136  * only look up existing records and raise an exception if missing
137  * @returns
138  * The station ID
139  */
140  int obtain_station(const Station& st, bool can_add=true);
141 
142 public:
143  virtual ~DB();
144 
145  db::Format format() const { return V6; }
146 
148  sql::Driver& driver();
149 
152 
155 
157  sql::LevTr& lev_tr();
158 
161 
163  sql::DataV6& data();
164 
166  sql::AttrV6& attr();
167 
168  void disappear();
169 
181  void reset(const char* repinfo_file = 0);
182 
186  void delete_tables();
187 
205  void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated);
206 
207  std::map<std::string, int> get_repinfo_priorities();
208 
212  int rep_cod_from_memo(const char* memo);
213 
214  void insert_station_data(StationValues& vals, bool can_replace, bool station_can_add) override;
215  void insert_data(DataValues& vals, bool can_replace, bool station_can_add) override;
216 
217  void remove_station_data(const Query& query) override;
218  void remove(const Query& query);
219  void remove_all();
220 
230  void vacuum();
231 
232  std::unique_ptr<db::CursorStation> query_stations(const Query& query);
233  std::unique_ptr<db::CursorStationData> query_station_data(const Query& query) override;
234  std::unique_ptr<db::CursorData> query_data(const Query& query);
235  std::unique_ptr<db::CursorSummary> query_summary(const Query& query);
236 
237  void attr_query_station(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) override;
238  void attr_query_data(int data_id, std::function<void(std::unique_ptr<wreport::Var>)>&& dest) override;
239  void attr_insert_station(int data_id, const Values& attrs) override;
240  void attr_insert_data(int data_id, const Values& attrs) override;
241  void attr_remove_station(int data_id, const db::AttrList& qcs) override;
242  void attr_remove_data(int data_id, const db::AttrList& qcs) override;
243  bool is_station_variable(int data_id, wreport::Varcode varcode) override;
244 
245  void import_msg(const Message& msg, const char* repmemo, int flags) override;
246  bool export_msgs(const Query& query, std::function<bool(std::unique_ptr<Message>&&)> dest) override;
247 
251  void dump(FILE* out);
252 
253  friend class dballe::DB;
254 };
255 
256 } // namespace v6
257 } // namespace db
258 } // namespace dballe
259 
260 /* vim:set ts=4 sw=4: */
261 #endif
void attr_query_station(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest) override
Query attributes on a station value.
std::unique_ptr< db::CursorSummary > query_summary(const Query &query)
Query a summary of what the result would be for a query.
sql::LevTrCache & lev_tr_cache()
Access the lev_tr cache.
sql::AttrV6 & attr()
Access the data table.
Definition: values.h:12
Definition: db/trace.h:39
void attr_remove_station(int data_id, const db::AttrList &qcs) override
Delete attributes from a station value.
Trace trace
Database query tracing.
Definition: v6/db.h:93
Definition: values.h:163
void insert_data(DataValues &vals, bool can_replace, bool station_can_add) override
Insert data values into the database.
std::unique_ptr< db::CursorData > query_data(const Query &query)
Query the database.
struct sql::DataV6 * m_data
Variable data.
Definition: v6/db.h:117
Precompiled queries to manipulate the attr table.
Definition: sql/attrv6.h:46
bool explain_queries
True if we print an EXPLAIN trace of all queries to stderr.
Definition: v6/db.h:95
A bulletin that has been decoded and physically interpreted.
Definition: message.h:25
sql::DataV6 & data()
Access the data table.
void vacuum()
Remove orphan values from the database.
std::unique_ptr< db::CursorStation > query_stations(const Query &query)
Start a query on the station variables archive.
db::Format format() const
Return the format of this DB.
Definition: v6/db.h:145
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
void reset(const char *repinfo_file=0)
Reset the database, removing all existing DBALLE tables and re-creating them empty.
sql::Driver & driver()
Access the backend DB driver.
Functions used to connect to DB-All.e and insert, query and delete data.
void insert_station_data(StationValues &vals, bool can_replace, bool station_can_add) override
Insert station values into the database.
bool is_station_variable(int data_id, wreport::Varcode varcode) override
Check if this varcode and data_id correspond to a station variable.
struct sql::LevTr * m_lev_tr
Level/timerange information.
Definition: v6/db.h:113
db::Connection * conn
Database connection.
Definition: v6/db.h:91
void attr_insert_data(int data_id, const Values &attrs) override
Insert new attributes on a data value.
struct sql::Repinfo * m_repinfo
Accessors for the various parts of the database.
Definition: v6/db.h:109
sql::Station & station()
Access the station table.
void update_repinfo(const char *repinfo_file, int *added, int *deleted, int *updated)
Update the repinfo table in the database, with the data found in the given file.
Fast cached access to the repinfo table.
Definition: sql/repinfo.h:77
void disappear()
Remove all our traces from the database, if applicable.
void delete_tables()
Delete all the DB-ALLe tables from the database.
Definition: db/sql/levtr.h:86
void remove_all()
Remove all data from the database.
Definition: sql/driver.h:84
Definition: db/sql/station.h:46
struct sql::Station * m_station
Station information.
Definition: v6/db.h:111
Definition: db.h:182
void attr_insert_station(int data_id, const Values &attrs) override
Insert new attributes on a station value.
void attr_remove_data(int data_id, const db::AttrList &qcs) override
Delete attributes from a data value.
Query used to filter DB-All.e data.
Definition: query.h:14
sql::Driver * m_driver
SQL driver backend.
Definition: v6/db.h:99
Definition: values.h:142
sql::Repinfo & repinfo()
Access the repinfo table.
std::unique_ptr< db::CursorStationData > query_station_data(const Query &query) override
Query the station variables in the database.
sql::LevTr & lev_tr()
Access the lev_tr table.
bool export_msgs(const Query &query, std::function< bool(std::unique_ptr< Message > &&)> dest) override
Perform the query in `query', and send the results to dest.
DB-ALLe database connection.
Definition: v6/db.h:87
void dump(FILE *out)
Dump the entire contents of the database to an output stream.
void import_msg(const Message &msg, const char *repmemo, int flags) override
Import a Message into the DB-All.e database.
std::map< std::string, int > get_repinfo_priorities()
Get a mapping between rep_memo and their priorities.
struct sql::LevTrCache * m_lev_tr_cache
Level/timerange cache.
Definition: v6/db.h:115
Precompiled queries to manipulate the lev_tr table.
Definition: db/sql/levtr.h:46
int rep_cod_from_memo(const char *memo)
Get the report code from a report mnemonic.
Definition: sql.h:69
void attr_query_data(int data_id, std::function< void(std::unique_ptr< wreport::Var >)> &&dest) override
Query attributes on a data value.
Definition: values.h:106
struct sql::AttrV6 * m_attr
Variable attributes.
Definition: v6/db.h:119
void remove_station_data(const Query &query) override
Remove data from the database.
Precompiled query to manipulate the data table.
Definition: sql/datav6.h:57