libdballe  6.8
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--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_V6_H
23 #define DBA_DB_V6_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 Station;
75 }
76 
77 namespace v6 {
78 struct Repinfo;
79 struct LevTr;
80 struct LevTrCache;
81 struct Data;
82 struct Attr;
83 
87 class DB : public dballe::DB
88 {
89 public:
92 
93 protected:
95  struct VarID
96  {
97  wreport::Varcode code;
98  DBALLE_SQL_C_SINT_TYPE id;
99  VarID(wreport::Varcode code, DBALLE_SQL_C_SINT_TYPE id) : code(code), id(id) {}
100  };
101 
103  std::vector<VarID> last_insert_varids;
104 
117  struct LevTr* m_lev_tr;
121  struct Data* m_data;
123  struct Attr* m_attr;
138  int _last_station_id;
139 
140  void init_after_connect();
141 
145  void run_sql(const char* query);
146 
147  DB(std::auto_ptr<Connection>& conn);
148 
149 public:
150  virtual ~DB();
151 
152  db::Format format() const { return V6; }
153 
155  v6::Repinfo& repinfo();
156 
158  v5::Station& station();
159 
161  LevTr& lev_tr();
162 
165 
167  Data& data();
168 
170  Attr& attr();
171 
172  void disappear();
173 
185  void reset(const char* repinfo_file = 0);
186 
190  void delete_tables();
191 
209  void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated);
210 
211  std::map<std::string, int> get_repinfo_priorities();
212 
216  int rep_cod_from_memo(const char* memo);
217 
226  bool check_rep_cod(int rep_cod);
227 
231  int last_lev_tr_insert_id();
232 
236  int last_data_insert_id();
237 
244  int get_rep_cod(const Record& rec);
245 
246  /*
247  * Lookup, insert or replace data in station taking the values from
248  * rec.
249  *
250  * If rec did not contain ana_id, it will be set by this function.
251  *
252  * @param rec
253  * The record with the station information
254  * @param can_add
255  * If true we can insert new stations in the database, if false we
256  * only look up existing records and raise an exception if missing
257  * @returns
258  * The station ID
259  */
260  int obtain_station(const Record& rec, bool can_add=true);
261 
262  /*
263  * Lookup, insert or replace data in station taking the values from
264  * rec.
265  *
266  * If rec did not contain context_id, it will be set by this function.
267  *
268  * @param rec
269  * The record with the lev_tr information
270  * @returns
271  * The lev_tr ID
272  */
273  int obtain_lev_tr(const Record& rec);
274 
289  void insert(const Record& rec, bool can_replace, bool station_can_add);
290 
291  int last_station_id() const;
292 
300  void remove(const Record& rec);
301 
302  void remove_all();
303 
313  void vacuum();
314 
323  std::auto_ptr<db::Cursor> query_stations(const Record& query);
324 
337  std::auto_ptr<db::Cursor> query_data(const Record& rec);
338 
339  std::auto_ptr<db::Cursor> query_summary(const Record& rec);
340 
356  unsigned query_attrs(int id_data, wreport::Varcode id_var, const db::AttrList& qcs, Record& attrs);
357 
358  void attr_insert(wreport::Varcode id_var, const Record& attrs);
359  void attr_insert(int id_data, wreport::Varcode id_var, const Record& attrs);
360 
373  void attr_remove(int id_data, wreport::Varcode id_var, const db::AttrList& qcs);
374 
389  void import_msg(const Msg& msg, const char* repmemo, int flags);
390 
400  void export_msgs(const Record& query, MsgConsumer& cons);
401 
405  void dump(FILE* out);
406 
407  friend class dballe::DB;
408 };
409 
410 } // namespace v6
411 } // namespace db
412 } // namespace dballe
413 
414 /* vim:set ts=4 sw=4: */
415 #endif
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...
Definition: db/v6/repinfo.h:31
Store information about the database ID of a variable.
Definition: v6/db.h:95
v5::Station & station()
Access the station table.
LevTrCache & lev_tr_cache()
Access the lev_tr cache.
struct LevTr * m_lev_tr
Level/timerange information.
Definition: v6/db.h:117
Storage for related physical data.
Definition: msg.h:119
void attr_remove(int id_data, 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
unsigned query_attrs(int id_data, wreport::Varcode id_var, const db::AttrList &qcs, Record &attrs)
Query attributes.
Precompiled queries to manipulate the attr table.
Definition: v6/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_summary(const Record &rec)
Query a summary of what the result would be for a query.
void vacuum()
Remove orphan values from the database.
db::Sequence * seq_lev_tr
Sequence accessors.
Definition: v6/db.h:133
DB-All.E record.
Definition: record.h:102
db::Format format() const
Return the format of this DB.
Definition: v6/db.h:152
Definition: cmdline.h:34
void reset(const char *repinfo_file=0)
Reset the database, removing all existing DBALLE tables and re-creating them empty.
std::auto_ptr< db::Cursor > query_data(const Record &rec)
Query the database.
struct Data * m_data
Variable data.
Definition: v6/db.h:121
Definition: lev_tr.h:132
struct LevTrCache * m_lev_tr_cache
Level/timerange cache.
Definition: v6/db.h:119
Functions used to connect to DB-All.e and insert, query and delete data.
Precompiled queries to manipulate the station table.
Definition: db/v5/station.h:46
std::vector< VarID > last_insert_varids
Store database variable IDs for all last inserted variables.
Definition: v6/db.h:103
struct v5::Station * m_station
Station information.
Definition: v6/db.h:115
db::Connection * conn
ODBC database connection.
Definition: v6/db.h:91
LevTr & lev_tr()
Access the lev_tr 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.
int last_data_insert_id()
Return the ID of the last inserted data.
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...
Attr & attr()
Access the data table.
void disappear()
Remove all our traces from the database, if applicable.
void delete_tables()
Delete all the DB-ALLe tables from the database.
void remove_all()
Remove all data from the database.
struct v6::Repinfo * m_repinfo
Accessors for the various parts of the database.
Definition: v6/db.h:113
db::Sequence * seq_data
data ID sequence
Definition: v6/db.h:135
Precompiled queries to manipulate the lev_tr table.
Definition: lev_tr.h:55
bool check_rep_cod(int rep_cod)
Verify that a rep_cod is supported by the database.
Definition: db.h:181
Data & data()
Access the data table.
Consumer interface used to stream messages as they are produced.
Definition: msg.h:478
void insert(const Record &rec, bool can_replace, bool station_can_add)
Insert a record into the database.
ODBC statement to read a sequence.
Definition: internals.h:299
struct Attr * m_attr
Variable attributes.
Definition: v6/db.h:123
std::auto_ptr< db::Cursor > query_stations(const Record &query)
Start a query on the station archive.
int get_rep_cod(const Record &rec)
Get the report id from this record.
DB-ALLe database connection.
Definition: v6/db.h:87
v6::Repinfo & repinfo()
Access the repinfo table.
void dump(FILE *out)
Dump the entire contents of the database to an output stream.
void run_sql(const char *query)
Run a one-off SQL query.
int last_lev_tr_insert_id()
Return the ID of the last inserted lev_tr.
std::map< std::string, int > get_repinfo_priorities()
Get a mapping between rep_memo and their priorities.
int rep_cod_from_memo(const char *memo)
Get the report code from a report mnemonic.
Precompiled query to manipulate the data table.
Definition: v6/data.h:48
void import_msg(const Msg &msg, const char *repmemo, int flags)
Import a Msg message into the DB-All.e database.