diff options
Diffstat (limited to 'kamailio-model.lua')
-rw-r--r-- | kamailio-model.lua | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/kamailio-model.lua b/kamailio-model.lua index c121dd1..bee2cb8 100644 --- a/kamailio-model.lua +++ b/kamailio-model.lua @@ -103,9 +103,15 @@ end local listtables = function() local result = {} if DBENGINE == "PGSQL" then +<<<<<<< HEAD + local tab = getselectresponse("SELECT tablename, schemaname FROM pg_tables WHERE tablename !~* 'pg_*' ORDER BY schemaname, tablename ASC") + for i,t in ipairs(tab) do + result[#result+1] = {schema=t.schemaname, table=t.tablename} +======= local tab = getselectresponse("SELECT tablename FROM pg_tables WHERE tablename !~* 'pg_*' ORDER BY tablename ASC") for i,t in ipairs(tab) do result[#result+1] = t.tablename +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 end else -- untested @@ -114,10 +120,23 @@ local listtables = function() return result end +<<<<<<< HEAD +local listcolumns = function(table, schema) + local result = {} + if DBENGINE == "PGSQL" then + local col + if schema then + col = getselectresponse("SELECT column_name AS field FROM information_schema.columns WHERE table_schema='"..schema.."' AND table_name='"..table.."' ORDER BY ordinal_position") + else + col = getselectresponse("SELECT column_name AS field FROM information_schema.columns WHERE table_name='"..table.."' ORDER BY ordinal_position") + end + +======= local listcolumns = function(table) local result = {} if DBENGINE == "PGSQL" then local col = getselectresponse("SELECT a.attname AS field FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = '"..table.."' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum") +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 for i,c in ipairs(col) do result[#result+1] = c.field end @@ -313,9 +332,22 @@ function list_tables() return cfe({ type="list", value=retval, label="List of Database Tables", errtxt=errtxt }) end +<<<<<<< HEAD +function list_table_entries(req_table) + local retval = {} + -- split up the schema from the table + local schema, table + schema, table = string.match(req_table, "(.+)%.(.+)") + -- if there's no schema with it then grab the req_table value + if not schema then + table = req_table + end + retval.table = cfe({ value=req_table or "", label="Table" }) +======= function list_table_entries(table) local retval = {} retval.table = cfe({ value=table or "", label="Table" }) +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 retval.fields = cfe({ type="list", value={}, label="List of Table Fields" }) retval.entries = cfe({ type="structure", value={}, label="List of Database Entries" }) local errtxt @@ -326,11 +358,24 @@ function list_table_entries(table) retval.table.errtxt = "Table does not exist" errtxt = "Table does not exist" for i,t in ipairs(tables) do +<<<<<<< HEAD + if t.table == table then + retval.table.errtxt = nil + errtxt = nil + -- if there's a schema, include it in the select statement + if schema then + retval.entries.value = getselectresponse("SELECT * FROM "..schema.."."..table) or {} + else + retval.entries.value = getselectresponse("SELECT * FROM "..table) or {} + end + retval.fields.value = listcolumns(table, schema) or {} +======= if t == table then retval.table.errtxt = nil errtxt = nil retval.entries.value = getselectresponse("SELECT * FROM "..table.." ORDER BY id ASC") or {} retval.fields.value = listcolumns(table) or {} +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 end end if connected then databasedisconnect() end @@ -338,6 +383,23 @@ function list_table_entries(table) if not res and err then errtxt = err end +<<<<<<< HEAD + return cfe({ type="group", value=retval, label="Database Table Entries", errtxt=errtxt }) +end + +function get_table_entry(req_table, id) + local retval = {} + -- split up the schema from the table + local schema, table + schema, table = string.match(req_table, "(.+)%.(.+)") + -- if there's no schema with it then grab the req_table value + if not schema then + table = req_table + end + retval.table = cfe({ value=req_table or "", label="Table", errtxt="Table does not exist", seq=0 }) + local errtxt = "Table does not exist" + if req_table and req_table ~= "" then +======= return cfe({ type="group", value=retval, label="Database Table Entries", errtxt=errtxt }) end @@ -347,23 +409,41 @@ function get_table_entry(table, id) retval.table = cfe({ value=table or "", label="Table", errtxt="Table does not exist", seq=0 }) local errtxt = "Table does not exist" if table and table ~= "" then +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 local res, err = pcall(function() local connected = databaseconnect() local tables = listtables() for i,t in ipairs(tables) do +<<<<<<< HEAD + if t.table == table then +======= if t == table then +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 retval.table.errtxt = nil errtxt = nil break end end if not errtxt then +<<<<<<< HEAD + local fields = listcolumns(table, schema) +======= local fields = listcolumns(table) +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 for i,f in ipairs(fields) do retval[f] = cfe({ label=f, seq=i }) end if id and id ~= "" then +<<<<<<< HEAD + local entry + if schema then + entry = getselectresponse("SELECT * FROM "..schema.."."..table.." WHERE id='"..escape(id).."'") + else + entry = getselectresponse("SELECT * FROM "..table.." WHERE id='"..escape(id).."'") + end +======= local entry = getselectresponse("SELECT * FROM "..table.." WHERE id='"..escape(id).."'") +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 if entry and #entry > 0 then for n,v in pairs(entry[1]) do if retval[n] then retval[n].value = v end @@ -406,10 +486,22 @@ function update_table_entry(entry, create) local res, err = pcall(function() local connected = databaseconnect() local tables = listtables() +<<<<<<< HEAD + local schema, tablename + schema, tablename = string.match(entry.value.table.value, "(.+)%.(.+)") + success = false + if not schema then + tablename = entry.value.table.value + end + entry.value.table.errtxt = "Table does not exist" + for i,t in ipairs(tables) do + if t.table == tablename then +======= success = false entry.value.table.errtxt = "Table does not exist" for i,t in ipairs(tables) do if t == entry.value.table.value then +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 success = true entry.value.table.errtxt = nil break @@ -428,6 +520,21 @@ function update_table_entry(entry, create) local values = {} for n,v in pairs(entry.value) do if n ~= "table" and n ~= "id" then +<<<<<<< HEAD + --- !!! HACK !!! --- + --- Fix for DISTributary Phone System Tool: ASHP + --- + --- Need to allow for insertion of NULL rather than + --- simply '' so that integers and booleans can be + --- added with empty values. + ---- ----------- --- + if n == "role_id" or n == "seq" then + if v.value == "" then + v.value = "0" + end + end +======= +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 names[#names+1] = n values[#values+1] = escape(v.value) end @@ -456,10 +563,24 @@ function update_table_entry(entry, create) return entry end +<<<<<<< HEAD +function delete_table_entry(req_table, id) + local result = "" + local errtxt + -- split up the schema from the table + local schema, table + schema, table = string.match(req_table, "(.+)%.(.+)") + -- if there's no schema with it then grab the req_table value + if not schema then + table = req_table + end + if not req_table or req_table == "" then +======= function delete_table_entry(table, id) local result = "" local errtxt if not table or table == "" then +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 errtxt = "Invalid table" elseif not id or id == "" then errtxt = "Invalid entry" @@ -469,13 +590,26 @@ function delete_table_entry(table, id) errtxt = "Invalid table" local tables = listtables() for i,t in ipairs(tables) do +<<<<<<< HEAD + if t.table == table then +======= if t == table then +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 errtxt = nil break end end if not errtxt then +<<<<<<< HEAD + local sql + if schema then + sql = "DELETE FROM "..schema.."."..table.." WHERE id='"..escape(id).."'" + else + sql = "DELETE FROM "..table.." WHERE id='"..escape(id).."'" + end +======= local sql = "DELETE FROM "..table.." WHERE id='"..escape(id).."'" +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 runsqlcommand(sql) result = "Entry Deleted" end @@ -506,9 +640,15 @@ function search_database(id, value, comparison) local connected = databaseconnect() local tables = listtables() or {} for i,t in ipairs(tables) do +<<<<<<< HEAD + local columns = listcolumns(t.table) or {} + for i,c in ipairs(columns) do + retval.id.option[#retval.id.option + 1] = t.table.."."..c +======= local columns = listcolumns(t) or {} for i,c in ipairs(columns) do retval.id.option[#retval.id.option + 1] = t.."."..c +>>>>>>> e19ee215667692e70cb4788dca4151e23aa73b96 end end -- Get the rows from the DB |