libdballe 9.11
qbuilder.h
1#ifndef DBA_DB_V7_QBUILDER_H
2#define DBA_DB_V7_QBUILDER_H
3
5#include <dballe/db/v7/db.h>
6#include <dballe/core/query.h>
7#include <regex.h>
8
9namespace dballe {
10struct Varmatch;
11
12namespace db {
13namespace v7 {
14
16struct QueryBuilder
17{
19
21 std::shared_ptr<v7::Transaction> tr;
22
29 const char* bind_in_ident = nullptr;
30
31 bool select_station = false; // ana_id, lat, lon, ident
32
33 bool select_varinfo = false; // rep_cod, id_ltr, varcode
34
35 // IdQuery
36 bool select_data_id = false; // id_data
37
38 // DataQuery
39 bool select_data = false; // datetime, value
40
41 // SummaryQuery
42 bool select_summary_details = false; // id_data, datetime, datetimemax
43
46
49
52
55
57 const unsigned int modifiers;
58
61
62 QueryBuilder(std::shared_ptr<v7::Transaction> tr, const core::Query& query, unsigned int modifiers, bool query_station_vars);
63 virtual ~QueryBuilder() {}
64
65 void build();
66
67protected:
68 // Add WHERE conditions
69 bool add_pa_where(const char* tbl);
70 bool add_dt_where(const char* tbl);
71 bool add_ltr_where(const char* tbl);
72 bool add_varcode_where(const char* tbl);
73 bool add_repinfo_where(const char* tbl);
74 bool add_datafilter_where(const char* tbl);
75
76 virtual void build_select() = 0;
77 virtual bool build_where() = 0;
78 virtual void build_order_by() = 0;
79};
80
81struct StationQueryBuilder : public QueryBuilder
82{
83 StationQueryBuilder(std::shared_ptr<v7::Transaction> tr, const core::Query& query, unsigned int modifiers)
84 : QueryBuilder(tr, query, modifiers, false) {}
85
86 void build_select() override;
87 bool build_where() override;
88 void build_order_by() override;
89};
90
91struct DataQueryBuilder : public QueryBuilder
92{
95
98
100 bool select_attrs = false;
101
102 DataQueryBuilder(std::shared_ptr<v7::Transaction> tr, const core::Query& query, unsigned int modifiers, bool query_station_vars);
103 ~DataQueryBuilder();
104
105 // bool add_attrfilter_where(const char* tbl);
106
108 bool match_attrs(const wreport::Var& var) const;
109
110 void build_select() override;
111 bool build_where() override;
112 void build_order_by() override;
113};
114
115struct IdQueryBuilder : public DataQueryBuilder
116{
117 IdQueryBuilder(std::shared_ptr<v7::Transaction> tr, const core::Query& query, unsigned int modifiers, bool query_station_vars)
118 : DataQueryBuilder(tr, query, modifiers, query_station_vars) {}
119
120 void build_select() override;
121 void build_order_by() override;
122};
123
124struct SummaryQueryBuilder : public DataQueryBuilder
125{
126 SummaryQueryBuilder(std::shared_ptr<v7::Transaction> tr, const core::Query& query, unsigned int modifiers, bool query_station_vars)
127 : DataQueryBuilder(tr, query, modifiers, query_station_vars) {}
128
129 void build_select() override;
130 void build_order_by() override;
131};
132
133}
134}
135}
136
137#endif
Definition sql.h:53
Buffer used to build SQL queries.
Match a variable code and value.
Definition varmatch.h:13
Standard dballe::Query implementation.
Definition core/query.h:35
Varmatch * attr_filter
Attribute filter, if requested.
Definition qbuilder.h:94
bool query_attrs
True if we also query attributes of data.
Definition qbuilder.h:97
bool match_attrs(const wreport::Var &var) const
Match the attributes of var against attr_filter.
bool select_attrs
True if the select includes the attrs field.
Definition qbuilder.h:100
dballe::sql::Querybuf sql_query
Dynamically generated SQL query.
Definition qbuilder.h:48
const char * bind_in_ident
If defined, it need to point to the identifier to be used as the only bound input parameter.
Definition qbuilder.h:29
std::shared_ptr< v7::Transaction > tr
Database to operate on.
Definition qbuilder.h:21
dballe::sql::Querybuf sql_from
FROM part of the SQL query.
Definition qbuilder.h:51
bool query_station_vars
True if we are querying station information, rather than measured data.
Definition qbuilder.h:60
dballe::sql::Querybuf sql_where
WHERE part of the SQL query.
Definition qbuilder.h:54
const core::Query & query
Query object.
Definition qbuilder.h:45
const unsigned int modifiers
Modifier flags to enable special query behaviours.
Definition qbuilder.h:57
String buffer for composing database queries.
Definition querybuf.h:16