From 31f68d1ea1390017773e691cccd0e6f9cc1fd0a7 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Wed, 23 Feb 2011 14:24:20 +0000 Subject: Added searchdatabase action --- kamailio-model.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'kamailio-model.lua') diff --git a/kamailio-model.lua b/kamailio-model.lua index 9c24346..7937ea8 100644 --- a/kamailio-model.lua +++ b/kamailio-model.lua @@ -469,3 +469,36 @@ function create_database() local result = f:read("*a") return cfe({ value=result, label="Create database result" }) end + +function search_database(id, value, comparison) + local errtxt + retval = {} + retval.id = cfe({type="select", value=id or "", label="Table.Column", option={}, seq=1}) + retval.comparison = cfe({type="select", value=comparison or "=", label="Comparison", option={"=", "!=", "~", "!~", "~*", "!*~"}, seq=2}) + retval.value = cfe({label="Value", value=value or "", descr="Value or SQL regular expression", seq=3}) + local res, err = pcall(function() + local connected = databaseconnect() + local tables = listtables() or {} + for i,t in ipairs(tables) do + local columns = listcolumns(t) or {} + for i,c in ipairs(columns) do + retval.id.option[#retval.id.option + 1] = t.."."..c + end + end + -- Get the rows from the DB + if id and modelfunctions.validateselect(retval.id) and modelfunctions.validateselect(retval.comparison) then + retval.result = cfe({type="structure", value={}, label="List of Rows", seq=4 }) + local table, column = string.match(id, "^([^.]*)%.(.*)") + if table then + local sql = "SELECT * FROM "..table.." WHERE "..column..comparison.."'"..value.."'" + retval.result.value = getselectresponse(sql) + end + end + if connected then databasedisconnect() end + end) + if not res and err then + errtxt = err + end + return cfe({type="group", value=retval, label="Database Search", errtxt=errtxt}) +end + -- cgit v1.2.3