Function HASH
New in Asterisk 1.6.x (?): The primary intended use is in conjunction with func_odbc, to allow you to retrieve multiple values and reference them without having to assign each field to an individual variable. Think ‘SELECT *’ where the fields included might change over time, and you don’t want the placement of a minor field to completely break your dialplan.
New in Asterisk 1.8: HASH-associated variables now can be inherited across channel creation, by prefixing the name of the hash at assignment with the appropriate number of underscores, just like variables.
Example 1
func_odbc.conf:
[FOO]
dsn=mysql
readsql=SELECT * FROM foo WHERE somefield=’${SQL_ESC(${ARG1})}’
extensions.conf:
Set(HASH(foo)=${ODBC_FOO(${bar})})
Now you can reference ${HASH(foo,somefield)} or ${HASH(foo,someotherfield)}.
You can even add things to the HASH foo with:
Set(HASH(foo,notinthetable)=baz)
Basically, it’s a way to retrieve multiple values from a database without using ARRAY() to enumerate each field, but also without polluting the variable namespace, which could cause unexpected behavior, such as when somebody adds a field to a database table whose name happens to coincide with an existing variable.
In short, it’s a way to be able to use a ‘SELECT *’ query which doesn’t break when somebody adds columns to a table.
Example 2
HASH(SIP_CAUSE,<slave-channel-name>) ; as set by Asterisk 1.8
See also
- function HASHKEYS
- function CLEARHASH
- Asterisk func array
- MASTER_CHANNEL of Asterisk 1.8: Retrieve and set variables on the channel which created the current channel
- Asterisk functions