Page 189 - Asterisk™: The Future of Telephony
P. 189

exten => 456,1,Set(DB(test/count)=1)
                   exten => 456,n,Set(COUNT=${DB(test/count)})
                   exten => 456,n,SayNumber(${COUNT})
               You may also check the value of a given key from the Asterisk command line by running
               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 => 457,1,Verbose(0, The value was ${DB_DELETE(test/count)})
               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 => 457,1,DBdeltree(test)
               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.

               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 show two simple examples. The first is a simple counting
               example to show that the Asterisk database is persistent (meaning that it survives sys-
               tem 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). In order to verify if a value exists in the database or
               not, we will introduce the ISNULL() function that will verify whether a value was re-
               turned, and if not, we will initialize the AstDB with the Set() application, where we
               will set the value in the database to 1. The next priority will send us back to priority 1.
               This will happen the very first time we dial this extension:
                   exten => 678,1,Set(COUNT=${DB(test/count)})
                   exten => 678,n,GotoIf($[${ISNULL(${COUNT})}]?:continue)
                   exten => 678,n,Set(DB(test/count)=1)
                   exten => 678,n,Goto(1)
                   exten => 678,n(continue),NoOp()
               Next, we’ll say the current value of COUNT, and then increment COUNT:
                   exten => 678,1,Set(COUNT=${DB(test/count)})
                   exten => 678,n,GotoIf($[${ISNULL(${COUNT})}]?:continue)


                                                             Using the Asterisk Database (AstDB) | 161
   184   185   186   187   188   189   190   191   192   193   194