[Asterisk-code-review] Add parameter dburl to res_config_pgsql to permit adding more than on... (asterisk[20])

Eric Dantie asteriskteam at digium.com
Tue Feb 21 14:06:39 CST 2023


Eric Dantie has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/19921 )


Change subject: Add parameter dburl to res_config_pgsql to permit adding more than one database and parameters\nASTERISK-30431
......................................................................

Add parameter dburl to res_config_pgsql to permit adding more than one database and parameters\nASTERISK-30431

Change-Id: I9ca8705c96acdd4a10989d981a5c2583a160aa44
---
M configs/samples/res_pgsql.conf.sample
M res/res_config_pgsql.c
2 files changed, 86 insertions(+), 51 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/19921/1

diff --git a/configs/samples/res_pgsql.conf.sample b/configs/samples/res_pgsql.conf.sample
index 015d68c..492fa3c 100644
--- a/configs/samples/res_pgsql.conf.sample
+++ b/configs/samples/res_pgsql.conf.sample
@@ -7,6 +7,7 @@
 ; to connect to the server.
 ;
 [general]
+dburl=postgresql://127.0.0.1:5432/asterisk?target_session_attrs=any&application_name=asterisk
 dbhost=127.0.0.1
 dbport=5432
 dbname=asterisk
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index 62a3565..a8026a7 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -55,6 +55,7 @@
 #define USE_BACKSLASH_AS_STRING	(version >= 90100 ? 1 : 0)
 
 #define MAX_DB_OPTION_SIZE 64
+#define MAX_URL_OPTION_SIZE 256
 
 struct columns {
 	char *name;
@@ -80,6 +81,7 @@
 static char dbname[MAX_DB_OPTION_SIZE] = "";
 static char dbappname[MAX_DB_OPTION_SIZE] = "";
 static char dbsock[MAX_DB_OPTION_SIZE] = "";
+static char dburl[MAX_URL_OPTION_SIZE] = "";
 static int dbport = 5432;
 static time_t connect_time = 0;
 
@@ -1458,61 +1460,68 @@
 		pgsqlConn = NULL;
 	}
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");
-		strcpy(dbuser, "asterisk");
-	} else {
-		ast_copy_string(dbuser, s, sizeof(dbuser));
-	}
+    if ((s = ast_variable_retrieve(config, "general", "dburl"))) {
+        ast_log(LOG_DEBUG,
+                "PostgreSQL RealTime: using dburl, skipping others parameters.\n");
+        ast_copy_string(dburl, s, sizeof(dburl));
+    } else {
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");
-		strcpy(dbpass, "asterisk");
-	} else {
-		ast_copy_string(dbpass, s, sizeof(dbpass));
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");
+            strcpy(dbuser, "asterisk");
+        } else {
+            ast_copy_string(dbuser, s, sizeof(dbuser));
+        }
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database host found, using localhost via socket.\n");
-		dbhost[0] = '\0';
-	} else {
-		ast_copy_string(dbhost, s, sizeof(dbhost));
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");
+            strcpy(dbpass, "asterisk");
+        } else {
+            ast_copy_string(dbpass, s, sizeof(dbpass));
+        }
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");
-		strcpy(dbname, "asterisk");
-	} else {
-		ast_copy_string(dbname, s, sizeof(dbname));
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database host found, using localhost via socket.\n");
+            dbhost[0] = '\0';
+        } else {
+            ast_copy_string(dbhost, s, sizeof(dbhost));
+        }
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database port found, using 5432 as default.\n");
-		dbport = 5432;
-	} else {
-		dbport = atoi(s);
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");
+            strcpy(dbname, "asterisk");
+        } else {
+            ast_copy_string(dbname, s, sizeof(dbname));
+        }
 
-	if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {
-		dbappname[0] = '\0';
-	} else {
-		ast_copy_string(dbappname, s, sizeof(dbappname));
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database port found, using 5432 as default.\n");
+            dbport = 5432;
+        } else {
+            dbport = atoi(s);
+        }
 
-	if (!ast_strlen_zero(dbhost)) {
-		/* No socket needed */
-	} else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
-		ast_log(LOG_WARNING,
-				"PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
-		strcpy(dbsock, "/tmp");
-	} else {
-		ast_copy_string(dbsock, s, sizeof(dbsock));
-	}
+        if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {
+            dbappname[0] = '\0';
+        } else {
+            ast_copy_string(dbappname, s, sizeof(dbappname));
+        }
+
+        if (!ast_strlen_zero(dbhost)) {
+            /* No socket needed */
+        } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
+            ast_log(LOG_WARNING,
+                    "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
+            strcpy(dbsock, "/tmp");
+        } else {
+            ast_copy_string(dbsock, s, sizeof(dbsock));
+        }
+    }
 
 	if (!(s = ast_variable_retrieve(config, "general", "requirements"))) {
 		ast_log(LOG_WARNING,
@@ -1570,8 +1579,24 @@
 		pgsqlConn = NULL;
 	}
 
-	/* DB password can legitimately be 0-length */
-	if ((!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
+    if (!ast_strlen_zero(dburl)) {
+        pgsqlConn = PQconnectdb(dburl);
+        ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);
+        if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
+            ast_debug(1, "PostgreSQL RealTime: Successfully connected to database.\n");
+            connect_time = time(NULL);
+            version = PQserverVersion(pgsqlConn);
+            return 1;
+        } else {
+            ast_log(LOG_ERROR,
+                    "PostgreSQL RealTime: Failed to connect database with dburl: [%s]: %s\n",
+                    dburl, PQresultErrorMessage(NULL));
+            return 0;
+        }
+    }
+
+    /* DB password can legitimately be 0-length */
+    if ((!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
 		struct ast_str *conn_info = ast_str_create(128);
 
 		if (!conn_info) {

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/19921
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 20
Gerrit-Change-Id: I9ca8705c96acdd4a10989d981a5c2583a160aa44
Gerrit-Change-Number: 19921
Gerrit-PatchSet: 1
Gerrit-Owner: Eric Dantie <edantie at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20230221/8d68c87f/attachment-0001.html>


More information about the asterisk-code-review mailing list