Now also parse new diffs
[handlerosm.git] / handler_osm_get.c
blob241bdd7a1510a6910ae4ec042348c9dcc53a9cf9
1 #include <cherokee/common-internal.h>
2 #include <cherokee/cherokee.h>
3 #include <limits.h>
6 #include "handler_osm.h"
7 #include "handler_osm_get.h"
8 #include "handler_osm_sql.h"
9 #include "handler_osm_db.h"
10 #include "handler_osm_gpx.h"
11 #include "handler_osm_user.h"
13 #define SQL_XAPI_NODE "SELECT DISTINCT id, long, lat, username, timestamp FROM " SQL_NODES ", node_tags WHERE id = node"
14 #define SQL_XAPI_NODE_END " ORDER BY id"
16 #define SQL_XAPI_NODE_ID "SELECT DISTINCT id FROM " SQL_NODES ", node_tags WHERE id = node"
18 #define SQL_XAPI_NTAG "SELECT node, k, v FROM node_tags WHERE node IN ("
19 #define SQL_XAPI_NTAG_END ") ORDER BY node"
21 #define SQL_XAPI_WAY "SELECT DISTINCT id, username, timestamp FROM ways, way_tags WHERE id = way AND (TRUE"
22 #define SQL_XAPI_WAY_MID " OR id IN (SELECT way FROM way_nds WHERE to_node IN ("
23 #define SQL_XAPI_WAY_END "))) ORDER BY id"
25 #define SQL_XAPI_WAY_ID "SELECT DISTINCT id FROM ways, way_tags WHERE id = way AND (TRUE"
26 #define SQL_XAPI_WAY_ID_END ")))"
28 #define SQL_XAPI_WAY_WT "SELECT way, k, v FROM way_tags WHERE way IN ("
29 #define SQL_XAPI_WAY_WT_END ") ORDER BY way"
31 #define SQL_XAPI_WAY_WN "SELECT way, to_node FROM way_nds WHERE way IN ("
32 #define SQL_XAPI_WAY_WN_END ") ORDER BY way, idx"
35 #define SQL_XAPI_REL "SELECT DISTINCT id, username, timestamp FROM relations, relation_tags WHERE id = relation AND (TRUE"
36 #define SQL_XAPI_REL_MID " OR id IN (SELECT relation FROM relation_members_node WHERE to_node IN ("
37 #define SQL_XAPI_REL_END "))) ORDER BY id"
39 #define SQL_XAPI_REL_ID "SELECT DISTINCT id FROM relations, relation_tags WHERE id = relation AND (TRUE"
40 #define SQL_XAPI_REL_ID_END ")))"
42 #define SQL_XAPI_REL_RT "SELECT relation, k, v FROM relation_tags WHERE relation IN ("
43 #define SQL_XAPI_REL_RT_END ") ORDER BY relation"
44 #define SQL_XAPI_REL_RR "SELECT relation, to_relation, role FROM relation_members_relation WHERE relation IN ("
45 #define SQL_XAPI_REL_RR_END ") ORDER BY relation, idx"
46 #define SQL_XAPI_REL_RN "SELECT relation, to_node, role FROM relation_members_node WHERE relation IN ("
47 #define SQL_XAPI_REL_RN_END ") ORDER BY relation, idx"
48 #define SQL_XAPI_REL_RW "SELECT relation, to_way, role FROM relation_members_way WHERE relation IN ("
49 #define SQL_XAPI_REL_RW_END ") ORDER BY relation, idx"
52 #define SQL_GET_OBJECT_BY_ID "SELECT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id = %lu", id
53 #define SQL_GET_OBJECT_BY_IDS "SELECT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id IN (%s) ORDER BY id"
54 #define SQL_GET_OBJECT_BY_WAY "SELECT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id IN (SELECT DISTINCT to_node FROM way_nds WHERE way = %lu) ORDER BY id", id
55 #define SQL_GET_OBJECT_BY_RELATION "SELECT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id IN (SELECT DISTINCT to_node FROM relation_members_node WHERE relation = %lu) ORDER BY id", id
57 #define SQL_GET_NODES_BY_RELATION "SELECT DISTINCT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id IN (SELECT to_node FROM relation_members_node WHERE relation = %lu) OR id IN (SELECT to_node FROM way_nds WHERE way IN (SELECT to_way FROM relation_members_way WHERE relation = %lu)) ORDER BY id;", id, id
59 #define SQL_GET_OBJTAG_BY_ID "SELECT node, k, v FROM node_tags WHERE node = %lu", id
60 #define SQL_GET_OBJTAG_BY_IDS "SELECT node, k, v FROM node_tags WHERE node IN (%s) ORDER BY node"
61 #define SQL_GET_OBJTAG_BY_WAY "SELECT node, k, v FROM node_tags WHERE node IN (SELECT DISTINCT to_node FROM way_nds WHERE way = %lu) ORDER BY node", id
62 #define SQL_GET_OBJTAG_BY_RELATION "SELECT node, k, v FROM node_tags WHERE node IN (SELECT DISTINCT to_node FROM relation_members_node WHERE relation = %lu) ORDER BY node", id
64 #define SQL_GET_NODES_TAGS_BY_RELATION "SELECT DISTINCT node, k, v FROM node_tags WHERE node IN (SELECT to_node FROM relation_members_node WHERE relation = %lu) OR id IN (SELECT to_node FROM way_nds WHERE way IN (SELECT to_way FROM relation_members_way WHERE relation = %lu)) ORDER BY node;", id, id
66 static ret_t result_way_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *ways, MapiHdl *way_tags, MapiHdl *way_nds, cherokee_buffer_t *buf);
68 static ret_t result_nodenew_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *nodes, MapiHdl *node_tags, cherokee_buffer_t *buf) {
69 if (mapi_get_row_count(*nodes) != 0) {
70 unsigned long int current_tag = 0;
72 while (mapi_fetch_row(*nodes)) {
73 unsigned long int current_node = strtoul(mapi_fetch_field(*nodes, 0), (char **) NULL, 10);
74 unsigned long len;
76 cherokee_buffer_add_va (buf, XML(NODE, " "), mapi_fetch_field(*nodes, 0),
77 mapi_fetch_field(*nodes, 1),
78 mapi_fetch_field(*nodes, 2),
79 mapi_fetch_field(*nodes, 3),
80 mapi_fetch_field(*nodes, 4));
82 len = buf->len;
84 while (current_tag < current_node) {
85 if (mapi_fetch_row(*node_tags) != 0)
86 current_tag = strtoul(mapi_fetch_field(*node_tags, 0), (char **) NULL, 10);
87 else
88 current_tag = ULONG_MAX;
91 while (current_tag == current_node) {
92 const char * key = mapi_fetch_field(*node_tags, 1);
93 const char * value = mapi_fetch_field(*node_tags, 2);
94 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
96 if (mapi_fetch_row(*node_tags) != 0)
97 current_tag = strtoul(mapi_fetch_field(*node_tags, 0), (char **) NULL, 10);
98 else
99 current_tag = ULONG_MAX;
102 if (buf->len > len)
103 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(NODE, " "));
104 else
105 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
109 return ret_ok;
112 #define SQL_GET_WAY_BY_ND "SELECT id, username, timestamp FROM ways WHERE id IN (SELECT DISTINCT way FROM way_nds WHERE to_node = %lu) ORDER BY id", id
113 #define SQL_GET_WAY_BY_RELATION "SELECT id, username, timestamp FROM ways WHERE id IN (SELECT DISTINCT to_way FROM relation_members_way WHERE relation = %lu) ORDER BY id", id
115 #define SQL_GET_WAY_TAGS_BY_ND "SELECT way, k, v FROM way_tags WHERE way IN (SELECT DISTINCT way FROM way_nds WHERE to_node = %lu) ORDER BY way", id
116 #define SQL_GET_WAY_TAGS_BY_RELATION "SELECT way, k, v FROM way_tags WHERE way IN (SELECT DISTINCT to_way FROM relation_members_way WHERE relation = %lu) ORDER BY way", id
118 #define SQL_GET_WAY_NDS_BY_ND "SELECT way, to_node FROM way_nds WHERE way IN (SELECT DISTINCT way FROM way_nds WHERE to_node = %lu) ORDER BY way, to_node", id
119 #define SQL_GET_WAY_NDS_BY_RELATION "SELECT way, to_node FROM way_nds WHERE way IN (SELECT DISTINCT to_way FROM relation_members_way WHERE relation = %lu) ORDER BY way, to_node, idx", id
121 #define SQL_GET_WAY_BY_NDS "SELECT id, username, timestamp FROM ways WHERE id IN (SELECT DISTINCT way FROM way_nds WHERE to_node IN (%s)) ORDER BY id"
122 #define SQL_GET_WAY_TAGS_BY_NDS "SELECT way, k, v FROM way_tags WHERE way IN (SELECT DISTINCT way FROM way_nds WHERE to_node IN (%s)) ORDER BY way"
123 #define SQL_GET_WAY_NDS_BY_NDS "SELECT way, to_node FROM way_nds WHERE AND way IN (SELECT DISTINCT way FROM way_nds WHERE to_node IN (%s)) ORDER BY way, to_node, idx"
126 static ret_t result_nodenew_ways_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *nodes, MapiHdl *node_tags, cherokee_buffer_t *buf) {
127 if (mapi_get_row_count(*nodes) != 0) {
128 unsigned long int current_tag = 0;
129 cherokee_buffer_t nds = CHEROKEE_BUF_INIT;
131 while (mapi_fetch_row(*nodes)) {
132 const char* id = mapi_fetch_field(*nodes, 0);
133 unsigned long int current_node = strtoul(mapi_fetch_field(*nodes, 0), (char **) NULL, 10);
134 unsigned long len;
136 cherokee_buffer_add(&nds, id, strlen(id));
137 cherokee_buffer_add_str(&nds, ", ");
139 cherokee_buffer_add_va (buf, XML(NODE, " "), id,
140 mapi_fetch_field(*nodes, 1),
141 mapi_fetch_field(*nodes, 2),
142 mapi_fetch_field(*nodes, 3),
143 mapi_fetch_field(*nodes, 4));
145 len = buf->len;
147 while (current_tag < current_node) {
148 if (mapi_fetch_row(*node_tags) != 0)
149 current_tag = strtoul(mapi_fetch_field(*node_tags, 0), (char **) NULL, 10);
150 else
151 current_tag = ULONG_MAX;
154 while (current_tag == current_node) {
155 const char * key = mapi_fetch_field(*node_tags, 1);
156 const char * value = mapi_fetch_field(*node_tags, 2);
157 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
159 if (mapi_fetch_row(*node_tags) != 0)
160 current_tag = strtoul(mapi_fetch_field(*node_tags, 0), (char **) NULL, 10);
161 else
162 current_tag = ULONG_MAX;
165 if (buf->len > len)
166 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(NODE, " "));
167 else
168 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
171 if (nds.len > 0) {
172 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
173 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
174 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
175 cherokee_buffer_drop_ending(&nds, 2);
176 cherokee_buffer_add_va (&sql1, SQL_GET_WAY_BY_NDS, nds.buf);
177 cherokee_buffer_add_va (&sql2, SQL_GET_WAY_TAGS_BY_NDS, nds.buf);
178 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_NDS_BY_NDS, nds.buf);
179 run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
180 cherokee_buffer_mrproper(&sql1);
181 cherokee_buffer_mrproper(&sql2);
182 cherokee_buffer_mrproper(&sql3);
185 cherokee_buffer_mrproper(&nds);
188 return ret_ok;
192 #define SQL_GET_WAY_BY_ID "SELECT id, username, timestamp FROM ways WHERE id = %lu", id
193 #define SQL_GET_WAY_BY_IDS "SELECT id, username, timestamp FROM ways WHERE id IN (%s) ORDER BY id"
194 #define SQL_GET_WAY_TAGS_BY_ID "SELECT way, k, v FROM way_tags WHERE way = %lu", id
195 #define SQL_GET_WAY_TAGS_BY_IDS "SELECT way, k, v FROM way_tags WHERE way IN (%s) ORDER BY way"
196 #define SQL_GET_WAY_NDS_BY_ID "SELECT way, to_node FROM way_nds WHERE way = %lu ORDER BY idx", id
197 #define SQL_GET_WAY_NDS_BY_IDS "SELECT way, to_node FROM way_nds WHERE way IN (%s) ORDER BY way, idx"
199 static ret_t result_way_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *ways, MapiHdl *way_tags,
200 MapiHdl *way_nds, cherokee_buffer_t *buf) {
202 if (mapi_get_row_count(*ways) != 0) {
203 unsigned long int current_tag = 0, current_member_node = 0;
205 while (mapi_fetch_row(*ways)) {
206 unsigned long int current_way = strtoul(mapi_fetch_field(*ways, 0), (char **) NULL, 10);
207 unsigned long len;
209 cherokee_buffer_add_va (buf, XML(WAY, " "), mapi_fetch_field(*ways, 0),
210 mapi_fetch_field(*ways, 1),
211 mapi_fetch_field(*ways, 2));
213 len = buf->len;
215 while (current_tag < current_way) {
216 if (mapi_fetch_row(*way_tags) != 0)
217 current_tag = strtoul(mapi_fetch_field(*way_tags, 0), (char **) NULL, 10);
218 else
219 current_tag = ULONG_MAX;
222 while (current_tag == current_way) {
223 const char * key = mapi_fetch_field(*way_tags, 1);
224 const char * value = mapi_fetch_field(*way_tags, 2);
225 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
227 if (mapi_fetch_row(*way_tags) != 0)
228 current_tag = strtoul(mapi_fetch_field(*way_tags, 0), (char **) NULL, 10);
229 else
230 current_tag = ULONG_MAX;
233 while (current_member_node < current_way) {
234 if (mapi_fetch_row(*way_nds) != 0)
235 current_member_node = strtoul(mapi_fetch_field(*way_nds, 0), (char **) NULL, 10);
236 else
237 current_member_node = ULONG_MAX;
240 while (current_member_node == current_way) {
241 const char * ref = mapi_fetch_field(*way_nds, 1);
242 cherokee_buffer_add_va (buf, ">" CRLF XML(ND, " ") "/", ref);
244 if (mapi_fetch_row(*way_nds) != 0)
245 current_member_node = strtoul(mapi_fetch_field(*way_nds, 0), (char **) NULL, 10);
246 else
247 current_member_node = ULONG_MAX;
250 if (buf->len > len)
251 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(WAY, " "));
252 else
253 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
257 return ret_ok;
260 #if MATERIALIZE
261 static ret_t result_way_full_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *ways, MapiHdl *way_tags,
262 MapiHdl *way_nds, cherokee_buffer_t *buf) {
264 if (mapi_get_row_count(*ways) != 0) {
265 unsigned long int current_tag = 0, current_member_node = 0;
266 cherokee_buffer_t nds = CHEROKEE_BUF_INIT;
268 while (mapi_fetch_row(*ways)) {
269 unsigned long int current_way = strtoul(mapi_fetch_field(*ways, 0), (char **) NULL, 10);
270 unsigned long len;
272 cherokee_buffer_add_va (buf, XML(WAY, " "), mapi_fetch_field(*ways, 0),
273 mapi_fetch_field(*ways, 1),
274 mapi_fetch_field(*ways, 2));
276 len = buf->len;
278 while (current_tag < current_way) {
279 if (mapi_fetch_row(*way_tags) != 0)
280 current_tag = strtoul(mapi_fetch_field(*way_tags, 0), (char **) NULL, 10);
281 else
282 current_tag = ULONG_MAX;
285 while (current_tag == current_way) {
286 const char * key = mapi_fetch_field(*way_tags, 1);
287 const char * value = mapi_fetch_field(*way_tags, 2);
288 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
290 if (mapi_fetch_row(*way_tags) != 0)
291 current_tag = strtoul(mapi_fetch_field(*way_tags, 0), (char **) NULL, 10);
292 else
293 current_tag = ULONG_MAX;
296 while (current_member_node < current_way) {
297 if (mapi_fetch_row(*way_nds) != 0)
298 current_member_node = strtoul(mapi_fetch_field(*way_nds, 0), (char **) NULL, 10);
299 else
300 current_member_node = ULONG_MAX;
303 while (current_member_node == current_way) {
304 const char * ref = mapi_fetch_field(*way_nds, 1);
305 cherokee_buffer_add_va (buf, ">" CRLF XML(ND, " ") "/", ref);
306 cherokee_buffer_add (&nds, ref, strlen(ref));
307 cherokee_buffer_add_str (&nds, ", ");
309 if (mapi_fetch_row(*way_nds) != 0)
310 current_member_node = strtoul(mapi_fetch_field(*way_nds, 0), (char **) NULL, 10);
311 else
312 current_member_node = ULONG_MAX;
315 if (buf->len > len)
316 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(WAY, " "));
317 else
318 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
321 if (nds.len > 0) {
322 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
323 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
324 cherokee_buffer_drop_ending(&nds, 2);
325 cherokee_buffer_add_va (&sql1, SQL_GET_OBJECT_BY_IDS, nds.buf);
326 cherokee_buffer_add_va (&sql2, SQL_GET_OBJTAG_BY_IDS, nds.buf);
327 run_sql2(hdl, &sql1, &sql2, buf, result_nodenew_to_xml);
328 cherokee_buffer_mrproper(&sql1);
329 cherokee_buffer_mrproper(&sql2);
332 cherokee_buffer_mrproper(&nds);
335 return ret_ok;
337 #endif
340 /* RELATION */
342 #define SQL_GET_RELATION_BY_ND "SELECT DISTINCT relation FROM relation_members_node WHERE to_node = %lu", id
343 #define SQL_GET_RELATION_BY_WAY "SELECT DISTINCT relation FROM relation_members_way WHERE to_way = %lu", id
344 #define SQL_GET_RELATION_BY_REL "SELECT DISTINCT relation FROM relation_members_relation WHERE to_relation = %lu", id
346 #if MATERIALIZE
347 static ret_t result_id_to_list(cherokee_handler_osm_t *hdl, MapiHdl *thingie, cherokee_buffer_t *buf) {
348 unsigned long int len = buf->len;
349 if (mapi_get_row_count(*thingie) != 0) {
350 while (mapi_fetch_row(*thingie)) {
351 const char *id = mapi_fetch_field(*thingie, 0);
352 cherokee_buffer_add (buf, id, strlen(id));
353 cherokee_buffer_add_str (buf, ", ");
356 if (buf->len > len) {
357 cherokee_buffer_drop_ending(buf, 2);
360 return ret_ok;
362 #endif
365 #define SQL_GET_RELATION_BY_ID "SELECT id, username, timestamp FROM relations WHERE id = %lu", id
366 #define SQL_GET_RELATION_BY_IDS "SELECT id, username, timestamp FROM relations WHERE id IN (%s) ORDER BY id"
367 #define SQL_GET_RELATION_BY_RELATION "SELECT id, username, timestamp FROM relations WHERE id IN (SELECT DISTINCT relation FROM relation_members_node WHERE to_node = %lu) ORDER BY id", id
368 #define SQL_GET_RELATION_TAGS_BY_ID "SELECT relation, k, v FROM relation_tags WHERE relation = %lu", id
369 #define SQL_GET_RELATION_TAGS_BY_IDS "SELECT relation, k, v FROM relation_tags WHERE relation IN (%s) ORDER BY relation"
370 #define SQL_GET_RELATION_TAGS_BY_RELATION "SELECT relation, k, v FROM relation_tags WHERE relation IN (SELECT DISTINCT relation FROM relation_members_node WHERE to_node = %lu) ORDER BY relation", id
371 #define SQL_GET_RELATION_MEMBER_NODE_BY_ID "SELECT relation, to_node, role FROM relation_members_node WHERE relation = %lu ORDER BY idx", id
372 #define SQL_GET_RELATION_MEMBER_NODE_BY_IDS "SELECT relation, to_node, role FROM relation_members_node WHERE relation IN (%s) ORDER BY relation, idx"
373 #define SQL_GET_RELATION_MEMBER_NODE_BY_RELATION "SELECT relation, to_node, role FROM relation_members_node WHERE relation IN (SELECT DISTINCT relation FROM relation_members_node WHERE to_node = %lu) ORDER BY relation", id
374 #define SQL_GET_RELATION_MEMBER_RELATION_BY_ID "SELECT relation, to_relation, role FROM relation_members_relation WHERE relation = %lu ORDER BY idx", id
375 #define SQL_GET_RELATION_MEMBER_RELATION_BY_IDS "SELECT relation, to_relation, role FROM relation_members_relation WHERE relation IN (%s) ORDER BY relation, idx"
376 #define SQL_GET_RELATION_MEMBER_RELATION_BY_RELATION "SELECT relation, to_relation, role FROM relation_members_relation WHERE relation IN (SELECT DISTINCT relation FROM relation_members_relation WHERE to_relation = %lu) ORDER BY relation", id
377 #define SQL_GET_RELATION_MEMBER_WAY_BY_ID "SELECT relation, to_way, role FROM relation_members_way WHERE relation = %lu ORDER BY idx", id
378 #define SQL_GET_RELATION_MEMBER_WAY_BY_IDS "SELECT relation, to_way, role FROM relation_members_way WHERE relation IN (%s) ORDER BY relation, idx"
379 #define SQL_GET_RELATION_MEMBER_WAY_BY_RELATION "SELECT relation, to_way, role FROM relation_members_way WHERE relation IN (SELECT DISTINCT relation FROM relation_members_way WHERE to_way = %lu) ORDER BY relation", id
382 static ret_t result_relation_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *relations, MapiHdl *relation_tags,
383 MapiHdl *relation_mn, MapiHdl *relation_mr, MapiHdl *relation_mw, cherokee_buffer_t *buf) {
385 if (mapi_get_row_count(*relations) != 0) {
386 unsigned long int current_tag = 0, current_member_node = 0, current_member_relation = 0, current_member_way = 0;
388 while (mapi_fetch_row(*relations)) {
389 unsigned long int current_relation = strtoul(mapi_fetch_field(*relations, 0), (char **) NULL, 10);
390 unsigned long len;
392 cherokee_buffer_add_va (buf, XML(RELATION, " "), mapi_fetch_field(*relations, 0),
393 mapi_fetch_field(*relations, 1),
394 mapi_fetch_field(*relations, 2));
396 len = buf->len;
398 while (current_tag < current_relation) {
399 if (mapi_fetch_row(*relation_tags) != 0)
400 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
401 else
402 current_tag = ULONG_MAX;
405 while (current_tag == current_relation) {
406 const char * key = mapi_fetch_field(*relation_tags, 1);
407 const char * value = mapi_fetch_field(*relation_tags, 2);
408 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
410 if (mapi_fetch_row(*relation_tags) != 0)
411 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
412 else
413 current_tag = ULONG_MAX;
416 while (current_member_node < current_relation) {
417 if (mapi_fetch_row(*relation_mn) != 0)
418 current_member_node = strtoul(mapi_fetch_field(*relation_mn, 0), (char **) NULL, 10);
419 else
420 current_member_node = ULONG_MAX;
423 while (current_member_node == current_relation) {
424 const char *to_node = mapi_fetch_field(*relation_mn, 1);
425 const char *role = mapi_fetch_field(*relation_mn, 2);
426 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_NODE, (to_node ? to_node : ""), (role ? role : ""));
428 if (mapi_fetch_row(*relation_mn) != 0)
429 current_member_node = strtoul(mapi_fetch_field(*relation_mn, 0), (char **) NULL, 10);
430 else
431 current_member_node = ULONG_MAX;
434 while (current_member_relation < current_relation) {
435 if (mapi_fetch_row(*relation_mr) != 0)
436 current_member_relation = strtoul(mapi_fetch_field(*relation_mr, 0), (char **) NULL, 10);
437 else
438 current_member_relation = ULONG_MAX;
441 while (current_member_relation == current_relation) {
442 const char *to_relation = mapi_fetch_field(*relation_mr, 1);
443 const char *role = mapi_fetch_field(*relation_mr, 2);
444 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_RELATION, (to_relation ? to_relation : ""), (role ? role : ""));
446 if (mapi_fetch_row(*relation_mr) != 0)
447 current_member_relation = strtoul(mapi_fetch_field(*relation_mr, 0), (char **) NULL, 10);
448 else
449 current_member_relation = ULONG_MAX;
452 while (current_member_way < current_relation) {
453 if (mapi_fetch_row(*relation_mw) != 0)
454 current_member_way = strtoul(mapi_fetch_field(*relation_mw, 0), (char **) NULL, 10);
455 else
456 current_member_way = ULONG_MAX;
459 while (current_member_way == current_relation) {
460 const char *to_way = mapi_fetch_field(*relation_mw, 1);
461 const char *role = mapi_fetch_field(*relation_mw, 2);
462 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_WAY, (to_way ? to_way : ""), (role ? role : ""));
464 if (mapi_fetch_row(*relation_mw) != 0)
465 current_member_way = strtoul(mapi_fetch_field(*relation_mw, 0), (char **) NULL, 10);
466 else
467 current_member_way = ULONG_MAX;
470 if (buf->len > len)
471 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(RELATION, " "));
472 else
473 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
477 return ret_ok;
480 #if MATERIALIZE
481 static ret_t result_relation_full_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *relations, MapiHdl *relation_tags,
482 MapiHdl *relation_mn, MapiHdl *relation_mr, MapiHdl *relation_mw, cherokee_buffer_t *buf) {
484 if (mapi_get_row_count(*relations) != 0) {
485 unsigned long int current_tag = 0, current_member_node = 0, current_member_relation = 0, current_member_way = 0;
486 cherokee_buffer_t nds = CHEROKEE_BUF_INIT;
487 cherokee_buffer_t wys = CHEROKEE_BUF_INIT;
488 cherokee_buffer_t rls = CHEROKEE_BUF_INIT;
490 while (mapi_fetch_row(*relations)) {
491 unsigned long int current_relation = strtoul(mapi_fetch_field(*relations, 0), (char **) NULL, 10);
492 unsigned long len;
494 cherokee_buffer_add_va (buf, XML(RELATION, " "), mapi_fetch_field(*relations, 0),
495 mapi_fetch_field(*relations, 1),
496 mapi_fetch_field(*relations, 2));
498 len = buf->len;
500 while (current_tag < current_relation) {
501 if (mapi_fetch_row(*relation_tags) != 0)
502 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
503 else
504 current_tag = ULONG_MAX;
507 while (current_tag == current_relation) {
508 const char * key = mapi_fetch_field(*relation_tags, 1);
509 const char * value = mapi_fetch_field(*relation_tags, 2);
510 cherokee_buffer_add_va (buf, ">" CRLF XML(TAG, " ") "/", key, (value == NULL ? "" : value));
512 if (mapi_fetch_row(*relation_tags) != 0)
513 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
514 else
515 current_tag = ULONG_MAX;
518 while (current_member_node < current_relation) {
519 if (mapi_fetch_row(*relation_mn) != 0)
520 current_member_node = strtoul(mapi_fetch_field(*relation_mn, 0), (char **) NULL, 10);
521 else
522 current_member_node = ULONG_MAX;
525 while (current_member_node == current_relation) {
526 const char *to_node = mapi_fetch_field(*relation_mn, 1);
527 const char *role = mapi_fetch_field(*relation_mn, 2);
528 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_NODE, (to_node ? to_node : ""), (role ? role : ""));
530 cherokee_buffer_add (&nds, to_node, strlen(to_node));
531 cherokee_buffer_add_str (&nds, ", ");
533 if (mapi_fetch_row(*relation_mn) != 0)
534 current_member_node = strtoul(mapi_fetch_field(*relation_mn, 0), (char **) NULL, 10);
535 else
536 current_member_node = ULONG_MAX;
539 while (current_member_relation < current_relation) {
540 if (mapi_fetch_row(*relation_mr) != 0)
541 current_member_relation = strtoul(mapi_fetch_field(*relation_mr, 0), (char **) NULL, 10);
542 else
543 current_member_relation = ULONG_MAX;
546 while (current_member_relation == current_relation) {
547 const char *to_relation = mapi_fetch_field(*relation_mr, 1);
548 const char *role = mapi_fetch_field(*relation_mr, 2);
549 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_RELATION, (to_relation ? to_relation : ""), (role ? role : ""));
551 cherokee_buffer_add (&rls, to_relation, strlen(to_relation));
552 cherokee_buffer_add_str (&rls, ", ");
554 if (mapi_fetch_row(*relation_mr) != 0)
555 current_member_relation = strtoul(mapi_fetch_field(*relation_mr, 0), (char **) NULL, 10);
556 else
557 current_member_relation = ULONG_MAX;
560 while (current_member_way < current_relation) {
561 if (mapi_fetch_row(*relation_mw) != 0)
562 current_member_way = strtoul(mapi_fetch_field(*relation_mw, 0), (char **) NULL, 10);
563 else
564 current_member_way = ULONG_MAX;
567 while (current_member_way == current_relation) {
568 const char *to_way = mapi_fetch_field(*relation_mw, 1);
569 const char *role = mapi_fetch_field(*relation_mw, 2);
570 cherokee_buffer_add_va (buf, ">" CRLF XML(MEMBER, " ") "/", MEMBER_TYPE_WAY, (to_way ? to_way : ""), (role ? role : ""));
572 cherokee_buffer_add (&wys, to_way, strlen(to_way));
573 cherokee_buffer_add_str (&wys, ", ");
575 if (mapi_fetch_row(*relation_mw) != 0)
576 current_member_way = strtoul(mapi_fetch_field(*relation_mw, 0), (char **) NULL, 10);
577 else
578 current_member_way = ULONG_MAX;
581 if (buf->len > len)
582 cherokee_buffer_add_str (buf, ">" CRLF XMLCLOSE(RELATION, " "));
583 else
584 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
587 if (nds.len > 0) {
588 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
589 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
590 cherokee_buffer_drop_ending(&nds, 2);
591 cherokee_buffer_add_va (&sql1, SQL_GET_OBJECT_BY_IDS, nds.buf);
592 cherokee_buffer_add_va (&sql2, SQL_GET_OBJTAG_BY_IDS, nds.buf);
593 run_sql2(hdl, &sql1, &sql2, buf, result_nodenew_to_xml);
594 cherokee_buffer_mrproper(&sql1);
595 cherokee_buffer_mrproper(&sql2);
598 if (rls.len > 0) {
599 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
600 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
601 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
602 cherokee_buffer_t sql4 = CHEROKEE_BUF_INIT;
603 cherokee_buffer_t sql5 = CHEROKEE_BUF_INIT;
604 cherokee_buffer_drop_ending(&rls, 2);
605 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_IDS, rls.buf);
606 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_IDS, rls.buf);
607 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_IDS, rls.buf);
608 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_IDS, rls.buf);
609 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_IDS, rls.buf);
610 run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
611 cherokee_buffer_mrproper(&sql1);
612 cherokee_buffer_mrproper(&sql2);
613 cherokee_buffer_mrproper(&sql3);
614 cherokee_buffer_mrproper(&sql4);
615 cherokee_buffer_mrproper(&sql5);
618 if (wys.len > 0) {
619 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
620 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
621 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
622 cherokee_buffer_drop_ending(&wys, 2);
623 cherokee_buffer_add_va (&sql1, SQL_GET_WAY_BY_IDS, wys.buf);
624 cherokee_buffer_add_va (&sql2, SQL_GET_WAY_TAGS_BY_IDS, wys.buf);
625 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_NDS_BY_IDS, wys.buf);
626 run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
627 cherokee_buffer_mrproper(&sql1);
628 cherokee_buffer_mrproper(&sql2);
629 cherokee_buffer_mrproper(&sql3);
632 cherokee_buffer_mrproper(&nds);
633 cherokee_buffer_mrproper(&rls);
634 cherokee_buffer_mrproper(&wys);
638 return ret_ok;
640 #endif
643 static ret_t result_relationway_to_xml(cherokee_handler_osm_t *hdl, MapiHdl *relations, MapiHdl *relation_tags,
644 MapiHdl *members_relation, MapiHdl *members_node,
645 cherokee_buffer_t *buf) {
646 unsigned long int current_tag = 0, current_member_node = 0, current_member_relation = 0;
648 enum WayRelation { RELATION_TYPE = 0, WAY_TYPE };
649 enum WayRelation type;
651 while (mapi_fetch_row(*relations)) {
652 cherokee_buffer_t tag = CHEROKEE_BUF_INIT;
653 type = RELATION_TYPE;
654 short int touched = 0;
655 unsigned long int current_relation = strtoul(mapi_fetch_field(*relations, 0), (char **) NULL, 10);
657 while (current_tag < current_relation) {
658 if (mapi_fetch_row(*relation_tags) != 0)
659 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
660 else
661 current_tag = ULONG_MAX;
664 while (current_tag == current_relation) {
665 const char * key = mapi_fetch_field(*relation_tags, 1);
666 const char * value = mapi_fetch_field(*relation_tags, 2);
668 if (key && value && strcmp(key, "type") == 0 && strcmp(value, "way") == 0)
669 type = WAY_TYPE;
670 else {
671 cherokee_buffer_add_va (&tag, XML(TAG, " ") XMLCLOSESHORT, key, (value == NULL ? "" : value));
672 touched = 1;
675 if (mapi_fetch_row(*relation_tags) != 0)
676 current_tag = strtoul(mapi_fetch_field(*relation_tags, 0), (char **) NULL, 10);
677 else
678 current_tag = ULONG_MAX;
680 if (type == RELATION_TYPE) {
681 cherokee_buffer_add_va (buf, XML(RELATION, " "), mapi_fetch_field(*relations, 0),
682 mapi_fetch_field(*relations, 1),
683 mapi_fetch_field(*relations, 2) );
684 } else {
685 cherokee_buffer_add_va (buf, XML(WAY, " "), mapi_fetch_field(*relations, 0),
686 mapi_fetch_field(*relations, 1),
687 mapi_fetch_field(*relations, 2) );
690 if (touched == 1) {
691 cherokee_buffer_add_str (buf, XMLCONTINUE);
692 cherokee_buffer_add_buffer (buf, &tag);
694 cherokee_buffer_mrproper(&tag);
696 while (current_member_node < current_relation) {
697 if (mapi_fetch_row(*members_node) != 0)
698 current_member_node = strtoul(mapi_fetch_field(*members_node, 0), (char **) NULL, 10);
699 else
700 current_member_node = ULONG_MAX;
703 while (current_member_node == current_relation) {
704 const char * to_node = mapi_fetch_field(*members_node, 1);
706 if (touched == 0) {
707 cherokee_buffer_add_str (buf, ">");
708 touched = 1;
711 if (type == RELATION_TYPE) {
712 const char * role = mapi_fetch_field(*members_node, 2);
713 cherokee_buffer_add_va (buf, XML(MEMBER, " ")XMLCLOSESHORT, MEMBER_TYPE_NODE, to_node, role);
714 } else
715 cherokee_buffer_add_va (buf, XML(ND, " ")XMLCLOSESHORT, to_node);
717 if (mapi_fetch_row(*members_node) != 0)
718 current_member_node = strtoul(mapi_fetch_field(*members_node, 0), (char **) NULL, 10);
719 else
720 current_member_node = ULONG_MAX;
723 if (type == RELATION_TYPE) {
724 while (current_member_relation < current_relation) {
725 if (mapi_fetch_row(*members_relation) != 0)
726 current_member_relation = strtoul(mapi_fetch_field(*members_relation, 0), (char **) NULL, 10);
727 else
728 current_member_relation = ULONG_MAX;
732 while (current_member_relation == current_relation) {
733 const char * to_relation = mapi_fetch_field(*members_relation, 1);
735 if (touched == 0) {
736 cherokee_buffer_add_str (buf, ">");
737 touched = 1;
740 if (type == RELATION_TYPE) {
741 const char * role = mapi_fetch_field(*members_relation, 2);
742 const char * wayornot = mapi_fetch_field(*members_relation, 3);
743 if (strcmp(wayornot, "true") == 0)
744 cherokee_buffer_add_va (buf, XML(MEMBER, " ")XMLCLOSESHORT, MEMBER_TYPE_WAY, to_relation, role);
745 else
746 cherokee_buffer_add_va (buf, XML(MEMBER, " ")XMLCLOSESHORT, MEMBER_TYPE_RELATION, to_relation, role);
747 } else
748 cherokee_buffer_add_va (buf, XML(ND, " ")XMLCLOSESHORT, MEMBER_TYPE_NODE, to_relation);
750 if (mapi_fetch_row(*members_relation) != 0)
751 current_member_relation = strtoul(mapi_fetch_field(*members_relation, 0), (char **) NULL, 10);
752 else
753 current_member_relation = ULONG_MAX;
757 if (touched == 0) {
758 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
759 } else if (type == RELATION_TYPE)
760 cherokee_buffer_add_va (buf, "%s", XMLCLOSE(RELATION, " "));
761 else
762 cherokee_buffer_add_va (buf, "%s", XMLCLOSE(WAY, " "));
765 return ret_ok;
769 ret_t
770 get_object_by_id(cherokee_handler_osm_t *hdl, unsigned long int id, cherokee_buffer_t *buf, osm_state_get_t state) {
771 ret_t ret;
772 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
773 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
774 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
775 cherokee_buffer_t sql4 = CHEROKEE_BUF_INIT;
776 cherokee_buffer_t sql5 = CHEROKEE_BUF_INIT;
778 switch (state) {
779 case OSM_NODE_COMMAND:
780 cherokee_buffer_add_va (&sql1, SQL_GET_OBJECT_BY_ID);
781 cherokee_buffer_add_va (&sql2, SQL_GET_OBJTAG_BY_ID);
782 ret = run_sql2(hdl, &sql1, &sql2, buf, result_nodenew_to_xml);
783 break;
784 case OSM_NODE_COMMAND_WAYS:
785 cherokee_buffer_add_va (&sql1, SQL_GET_WAY_BY_ND);
786 cherokee_buffer_add_va (&sql2, SQL_GET_WAY_TAGS_BY_ND);
787 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_NDS_BY_ND);
788 ret = run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
789 break;
790 case OSM_NODE_COMMAND_RELATIONS: {
791 cherokee_buffer_t relations = CHEROKEE_BUF_INIT;
792 #ifdef MATERIALIZE
793 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_ND);
794 run_sql(hdl, &sql1, &relations, result_id_to_list);
795 cherokee_buffer_mrproper(&sql1);
796 #else
797 cherokee_buffer_add_va (&relations, SQL_GET_RELATION_BY_ND);
798 #endif
800 if (relations.len > 0) {
801 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_IDS, relations.buf);
802 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_IDS, relations.buf);
803 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_IDS, relations.buf);
804 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_IDS, relations.buf);
805 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_IDS, relations.buf);
807 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
808 } else {
809 ret = ret_ok;
811 cherokee_buffer_mrproper(&relations);
812 break;
814 case OSM_WAY_COMMAND:
815 cherokee_buffer_add_va (&sql1, SQL_GET_WAY_BY_ID);
816 cherokee_buffer_add_va (&sql2, SQL_GET_WAY_TAGS_BY_ID);
817 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_NDS_BY_ID);
818 ret = run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
819 break;
820 case OSM_WAY_COMMAND_FULL:
821 cherokee_buffer_add_va (&sql1, SQL_GET_WAY_BY_ID);
822 cherokee_buffer_add_va (&sql2, SQL_GET_WAY_TAGS_BY_ID);
823 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_NDS_BY_ID);
824 #ifdef MATERIALIZE
825 ret = run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_full_to_xml);
826 #else
827 ret = run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
829 cherokee_buffer_add_va (&sql4, SQL_GET_OBJECT_BY_WAY);
830 cherokee_buffer_add_va (&sql5, SQL_GET_OBJTAG_BY_WAY);
831 ret = run_sql2(hdl, &sql4, &sql5, buf, result_nodenew_to_xml);
832 #endif
833 break;
834 case OSM_WAY_COMMAND_RELATIONS: {
835 cherokee_buffer_t relations = CHEROKEE_BUF_INIT;
836 #ifdef MATERIALIZE
837 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_WAY);
838 run_sql(hdl, &sql1, &relations, result_id_to_list);
839 cherokee_buffer_mrproper(&sql1);
840 #else
841 cherokee_buffer_add_va (&relations, SQL_GET_RELATION_BY_WAY);
842 #endif
844 if (relations.len > 0) {
845 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_IDS, relations.buf);
846 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_IDS, relations.buf);
847 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_IDS, relations.buf);
848 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_IDS, relations.buf);
849 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_IDS, relations.buf);
851 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
852 } else {
853 ret = ret_ok;
855 cherokee_buffer_mrproper(&relations);
856 break;
858 case OSM_RELATION_COMMAND:
859 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_ID);
860 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_ID);
861 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_ID);
862 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_ID);
863 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_ID);
864 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
865 break;
866 case OSM_RELATION_COMMAND_FULL:
867 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_ID);
868 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_ID);
869 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_ID);
870 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_ID);
871 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_ID);
872 #ifdef MATERIALIZE
873 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_full_to_xml);
874 #else
875 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
877 cherokee_buffer_mrproper(&sql1);
878 cherokee_buffer_mrproper(&sql2);
879 cherokee_buffer_mrproper(&sql3);
880 cherokee_buffer_mrproper(&sql4);
881 cherokee_buffer_mrproper(&sql5);
883 cherokee_buffer_add_va (&sql1, SQL_GET_NODES_BY_RELATION);
884 cherokee_buffer_add_va (&sql2, SQL_GET_NODES_TAGS_BY_RELATION);
885 ret = run_sql2(hdl, &sql1, &sql1, buf, result_nodenew_to_xml);
887 cherokee_buffer_add_va (&sql3, SQL_GET_WAY_BY_RELATION);
888 cherokee_buffer_add_va (&sql4, SQL_GET_WAY_TAGS_BY_RELATION);
889 cherokee_buffer_add_va (&sql5, SQL_GET_WAY_NDS_BY_RELATION);
890 ret = run_sql3(hdl, &sql3, &sql4, &sql5, buf, result_way_to_xml);
892 cherokee_buffer_mrproper(&sql1);
893 cherokee_buffer_mrproper(&sql2);
894 cherokee_buffer_mrproper(&sql3);
895 cherokee_buffer_mrproper(&sql4);
896 cherokee_buffer_mrproper(&sql5);
898 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_RELATION);
899 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_RELATION);
900 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_RELATION);
901 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_RELATION);
902 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_RELATION);
903 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
904 #endif
905 break;
906 case OSM_RELATION_COMMAND_RELATIONS: {
907 cherokee_buffer_t relations = CHEROKEE_BUF_INIT;
908 #ifdef MATERIALIZE
909 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_REL);
910 run_sql(hdl, &sql1, &relations, result_id_to_list);
911 cherokee_buffer_mrproper(&sql1);
912 #else
913 cherokee_buffer_add_va (&relations, SQL_GET_RELATION_BY_REL);
914 #endif
916 if (relations.len > 0) {
917 cherokee_buffer_add_va (&sql1, SQL_GET_RELATION_BY_IDS, relations.buf);
918 cherokee_buffer_add_va (&sql2, SQL_GET_RELATION_TAGS_BY_IDS, relations.buf);
919 cherokee_buffer_add_va (&sql3, SQL_GET_RELATION_MEMBER_NODE_BY_IDS, relations.buf);
920 cherokee_buffer_add_va (&sql4, SQL_GET_RELATION_MEMBER_RELATION_BY_IDS, relations.buf);
921 cherokee_buffer_add_va (&sql5, SQL_GET_RELATION_MEMBER_WAY_BY_IDS, relations.buf);
923 ret = run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
924 } else {
925 ret = ret_ok;
927 cherokee_buffer_mrproper(&relations);
928 break;
930 default:
931 ret = ret_error;
933 cherokee_buffer_mrproper(&sql5);
934 cherokee_buffer_mrproper(&sql4);
935 cherokee_buffer_mrproper(&sql3);
936 cherokee_buffer_mrproper(&sql2);
937 cherokee_buffer_mrproper(&sql1);
938 return ret;
941 static ret_t fetch_bbox(char *string, double *left, double *bottom, double *right, double *top) {
942 char *token;
943 if ((token = (char *) strsep( &string , ",")) != NULL) {
944 *top = strtod(token, (char **) NULL);
945 if (errno != ERANGE && (token = (char *) strsep( &string , ",")) != NULL) {
946 *left = strtod(token, (char **) NULL);
947 if (errno != ERANGE && (token = (char *) strsep( &string , ",")) != NULL) {
948 *bottom = strtod(token, (char **) NULL);
949 if (errno != ERANGE && (token = (char *) strsep( &string , ",")) != NULL) {
950 *right = strtod(token, (char **) NULL);
951 if (errno != ERANGE) {
952 return ret_ok;
958 return ret_error;
961 #define SQL_PARSE_MAP_NODE_BBOX "SELECT id, long, lat, username, timestamp FROM " SQL_NODES " WHERE id IN (SELECT to_node FROM relation_members_node WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) OR id IN (SELECT to_node FROM way_nds WHERE way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) OR " SQL_BY_BBOX " ORDER BY id", BBOX_VA_ARGS, BBOX_VA_ARGS, BBOX_VA_ARGS
963 #define SQL_PARSE_MAP_NTAG_BBOX "SELECT node, k, v FROM node_tags WHERE node IN (SELECT id FROM " SQL_NODES " WHERE id IN (SELECT to_node FROM relation_members_node WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) OR id IN (SELECT to_node FROM way_nds WHERE way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) OR " SQL_BY_BBOX ") ORDER BY node", BBOX_VA_ARGS, BBOX_VA_ARGS, BBOX_VA_ARGS
965 #define SQL_PARSE_MAP_RELATIONS_BBOX "SELECT DISTINCT id, username, timestamp FROM relations WHERE id IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) OR id IN (SELECT relation FROM relation_members_way WHERE to_way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) ORDER BY id", BBOX_VA_ARGS, BBOX_VA_ARGS
966 #define SQL_PARSE_MAP_RTAGS_BBOX "SELECT relation, k, v FROM relation_tags WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) OR relation IN (SELECT relation FROM relation_members_way WHERE to_way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) ORDER BY relation", BBOX_VA_ARGS, BBOX_VA_ARGS
968 #define SQL_PARSE_MAP_MEMBERSN_BBOX "SELECT relation, to_node, role FROM relation_members_node WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) OR relation IN (SELECT relation FROM relation_members_way WHERE to_way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) ORDER BY relation", BBOX_VA_ARGS, BBOX_VA_ARGS
969 #define SQL_PARSE_MAP_MEMBERSW_BBOX "SELECT relation, to_way, role FROM relation_members_way WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) OR relation IN (SELECT relation FROM relation_members_way WHERE to_way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX "))) ORDER BY relation", BBOX_VA_ARGS, BBOX_VA_ARGS
970 #define SQL_PARSE_MAP_MEMBERSR_BBOX "SELECT relation, to_relation, role FROM relation_members_relation WHERE relation IN (SELECT relation FROM relation_members_node WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) ORDER BY relation", BBOX_VA_ARGS
972 #define SQL_PARSE_MAP_WAYS_BBOX "SELECT id, username, timestamp FROM ways WHERE id IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) ORDER BY id", BBOX_VA_ARGS
973 #define SQL_PARSE_MAP_WTAGS_BBOX "SELECT way, k, v FROM way_tags WHERE way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) ORDER BY way", BBOX_VA_ARGS
974 #define SQL_PARSE_MAP_WNDS_BBOX "SELECT way, to_node FROM way_nds WHERE way IN (SELECT way FROM way_nds WHERE to_node IN (SELECT id FROM " SQL_NODES " WHERE " SQL_BY_BBOX ")) ORDER BY way", BBOX_VA_ARGS
978 static void parse_map2(cherokee_handler_osm_t *hdl, cherokee_buffer_t *buf) {
979 void *param;
980 double left, bottom, right, top;
981 cherokee_avl_t *arguments = HANDLER_CONN(hdl)->arguments;
983 if (ret_ok == cherokee_avl_get_ptr (arguments, "bbox", &param)) {
985 if (ret_ok == fetch_bbox((char *) param, &left, &bottom, &right, &top)) {
986 TRACE ("osmapi", "%s BBOX: %f %f %f %f\n", __func__, left, bottom, right, top);
987 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
988 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
989 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
990 cherokee_buffer_t sql4 = CHEROKEE_BUF_INIT;
991 cherokee_buffer_t sql5 = CHEROKEE_BUF_INIT;
992 cherokee_buffer_add_va (&sql1, SQL_PARSE_MAP_NODE_BBOX);
993 cherokee_buffer_add_va (&sql2, SQL_PARSE_MAP_NTAG_BBOX);
994 run_sql2(hdl, &sql1, &sql2, buf, result_nodenew_to_xml);
995 cherokee_buffer_mrproper(&sql1);
996 cherokee_buffer_mrproper(&sql2);
998 cherokee_buffer_add_va (&sql1, SQL_PARSE_MAP_WAYS_BBOX);
999 cherokee_buffer_add_va (&sql2, SQL_PARSE_MAP_WTAGS_BBOX);
1000 cherokee_buffer_add_va (&sql3, SQL_PARSE_MAP_WNDS_BBOX);
1001 run_sql3(hdl, &sql1, &sql2, &sql3, buf, result_way_to_xml);
1002 cherokee_buffer_mrproper(&sql1);
1003 cherokee_buffer_mrproper(&sql2);
1004 cherokee_buffer_mrproper(&sql3);
1006 cherokee_buffer_add_va (&sql1, SQL_PARSE_MAP_RELATIONS_BBOX);
1007 cherokee_buffer_add_va (&sql2, SQL_PARSE_MAP_RTAGS_BBOX);
1008 cherokee_buffer_add_va (&sql3, SQL_PARSE_MAP_MEMBERSN_BBOX);
1009 cherokee_buffer_add_va (&sql4, SQL_PARSE_MAP_MEMBERSR_BBOX);
1010 cherokee_buffer_add_va (&sql5, SQL_PARSE_MAP_MEMBERSW_BBOX);
1011 run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, buf, result_relation_to_xml);
1012 cherokee_buffer_mrproper(&sql1);
1013 cherokee_buffer_mrproper(&sql2);
1014 cherokee_buffer_mrproper(&sql3);
1015 cherokee_buffer_mrproper(&sql4);
1016 cherokee_buffer_mrproper(&sql5);
1023 static void parse_nodes(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1025 static void parse_nodes_search(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1028 static void parse_relations(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1030 static void parse_relations_search(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1033 static void parse_ways(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1035 static void parse_ways_search(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1037 static void parse_trackpoints(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1038 double left, bottom, right, top;
1039 unsigned long int page;
1040 void *param;
1041 printf("%s\n", __func__);
1043 if (ret_ok == cherokee_avl_get_ptr (arguments, "bbox", &param)) {
1045 if (ret_ok == fetch_bbox((char *) param, &left, &bottom, &right, &top)) {
1046 cherokee_buffer_add_va (buf, "%f %f %f %f", left, bottom, right, top);
1051 if (ret_ok == cherokee_avl_get_ptr (arguments, "page", &param)) {
1052 page = strtoul(param, (char **) NULL, 10);
1053 if (errno == ERANGE)
1054 page = 1;
1055 } else {
1056 page = 1;
1060 static void
1061 changes_byhour(cherokee_buffer_t *buf, unsigned long int zoom, unsigned long int hours) {
1062 cherokee_buffer_add_va (buf, "%d %d", zoom, hours);
1065 static void
1066 changes_bystartend(cherokee_buffer_t *buf, unsigned long int zoom, char *start, char *end) {
1067 cherokee_buffer_add_va (buf, "%d %s %s", zoom, start, end);
1070 static void
1071 parse_changes(cherokee_avl_t *arguments, cherokee_buffer_t *buf) {
1072 unsigned long int hours;
1073 unsigned long int zoom;
1074 void *param;
1075 ret_t ret;
1077 ret = cherokee_avl_get_ptr (arguments, "zoom", &param);
1078 if (ret == ret_ok) {
1079 zoom = strtoul(param, (char **) NULL, 10);
1080 } else
1081 zoom = 12;
1083 ret = cherokee_avl_get_ptr (arguments, "start", &param);
1084 if (ret == ret_ok) {
1085 void *param2;
1086 ret = cherokee_avl_get_ptr (arguments, "end", &param2);
1087 if (ret == ret_ok) {
1088 changes_bystartend(buf, zoom, (char *)param, (char *)param2);
1090 } else {
1091 ret = cherokee_avl_get_ptr (arguments, "hours", &param);
1092 if (ret == ret_ok) {
1093 hours = strtoul(param, (char **) NULL, 10);
1094 } else
1095 hours = 1;
1097 changes_byhour(buf, zoom, hours);
1101 static void
1102 objtype_history(cherokee_buffer_t *buf, unsigned long int id, osm_state_get_t state)
1104 printf("%s\n", __func__);
1108 static ret_t
1109 osm_build_page (cherokee_handler_osm_t *hdl)
1111 ret_t ret = ret_ok;
1112 cherokee_server_t *srv;
1113 cherokee_connection_t *conn;
1114 cherokee_buffer_t *buf;
1115 cherokee_buffer_t content = CHEROKEE_BUF_INIT;
1117 /* Init
1119 buf = &hdl->buffer;
1120 srv = HANDLER_SRV(hdl);
1121 conn = HANDLER_CONN(hdl);
1123 /* Parse the Request */
1125 if (conn->request.len > 1) {
1126 osm_state_get_t state = OSM_FIND_FIRST;
1127 unsigned long int id = 0;
1128 char *string = strdupa(conn->request.buf);
1129 char *token;
1130 char *xapi_offset;
1131 // void *param;
1132 while (state != OSM_DONE && (token = (char *) strsep( &string , "/")) != NULL) {
1133 if (*token == '\0')
1134 continue;
1136 switch (state) {
1137 case OSM_FIND_FIRST:
1138 switch (token[0]) {
1139 case '*':
1140 if (token[1] == '[') {
1141 xapi_offset = &token[1];
1142 state = OSM_ALL_XAPI_PARSE;
1143 } else
1144 state = OSM_DONE;
1145 break;
1146 case 'c':
1147 /* GET /api/0.5/changes?hours=1&zoom=16&start=none&end=none */
1148 parse_changes(conn->arguments, &content);
1149 state = OSM_DONE;
1150 break;
1151 case 'n':
1152 if (strncmp(token, "node", 4) == 0) {
1153 switch (token[4]) {
1154 case '\0':
1155 state = OSM_NODE_ID;
1156 break;
1157 case '[':
1158 xapi_offset = &token[4];
1159 state = OSM_NODE_XAPI_PARSE;
1160 break;
1161 case 's':
1162 state = OSM_NODES_PRE_PARSE;
1163 break;
1165 } else {
1166 state = OSM_DONE;
1168 break;
1169 case 'w':
1170 if (strncmp(token, "way", 3) == 0) {
1171 switch (token[3]) {
1172 case '\0':
1173 state = OSM_WAY_ID;
1174 break;
1175 case '[':
1176 xapi_offset = &token[3];
1177 state = OSM_WAY_XAPI_PARSE;
1178 break;
1179 case 's':
1180 state = OSM_WAYS_PRE_PARSE;
1181 break;
1183 } else
1184 state = OSM_DONE;
1186 break;
1187 case 'r':
1188 if (strncmp(token, "relation", 8) == 0) {
1189 switch (token[8]) {
1190 case '\0':
1191 state = OSM_RELATION_ID;
1192 break;
1193 case '[':
1194 xapi_offset = &token[8];
1195 state = OSM_RELATION_XAPI_PARSE;
1196 break;
1197 case 's':
1198 state = OSM_RELATIONS_PRE_PARSE;
1199 break;
1201 } else
1202 state = OSM_DONE;
1204 break;
1205 case 'g':
1206 state = OSM_GPX_ID;
1207 break;
1208 case 'u':
1209 user_preferences(&content);
1210 break;
1211 case 'm':
1212 /* GET /api/0.5/map?bbox=LEFT,BOTTOM,RIGHT,TOP */
1213 parse_map2(hdl, &content);
1214 break;
1215 case 't':
1216 /* GET /api/0.5/trackpoints?bbox=LEFT,BOTTOM,RIGHT,TOP&page=PAGENUMBER */
1217 parse_trackpoints(conn->arguments, &content);
1218 break;
1219 case 's':
1220 break;
1222 break;
1225 case OSM_GPX_ID:
1226 case OSM_NODE_ID:
1227 case OSM_WAY_ID:
1228 case OSM_RELATION_ID:
1229 id = strtoul(token, (char **) NULL, 10);
1230 if (errno != ERANGE) {
1231 state++;
1232 } else
1233 state = OSM_DONE;
1234 break;
1235 case OSM_GPX_COMMAND:
1236 if (strlen(token) > 1) {
1237 switch (token[1]) {
1238 case 'a':
1239 /* GET /api/0.5/gpx/<id>/data */
1240 gpx_data(&content, id);
1241 break;
1242 case 'e':
1243 /* GET /api/0.5/gpx/<id>/details */
1244 gpx_details(&content, id);
1245 break;
1248 break;
1250 /* GET /api/0.5/<objtype>/<id>/full
1251 * GET /api/0.5/<objtype>/<id>/history
1252 * GET /api/0.5/<objtype>/<id>/relations */
1254 case OSM_NODE_COMMAND:
1255 switch (token[0]) {
1256 case 'w':
1257 /* GET /api/0.5/node/<id>/ways */
1258 state++;
1259 break;
1260 case 'r':
1261 /* GET /api/0.5/?/<id>/relations */
1262 state += 2;
1263 break;
1265 break;
1266 case OSM_WAY_COMMAND:
1267 case OSM_RELATION_COMMAND:
1268 switch (token[0]) {
1269 case 'f':
1270 /* GET /api/0.5/?/<id>/full */
1271 state++;
1272 break;
1273 case 'h':
1274 /* GET /api/0.5/?/<id>/history */
1275 objtype_history(&content, id, state);
1276 break;
1277 case 'r':
1278 /* GET /api/0.5/?/<id>/relations */
1279 state += 2;
1280 break;
1282 break;
1283 case OSM_NODES_PRE_PARSE:
1284 switch (token[0]) {
1285 case '?':
1286 /* GET /api/0.5/<objtype>s?<objtype>s=<id>[,<id>...] */
1287 parse_nodes(conn->arguments, &content);
1288 break;
1289 case 's':
1290 /* GET /api/0.5/nodes/search?type=<type>&value=<value> */
1291 parse_nodes_search(conn->arguments, &content);
1292 break;
1294 break;
1296 case OSM_RELATIONS_PRE_PARSE:
1297 switch (token[0]) {
1298 case '?':
1299 /* GET /api/0.5/<objtype>s?<objtype>s=<id>[,<id>...] */
1300 parse_relations(conn->arguments, &content);
1301 break;
1302 case 's':
1303 /* GET /api/0.5/relations/search?type=<type>&value=<value> */
1304 parse_relations_search(conn->arguments, &content);
1305 break;
1307 break;
1309 case OSM_WAYS_PRE_PARSE:
1310 switch (token[0]) {
1311 case '?':
1312 /* GET /api/0.5/<objtype>s?<objtype>s=<id>[,<id>...] */
1313 parse_ways(conn->arguments, &content);
1314 break;
1315 case 's':
1316 /* GET /api/0.5/ways/search?type=<type>&value=<value> */
1317 parse_ways_search(conn->arguments, &content);
1318 break;
1320 break;
1322 default:
1323 state = OSM_DONE;
1327 switch (state) {
1328 case OSM_GPX_ID:
1329 case OSM_NODE_ID:
1330 case OSM_WAY_ID:
1331 case OSM_RELATION_ID:
1332 case OSM_NODE_COMMAND:
1333 case OSM_WAY_COMMAND:
1334 case OSM_RELATION_COMMAND:
1335 case OSM_NODE_COMMAND_WAYS:
1336 case OSM_WAY_COMMAND_FULL:
1337 case OSM_RELATION_COMMAND_FULL:
1338 case OSM_NODE_COMMAND_RELATIONS:
1339 case OSM_WAY_COMMAND_RELATIONS:
1340 case OSM_RELATION_COMMAND_RELATIONS:
1341 ret = get_object_by_id(hdl, id, &content, state);
1342 default:
1343 break;
1346 if (state == OSM_NODE_XAPI_PARSE ||
1347 state == OSM_WAY_XAPI_PARSE ||
1348 state == OSM_RELATION_XAPI_PARSE ||
1349 state == OSM_ALL_XAPI_PARSE) {
1350 char *key, *value, *final;
1351 unsigned short int first_tag = TRUE;
1352 unsigned short int first_bbox = TRUE;
1353 cherokee_buffer_t sql = CHEROKEE_BUF_INIT;
1354 cherokee_buffer_t sql_tags = CHEROKEE_BUF_INIT;
1355 cherokee_buffer_t sql_bbox = CHEROKEE_BUF_INIT;
1356 cherokee_buffer_t sql1 = CHEROKEE_BUF_INIT;
1357 cherokee_buffer_t sql2 = CHEROKEE_BUF_INIT;
1359 key = xapi_offset;
1361 while (*key == '[' && (value = ++key) != NULL &&
1362 (key = strsep(&value, "=")) != NULL && (final = value) != NULL &&
1363 (value = strsep(&final, "]")) != NULL
1364 ) {
1365 TRACE("xapi", "%s %s\n", key, value);
1367 if (strcmp(key, "bbox") == 0) {
1368 double left, bottom, right, top;
1369 if (fetch_bbox(value, &left, &bottom, &right, &top) == ret_ok) {
1370 if (first_bbox == FALSE) {
1371 cherokee_buffer_add_str (&sql_bbox, " OR ");
1372 } else
1373 first_tag = FALSE;
1374 cherokee_buffer_add_str (&sql_bbox, "(");
1375 cherokee_buffer_add_va(&sql_bbox, SQL_BY_BBOX, BBOX_VA_ARGS);
1376 cherokee_buffer_add_str (&sql_bbox, ")");
1378 } else {
1379 if (first_tag == FALSE) {
1380 cherokee_buffer_add_str (&sql_tags, " OR ");
1381 } else
1382 first_tag = FALSE;
1384 cherokee_buffer_t temp2 = CHEROKEE_BUF_INIT;
1385 cherokee_buffer_add(&temp2, value, strlen(value));
1386 cherokee_buffer_replace_string (&temp2, "'", 1, "\\\'", 2); /* escape quotes */
1387 cherokee_buffer_replace_string (&temp2, "|", 1, "', '", 4);
1389 if (strcmp(key, "*") == 0) {
1390 if (strcmp(value, "*") != 0) {
1391 cherokee_buffer_add_va (&sql_tags, "(v IN ('%s'))", temp2.buf);
1393 } else {
1394 cherokee_buffer_t temp1 = CHEROKEE_BUF_INIT;
1395 cherokee_buffer_add(&temp1, key, strlen(key));
1396 cherokee_buffer_replace_string (&temp1, "'", 1, "\\\'", 2); /* escape quotes */
1397 cherokee_buffer_replace_string (&temp1, "|", 1, "', '", 4);
1398 if (strcmp(value, "*") == 0) {
1399 cherokee_buffer_add_va (&sql_tags, "(k IN ('%s'))", temp1.buf);
1400 } else {
1401 cherokee_buffer_add_va (&sql_tags, "(k IN ('%s') AND v IN ('%s'))", temp1.buf, temp2.buf);
1403 cherokee_buffer_mrproper(&temp1);
1406 cherokee_buffer_mrproper(&temp2);
1408 key = final;
1411 if (sql_tags.len > 0) {
1412 cherokee_buffer_add_str (&sql, " AND (");
1413 cherokee_buffer_add_buffer (&sql, &sql_tags);
1414 cherokee_buffer_add_str (&sql, ")");
1417 if (sql_bbox.len > 0) {
1418 cherokee_buffer_add_str (&sql, " AND (");
1419 cherokee_buffer_add_buffer (&sql, &sql_bbox);
1420 cherokee_buffer_add_str (&sql, ")");
1424 if (state == OSM_NODE_XAPI_PARSE || state == OSM_ALL_XAPI_PARSE) {
1426 cherokee_buffer_add_str (&sql1, SQL_XAPI_NODE);
1427 cherokee_buffer_add_buffer (&sql1, &sql);
1428 cherokee_buffer_add_str (&sql1, SQL_XAPI_NODE_END);
1429 TRACE("xapi", "%s\n", sql1.buf);
1433 cherokee_buffer_t sql_id = CHEROKEE_BUF_INIT;
1434 cherokee_buffer_add_str (&sql_id, SQL_XAPI_NODE_ID);
1435 cherokee_buffer_add_buffer (&sql_id, &sql);
1437 if (state == OSM_NODE_XAPI_PARSE || state == OSM_ALL_XAPI_PARSE) {
1439 cherokee_buffer_add_str (&sql2, SQL_XAPI_NTAG);
1440 cherokee_buffer_add_buffer (&sql2, &sql_id);
1441 cherokee_buffer_add_str (&sql2, SQL_XAPI_NTAG_END);
1442 TRACE("xapi", "%s\n", sql2.buf);
1445 run_sql2(hdl, &sql1, &sql2, &content, result_nodenew_to_xml);
1447 cherokee_buffer_mrproper(&sql1);
1448 cherokee_buffer_mrproper(&sql2);
1451 if (state == OSM_WAY_XAPI_PARSE || state == OSM_ALL_XAPI_PARSE) {
1452 cherokee_buffer_t sql_way_id = CHEROKEE_BUF_INIT;
1453 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
1455 cherokee_buffer_add_str (&sql1, SQL_XAPI_WAY);
1456 cherokee_buffer_add_str (&sql1, " AND (");
1457 cherokee_buffer_add_buffer (&sql1, &sql_tags);
1458 cherokee_buffer_add_str (&sql1, ")");
1459 cherokee_buffer_add_str (&sql1, SQL_XAPI_WAY_MID);
1460 cherokee_buffer_add_buffer (&sql1, &sql_id);
1461 cherokee_buffer_add_str (&sql1, SQL_XAPI_WAY_END);
1462 TRACE("xapi", "%s\n", sql1.buf);
1464 cherokee_buffer_add_str (&sql_way_id, SQL_XAPI_WAY_ID);
1465 cherokee_buffer_add_str (&sql_way_id, " AND (");
1466 cherokee_buffer_add_buffer (&sql_way_id, &sql_tags);
1467 cherokee_buffer_add_str (&sql_way_id, ")");
1468 cherokee_buffer_add_str (&sql_way_id, SQL_XAPI_WAY_MID);
1469 cherokee_buffer_add_buffer (&sql_way_id, &sql_id);
1470 cherokee_buffer_add_str (&sql_way_id, SQL_XAPI_WAY_ID_END);
1472 cherokee_buffer_add_str (&sql2, SQL_XAPI_WAY_WT);
1473 cherokee_buffer_add_buffer (&sql2, &sql_way_id);
1474 cherokee_buffer_add_str (&sql2, SQL_XAPI_WAY_WT_END);
1475 TRACE("xapi", "%s\n", sql2.buf);
1477 cherokee_buffer_add_str (&sql3, SQL_XAPI_WAY_WN);
1478 cherokee_buffer_add_buffer (&sql3, &sql_way_id);
1479 cherokee_buffer_add_str (&sql3, SQL_XAPI_WAY_WN_END);
1480 TRACE("xapi", "%s\n", sql3.buf);
1482 run_sql3(hdl, &sql1, &sql2, &sql3, &content, result_way_to_xml);
1484 // TRACE("xapi", "%s\n", content.buf);
1486 cherokee_buffer_mrproper(&sql1);
1487 cherokee_buffer_mrproper(&sql2);
1488 cherokee_buffer_mrproper(&sql3);
1490 cherokee_buffer_mrproper(&sql_way_id);
1493 if (state == OSM_RELATION_XAPI_PARSE || state == OSM_ALL_XAPI_PARSE) {
1494 cherokee_buffer_t sql_rel_id = CHEROKEE_BUF_INIT;
1495 cherokee_buffer_t sql3 = CHEROKEE_BUF_INIT;
1496 cherokee_buffer_t sql4 = CHEROKEE_BUF_INIT;
1497 cherokee_buffer_t sql5 = CHEROKEE_BUF_INIT;
1499 cherokee_buffer_add_str (&sql1, SQL_XAPI_REL);
1500 cherokee_buffer_add_str (&sql1, " AND (");
1501 cherokee_buffer_add_buffer (&sql1, &sql_tags);
1502 cherokee_buffer_add_str (&sql1, ")");
1503 cherokee_buffer_add_str (&sql1, SQL_XAPI_REL_MID);
1504 cherokee_buffer_add_buffer (&sql1, &sql_id);
1505 cherokee_buffer_add_str (&sql1, SQL_XAPI_REL_END);
1506 TRACE("xapi", "%s\n", sql1.buf);
1508 cherokee_buffer_add_str (&sql_rel_id, SQL_XAPI_REL_ID);
1509 cherokee_buffer_add_str (&sql_rel_id, " AND (");
1510 cherokee_buffer_add_buffer (&sql_rel_id, &sql_tags);
1511 cherokee_buffer_add_str (&sql_rel_id, ")");
1512 cherokee_buffer_add_str (&sql_rel_id, SQL_XAPI_REL_MID);
1513 cherokee_buffer_add_buffer (&sql_rel_id, &sql_id);
1514 cherokee_buffer_add_str (&sql_rel_id, SQL_XAPI_REL_ID_END);
1516 cherokee_buffer_add_str (&sql2, SQL_XAPI_REL_RT);
1517 cherokee_buffer_add_buffer (&sql2, &sql_rel_id);
1518 cherokee_buffer_add_str (&sql2, SQL_XAPI_REL_RT_END);
1519 TRACE("xapi", "%s\n", sql2.buf);
1521 cherokee_buffer_add_str (&sql3, SQL_XAPI_REL_RN);
1522 cherokee_buffer_add_buffer (&sql3, &sql_rel_id);
1523 cherokee_buffer_add_str (&sql3, SQL_XAPI_REL_RN_END);
1524 TRACE("xapi", "%s\n", sql3.buf);
1526 cherokee_buffer_add_str (&sql4, SQL_XAPI_REL_RR);
1527 cherokee_buffer_add_buffer (&sql4, &sql_rel_id);
1528 cherokee_buffer_add_str (&sql4, SQL_XAPI_REL_RR_END);
1529 TRACE("xapi", "%s\n", sql4.buf);
1531 cherokee_buffer_add_str (&sql5, SQL_XAPI_REL_RW);
1532 cherokee_buffer_add_buffer (&sql5, &sql_rel_id);
1533 cherokee_buffer_add_str (&sql5, SQL_XAPI_REL_RW_END);
1534 TRACE("xapi", "%s\n", sql4.buf);
1536 run_sql5(hdl, &sql1, &sql2, &sql3, &sql4, &sql5, &content, result_relation_to_xml);
1538 cherokee_buffer_mrproper(&sql1);
1539 cherokee_buffer_mrproper(&sql2);
1540 cherokee_buffer_mrproper(&sql3);
1541 cherokee_buffer_mrproper(&sql4);
1542 cherokee_buffer_mrproper(&sql5);
1544 cherokee_buffer_mrproper(&sql_rel_id);
1547 cherokee_buffer_mrproper(&sql);
1548 cherokee_buffer_mrproper(&sql_id);
1549 cherokee_buffer_mrproper(&sql_tags);
1550 cherokee_buffer_mrproper(&sql_bbox);
1555 cherokee_buffer_add_str (buf, XMLHEADER);
1556 cherokee_buffer_add_str (buf, XML(OSM, ""));
1557 if (content.len > 0) {
1558 ret_t ret;
1559 cherokee_buffer_add_str (buf, XMLCONTINUE);
1560 ret = cherokee_buffer_add_buffer (buf, &content);
1561 cherokee_buffer_add_str (buf, XMLCLOSE(OSM, ""));
1562 } else
1563 cherokee_buffer_add_str (buf, XMLCLOSESHORT);
1565 cherokee_buffer_mrproper (&content);
1567 return ret_ok;
1570 ret_t
1571 cherokee_handler_osm_init_get (cherokee_handler_osm_t *hdl) {
1572 ret_t ret;
1573 cherokee_connection_t *conn = HANDLER_CONN(hdl);
1575 ret = cherokee_connection_parse_args (conn);
1576 if (unlikely(ret != ret_ok)) {
1577 conn->error_code = http_internal_error;
1578 return ret_error;
1581 /* integrate this later */
1582 ret = osm_build_page(hdl);
1584 return ret;