Skip to content

Commit 348511d

Browse files
A couple of minor cleanups in the PostgreSQL connector (#518)
* Replace ()-init with {}-init * Move the method definitions for sqlpp::postgresql::detail::connection_handle inside the class.
1 parent d0dc081 commit 348511d

File tree

1 file changed

+154
-159
lines changed

1 file changed

+154
-159
lines changed

include/sqlpp11/postgresql/detail/connection_handle.h

+154-159
Original file line numberDiff line numberDiff line change
@@ -58,179 +58,174 @@ namespace sqlpp
5858
std::unique_ptr<PGconn, void(*)(PGconn*)> postgres;
5959
std::set<std::string> prepared_statement_names;
6060

61-
connection_handle(const std::shared_ptr<const connection_config>& config);
62-
connection_handle(const connection_handle&) = delete;
63-
connection_handle(connection_handle&&) = default;
64-
~connection_handle();
65-
connection_handle& operator=(const connection_handle&) = delete;
66-
connection_handle& operator=(connection_handle&&) = default;
67-
68-
void deallocate_prepared_statement(const std::string& name);
69-
PGconn* native_handle() const;
70-
bool check_connection() const;
71-
};
72-
73-
inline connection_handle::connection_handle(const std::shared_ptr<const connection_config>& conf)
74-
: config(conf), postgres{nullptr, PQfinish}
75-
{
61+
connection_handle(const std::shared_ptr<const connection_config>& conf)
62+
: config{conf}, postgres{nullptr, PQfinish}
63+
{
7664
#ifdef SQLPP_DYNAMIC_LOADING
77-
init_pg("");
65+
init_pg("");
7866
#endif
79-
if (config->debug)
80-
{
81-
std::cerr << "PostgreSQL debug: connecting to the database server." << std::endl;
67+
if (config->debug)
68+
{
69+
std::cerr << "PostgreSQL debug: connecting to the database server." << std::endl;
70+
}
71+
72+
// Open connection
73+
std::string conninfo = "";
74+
if (!config->host.empty())
75+
{
76+
conninfo.append("host=" + config->host);
77+
}
78+
if (!config->hostaddr.empty())
79+
{
80+
conninfo.append(" hostaddr=" + config->hostaddr);
81+
}
82+
if (config->port != 5432)
83+
{
84+
conninfo.append(" port=" + std::to_string(config->port));
85+
}
86+
if (!config->dbname.empty())
87+
{
88+
conninfo.append(" dbname=" + config->dbname);
89+
}
90+
if (!config->user.empty())
91+
{
92+
conninfo.append(" user=" + config->user);
93+
}
94+
if (!config->password.empty())
95+
{
96+
conninfo.append(" password=" + config->password);
97+
}
98+
if (config->connect_timeout != 0)
99+
{
100+
conninfo.append(" connect_timeout=" + std::to_string(config->connect_timeout));
101+
}
102+
if (!config->client_encoding.empty())
103+
{
104+
conninfo.append(" client_encoding=" + config->client_encoding);
105+
}
106+
if (!config->options.empty())
107+
{
108+
conninfo.append(" options=" + config->options);
109+
}
110+
if (!config->application_name.empty())
111+
{
112+
conninfo.append(" application_name=" + config->application_name);
113+
}
114+
if (!config->fallback_application_name.empty())
115+
{
116+
conninfo.append(" fallback_application_name=" + config->fallback_application_name);
117+
}
118+
if (!config->keepalives)
119+
{
120+
conninfo.append(" keepalives=0");
121+
}
122+
if (config->keepalives_idle != 0)
123+
{
124+
conninfo.append(" keepalives_idle=" + std::to_string(config->keepalives_idle));
125+
}
126+
if (config->keepalives_interval != 0)
127+
{
128+
conninfo.append(" keepalives_interval=" + std::to_string(config->keepalives_interval));
129+
}
130+
if (config->keepalives_count != 0)
131+
{
132+
conninfo.append(" keepalives_count=" + std::to_string(config->keepalives_count));
133+
}
134+
switch (config->sslmode)
135+
{
136+
case connection_config::sslmode_t::disable:
137+
conninfo.append(" sslmode=disable");
138+
break;
139+
case connection_config::sslmode_t::allow:
140+
conninfo.append(" sslmode=allow");
141+
break;
142+
case connection_config::sslmode_t::require:
143+
conninfo.append(" sslmode=require");
144+
break;
145+
case connection_config::sslmode_t::verify_ca:
146+
conninfo.append(" sslmode=verify-ca");
147+
break;
148+
case connection_config::sslmode_t::verify_full:
149+
conninfo.append(" sslmode=verify-full");
150+
break;
151+
case connection_config::sslmode_t::prefer:
152+
break;
153+
}
154+
if (!config->sslcompression)
155+
{
156+
conninfo.append(" sslcompression=0");
157+
}
158+
if (!config->sslcert.empty())
159+
{
160+
conninfo.append(" sslcert=" + config->sslcert);
161+
}
162+
if (!config->sslkey.empty())
163+
{
164+
conninfo.append(" sslkey=" + config->sslkey);
165+
}
166+
if (!config->sslrootcert.empty())
167+
{
168+
conninfo.append(" sslrootcert=" + config->sslrootcert);
169+
}
170+
if (!config->requirepeer.empty())
171+
{
172+
conninfo.append(" requirepeer=" + config->requirepeer);
173+
}
174+
if (!config->krbsrvname.empty())
175+
{
176+
conninfo.append(" krbsrvname=" + config->krbsrvname);
177+
}
178+
if (!config->service.empty())
179+
{
180+
conninfo.append(" service=" + config->service);
181+
}
182+
183+
postgres.reset(PQconnectdb(conninfo.c_str()));
184+
185+
if (!postgres)
186+
throw std::bad_alloc{};
187+
188+
if (check_connection() == false)
189+
{
190+
std::string msg{PQerrorMessage(native_handle())};
191+
throw broken_connection{std::move(msg)};
192+
}
82193
}
83194

84-
// Open connection
85-
std::string conninfo = "";
86-
if (!config->host.empty())
87-
{
88-
conninfo.append("host=" + config->host);
89-
}
90-
if (!config->hostaddr.empty())
91-
{
92-
conninfo.append(" hostaddr=" + config->hostaddr);
93-
}
94-
if (config->port != 5432)
95-
{
96-
conninfo.append(" port=" + std::to_string(config->port));
97-
}
98-
if (!config->dbname.empty())
99-
{
100-
conninfo.append(" dbname=" + config->dbname);
101-
}
102-
if (!config->user.empty())
103-
{
104-
conninfo.append(" user=" + config->user);
105-
}
106-
if (!config->password.empty())
107-
{
108-
conninfo.append(" password=" + config->password);
109-
}
110-
if (config->connect_timeout != 0)
111-
{
112-
conninfo.append(" connect_timeout=" + std::to_string(config->connect_timeout));
113-
}
114-
if (!config->client_encoding.empty())
115-
{
116-
conninfo.append(" client_encoding=" + config->client_encoding);
117-
}
118-
if (!config->options.empty())
119-
{
120-
conninfo.append(" options=" + config->options);
121-
}
122-
if (!config->application_name.empty())
123-
{
124-
conninfo.append(" application_name=" + config->application_name);
125-
}
126-
if (!config->fallback_application_name.empty())
127-
{
128-
conninfo.append(" fallback_application_name=" + config->fallback_application_name);
129-
}
130-
if (!config->keepalives)
131-
{
132-
conninfo.append(" keepalives=0");
133-
}
134-
if (config->keepalives_idle != 0)
135-
{
136-
conninfo.append(" keepalives_idle=" + std::to_string(config->keepalives_idle));
137-
}
138-
if (config->keepalives_interval != 0)
139-
{
140-
conninfo.append(" keepalives_interval=" + std::to_string(config->keepalives_interval));
141-
}
142-
if (config->keepalives_count != 0)
143-
{
144-
conninfo.append(" keepalives_count=" + std::to_string(config->keepalives_count));
145-
}
146-
switch (config->sslmode)
147-
{
148-
case connection_config::sslmode_t::disable:
149-
conninfo.append(" sslmode=disable");
150-
break;
151-
case connection_config::sslmode_t::allow:
152-
conninfo.append(" sslmode=allow");
153-
break;
154-
case connection_config::sslmode_t::require:
155-
conninfo.append(" sslmode=require");
156-
break;
157-
case connection_config::sslmode_t::verify_ca:
158-
conninfo.append(" sslmode=verify-ca");
159-
break;
160-
case connection_config::sslmode_t::verify_full:
161-
conninfo.append(" sslmode=verify-full");
162-
break;
163-
case connection_config::sslmode_t::prefer:
164-
break;
165-
}
166-
if (!config->sslcompression)
167-
{
168-
conninfo.append(" sslcompression=0");
169-
}
170-
if (!config->sslcert.empty())
171-
{
172-
conninfo.append(" sslcert=" + config->sslcert);
173-
}
174-
if (!config->sslkey.empty())
175-
{
176-
conninfo.append(" sslkey=" + config->sslkey);
177-
}
178-
if (!config->sslrootcert.empty())
179-
{
180-
conninfo.append(" sslrootcert=" + config->sslrootcert);
181-
}
182-
if (!config->requirepeer.empty())
183-
{
184-
conninfo.append(" requirepeer=" + config->requirepeer);
185-
}
186-
if (!config->krbsrvname.empty())
187-
{
188-
conninfo.append(" krbsrvname=" + config->krbsrvname);
189-
}
190-
if (!config->service.empty())
195+
connection_handle(const connection_handle&) = delete;
196+
connection_handle(connection_handle&&) = default;
197+
198+
~connection_handle()
191199
{
192-
conninfo.append(" service=" + config->service);
200+
// Debug
201+
if (config->debug)
202+
{
203+
std::cerr << "PostgreSQL debug: closing database connection." << std::endl;
204+
}
193205
}
194206

195-
postgres.reset(PQconnectdb(conninfo.c_str()));
196-
197-
if (!postgres)
198-
throw std::bad_alloc{};
207+
connection_handle& operator=(const connection_handle&) = delete;
208+
connection_handle& operator=(connection_handle&&) = default;
199209

200-
if (check_connection() == false)
210+
void deallocate_prepared_statement(const std::string& name)
201211
{
202-
std::string msg{PQerrorMessage(native_handle())};
203-
throw broken_connection{std::move(msg)};
212+
std::string cmd = "DEALLOCATE \"" + name + "\"";
213+
PGresult* result = PQexec(native_handle(), cmd.c_str());
214+
PQclear(result);
215+
prepared_statement_names.erase(name);
204216
}
205-
}
206217

207-
inline connection_handle::~connection_handle()
208-
{
209-
// Debug
210-
if (config->debug)
218+
PGconn* native_handle() const
211219
{
212-
std::cerr << "PostgreSQL debug: closing database connection." << std::endl;
220+
return postgres.get();
213221
}
214-
}
215222

216-
inline void connection_handle::deallocate_prepared_statement(const std::string& name)
217-
{
218-
std::string cmd = "DEALLOCATE \"" + name + "\"";
219-
PGresult* result = PQexec(native_handle(), cmd.c_str());
220-
PQclear(result);
221-
prepared_statement_names.erase(name);
222-
}
223-
224-
inline PGconn* connection_handle::native_handle() const
225-
{
226-
return postgres.get();
227-
}
228-
229-
inline bool connection_handle::check_connection() const
230-
{
231-
auto nh = native_handle();
232-
return nh && (PQstatus(nh) == CONNECTION_OK);
233-
}
223+
bool check_connection() const
224+
{
225+
auto nh = native_handle();
226+
return nh && (PQstatus(nh) == CONNECTION_OK);
227+
}
228+
};
234229
}
235230
}
236231
}

0 commit comments

Comments
 (0)