changeset 146:11c4f0a4b647

Fix issue #63 (allow to connect without specifing username and/or port)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 24 Aug 2017 20:26:07 +0200
parents d7cae66abeb6
children 1149cefd1cee
files Core/PostgreSQLConnection.cpp NEWS UnitTestsSources/UnitTestsMain.cpp
diffstat 3 files changed, 70 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Core/PostgreSQLConnection.cpp	Thu Aug 24 01:52:57 2017 +0200
+++ b/Core/PostgreSQLConnection.cpp	Thu Aug 24 20:26:07 2017 +0200
@@ -80,8 +80,32 @@
   {
     if (uri_.empty())
     {
-      return ("postgresql://" + username_ + ":" + password_ + "@" + 
-              host_ + ":" + boost::lexical_cast<std::string>(port_) + "/" + database_);
+      std::string actualUri = "postgresql://";
+
+      if (!username_.empty())
+      {
+        actualUri += username_;
+
+        if (!password_.empty())
+        {
+          actualUri += ":" + password_;
+        }
+
+        actualUri += "@" + host_;
+      }
+      else
+      {
+        actualUri += host_;
+      }
+      
+      if (port_ > 0)
+      {
+        actualUri += ":" + boost::lexical_cast<std::string>(port_);
+      }
+
+      actualUri += "/" + database_;
+
+      return actualUri;
     }
     else
     {
--- a/NEWS	Thu Aug 24 01:52:57 2017 +0200
+++ b/NEWS	Thu Aug 24 20:26:07 2017 +0200
@@ -4,6 +4,7 @@
 * Upgrade to PostgreSQL 9.6.1 client library for static builds
 * Performance warning if runtime debug assertions are turned on
 * Fix issue 62 (use correct type for lo_read() value)
+* Fix issue 63 (allow to connect without specifing username and/or port)
 * Support of OpenBSD and FreeBSD
 
 
--- a/UnitTestsSources/UnitTestsMain.cpp	Thu Aug 24 01:52:57 2017 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Thu Aug 24 20:26:07 2017 +0200
@@ -49,6 +49,49 @@
 }
 
 
+TEST(PostgreSQLConnection, GetConnectionUri)
+{
+  OrthancPlugins::PostgreSQLConnection c;
+  c.SetDatabase("world");
+  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/world", c.GetConnectionUri());
+
+  c.ResetDatabase();
+  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/", c.GetConnectionUri());
+
+  c.SetDatabase("hello");
+  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/hello", c.GetConnectionUri());
+
+  c.SetHost("server");
+  ASSERT_EQ("postgresql://postgres:postgres@server:5432/hello", c.GetConnectionUri());
+
+  c.SetPortNumber(1234);
+  ASSERT_EQ("postgresql://postgres:postgres@server:1234/hello", c.GetConnectionUri());
+
+  c.SetPortNumber(0);
+  ASSERT_EQ("postgresql://postgres:postgres@server/hello", c.GetConnectionUri());
+
+  c.SetPortNumber(5432);
+  ASSERT_EQ("postgresql://postgres:postgres@server:5432/hello", c.GetConnectionUri());
+
+  c.SetUsername("user");
+  c.SetPassword("pass");
+  ASSERT_EQ("postgresql://user:pass@server:5432/hello", c.GetConnectionUri());
+
+  c.SetPassword("");
+  ASSERT_EQ("postgresql://user@server:5432/hello", c.GetConnectionUri());
+
+  c.SetUsername("");
+  c.SetPassword("pass");
+  ASSERT_EQ("postgresql://server:5432/hello", c.GetConnectionUri());
+
+  c.SetUsername("");
+  c.SetPassword("");
+  ASSERT_EQ("postgresql://server:5432/hello", c.GetConnectionUri());
+
+  c.SetConnectionUri("hello://world");
+  ASSERT_EQ("hello://world", c.GetConnectionUri());
+}
+
 
 int main(int argc, char **argv)
 {