-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathflex-write.cpp
More file actions
448 lines (411 loc) · 15 KB
/
flex-write.cpp
File metadata and controls
448 lines (411 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-lua-geom.hpp"
#include "flex-write.hpp"
#include "geom-functions.hpp"
#include "json-writer.hpp"
#include "lua-utils.hpp"
#include "wkb.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <vector>
namespace {
int sgn(double val) noexcept
{
if (val > 0) {
return 1;
}
if (val < 0) {
return -1;
}
return 0;
}
void write_null(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column)
{
if (column.not_null()) {
throw not_null_exception_t{
fmt::format("Can not add NULL to column '{}' declared NOT NULL.",
column.name()),
&column};
}
copy_mgr->add_null_column();
}
void write_boolean(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column, char const *str)
{
if ((std::strcmp(str, "yes") == 0) || (std::strcmp(str, "true") == 0) ||
std::strcmp(str, "1") == 0) {
copy_mgr->add_column(true);
return;
}
if ((std::strcmp(str, "no") == 0) || (std::strcmp(str, "false") == 0) ||
std::strcmp(str, "0") == 0) {
copy_mgr->add_column(false);
return;
}
write_null(copy_mgr, column);
}
void write_direction(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column, char const *str)
{
if ((std::strcmp(str, "yes") == 0) || (std::strcmp(str, "1") == 0)) {
copy_mgr->add_column(1);
return;
}
if ((std::strcmp(str, "no") == 0) || (std::strcmp(str, "0") == 0)) {
copy_mgr->add_column(0);
return;
}
if (std::strcmp(str, "-1") == 0) {
copy_mgr->add_column(-1);
return;
}
write_null(copy_mgr, column);
}
template <typename T>
void write_integer(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column, char const *str)
{
if (*str == '\0') {
write_null(copy_mgr, column);
return;
}
char *end = nullptr;
errno = 0;
auto const value = std::strtoll(str, &end, 10);
if (errno != 0 || *end != '\0') {
write_null(copy_mgr, column);
return;
}
if (value >= std::numeric_limits<T>::min() &&
value <= std::numeric_limits<T>::max()) {
copy_mgr->add_column(value);
return;
}
write_null(copy_mgr, column);
}
void write_double(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column, char const *str)
{
if (*str == '\0') {
write_null(copy_mgr, column);
return;
}
char *end = nullptr;
double const value = std::strtod(str, &end);
if (end && *end != '\0') {
write_null(copy_mgr, column);
return;
}
copy_mgr->add_column(value);
}
using table_register_type = std::vector<void const *>;
void write_json(json_writer_t *writer, lua_State *lua_state,
table_register_type *tables);
void write_json_table(json_writer_t *writer, lua_State *lua_state,
table_register_type *tables)
{
void const *table_ptr = lua_topointer(lua_state, -1);
assert(table_ptr);
auto const it = std::find(tables->cbegin(), tables->cend(), table_ptr);
if (it != tables->cend()) {
throw std::runtime_error{"Loop detected in table"};
}
tables->push_back(table_ptr);
if (luaX_is_empty_table(lua_state)) {
// An empty lua table could be both, we decide here that it is not
// stored as a JSON array but as a JSON object.
writer->start_object();
writer->end_object();
} else if (luaX_is_array(lua_state)) {
writer->start_array();
luaX_for_each(lua_state, [&]() {
write_json(writer, lua_state, tables);
writer->next();
});
writer->end_array();
} else {
writer->start_object();
luaX_for_each(lua_state, [&]() {
int const ltype_key = lua_type(lua_state, -2);
if (ltype_key != LUA_TSTRING) {
throw fmt_error("Incorrect data type '{}' as key.",
lua_typename(lua_state, ltype_key));
}
char const *const key = lua_tostring(lua_state, -2);
writer->key(key);
write_json(writer, lua_state, tables);
writer->next();
});
writer->end_object();
}
}
void write_json_number(json_writer_t *writer, lua_State *lua_state)
{
#if LUA_VERSION_NUM >= 503
int okay = 0;
auto const num = lua_tointegerx(lua_state, -1, &okay);
if (okay) {
writer->number(num);
} else {
writer->number(lua_tonumber(lua_state, -1));
}
#else
double const num = lua_tonumber(lua_state, -1);
double intpart = 0.0;
if (std::modf(num, &intpart) == 0.0) {
writer->number(static_cast<int64_t>(num));
} else {
writer->number(num);
}
#endif
}
void write_json(json_writer_t *writer, lua_State *lua_state,
table_register_type *tables)
{
assert(writer);
assert(lua_state);
int const ltype = lua_type(lua_state, -1);
switch (ltype) {
case LUA_TNIL:
writer->null();
break;
case LUA_TBOOLEAN:
writer->boolean(lua_toboolean(lua_state, -1) != 0);
break;
case LUA_TNUMBER:
write_json_number(writer, lua_state);
break;
case LUA_TSTRING:
writer->string(lua_tostring(lua_state, -1));
break;
case LUA_TTABLE:
write_json_table(writer, lua_state, tables);
break;
default:
throw fmt_error("Invalid type '{}' for json/jsonb column.",
lua_typename(lua_state, ltype));
}
}
bool is_compatible(geom::geometry_t const &geom,
table_column_type type) noexcept
{
switch (type) {
case table_column_type::geometry:
return true;
case table_column_type::point:
return geom.is_point();
case table_column_type::linestring:
return geom.is_linestring();
case table_column_type::polygon:
return geom.is_polygon();
case table_column_type::multipoint:
return geom.is_point() || geom.is_multipoint();
case table_column_type::multilinestring:
return geom.is_linestring() || geom.is_multilinestring();
case table_column_type::multipolygon:
return geom.is_polygon() || geom.is_multipolygon();
case table_column_type::geometrycollection:
return geom.is_collection();
default:
break;
}
return false;
}
} // anonymous namespace
void flex_write_column(lua_State *lua_state,
db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
flex_table_column_t const &column,
std::vector<expire_tiles_t> *expire,
std::vector<expire_output_t> *expire_outputs)
{
lua_getfield(lua_state, -1, column.name().c_str());
int const ltype = lua_type(lua_state, -1);
// Certain Lua types can never be added to the database
if (ltype == LUA_TFUNCTION || ltype == LUA_TTHREAD) {
throw std::runtime_error{
"Can not add Lua objects of type function or thread."};
}
// A Lua nil value is always translated to a database NULL
if (ltype == LUA_TNIL) {
write_null(copy_mgr, column);
lua_pop(lua_state, 1);
return;
}
if (column.type() == table_column_type::text) {
auto const *const str = lua_tolstring(lua_state, -1, nullptr);
if (!str) {
throw fmt_error("Invalid type '{}' for text column.",
lua_typename(lua_state, ltype));
}
copy_mgr->add_column(str);
} else if (column.type() == table_column_type::boolean) {
switch (ltype) {
case LUA_TBOOLEAN:
copy_mgr->add_column(lua_toboolean(lua_state, -1) != 0);
break;
case LUA_TNUMBER:
copy_mgr->add_column(lua_tonumber(lua_state, -1) != 0);
break;
case LUA_TSTRING:
write_boolean(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
break;
default:
throw fmt_error("Invalid type '{}' for boolean column.",
lua_typename(lua_state, ltype));
}
} else if (column.type() == table_column_type::int2) {
if (ltype == LUA_TNUMBER) {
int64_t const value = lua_tointeger(lua_state, -1);
if (value >= std::numeric_limits<int16_t>::min() &&
value <= std::numeric_limits<int16_t>::max()) {
copy_mgr->add_column(value);
} else {
write_null(copy_mgr, column);
}
} else if (ltype == LUA_TSTRING) {
write_integer<int16_t>(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
} else if (ltype == LUA_TBOOLEAN) {
copy_mgr->add_column(lua_toboolean(lua_state, -1));
} else {
throw fmt_error("Invalid type '{}' for int2 column.",
lua_typename(lua_state, ltype));
}
} else if (column.type() == table_column_type::int4) {
if (ltype == LUA_TNUMBER) {
int64_t const value = lua_tointeger(lua_state, -1);
if (value >= std::numeric_limits<int32_t>::min() &&
value <= std::numeric_limits<int32_t>::max()) {
copy_mgr->add_column(value);
} else {
write_null(copy_mgr, column);
}
} else if (ltype == LUA_TSTRING) {
write_integer<int32_t>(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
} else if (ltype == LUA_TBOOLEAN) {
copy_mgr->add_column(lua_toboolean(lua_state, -1));
} else {
throw fmt_error("Invalid type '{}' for int4 column.",
lua_typename(lua_state, ltype));
}
} else if (column.type() == table_column_type::int8) {
if (ltype == LUA_TNUMBER) {
copy_mgr->add_column(lua_tointeger(lua_state, -1));
} else if (ltype == LUA_TSTRING) {
write_integer<int64_t>(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
} else if (ltype == LUA_TBOOLEAN) {
copy_mgr->add_column(lua_toboolean(lua_state, -1));
} else {
throw fmt_error("Invalid type '{}' for int8 column.",
lua_typename(lua_state, ltype));
}
} else if (column.type() == table_column_type::real) {
if (ltype == LUA_TNUMBER) {
copy_mgr->add_column(lua_tonumber(lua_state, -1));
} else if (ltype == LUA_TSTRING) {
write_double(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
} else {
throw fmt_error("Invalid type '{}' for real column.",
lua_typename(lua_state, ltype));
}
} else if (column.type() == table_column_type::hstore) {
if (ltype == LUA_TTABLE) {
copy_mgr->new_hash();
luaX_for_each(lua_state, [&]() {
char const *const key = lua_tostring(lua_state, -2);
char const *const val = lua_tostring(lua_state, -1);
if (key == nullptr) {
int const ltype_key = lua_type(lua_state, -2);
throw fmt_error(
"NULL key for hstore. Possibly this is due to"
" an incorrect data type '{}' as key.",
lua_typename(lua_state, ltype_key));
}
if (val == nullptr) {
int const ltype_value = lua_type(lua_state, -1);
throw fmt_error(
"NULL value for hstore. Possibly this is due to"
" an incorrect data type '{}' for key '{}'.",
lua_typename(lua_state, ltype_value), key);
}
copy_mgr->add_hash_elem(key, val);
});
copy_mgr->finish_hash();
} else {
throw fmt_error("Invalid type '{}' for hstore column.",
lua_typename(lua_state, ltype));
}
} else if ((column.type() == table_column_type::json) ||
(column.type() == table_column_type::jsonb)) {
json_writer_t writer;
table_register_type tables;
write_json(&writer, lua_state, &tables);
copy_mgr->add_column(writer.json());
} else if (column.type() == table_column_type::direction) {
switch (ltype) {
case LUA_TBOOLEAN:
copy_mgr->add_column(lua_toboolean(lua_state, -1));
break;
case LUA_TNUMBER:
copy_mgr->add_column(sgn(lua_tonumber(lua_state, -1)));
break;
case LUA_TSTRING:
write_direction(copy_mgr, column,
lua_tolstring(lua_state, -1, nullptr));
break;
default:
throw fmt_error("Invalid type '{}' for direction column.",
lua_typename(lua_state, ltype));
}
} else if (column.is_geometry_column()) {
if (ltype == LUA_TUSERDATA) {
auto const *const geom = unpack_geometry(lua_state, -1);
if (geom && !geom->is_null()) {
auto const type = column.type();
if (!is_compatible(*geom, type)) {
throw fmt_error("Geometry data for geometry column '{}'"
" has the wrong type ({}).",
column.name(), geometry_type(*geom));
}
bool const wrap_multi =
(type == table_column_type::multipoint ||
type == table_column_type::multilinestring ||
type == table_column_type::multipolygon);
if (geom->srid() == column.srid()) {
column.do_expire(*geom, expire, expire_outputs);
copy_mgr->add_hex_geom(geom_to_ewkb(*geom, wrap_multi));
} else {
auto const &proj = get_projection(column.srid());
auto const tgeom = geom::transform(*geom, proj);
column.do_expire(tgeom, expire, expire_outputs);
copy_mgr->add_hex_geom(geom_to_ewkb(tgeom, wrap_multi));
}
} else {
write_null(copy_mgr, column);
}
} else {
throw fmt_error("Need geometry data for geometry column '{}'.",
column.name());
}
} else {
throw fmt_error("Column type {} not implemented.",
static_cast<uint8_t>(column.type()));
}
lua_pop(lua_state, 1);
}