[Asterisk-code-review] db: Add AMI action to retrieve DB keys at prefix. (asterisk[master])

N A asteriskteam at digium.com
Mon Jul 11 06:34:23 CDT 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18785 )


Change subject: db: Add AMI action to retrieve DB keys at prefix.
......................................................................

db: Add AMI action to retrieve DB keys at prefix.

Adds the DBPrefixGet action, which can be used to
retrieve all of the DB keys beginning with a
particular prefix, similar to the capability
provided by the database show CLI command.

ASTERISK-30136 #close

Change-Id: I3be9425e53be71f24303fdd4d2923c14e84337e6
---
A doc/CHANGES-staging/db_prefix.txt
M main/db.c
2 files changed, 75 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/85/18785/1

diff --git a/doc/CHANGES-staging/db_prefix.txt b/doc/CHANGES-staging/db_prefix.txt
new file mode 100644
index 0000000..4126815
--- /dev/null
+++ b/doc/CHANGES-staging/db_prefix.txt
@@ -0,0 +1,5 @@
+Subject: db
+
+The DBPrefixGet AMI action now allows retrieving
+all of the DB keys beginning with a particular
+prefix.
diff --git a/main/db.c b/main/db.c
index 8965014..8a5961e 100644
--- a/main/db.c
+++ b/main/db.c
@@ -65,6 +65,17 @@
 		<description>
 		</description>
 	</manager>
+	<manager name="DBPrefixGet" language="en_US">
+		<synopsis>
+			Get DB entries at a particular prefix.
+		</synopsis>
+		<syntax>
+			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+			<parameter name="Prefix" required="true" />
+		</syntax>
+		<description>
+		</description>
+	</manager>
 	<manager name="DBPut" language="en_US">
 		<synopsis>
 			Put DB entry.
@@ -979,6 +990,63 @@
 	return 0;
 }
 
+static int manager_db_prefix_get(struct mansession *s, const struct message *m)
+{
+	char prefix[MAX_DB_FIELD];
+	char idText[256];
+	const char *id = astman_get_header(m,"ActionID");
+	const char *dbprefix = astman_get_header(m, "Prefix");
+	sqlite3_stmt *stmt = gettree_stmt;
+
+	if (ast_strlen_zero(dbprefix)) {
+		snprintf(prefix, sizeof(prefix), "/");
+	} else {
+		snprintf(prefix, sizeof(prefix), "/%s", dbprefix);
+	}
+
+	idText[0] = '\0';
+	if (!ast_strlen_zero(id)) {
+		snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
+	}
+
+	ast_mutex_lock(&dblock);
+	if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
+		ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
+		sqlite3_reset(stmt);
+		ast_mutex_unlock(&dblock);
+		astman_send_error(s, m, "Unable to search database");
+		return 0;
+	}
+
+	astman_send_listack(s, m, "Result will follow", "start");
+
+	while (sqlite3_step(stmt) == SQLITE_ROW) {
+		const char *key_s, *value_s;
+		if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
+			ast_log(LOG_WARNING, "Skipping invalid key!\n");
+			continue;
+		}
+		if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
+			ast_log(LOG_WARNING, "Skipping invalid value!\n");
+			continue;
+		}
+		astman_append(s, "Event: DBPrefixGetResponse\r\n"
+			"Key: %s\r\n"
+			"Val: %s\r\n"
+			"%s"
+			"\r\n",
+			key_s, value_s, idText);
+	}
+
+	sqlite3_reset(stmt);
+	ast_mutex_unlock(&dblock);
+
+	astman_send_list_complete_start(s, m, "DBPrefixGetComplete", 1);
+	astman_send_list_complete_end(s);
+
+	return 0;
+}
+
 static int manager_dbdel(struct mansession *s, const struct message *m)
 {
 	const char *family = astman_get_header(m, "Family");
@@ -1091,6 +1159,7 @@
 {
 	ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database));
 	ast_manager_unregister("DBGet");
+	ast_manager_unregister("DBPrefixGet");
 	ast_manager_unregister("DBPut");
 	ast_manager_unregister("DBDel");
 	ast_manager_unregister("DBDelTree");
@@ -1126,6 +1195,7 @@
 	ast_register_atexit(astdb_atexit);
 	ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
 	ast_manager_register_xml_core("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
+	ast_manager_register_xml_core("DBPrefixGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_db_prefix_get);
 	ast_manager_register_xml_core("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);
 	ast_manager_register_xml_core("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);
 	ast_manager_register_xml_core("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I3be9425e53be71f24303fdd4d2923c14e84337e6
Gerrit-Change-Number: 18785
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220711/3a4a7934/attachment.html>


More information about the asterisk-code-review mailing list