Browse Source

allow to use host database connection parameter

Add config option:
db_host (default empty) - name of host to connect to
master
Tomasz Sowa 3 months ago
parent
commit
c6c50a5d23
  1. 4
      winixd/core/app.cpp
  2. 1
      winixd/core/config.cpp
  3. 31
      winixd/core/config.h
  4. 2
      winixd/core/threadmanager.cpp
  5. 22
      winixd/db/dbconn.cpp
  6. 7
      winixd/db/dbconn.h

4
winixd/core/app.cpp

@ -355,7 +355,7 @@ bool App::Init()
if( !config.db_conn_string.empty() )
postgresql_connector.set_conn_param(config.db_conn_string);
else
postgresql_connector.set_conn_param(config.db_hostaddr, config.db_port, config.db_database, config.db_user, config.db_pass);
postgresql_connector.set_conn_param(config.db_host, config.db_hostaddr, config.db_port, config.db_database, config.db_user, config.db_pass);
postgresql_connector.set_logger(log);
postgresql_connector.set_log_queries(config.log_db_query);
@ -385,7 +385,7 @@ bool App::Init()
if( !config.db_conn_string.empty() )
db_conn.SetConnParam(config.db_conn_string);
else
db_conn.SetConnParam(config.db_hostaddr, config.db_port, config.db_database, config.db_user, config.db_pass);
db_conn.SetConnParam(config.db_host, config.db_hostaddr, config.db_port, config.db_database, config.db_user, config.db_pass);
db_conn.WaitForConnection();
db.LogQueries(config.log_db_query);

1
winixd/core/config.cpp

@ -181,6 +181,7 @@ void Config::AssignValues()
http_session_id_name = Text(L"http_session_id_name", L"session_id");
db_conn_string = Text(L"db_conn_string");
db_host = Text(L"db_host");
db_hostaddr = Text(L"db_hostaddr");
db_port = Text(L"db_port");
db_database = Text(L"db_database");

31
winixd/core/config.h

@ -204,12 +204,37 @@ public:
// the database connection string
// https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING
// default: empty (not used if empty)
// default: empty
// if empty then winix uses db_host and db_hostaddr
std::wstring db_conn_string;
// the database host ip address and port number
// default: empty which means winix connects to a Unix-domain socket
// the database host name used if db_conn_string is empty
// default: empty
std::wstring db_host;
// the database host ip address used if db_conn_string is empty
// default: empty
// if db_host is empty and db_hostaddr is empty then winix connects to a Unix-domain socket
// meaning of db_host and db_hostaddr parameters is the same as described in
// https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING
//
// from above documentation:
// Using hostaddr allows the application to avoid a host name look-up, which might be important
// in applications with time constraints. However, a host name is required for GSSAPI or SSPI
// authentication methods, as well as for verify-full SSL certificate verification.
// The following rules are used:
//
// - If host is specified without hostaddr, a host name lookup occurs.
//
// - If hostaddr is specified without host, the value for hostaddr gives the server network address.
// The connection attempt will fail if the authentication method requires a host name.
//
// - If both host and hostaddr are specified, the value for hostaddr gives the server network address.
// The value for host is ignored unless the authentication method requires it, in which case it will
// be used as the host name.
std::wstring db_hostaddr;
// the database port number
std::wstring db_port;
// the database name, user name and a password for the PostgreSQL database

2
winixd/core/threadmanager.cpp

@ -92,7 +92,7 @@ void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name)
if( !config->db_conn_string.empty() )
data.postgresql_connector.set_conn_param(config->db_conn_string);
else
data.postgresql_connector.set_conn_param(config->db_hostaddr, config->db_port, config->db_database, config->db_user, config->db_pass);
data.postgresql_connector.set_conn_param(config->db_host, config->db_hostaddr, config->db_port, config->db_database, config->db_user, config->db_pass);
data.postgresql_connector.set_logger(item.object->get_logger());
data.postgresql_connector.set_log_queries(config->log_db_query);

22
winixd/db/dbconn.cpp

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -68,6 +68,7 @@ void DbConn::SetConnParam(const std::wstring & conn_string)
{
db_conn_string = conn_string;
db_host.clear();
db_hostaddr.clear();
db_port.clear();
db_database.clear();
db_user.clear();
@ -75,14 +76,16 @@ void DbConn::SetConnParam(const std::wstring & conn_string)
}
void DbConn::SetConnParam(const std::wstring & host, const std::wstring & port, const std::wstring & d, const std::wstring & u, const std::wstring & p)
void DbConn::SetConnParam(const std::wstring & host, const std::wstring & hostaddr, const std::wstring & port,
const std::wstring & database, const std::wstring & user, const std::wstring & pass)
{
db_conn_string.clear();
db_host = host;
db_hostaddr = hostaddr;
db_port = port;
db_database = d;
db_user = u;
db_pass = p;
db_database = database;
db_user = user;
db_pass = pass;
}
@ -106,12 +109,17 @@ void DbConn::Connect()
if( !db_host.empty() )
{
conn_info << R(L"hostaddr=") << db_host;
conn_info << R(L" host=") << db_host;
}
if( !db_hostaddr.empty() )
{
conn_info << R(L" hostaddr=") << db_hostaddr;
}
if( !db_port.empty() )
{
conn_info << R(L"port=") << db_port;
conn_info << R(L" port=") << db_port;
}
}

7
winixd/db/dbconn.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2018, Tomasz Sowa
* Copyright (c) 2010-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -54,7 +54,8 @@ public:
~DbConn();
void SetConnParam(const std::wstring & conn_string);
void SetConnParam(const std::wstring & host, const std::wstring & port, const std::wstring & database, const std::wstring & user, const std::wstring & pass);
void SetConnParam(const std::wstring & host, const std::wstring & hostaddr, const std::wstring & port,
const std::wstring & database, const std::wstring & user, const std::wstring & pass);
void Connect();
void WaitForConnection();
void Close();
@ -68,7 +69,7 @@ private:
PGconn * pg_conn;
std::wstring db_conn_string;
std::wstring db_host, db_port, db_database, db_user, db_pass;
std::wstring db_host, db_hostaddr, db_port, db_database, db_user, db_pass;
DbTextStream conn_info;
// a helper method for escaping strings

Loading…
Cancel
Save