Browse Source

allow to use db_hostaddr, db_port or db_conn_string config parameters to connect to PostgreSQL

master
Tomasz Sowa 4 months ago
parent
commit
aae93d018b
  1. 13
      winixd/core/app.cpp
  2. 3
      winixd/core/config.cpp
  3. 10
      winixd/core/config.h
  4. 6
      winixd/core/threadmanager.cpp
  5. 38
      winixd/db/dbconn.cpp
  6. 6
      winixd/db/dbconn.h

13
winixd/core/app.cpp

@ -334,7 +334,11 @@ bool App::TryToMakeDatabaseMigration()
bool App::Init()
{
postgresql_connector.set_conn_param(config.db_database, config.db_user, config.db_pass);
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_logger(log);
postgresql_connector.set_log_queries(config.log_db_query);
postgresql_connector.wait_for_connection();
@ -359,7 +363,12 @@ bool App::Init()
if( !TryToMakeDatabaseMigration() )
return false;
db_conn.SetConnParam(config.db_database, config.db_user, config.db_pass);
// will be removed
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.WaitForConnection();
db.LogQueries(config.log_db_query);

3
winixd/core/config.cpp

@ -178,6 +178,9 @@ void Config::AssignValues()
template_only_root_use_template_fun = Bool(L"template_only_root_use_template_fun", false);
http_session_id_name = Text(L"http_session_id_name", L"session_id");
db_conn_string = Text(L"db_conn_string");
db_hostaddr = Text(L"db_hostaddr");
db_port = Text(L"db_port");
db_database = Text(L"db_database");
db_user = Text(L"db_user");
db_pass = Text(L"db_pass");

10
winixd/core/config.h

@ -189,6 +189,16 @@ public:
// default: false
bool template_only_root_use_template_fun;
// the database connection string
// https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING
// default: empty (not used if empty)
std::wstring db_conn_string;
// the database host ip address and port number
// default: empty which means winix connects to a Unix-domain socket
std::wstring db_hostaddr;
std::wstring db_port;
// the database name, user name and a password for the PostgreSQL database
std::wstring db_database;
std::wstring db_user;

6
winixd/core/threadmanager.cpp

@ -89,7 +89,11 @@ void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name)
item.object->set_log_buffer(&data.log_buffer);
//data.postgresql_connector.set_logger(logger);
data.postgresql_connector.set_conn_param(config->db_database, config->db_user, config->db_pass);
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_logger(item.object->get_logger());
data.postgresql_connector.set_log_queries(config->log_db_query);
data.postgresql_connector.wait_for_connection();

38
winixd/db/dbconn.cpp

@ -64,9 +64,22 @@ PGconn * DbConn::GetPgConn()
}
void DbConn::SetConnParam(const std::wstring & conn_string)
{
db_conn_string = conn_string;
db_host.clear();
db_port.clear();
db_database.clear();
db_user.clear();
db_pass.clear();
}
void DbConn::SetConnParam(const std::wstring & d, const std::wstring & u, const std::wstring & p)
void DbConn::SetConnParam(const std::wstring & host, const std::wstring & port, const std::wstring & d, const std::wstring & u, const std::wstring & p)
{
db_conn_string.clear();
db_host = host;
db_port = port;
db_database = d;
db_user = u;
db_pass = p;
@ -81,9 +94,26 @@ void DbConn::Connect()
conn_info.Clear();
conn_info.SetExtented(false);
conn_info << R("dbname=") << db_database
<< R(" user=") << db_user
<< R(" password=") << db_pass;
if( !db_conn_string.empty() )
{
conn_info << R(db_conn_string);
}
else
{
conn_info << R("dbname=") << db_database
<< R(" user=") << db_user
<< R(" password=") << db_pass;
if( !db_host.empty() )
{
conn_info << R(L"hostaddr=") << db_host;
}
if( !db_port.empty() )
{
conn_info << R(L"port=") << db_port;
}
}
pg_conn = PQconnectdb(conn_info.CStr());

6
winixd/db/dbconn.h

@ -53,7 +53,8 @@ public:
DbConn();
~DbConn();
void SetConnParam(const std::wstring & database, const std::wstring & user, const std::wstring & pass);
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 Connect();
void WaitForConnection();
void Close();
@ -66,7 +67,8 @@ private:
void LogConnectionSocket();
PGconn * pg_conn;
std::wstring db_database, db_user, db_pass;
std::wstring db_conn_string;
std::wstring db_host, db_port, db_database, db_user, db_pass;
DbTextStream conn_info;
// a helper method for escaping strings

Loading…
Cancel
Save