[Asterisk-code-review] res_config_pgsql: Limit realtime_pgsql() to return one (no more) record. (asterisk[master])

Boris P. Korzun asteriskteam at digium.com
Mon Feb 15 13:09:13 CST 2021


Boris P. Korzun has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15446 )


Change subject: res_config_pgsql: Limit realtime_pgsql() to return one (no more) record.
......................................................................

res_config_pgsql: Limit realtime_pgsql() to return one (no more) record.

Added a SELECT 'LIMIT' clause to realtime_pgsql() and refactored the function.

ASTERISK-29293 #close

Change-Id: If5a6d4b1072ea2e6e89059b21139d554a74b34f5
---
M res/res_config_pgsql.c
1 file changed, 14 insertions(+), 16 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/46/15446/1

diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index 63eabf8..b6c0627 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -472,6 +472,7 @@
 
 		ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape);
 	}
+	ast_str_append(&sql, 0, " LIMIT 1");
 
 	/* We now have our complete statement; Lets connect to the server and execute it. */
         if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
@@ -481,13 +482,12 @@
 
 	ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));
 
-	if ((num_rows = PQntuples(result)) > 0) {
+	if (PQntuples(result) > 0) {
 		int i = 0;
-		int rowIndex = 0;
 		int numFields = PQnfields(result);
 		char **fieldnames = NULL;
 
-		ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
+		ast_debug(1, "PostgreSQL RealTime: Found a row.\n");
 
 		if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
 			ast_mutex_unlock(&pgsql_lock);
@@ -495,20 +495,18 @@
 		}
 		for (i = 0; i < numFields; i++)
 			fieldnames[i] = PQfname(result, i);
-		for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
-			for (i = 0; i < numFields; i++) {
-				stringp = PQgetvalue(result, rowIndex, i);
-				while (stringp) {
-					chunk = strsep(&stringp, ";");
-					if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
-						if (prev) {
-							prev->next = ast_variable_new(fieldnames[i], chunk, "");
-							if (prev->next) {
-								prev = prev->next;
-							}
-						} else {
-							prev = var = ast_variable_new(fieldnames[i], chunk, "");
+		for (i = 0; i < numFields; i++) {
+			stringp = PQgetvalue(result, 0, i);
+			while (stringp) {
+				chunk = strsep(&stringp, ";");
+				if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
+					if (prev) {
+						prev->next = ast_variable_new(fieldnames[i], chunk, "");
+						if (prev->next) {
+							prev = prev->next;
 						}
+					} else {
+						prev = var = ast_variable_new(fieldnames[i], chunk, "");
 					}
 				}
 			}

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: If5a6d4b1072ea2e6e89059b21139d554a74b34f5
Gerrit-Change-Number: 15446
Gerrit-PatchSet: 1
Gerrit-Owner: Boris P. Korzun <drtr0jan at yandex.ru>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210215/c4c5e186/attachment.html>


More information about the asterisk-code-review mailing list