libdballe  5.18
db.h
Go to the documentation of this file.
1 /*
2  * dballe/db - Archive for point-based meteorological data
3  *
4  * Copyright (C) 2005--2010 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_H
23 #define DBA_DB_H
24 
26 #include <dballe/db/cursor.h>
27 #include <wreport/varinfo.h>
28 #include <string>
29 #include <vector>
30 #include <memory>
31 
42 /* Import the attributes. */
43 #define DBA_IMPORT_ATTRS 1
44 /* Attempt to merge pseudoana extra information into the existing ones. */
45 #define DBA_IMPORT_FULL_PSEUDOANA 2
46 /* Import datetime information as data to preserve their attributes. */
47 #define DBA_IMPORT_DATETIME_ATTRS 4
48 /* Message data will overwrite existing values; otherwise, trying to insert
49  * existing data will cause an error. */
50 #define DBA_IMPORT_OVERWRITE 8
51 /* Perform the import outside of the transaction. This will make the function
52  * faster but not atomic: if interrupted, for example in case of error, then
53  * the data inserted before the interruption will stay in the database. */
54 #define DBA_IMPORT_NO_TRANSACTIONS 16
55 
56 
61 #define DBA_DB_WANT_COORDS (1 << 0)
62 
63 #define DBA_DB_WANT_IDENT (1 << 1)
64 
65 #define DBA_DB_WANT_LEVEL (1 << 2)
66 
67 #define DBA_DB_WANT_TIMERANGE (1 << 3)
68 
69 #define DBA_DB_WANT_DATETIME (1 << 4)
70 
71 #define DBA_DB_WANT_VAR_NAME (1 << 5)
72 
73 #define DBA_DB_WANT_VAR_VALUE (1 << 6)
74 
75 #define DBA_DB_WANT_REPCOD (1 << 7)
76 
77 #define DBA_DB_WANT_ANA_ID (1 << 8)
78 
79 #define DBA_DB_WANT_CONTEXT_ID (1 << 9)
80 
86 #define DBA_DB_MODIFIER_BEST (1 << 0)
87 
90 #define DBA_DB_MODIFIER_BIGANA (1 << 1)
91 
92 #define DBA_DB_MODIFIER_DISTINCT (1 << 2)
93 
94 #define DBA_DB_MODIFIER_ANAEXTRA (1 << 3)
95 
96 #define DBA_DB_MODIFIER_NOANAEXTRA (1 << 4)
97 
98 #define DBA_DB_MODIFIER_UNSORTED (1 << 5)
99 
103 #define DBA_DB_MODIFIER_STREAM (1 << 6)
104 
105 #define DBA_DB_MODIFIER_SORT_FOR_EXPORT (1 << 7)
106 
107 namespace dballe {
108 struct Record;
109 struct Msg;
110 struct Msgs;
111 struct MsgConsumer;
112 
113 namespace db {
114 struct Connection;
115 struct Statement;
116 struct Sequence;
117 struct Repinfo;
118 struct Station;
119 struct Context;
120 struct Data;
121 struct Attr;
122 }
123 
127 class DB
128 {
129 public:
132 
133 protected:
148  struct db::Data* m_data;
150  struct db::Attr* m_attr;
156  DBALLE_SQL_C_SINT_TYPE m_last_insert_id;
157 
170  void init_after_connect();
171 
175  void run_sql(const char* query);
176 
180  void drop_table_if_exists(const char* name);
181 
185  void drop_sequence_if_exists(const char* name);
186 
191  void fill_ana_layer(Msg& msg, int id_station, int id_report);
192 
193 public:
194  DB();
195  ~DB();
196 
212  void connect(const char* dsn, const char* user, const char* password);
213 
222  void connect_generic(const char* config);
223 
230  void connect_from_file(const char* pathname);
231 
242  void connect_from_url(const char* url);
243 
250  void connect_test();
251 
260  static bool is_url(const char* str);
261 
263  db::Repinfo& repinfo();
264 
266  db::Station& station();
267 
269  db::Context& context();
270 
272  db::Data& data();
273 
275  db::Attr& attr();
276 
288  void reset(const char* repinfo_file = 0);
289 
293  void delete_tables();
294 
312  void update_repinfo(const char* repinfo_file, int* added, int* deleted, int* updated);
313 
317  int rep_cod_from_memo(const char* memo);
318 
322  const std::string& rep_memo_from_cod(int rep_cod);
323 
332  bool check_rep_cod(int rep_cod);
333 
338 
343 
350  int get_rep_cod(Record& rec);
351 
352  /*
353  * Lookup, insert or replace data in station taking the values from
354  * rec.
355  *
356  * If rec did not contain ana_id, it will be set by this function.
357  *
358  * @param rec
359  * The record with the station information
360  * @param can_add
361  * If true we can insert new stations in the database, if false we
362  * only look up existing records and raise an exception if missing
363  * @returns
364  * The station ID
365  */
366  int obtain_station(Record& rec, bool can_add=true);
367 
368  /*
369  * Lookup, insert or replace data in station taking the values from
370  * rec.
371  *
372  * If rec did not contain context_id, it will be set by this function.
373  *
374  * @param rec
375  * The record with the context information
376  * @returns
377  * The context ID
378  */
379  int obtain_context(Record& rec);
380 
397  void insert(Record& rec, bool can_replace, bool station_can_add);
398 
406  void remove(const Record& rec);
407 
417  void remove_orphans();
418 
434  std::auto_ptr<db::Cursor> query(const Record& query, unsigned int wanted, unsigned int modifiers);
435 
444  std::auto_ptr<db::Cursor> query_stations(const Record& query);
445 
458  std::auto_ptr<db::Cursor> query_data(const Record& rec);
459 
475  unsigned query_attrs(int id_context, wreport::Varcode id_var, const db::AttrList& qcs, Record& attrs);
476 
489  void attr_insert_or_replace(int id_context, wreport::Varcode id_var, const Record& attrs, bool can_replace);
490 
504  void attr_insert(int id_context, wreport::Varcode id_var, const Record& attrs);
505 
518  void attr_insert_new(int id_context, wreport::Varcode id_var, const Record& attrs);
519 
532  void attr_remove(int id_context, wreport::Varcode id_var, const db::AttrList& qcs);
533 
548  void import_msg(const Msg& msg, const char* repmemo, int flags);
549 
564  void import_msgs(const Msgs& msgs, const char* repmemo, int flags);
565 
575  void export_msgs(const Record& query, MsgConsumer& cons);
576 
580  void dump(FILE* out);
581 };
582 
583 } // namespace dballe
584 
585 /* vim:set ts=4 sw=4: */
586 #endif