libdballe  6.8
v5/db.h
Go to the documentation of this file.
1 /*
2  * dballe/v5/db - Archive for point-based meteorological data, db layout version 5
3  *
4  * Copyright (C) 2005--2013 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_V5_H
23 #define DBA_DB_V5_H
24 
26 #include <dballe/db/db.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 Record;
64 struct Msg;
65 struct Msgs;
66 struct MsgConsumer;
67 
68 namespace db {
69 struct Connection;
70 struct Statement;
71 struct Sequence;
72 
73 namespace v5 {
74 struct Repinfo;
75 struct Station;
76 struct Context;
77 struct Data;
78 struct Attr;
79 
83 class DB : public dballe::DB
84 {
85 public:
88 
89 protected:
90  int last_context_id;
91 
106  struct Data* m_data;
108  struct Attr* m_attr;
114  DBALLE_SQL_C_SINT_TYPE m_last_insert_id;
115 
128  int _last_station_id;
129 
130  void init_after_connect();
131 
135  void run_sql(const char* query);
136 
141  void fill_ana_layer(Msg& msg, int id_station, int id_report);
142 
143  DB(std::auto_ptr<Connection>& conn);
144 
145 public:
146  ~DB();
147 
148  db::Format format() const { return V5; }
149 
151  Repinfo& repinfo();
152 
154  Station& station();
155 
157  Context& context();
158 
160  Data& data();
161 
163  Attr& attr();
164 
165  void disappear();
166 
178  void reset(const char* repinfo_file = 0);
179 
183  void delete_tables();
184 
202  void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated);
203 
204  virtual std::map<std::string, int> get_repinfo_priorities();
205 
209  int rep_cod_from_memo(const char* memo);
210 
219  bool check_rep_cod(int rep_cod);
220 
225 
230 
237  int get_rep_cod(const Record& rec);
238 
239  /*
240  * Lookup, insert or replace data in station taking the values from
241  * rec.
242  *
243  * If rec did not contain ana_id, it will be set by this function.
244  *
245  * @param rec
246  * The record with the station information
247  * @param can_add
248  * If true we can insert new stations in the database, if false we
249  * only look up existing records and raise an exception if missing
250  * @returns
251  * The station ID
252  */
253  int obtain_station(const Record& rec, bool can_add=true);
254 
255  /*
256  * Lookup, insert or replace data in station taking the values from
257  * rec.
258  *
259  * If rec did not contain context_id, it will be set by this function.
260  *
261  * @param rec
262  * The record with the context information
263  * @returns
264  * The context ID
265  */
266  int obtain_context(const Record& rec);
267 
268  void insert(const Record& rec, bool can_replace, bool station_can_add);
269 
270  int last_station_id() const;
271 
279  void remove(const Record& rec);
280 
281  void remove_all();
282 
292  void vacuum();
293 
309  std::auto_ptr<db::Cursor> query(const Record& query, unsigned int wanted, unsigned int modifiers);
310 
319  std::auto_ptr<db::Cursor> query_stations(const Record& query);
320 
333  std::auto_ptr<db::Cursor> query_data(const Record& rec);
334 
335  virtual std::auto_ptr<db::Cursor> query_summary(const Record& rec);
336 
337  void query_datetime_extremes(const Record& query, Record& result);
338 
354  unsigned query_attrs(int reference_id, wreport::Varcode id_var, const db::AttrList& qcs, Record& attrs);
355 
356  void attr_insert(wreport::Varcode id_var, const Record& attrs);
357  void attr_insert(int reference_id, wreport::Varcode id_var, const Record& attrs);
358 
371  void attr_remove(int reference_id, wreport::Varcode id_var, const db::AttrList& qcs);
372 
387  void import_msg(const Msg& msg, const char* repmemo, int flags);
388 
398  void export_msgs(const Record& query, MsgConsumer& cons);
399 
403  void dump(FILE* out);
404 
405  friend class dballe::DB;
406 };
407 
408 } // namespace v5
409 } // namespace db
410 } // namespace dballe
411 
412 /* vim:set ts=4 sw=4: */
413 #endif
void delete_tables()
Delete all the DB-ALLe tables from the database.
void remove_all()
Remove all data from the database.
DB-ALLe database connection.
Definition: v5/db.h:83
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.
struct Data * m_data
Variable data.
Definition: v5/db.h:106
struct Context * m_context
Variable context.
Definition: v5/db.h:104
void import_msg(const Msg &msg, const char *repmemo, int flags)
Import a Msg message into the DB-All.e database.
bool check_rep_cod(int rep_cod)
Verify that a rep_cod is supported by the database.
db::Statement * stm_last_insert_id
Precompiled LAST_INSERT_ID (or equivalent) SQL statement.
Definition: v5/db.h:112
struct Station * m_station
Station information.
Definition: v5/db.h:102
Storage for related physical data.
Definition: msg.h:119
int last_station_insert_id()
Return the ID of the last inserted station.
struct Attr * m_attr
Variable attributes.
Definition: v5/db.h:108
Fast cached access to the repinfo table.
Definition: db/v5/repinfo.h:94
Precompiled queries to manipulate the context table.
Definition: db/v5/context.h:46
std::auto_ptr< db::Cursor > query_data(const Record &rec)
Query the database.
void attr_remove(int reference_id, wreport::Varcode id_var, const db::AttrList &qcs)
Delete QC data for the variable `var' in record `rec' (coming from a previous dba_query) ...
Database connection.
Definition: internals.h:124
Precompiled queries to manipulate the attr table.
Definition: v5/attr.h:48
int last_station_id() const
Return the station id for the last data that was inserted.
The ODBC specification is imperfect with regards to integer sizes on 64bit platforms, and different ODBC drivers are currently interpreting it differently.
std::auto_ptr< db::Cursor > query_stations(const Record &query)
Start a query on the station variables archive.
int last_context_insert_id()
Return the ID of the last inserted context.
DB-All.E record.
Definition: record.h:102
Definition: cmdline.h:34
Context & context()
Access the context table.
Station & station()
Access the station table.
virtual std::map< std::string, int > get_repinfo_priorities()
Get a mapping between rep_memo and their priorities.
db::Connection * conn
ODBC database connection.
Definition: v5/db.h:87
Functions used to connect to DB-All.e and insert, query and delete data.
DBALLE_SQL_C_SINT_TYPE m_last_insert_id
ID of the last autogenerated primary key.
Definition: v5/db.h:114
Precompiled queries to manipulate the station table.
Definition: db/v5/station.h:46
db::Sequence * seq_station
Sequence accessors.
Definition: v5/db.h:123
Attr & attr()
Access the data table.
void run_sql(const char *query)
Run a one-off SQL query.
int rep_cod_from_memo(const char *memo)
Get the report code from a report mnemonic.
std::auto_ptr< db::Cursor > query(const Record &query, unsigned int wanted, unsigned int modifiers)
Create and execute a database query.
void fill_ana_layer(Msg &msg, int id_station, int id_report)
Fill a message station info layer with information from the given station and report IDs...
void export_msgs(const Record &query, MsgConsumer &cons)
Perform the query in `query', and return the results as a NULL-terminated array of dba_msg...
void insert(const Record &rec, bool can_replace, bool station_can_add)
Insert a record into the database.
void reset(const char *repinfo_file=0)
Reset the database, removing all existing DBALLE tables and re-creating them empty.
ODBC statement.
Definition: internals.h:220
db::Format format() const
Return the format of this DB.
Definition: v5/db.h:148
Definition: db.h:181
virtual std::auto_ptr< db::Cursor > query_summary(const Record &rec)
Query a summary of what the result would be for a query.
unsigned query_attrs(int reference_id, wreport::Varcode id_var, const db::AttrList &qcs, Record &attrs)
Query attributes.
void dump(FILE *out)
Dump the entire contents of the database to an output stream.
struct Repinfo * m_repinfo
Accessors for the various parts of the database.
Definition: v5/db.h:100
Consumer interface used to stream messages as they are produced.
Definition: msg.h:478
ODBC statement to read a sequence.
Definition: internals.h:299
Repinfo & repinfo()
Access the repinfo table.
int get_rep_cod(const Record &rec)
Get the report id from this record.
void disappear()
Remove all our traces from the database, if applicable.
Data & data()
Access the data table.
db::Sequence * seq_context
Context ID sequence.
Definition: v5/db.h:125
void vacuum()
Remove orphan values from the database.
Precompiled query to manipulate the data table.
Definition: v5/data.h:48
void attr_insert(wreport::Varcode id_var, const Record &attrs)
Insert new attributes into the database, reusing the reference IDs stored by the last insert...