<p>Boris P. Korzun has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15514">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_config_pgsql: Limit realtime_pgsql() to return one (no more) record.<br><br>Added a SELECT 'LIMIT' clause to realtime_pgsql() and refactored the function.<br><br>ASTERISK-29293 #close<br><br>Change-Id: If5a6d4b1072ea2e6e89059b21139d554a74b34f5<br>---<br>M res/res_config_pgsql.c<br>1 file changed, 15 insertions(+), 17 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/14/15514/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c</span><br><span>index 63eabf8..6f49f8e 100644</span><br><span>--- a/res/res_config_pgsql.c</span><br><span>+++ b/res/res_config_pgsql.c</span><br><span>@@ -390,7 +390,7 @@</span><br><span> static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, const struct ast_variable *fields)</span><br><span> {</span><br><span> RAII_VAR(PGresult *, result, NULL, PQclear);</span><br><span style="color: hsl(0, 100%, 40%);">- int num_rows = 0, pgresult;</span><br><span style="color: hsl(120, 100%, 40%);">+ int pgresult;</span><br><span> struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);</span><br><span> struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);</span><br><span> char *stringp;</span><br><span>@@ -472,6 +472,7 @@</span><br><span> </span><br><span> ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_str_append(&sql, 0, " LIMIT 1");</span><br><span> </span><br><span> /* We now have our complete statement; Lets connect to the server and execute it. */</span><br><span> if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {</span><br><span>@@ -481,13 +482,12 @@</span><br><span> </span><br><span> ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if ((num_rows = PQntuples(result)) > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (PQntuples(result) > 0) {</span><br><span> int i = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- int rowIndex = 0;</span><br><span> int numFields = PQnfields(result);</span><br><span> char **fieldnames = NULL;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "PostgreSQL RealTime: Found a row.\n");</span><br><span> </span><br><span> if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {</span><br><span> ast_mutex_unlock(&pgsql_lock);</span><br><span>@@ -495,20 +495,18 @@</span><br><span> }</span><br><span> for (i = 0; i < numFields; i++)</span><br><span> fieldnames[i] = PQfname(result, i);</span><br><span style="color: hsl(0, 100%, 40%);">- for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < numFields; i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- stringp = PQgetvalue(result, rowIndex, i);</span><br><span style="color: hsl(0, 100%, 40%);">- while (stringp) {</span><br><span style="color: hsl(0, 100%, 40%);">- chunk = strsep(&stringp, ";");</span><br><span style="color: hsl(0, 100%, 40%);">- if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (prev) {</span><br><span style="color: hsl(0, 100%, 40%);">- prev->next = ast_variable_new(fieldnames[i], chunk, "");</span><br><span style="color: hsl(0, 100%, 40%);">- if (prev->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- prev = prev->next;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- } else {</span><br><span style="color: hsl(0, 100%, 40%);">- prev = var = ast_variable_new(fieldnames[i], chunk, "");</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < numFields; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ stringp = PQgetvalue(result, 0, i);</span><br><span style="color: hsl(120, 100%, 40%);">+ while (stringp) {</span><br><span style="color: hsl(120, 100%, 40%);">+ chunk = strsep(&stringp, ";");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (prev) {</span><br><span style="color: hsl(120, 100%, 40%);">+ prev->next = ast_variable_new(fieldnames[i], chunk, "");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (prev->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ prev = prev->next;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ prev = var = ast_variable_new(fieldnames[i], chunk, "");</span><br><span> }</span><br><span> }</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15514">change 15514</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/+/15514"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: If5a6d4b1072ea2e6e89059b21139d554a74b34f5 </div>
<div style="display:none"> Gerrit-Change-Number: 15514 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Boris P. Korzun <drtr0jan@yandex.ru> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>