forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.hpp
More file actions
188 lines (148 loc) · 5.2 KB
/
Copy pathpgsql.hpp
File metadata and controls
188 lines (148 loc) · 5.2 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
#ifndef OSM2PGSQL_PGSQL_HPP
#define OSM2PGSQL_PGSQL_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2021 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
/**
* \file
*
* This file is part of osm2pgsql (https://github.com/openstreetmap/osm2pgsql).
*
* Helper classes and functions for PostgreSQL access.
*/
#include "osmtypes.hpp"
#include <libpq-fe.h>
#include <cassert>
#include <map>
#include <memory>
#include <string>
/**
* PostgreSQL query result.
*
* Wraps the PGresult object of the libpq library.
*/
class pg_result_t
{
public:
explicit pg_result_t(PGresult *result) noexcept : m_result(result) {}
/// Get a pointer to the underlying PGresult object.
PGresult *get() const noexcept { return m_result.get(); }
/// Get the status of this result.
ExecStatusType status() const noexcept
{
return PQresultStatus(m_result.get());
}
/// The number of fields (columns) in this result.
int num_fields() const noexcept { return PQnfields(m_result.get()); }
/// The number of tuples (rows) in this result.
int num_tuples() const noexcept { return PQntuples(m_result.get()); }
/// Does the field at (row, col) has the NULL value?
bool is_null(int row, int col) const noexcept
{
assert(row < num_tuples() && col < num_fields());
return PQgetisnull(m_result.get(), row, col) != 0;
}
/// The length of the field at (row, col) in bytes.
int get_length(int row, int col) const noexcept
{
assert(row < num_tuples() && col < num_fields());
return PQgetlength(m_result.get(), row, col);
}
/**
* Get value of the field at (row, col) as char pointer. The string is
* null-terminated. Only valid as long as the pg_result_t is in scope.
*/
char const *get_value(int row, int col) const noexcept
{
assert(row < num_tuples() && col < num_fields());
return PQgetvalue(m_result.get(), row, col);
}
/**
* Create a std::string with the value of the field at (row, col). This
* does the correct thing for binary data.
*/
std::string get_value_as_string(int row, int col) const noexcept
{
return std::string(get_value(row, col),
(std::size_t)get_length(row, col));
}
/**
* Get the column number from the name. Returns -1 if there is no column
* of that name.
*/
int get_column_number(std::string const &name) const noexcept
{
return PQfnumber(m_result.get(), ('"' + name + '"').c_str());
}
private:
struct pg_result_deleter_t
{
void operator()(PGresult *p) const noexcept { PQclear(p); }
};
std::unique_ptr<PGresult, pg_result_deleter_t> m_result;
};
/**
* PostgreSQL connection.
*
* Wraps the PGconn object of the libpq library.
*
* The connection is automatically closed when the object is destroyed or
* you can close it explicitly by calling close().
*/
class pg_conn_t
{
public:
explicit pg_conn_t(std::string const &conninfo);
/// Execute a prepared statement with one parameter.
pg_result_t exec_prepared(char const *stmt, char const *param) const;
/// Execute a prepared statement with two parameters.
pg_result_t exec_prepared(char const *stmt, char const *p1, char const *p2) const;
/// Execute a prepared statement with one string parameter.
pg_result_t exec_prepared(char const *stmt, std::string const ¶m) const;
/// Execute a prepared statement with one integer parameter.
pg_result_t exec_prepared(char const *stmt, osmid_t id) const;
pg_result_t query(ExecStatusType expect, char const *sql) const;
pg_result_t query(ExecStatusType expect, std::string const &sql) const;
void set_config(char const *setting, char const *value) const;
void exec(char const *sql) const;
void exec(std::string const &sql) const;
void copy_data(std::string const &sql, std::string const &context) const;
void end_copy(std::string const &context) const;
char const *error_msg() const noexcept;
/// Close database connection.
void close() noexcept { m_conn.reset(); }
private:
pg_result_t exec_prepared_internal(char const *stmt, int num_params,
char const *const *param_values) const;
struct pg_conn_deleter_t
{
void operator()(PGconn *p) const noexcept { PQfinish(p); }
};
std::unique_ptr<PGconn, pg_conn_deleter_t> m_conn;
};
/**
* Return a TABLESPACE clause with the specified tablespace name or an empty
* string if the name is empty.
*/
std::string tablespace_clause(std::string const &name);
/**
* Return the possibly schema-qualified name of a table. Names are enclosed
* in double quotes.
*/
std::string qualified_name(std::string const &schema, std::string const &name);
struct postgis_version
{
int major;
int minor;
};
/// Get all config settings from the database.
std::map<std::string, std::string>
get_postgresql_settings(pg_conn_t const &db_connection);
/// Get PostGIS major and minor version.
postgis_version get_postgis_version(pg_conn_t const &db_connection);
#endif // OSM2PGSQL_PGSQL_HPP