<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18785">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">db: Add AMI action to retrieve DB keys at prefix.<br><br>Adds the DBPrefixGet action, which can be used to<br>retrieve all of the DB keys beginning with a<br>particular prefix, similar to the capability<br>provided by the database show CLI command.<br><br>ASTERISK-30136 #close<br><br>Change-Id: I3be9425e53be71f24303fdd4d2923c14e84337e6<br>---<br>A doc/CHANGES-staging/db_prefix.txt<br>M main/db.c<br>2 files changed, 75 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/85/18785/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/db_prefix.txt b/doc/CHANGES-staging/db_prefix.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..4126815</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/db_prefix.txt</span><br><span>@@ -0,0 +1,5 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: db</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+The DBPrefixGet AMI action now allows retrieving</span><br><span style="color: hsl(120, 100%, 40%);">+all of the DB keys beginning with a particular</span><br><span style="color: hsl(120, 100%, 40%);">+prefix.</span><br><span>diff --git a/main/db.c b/main/db.c</span><br><span>index 8965014..8a5961e 100644</span><br><span>--- a/main/db.c</span><br><span>+++ b/main/db.c</span><br><span>@@ -65,6 +65,17 @@</span><br><span> <description></span><br><span> </description></span><br><span> </manager></span><br><span style="color: hsl(120, 100%, 40%);">+ <manager name="DBPrefixGet" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Get DB entries at a particular prefix.</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" /></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="Prefix" required="true" /></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ </manager></span><br><span> <manager name="DBPut" language="en_US"></span><br><span> <synopsis></span><br><span> Put DB entry.</span><br><span>@@ -979,6 +990,63 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int manager_db_prefix_get(struct mansession *s, const struct message *m)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char prefix[MAX_DB_FIELD];</span><br><span style="color: hsl(120, 100%, 40%);">+ char idText[256];</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *id = astman_get_header(m,"ActionID");</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *dbprefix = astman_get_header(m, "Prefix");</span><br><span style="color: hsl(120, 100%, 40%);">+ sqlite3_stmt *stmt = gettree_stmt;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(dbprefix)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(prefix, sizeof(prefix), "/");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(prefix, sizeof(prefix), "/%s", dbprefix);</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%);">+ idText[0] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(id)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);</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%);">+ ast_mutex_lock(&dblock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));</span><br><span style="color: hsl(120, 100%, 40%);">+ sqlite3_reset(stmt);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&dblock);</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_send_error(s, m, "Unable to search database");</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%);">+ astman_send_listack(s, m, "Result will follow", "start");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while (sqlite3_step(stmt) == SQLITE_ROW) {</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *key_s, *value_s;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Skipping invalid key!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Skipping invalid value!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_append(s, "Event: DBPrefixGetResponse\r\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "Key: %s\r\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "Val: %s\r\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ "%s"</span><br><span style="color: hsl(120, 100%, 40%);">+ "\r\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ key_s, value_s, idText);</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%);">+ sqlite3_reset(stmt);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&dblock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_send_list_complete_start(s, m, "DBPrefixGetComplete", 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_send_list_complete_end(s);</span><br><span style="color: hsl(120, 100%, 40%);">+</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> static int manager_dbdel(struct mansession *s, const struct message *m)</span><br><span> {</span><br><span> const char *family = astman_get_header(m, "Family");</span><br><span>@@ -1091,6 +1159,7 @@</span><br><span> {</span><br><span> ast_cli_unregister_multiple(cli_database, ARRAY_LEN(cli_database));</span><br><span> ast_manager_unregister("DBGet");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_manager_unregister("DBPrefixGet");</span><br><span> ast_manager_unregister("DBPut");</span><br><span> ast_manager_unregister("DBDel");</span><br><span> ast_manager_unregister("DBDelTree");</span><br><span>@@ -1126,6 +1195,7 @@</span><br><span> ast_register_atexit(astdb_atexit);</span><br><span> ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));</span><br><span> ast_manager_register_xml_core("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_manager_register_xml_core("DBPrefixGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_db_prefix_get);</span><br><span> ast_manager_register_xml_core("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);</span><br><span> ast_manager_register_xml_core("DBDel", EVENT_FLAG_SYSTEM, manager_dbdel);</span><br><span> ast_manager_register_xml_core("DBDelTree", EVENT_FLAG_SYSTEM, manager_dbdeltree);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18785">change 18785</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/+/18785"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I3be9425e53be71f24303fdd4d2923c14e84337e6 </div>
<div style="display:none"> Gerrit-Change-Number: 18785 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>