Overpass API regression suite coverage (Saxony) / overpass_api/statements/query.h

overpass_api/statements/query.h

57 of 78 instrumented lines run by the suite (73.1%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API 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 Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__QUERY_H
20#define DE__OSM3S___OVERPASS_API__STATEMENTS__QUERY_H
22#include <map>
23#include <string>
24#include <vector>
25#include "../../expat/escape_json.h"
26#include "../../expat/escape_xml.h"
27#include "statement.h"
30const int QUERY_NODE = 1;
31const int QUERY_WAY = 2;
32const int QUERY_CLOSED_WAY = 4;
33const int QUERY_RELATION = 8;
34const int QUERY_DERIVED = 16;
35const int QUERY_AREA = 32;
38class Regular_Expression;
39class Bbox_Query_Statement;
42/* === The Query Statement ===
44The most important statement is the ''query'' statement. This is not a single statement but rather consists of one of the type specifiers ''node'', ''way'', ''relation'' (or shorthand ''rel''), ''derived'', ''area'', or ''nwr'' (shorthand for nodes, ways or relations) followed by one or more filters. The result set is the set of all elements that match the conditions of all the filters.
46Example:
48<source lang="cpp">
49// one filter
50 node[name="Foo"];
51 way[name="Foo"];
52 rel[name="Foo"];
53 nwr[name="Foo"];
54 derived[name="Foo"];
55 area[name="Foo"];
57// many filters
58 node[name="Foo"][type="Bar"];
59</source>
61Here, ''node'', ''way'', ''rel'', ''nwr'', ''derived'', and ''area'' are the type specifier, ''[name="Foo"]'' resp. ''[type="Bar"]'' is the filter and the semicolon ends the statement.
63The ''query'' statement has a result set that can be changed with the usual postfix notation.
65<source lang="cpp">
66 node[name="Foo"]->.a;
67</source>
69The individual filters may have in addition input sets that can be changed in the individual filters. Please see for this at the respective filter.
70*/
72class Query_Statement : public Output_Statement
74 public:
75 Query_Statement(int line_number_, const std::map< std::string, std::string >& input_attributes,
76 Parsed_Query& global_settings);
77 virtual ~Query_Statement();
78 virtual void add_statement(Statement* statement, std::string text);
797474582 q virtual std::string get_name() const { return "query"; }
80 virtual void execute(Resource_Manager& rman);
82 static Generic_Statement_Maker< Query_Statement > statement_maker;
84569569 q static bool area_query_exists() { return area_query_exists_; }
86 static std::string to_string(int type)
871700574 q {
881700574 q if (type == QUERY_NODE)
89312133 q return "node";
901388493 q else if (type == QUERY_WAY)
91396167 q return "way";
92992391 q else if (type == QUERY_RELATION)
93306129 q return "relation";
94686302 q else if (type == QUERY_DERIVED)
950 return "derived";
96686302 q else if (type == QUERY_AREA)
970 return "area";
98686302 q else if (type == (QUERY_NODE | QUERY_WAY | QUERY_RELATION))
99236113 q return "nwr";
100450212 q else if (type == (QUERY_NODE | QUERY_WAY))
10118893 q return "nwr";
102262122 q else if (type == (QUERY_WAY | QUERY_RELATION))
10313466 q return "wr";
10412858 q else if (type == (QUERY_NODE | QUERY_RELATION))
1050 return "nr";
10712858 q return "area";
1081700574 q }
110 virtual std::string dump_xml(const std::string& indent) const
1110 {
1120 std::string result = indent + "<query" + dump_xml_result_name() + " type=\"" + to_string(type) + "\">\n";
1140 for (std::vector< Statement* >::const_iterator it = substatements.begin(); it != substatements.end(); ++it)
1150 result += *it ? (*it)->dump_xml(indent + " ") : "";
1170 return result + indent + "</query>\n";
1180 }
120850574 q virtual std::string dump_compact_ql(const std::string& indent) const { return dump_subquery_map_ql(indent, false); }
121850574 q virtual std::string dump_pretty_ql(const std::string& indent) const { return dump_subquery_map_ql(indent, true); }
123 std::string dump_subquery_map_ql(const std::string& indent, bool pretty) const
1241700574 q {
1251700574 q std::string result = (pretty ? indent : "") + to_string(type);
1271700574 q uint proper_substatement_count = 0;
1281700574 q for (std::vector< Statement* >::const_iterator it = substatements.begin();
1294314574 q it != substatements.end(); ++it)
1302614574 q {
1312614574 q if ((*it)->get_name() == "item")
13221497 q result += (*it)->dump_ql_in_query("");
1332400570 q else
1342400570 q ++proper_substatement_count;
1352614574 q }
1371700574 q std::string prefix = (pretty && proper_substatement_count > 1 ? "\n " + indent : "");
1391700574 q for (std::vector< Statement* >::const_iterator it = substatements.begin();
1404314574 q it != substatements.end(); ++it)
1412614574 q {
1422614574 q if ((*it)->get_name() != "item")
1432400570 q result += prefix + (*it)->dump_ql_in_query("");
1442614574 q }
1461700574 q if (indent == "(bbox)" && type != QUERY_AREA)
1470 result += "(bbox)";
1491700574 q return result + (pretty && proper_substatement_count > 1 && dump_ql_result_name() != "" ? "\n " + indent : "")
1501700574 q + dump_ql_result_name() + ";";
1511700574 q }
153 private:
154 int type;
155 std::vector< std::string > keys;
156 std::vector< std::pair< std::string, std::string > > key_values;
157 std::vector< std::pair< std::string, Regular_Expression* > > key_regexes;
158 std::vector< std::pair< Regular_Expression*, Regular_Expression* > > regkey_regexes;
159 std::vector< std::pair< std::string, std::string > > key_nvalues;
160 std::vector< std::pair< std::string, Regular_Expression* > > key_nregexes;
161 std::vector< std::pair< Regular_Expression*, Regular_Expression* > > regkey_nregexes;
162 std::vector< Query_Constraint* > constraints;
163 std::vector< Statement* > substatements;
164 Bbox_Query_Statement* global_bbox_statement;
166 static bool area_query_exists_;
168 void get_elements_by_id_from_db
169 (std::map< Uint31_Index, std::vector< Area_Skeleton > >& elements,
170 const std::vector< Area_Skeleton::Id_Type >& ids, bool invert_ids,
171 Resource_Manager& rman, File_Properties& file_prop);
173 template< class TIndex, class TObject >
174 void filter_by_tags
175 (std::map< TIndex, std::vector< TObject > >& items,
176 std::map< TIndex, std::vector< Attic< TObject > > >* attic_items,
177 uint64 timestamp,
178 const File_Properties& file_prop, const File_Properties* attic_file_prop,
179 Resource_Manager& rman, Transaction& transaction);
181 template< class TIndex, class TObject >
182 void filter_by_tags
183 (std::map< TIndex, std::vector< TObject > >& items,
184 const File_Properties& file_prop,
185 Resource_Manager& rman, Transaction& transaction);
187 void filter_by_tags(std::map< Uint31_Index, std::vector< Derived_Structure > >& items);
189 template< class Id_Type >
190 void collect_nodes(
191 const Id_Constraint< Id_Type >& ids, Answer_State& answer_state, Set& into, Resource_Manager& rman);
192 template< class Id_Type >
193 void collect_elems(
194 int type, const Id_Constraint< Id_Type >& ids, Answer_State& answer_state, Set& into, Resource_Manager& rman);
196 void collect_elems(Answer_State& answer_state, Set& into, Resource_Manager& rman);
197 void apply_all_filters(
198 Resource_Manager& rman, uint64 timestamp, Query_Filter_Strategy check_keys_late, Set& into);
199};
202class Has_Kv_Statement : public Statement
204 public:
205 Has_Kv_Statement(int line_number_, const std::map< std::string, std::string >& input_attributes,
206 Parsed_Query& global_settings);
2074045376 q virtual std::string get_name() const { return "has-kv"; }
2080 virtual std::string get_result_name() const { return ""; }
2090 virtual void execute(Resource_Manager& rman) {}
210 virtual ~Has_Kv_Statement();
212 static Generic_Statement_Maker< Has_Kv_Statement > statement_maker;
2141578355 q std::string get_key() const { return key_regex ? "" : key; }
215888180 q Regular_Expression* get_key_regex() { return key_regex; }
2162727376 q std::string get_value() const { return regex ? "" : value; }
2171092180 q Regular_Expression* get_regex() { return regex; }
2181506343 q bool get_straight() const { return straight; }
220 virtual std::string dump_xml(const std::string& indent) const
2210 {
2220 return indent + "<has-kv"
2230 + (key != "" ? (key_regex ? std::string(" regk=\"") : std::string(" k=\"")) + escape_xml(key) + "\"" : "")
2240 + (value != "" ? (regex ? std::string(" regv=\"") : std::string(" v=\"")) + escape_xml(value) + "\"" : "")
2250 + (straight ? "" : " modv=\"not\"")
2260 + (case_sensitive ? "" : " case=\"ignore\"")
2270 + "/>\n";
2280 }
230 virtual std::string dump_compact_ql(const std::string&) const
2311154376 q {
2321154376 q return std::string("[")
2331154376 q + (key_regex ? "~\"" : "\"") + escape_cstr(key) + "\""
2341154376 q + (value != "" ? std::string(straight ? "" : "!") + (regex ? "~\"" : "=\"") + escape_cstr(value) + "\"" : "")
2351154376 q + (case_sensitive ? "" : ",i")
2361154376 q + "]";
2371154376 q }
2380 virtual std::string dump_pretty_ql(const std::string& indent) const { return dump_compact_ql(indent); }
240 private:
241 std::string key, value;
242 Regular_Expression* regex;
243 Regular_Expression* key_regex;
244 bool straight;
245 bool case_sensitive;
246};
249#endif