Mysql, CallerID checking?

I have MySQL table with two fields - “callerID” and “active” (0/1). I want every call routed by Asterisk is matched against this table. If active=0, hangup. How to do this right way? odbc_func and GotoIf, or AGI + MySQL queries in script, or something else?

I know about Radius but it is supported horribly bad by our billing software…

You have mentioned all the elements needed to achieve your goal. Also you can use the Asterisk System() command, that could be the easier and faster method, Noticed ${cid:0:3} I did it because , I just need the first 3 characters to validate the caller id

[cidlookup]
exten=>_x.,1,Noop( checking the callerid ${CALLERID(num)})
same=>n,Set(__cid=${SHELL(mysql --user=root --password='12340' --skip-column-names  dialer -e 'select dnc_phone  from dnc where dnc_phone="${CALLERID(num)}" limit 0,1 ')})
same=>n,Noop( result ${cid:0:3})
same => n,GotoIf($["${CALLERID(num)}"="${cid:0:3}"]?allow:disallow)
same=>n(allow),Playback(demo-thanks)
same=>n,Hangup()
same=>n(disallow),Playback(im-sorry)
same=>n,Hangup()