<p>Eric Dantie has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19921">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">Add parameter dburl to res_config_pgsql to permit adding more than one database and parameters\nASTERISK-30431<br><br>Change-Id: I9ca8705c96acdd4a10989d981a5c2583a160aa44<br>---<br>M configs/samples/res_pgsql.conf.sample<br>M res/res_config_pgsql.c<br>2 files changed, 86 insertions(+), 51 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/19921/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/res_pgsql.conf.sample b/configs/samples/res_pgsql.conf.sample</span><br><span>index 015d68c..492fa3c 100644</span><br><span>--- a/configs/samples/res_pgsql.conf.sample</span><br><span>+++ b/configs/samples/res_pgsql.conf.sample</span><br><span>@@ -7,6 +7,7 @@</span><br><span> ; to connect to the server.</span><br><span> ;</span><br><span> [general]</span><br><span style="color: hsl(120, 100%, 40%);">+dburl=postgresql://127.0.0.1:5432/asterisk?target_session_attrs=any&application_name=asterisk</span><br><span> dbhost=127.0.0.1</span><br><span> dbport=5432</span><br><span> dbname=asterisk</span><br><span>diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c</span><br><span>index 62a3565..a8026a7 100644</span><br><span>--- a/res/res_config_pgsql.c</span><br><span>+++ b/res/res_config_pgsql.c</span><br><span>@@ -55,6 +55,7 @@</span><br><span> #define USE_BACKSLASH_AS_STRING (version >= 90100 ? 1 : 0)</span><br><span> </span><br><span> #define MAX_DB_OPTION_SIZE 64</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAX_URL_OPTION_SIZE 256</span><br><span> </span><br><span> struct columns {</span><br><span> char *name;</span><br><span>@@ -80,6 +81,7 @@</span><br><span> static char dbname[MAX_DB_OPTION_SIZE] = "";</span><br><span> static char dbappname[MAX_DB_OPTION_SIZE] = "";</span><br><span> static char dbsock[MAX_DB_OPTION_SIZE] = "";</span><br><span style="color: hsl(120, 100%, 40%);">+static char dburl[MAX_URL_OPTION_SIZE] = "";</span><br><span> static int dbport = 5432;</span><br><span> static time_t connect_time = 0;</span><br><span> </span><br><span>@@ -1458,61 +1460,68 @@</span><br><span> pgsqlConn = NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(dbuser, "asterisk");</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbuser, s, sizeof(dbuser));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((s = ast_variable_retrieve(config, "general", "dburl"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: using dburl, skipping others parameters.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dburl, s, sizeof(dburl));</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(dbpass, "asterisk");</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbpass, s, sizeof(dbpass));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(dbuser, "asterisk");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbuser, s, sizeof(dbuser));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database host found, using localhost via socket.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- dbhost[0] = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbhost, s, sizeof(dbhost));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(dbpass, "asterisk");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbpass, s, sizeof(dbpass));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(dbname, "asterisk");</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbname, s, sizeof(dbname));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database host found, using localhost via socket.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ dbhost[0] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbhost, s, sizeof(dbhost));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database port found, using 5432 as default.\n");</span><br><span style="color: hsl(0, 100%, 40%);">- dbport = 5432;</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- dbport = atoi(s);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(dbname, "asterisk");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbname, s, sizeof(dbname));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- dbappname[0] = '\0';</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbappname, s, sizeof(dbappname));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database port found, using 5432 as default.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ dbport = 5432;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ dbport = atoi(s);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!ast_strlen_zero(dbhost)) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* No socket needed */</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING,</span><br><span style="color: hsl(0, 100%, 40%);">- "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);</span><br><span style="color: hsl(0, 100%, 40%);">- strcpy(dbsock, "/tmp");</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(dbsock, s, sizeof(dbsock));</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ dbappname[0] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbappname, s, sizeof(dbappname));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(dbhost)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No socket needed */</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);</span><br><span style="color: hsl(120, 100%, 40%);">+ strcpy(dbsock, "/tmp");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(dbsock, s, sizeof(dbsock));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> if (!(s = ast_variable_retrieve(config, "general", "requirements"))) {</span><br><span> ast_log(LOG_WARNING,</span><br><span>@@ -1570,8 +1579,24 @@</span><br><span> pgsqlConn = NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* DB password can legitimately be 0-length */</span><br><span style="color: hsl(0, 100%, 40%);">- if ((!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(dburl)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pgsqlConn = PQconnectdb(dburl);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "PostgreSQL RealTime: Successfully connected to database.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ connect_time = time(NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ version = PQserverVersion(pgsqlConn);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+ "PostgreSQL RealTime: Failed to connect database with dburl: [%s]: %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ dburl, PQresultErrorMessage(NULL));</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* DB password can legitimately be 0-length */</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {</span><br><span> struct ast_str *conn_info = ast_str_create(128);</span><br><span> </span><br><span> if (!conn_info) {</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19921">change 19921</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19921"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 20 </div>
<div style="display:none"> Gerrit-Change-Id: I9ca8705c96acdd4a10989d981a5c2583a160aa44 </div>
<div style="display:none"> Gerrit-Change-Number: 19921 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Eric Dantie <edantie@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>