text
stringlengths
0
152
You can also store values from the Asterisk command line, by running the command
database put family key value. For our example, you would type database put
test count 1.
So, while we’re at it, let’s also plug a value into the database from the console:
*CLI> database put somekey somevalue 42
Let’s query the database from the console to see what values are in there:
*CLI> database show
If all is well, you should see output similar to the following:
/pbx/UUID : d562019a-d2c4-4b88-bcd9-602b3b46fe07
/somekey/count : 1
/somekey/somevalue : 42
/testkey/count : 1
4 results found.
localhost*CLI>
Using the Asterisk Database
|
181
Retrieving Data from the AstDB
To retrieve a value from the Asterisk database and assign it to a variable, we will again
use the Set() application and the DB() function. Let’s retrieve the value of somevalue
(from the somekey family), assign it to a variable called THE_ANSWER, and then speak
the value to the caller:
exten => 217,1,NoOp()
same => n,Set(THE_ANSWER=${DB(somekey/somevalue)})
same => n,Answer()
same => n,SayNumber(${THE_ANSWER})
You may also check the value of a given key from the Asterisk command line by run‐
ning the command database get family key. To view the entire contents of the
AstDB, use the database show command.
Deleting Data from the AstDB
There are two ways to delete data from the Asterisk database. To delete a key, you can
use the DB_DELETE() application. It takes the path to the key as its arguments, like
this:
; deletes the key and returns its value in one step
exten => 218,1,Verbose(0, We just blew away ${DB_DELETE(somekey/somevalue)})
You can also delete an entire key family by using the DBdeltree() application. The
DBdeltree() application takes a single argument: the name of the key family to
delete. To delete the entire test family, do the following:
exten => 219,1,DBdeltree(somekey)
To delete keys and key families from the AstDB via the command-line interface, use
the database del key and database deltree family commands, respectively.
If you call extension 217 now, you will see that there is nothing said, because nothing
is returned by the database. You can also run database show from the CLI, and note
that that family and key have been removed.
Using the AstDB in the Dialplan
There are an infinite number of ways to use the Asterisk database in a dialplan. To
introduce the AstDB, we’ll look at two simple examples. The first is a simple counting
example to show that the Asterisk database is persistent (it even survives system
reboots). In the second example, we’ll use the BLACKLIST() function to evaluate
whether or not a number is on the blacklist and should be blocked.
To begin the counting example, let’s first retrieve a number (the value of the count
key) from the database and assign it to a variable named COUNT. If the key doesn’t
exist, DB() will return NULL (no value). Therefore, we can use the ISNULL() function
182
|
Chapter 10: Deeper into the Dialplan
to verify whether or not a value was returned. If not, we will initialize the AstDB with
the Set() application, where we will set the value in the database to 1. This will only
happen if the database entry does not exist:
exten => 220,1,NoOp()
same => n,Set(COUNT=${DB(test/count)}) ; retrieve current value in database
same => n,GotoIf($[${ISNULL(${COUNT})}]?firstcount:saycount) ; is there a value?
same => n(firstcount),Set(DB(test/count)=1) ; set the value to 1
same => n,Goto(saycount)
same => n(saycount),NoOp()
same => n,Answer
same => n,SayNumber(${COUNT})
same => n,Goto(increment) ; not reqd but a good habit
same => n(increment),Set(COUNT=$[${COUNT} + 1]) ; increment by one
same => n,Set(DB(test/count)=${COUNT}) ; and assign new value to database
same => n,Goto(saycount) ; loop back and say it again