-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathpgsql-helper.cpp
More file actions
84 lines (69 loc) · 2.69 KB
/
Copy pathpgsql-helper.cpp
File metadata and controls
84 lines (69 loc) · 2.69 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
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "pgsql-helper.hpp"
#include "format.hpp"
#include "pgsql.hpp"
#include "pgsql-capabilities.hpp"
#include <cassert>
idlist_t get_ids_from_result(pg_result_t const &result) {
assert(result.num_tuples() >= 0);
idlist_t ids;
ids.reserve(static_cast<std::size_t>(result.num_tuples()));
for (int i = 0; i < result.num_tuples(); ++i) {
ids.push_back(osmium::string_to_object_id(result.get_value(i, 0)));
}
return ids;
}
void create_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table,
std::string const &condition)
{
std::string const func_name =
qualified_name(schema, table + "_osm2pgsql_valid");
db_connection.exec("CREATE OR REPLACE FUNCTION {}()\n"
"RETURNS TRIGGER AS $$\n"
"BEGIN\n"
" IF {} THEN \n"
" RETURN NEW;\n"
" END IF;\n"
" RETURN NULL;\n"
"END;"
"$$ LANGUAGE plpgsql",
func_name, condition);
db_connection.exec("CREATE TRIGGER \"{}\""
" BEFORE INSERT OR UPDATE"
" ON {}"
" FOR EACH ROW EXECUTE PROCEDURE"
" {}()",
table + "_osm2pgsql_valid",
qualified_name(schema, table), func_name);
}
void drop_geom_check_trigger(pg_conn_t const &db_connection,
std::string const &schema,
std::string const &table)
{
std::string const func_name =
qualified_name(schema, table + "_osm2pgsql_valid");
db_connection.exec(R"(DROP TRIGGER "{}" ON {})", table + "_osm2pgsql_valid",
qualified_name(schema, table));
db_connection.exec("DROP FUNCTION IF EXISTS {} ()", func_name);
}
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
std::string const &name)
{
auto const qual_name = qualified_name(schema, name);
db_connection.exec("ANALYZE {}", qual_name);
}
void drop_table_if_exists(pg_conn_t const &db_connection,
std::string const &schema, std::string const &name)
{
db_connection.exec("DROP TABLE IF EXISTS {} CASCADE",
qualified_name(schema, name));
}