0

I've got an ODBC function that's pulling multiple values from my database and storing them in an array of variables. Later in the dialplan I'm using that variable, but all the double quote marks are getting removed – single quotes are untouched. If I store the value directly into a variable (as opposed to an array) quotes are maintained, but of course a comma-delimited string is not very usable.

Dialplan:

exten => 123,1,Set(ARRAY(FOO,BAR)=${MY_FUNC()})
exten => 123,2,NoOp(The value is now '${FOO}')
exten => 123,3,Set(FOO=${MY_FUNC()})
exten => 123,4,NoOp(The value is now '${FOO}')

Output:

Executing [123@mycontext:1] Set("SIP/7040-0000105b", "ARRAY(FOO,BAR)=I am "testing" it,23") in new stack
Executing [123@mycontext:2] NoOp("SIP/7040-0000105b", "The value is now 'I am testing it'") in new stack
Executing [123@mycontext:3] Set("SIP/7040-0000105b", "FOO=I am "testing" it,23") in new stack
Executing [123@mycontext:4] NoOp("SIP/7040-0000105b", "The value is now 'I am "testing" it,23'") in new stack

I've tried wrapping the function call in quotes, I have tried prefixing a backslash at the database level. Nothing seems to work, they are always removed.

Using HASH as suggested below does not change anything:

exten => 123,1,Set(HASH(FOO)=${MY_FUNC()})
exten => 123,2,NoOp(The value is now '${HASH(FOO,data1)}')

Output:

Executing [123@mycontext:1] Set("SIP/7040-00002bf9", "HASH(FOO)=I am "testing" it,23") in new stack
Executing [123@mycontext:2] NoOp("SIP/7040-00002bf9", "The value is now 'I am testing it'") in new stack

Storing data in an array is not a requirement, but I am pulling multiple values from the database and need to access them without corrupting the values, and without resorting to multiple functions each pulling a single value.

miken32
  • 42,008
  • 16
  • 111
  • 154

2 Answers2

0

Func_ODBC designed to be used with HASH function.

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)

https://www.voip-info.org/asterisk-func-hash/

arheops
  • 15,544
  • 1
  • 21
  • 27
0

Try escaping the double quotes, it will work.

same =>        n,Set(ARRAY(FOO,BAR)=I am \"testing\" it,23)
Anees Sadeek
  • 168
  • 1
  • 10
  • From the question: "I have tried prefixing a backslash at the database level." But TBH I can't even remember what this was for so probably won't be able to test it. – miken32 May 27 '22 at 14:37
  • Func_odbc is returning hash. So no point in using array anyway. – arheops May 27 '22 at 17:32
  • @miken32 I also faced the same kind of issue, that's when I found this. Mine was a shell response string. And I made some trial and error and the above code worked for me. Make sure the backslash is there when you assign the Array, maybe used for escape on the function itself. – Anees Sadeek Jun 03 '22 at 06:06
  • @miken32 From the log you shared, I see the backslash is missing. `Set("SIP/00008d90", "ARRAY(FOO,BAR)=I am \"testing\" it,23")` this is how my log look like. – Anees Sadeek Jun 03 '22 at 06:07
  • Maybe you need to add double escape `\\\ ` or store as string and replace the string before storing as array, like `STRREPLACE(myText,"\"","\\\"") ` – Anees Sadeek Jun 03 '22 at 06:17