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

Boris P. Korzun asteriskteam at digium.com
Wed Feb 24 05:20:14 CST 2021


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


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, 15 insertions(+), 17 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/14/15514/1

diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index 63eabf8..6f49f8e 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -390,7 +390,7 @@
 static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, const struct ast_variable *fields)
 {
 	RAII_VAR(PGresult *, result, NULL, PQclear);
-	int num_rows = 0, pgresult;
+	int pgresult;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
 	struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
 	char *stringp;
@@ -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/+/15514
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 18
Gerrit-Change-Id: If5a6d4b1072ea2e6e89059b21139d554a74b34f5
Gerrit-Change-Number: 15514
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/20210224/89b97277/attachment.html>


More information about the asterisk-code-review mailing list