Difference between revisions of "Macros For MySQL Functions"

From Baranoski.ca
Jump to navigation Jump to search
Line 3: Line 3:
 
<PRE style="color:white;background-color:black;font-weight:bold;font-size:1.2em;">
 
<PRE style="color:white;background-color:black;font-weight:bold;font-size:1.2em;">
 
[macro-MYSQLFETCH]
 
[macro-MYSQLFETCH]
exten => s,1,MYSQL(Connect connid localhost asterisk yourpassword asterisk)
+
exten => s,1,MYSQL(Connect CONNID localhost databaseuser databasepassword databasename)
exten => s,n,MYSQL(Query resultid ${connid} ${ARG2})
+
exten => s,n,MYSQL(Query RESULTID ${CONNID} ${ARG2})
exten => s,n,MYSQL(Fetch fetchid ${resultid} ${ARG1})
+
exten => s,n,MYSQL(Fetch FETCHID ${RESULTID} ${ARG1})
exten => s,n,MYSQL(Clear ${resultid})
+
exten => s,n,MYSQL(Clear ${RESULTID})
exten => s,n,MYSQL(Disconnect ${connid})
+
exten => s,n,MYSQL(Disconnect ${RESULTID})
 
</PRE>
 
</PRE>
  
Line 23: Line 23:
 
<PRE style="color:white;background-color:black;font-weight:bold;font-size:1.2em;">
 
<PRE style="color:white;background-color:black;font-weight:bold;font-size:1.2em;">
 
[macro-MYSQLUPDATE]
 
[macro-MYSQLUPDATE]
exten => s,1,MYSQL(Connect connid localhost databaseuser databasepassword databasename)
+
exten => s,1,MYSQL(Connect CONNID localhost databaseuser databasepassword databasename)
exten => s,n,MYSQL(Query resultid ${connid} ${URIDECODE(${ARG1})})
+
exten => s,n,MYSQL(Query RESULTID ${CONNID} ${URIDECODE(${ARG1})})
exten => s,n,MYSQL(Disconnect ${connid})
+
exten => s,n,MYSQL(Disconnect ${CONNID})
 
</PRE>
 
</PRE>
  

Revision as of 14:12, 13 June 2013

To Fetch Data From MySQL

[macro-MYSQLFETCH]
exten => s,1,MYSQL(Connect CONNID localhost databaseuser databasepassword databasename)
exten => s,n,MYSQL(Query RESULTID ${CONNID} ${ARG2})
exten => s,n,MYSQL(Fetch FETCHID ${RESULTID} ${ARG1})
exten => s,n,MYSQL(Clear ${RESULTID})
exten => s,n,MYSQL(Disconnect ${RESULTID})

To call the macro:

exten => s,n,Macro(MYSQLFETCH,DNDVALUE,SELECT value FROM features WHERE name="dnd" AND subname="status")
exten => s,n,GotoIf($[${DNDVALUE} = 1 ]?LINEISDND,s,1)

To Perform Other Queries MySQL

This is used for doing inserts, updates, deletes, etc.

[macro-MYSQLUPDATE]
exten => s,1,MYSQL(Connect CONNID localhost databaseuser databasepassword databasename)
exten => s,n,MYSQL(Query RESULTID ${CONNID} ${URIDECODE(${ARG1})})
exten => s,n,MYSQL(Disconnect ${CONNID})

To call the macro:

exten => s,n,Macro(MYSQLUPDATE,${URIENCODE(UPDATE blacklist SET lastcalled=NOW() WHERE number="${CALLERID(number)}")})

Not the use of the URIENCODE and URIDECODE functions. This allows the values in the query to contain commas (ie. "Smith, John"). Otherwise, if a macro is called, and an argument contains a comma, it will appears as multiple arguments.

Ie: if MYMACRO were called with ARG1 as "Smith, John" and ARG2 as "123-456-7890", the result would be: s,n,Macro(MYMACRO,"Smith,John",123-456-7890) which would be three arguments, not two.