[asterisk-users] Can someone give a plain english explanation of the HASH function?

Tilghman Lesher tilghman at mail.jeffandtilghman.com
Sun Sep 14 11:50:28 CDT 2008


On Sunday 14 September 2008 11:24:53 Eric Chamberlain wrote:
> On Sep 13, 2008, at 7:08 PM, Tilghman Lesher wrote:
> > On Saturday 13 September 2008 19:40:45 Eric Chamberlain wrote:
> >> Can someone explain what the HASH function does and how to use it?
> >
> > 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.
>
> Thank you that's exactly the explanation I was looking for.
>
> Will HASH work with a single argument on more than just ODBC_
> functions?  Ideally, I'd like to format a CURL response such that it
> can be used with HASH.  Allowing access to a CURL response key-value
> pairs without having to use ARRAY to enumerate each field.

Unfortunately, no.  func_odbc is actually doing something in the background
that you aren't seeing, which is to set a variable with the field names, and
then the HASH function is referencing that variable to know how to set each
field name.  HASH is very similar in functionality to the ARRAY function; in
fact, they are internally the same function.  The only real difference is that
HASH is using that variable for field names, instead of using an enumerated
list in the dialplan.

CURL() is good idea, though.  I'll have to think about that and see if I can
come up with a compatible solution.

-- 
Tilghman



More information about the asterisk-users mailing list