Commit 962f455c authored by Ted Trask's avatar Ted Trask

Allow model functions to accept function or db reference

parent 2e9893a0
......@@ -2,10 +2,14 @@ local mymodule = {}
-- Helper library for model functions for viewing and editing database entries
-- Relies heavily on the acf-lib/db.lua library (db object passed into each function)
function mymodule.list_databases(db, self, clientdata)
function mymodule.list_databases(dbase, self, clientdata)
local retval = {}
local errtxt
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
retval = db.listdatabases()
end)
if not res and err then
......@@ -15,10 +19,14 @@ function mymodule.list_databases(db, self, clientdata)
return cfe({ type="list", value=retval, label="List of Databases", errtxt=errtxt })
end
function mymodule.list_tables(db, self, clientdata)
function mymodule.list_tables(dbase, self, clientdata)
local retval = {}
local errtxt
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
retval = db.listtables()
if connected then db.databasedisconnect() end
......@@ -30,7 +38,7 @@ function mymodule.list_tables(db, self, clientdata)
return cfe({ type="list", value=retval, label="List of Database Tables", errtxt=errtxt })
end
function mymodule.list_table_entries(db, self, clientdata)
function mymodule.list_table_entries(dbase, self, clientdata)
local table = clientdata.table
local retval = {}
retval.table = cfe({ value=table or "", label="Table" })
......@@ -38,6 +46,10 @@ function mymodule.list_table_entries(db, self, clientdata)
retval.entries = cfe({ type="structure", value={}, label="List of Database Entries" })
local errtxt
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
local tables = db.listtables()
retval.table.errtxt = "Table does not exist"
......@@ -59,12 +71,16 @@ function mymodule.list_table_entries(db, self, clientdata)
return cfe({ type="group", value=retval, label="Database Table Entries", errtxt=errtxt })
end
function mymodule.get_new_table_entry(db, self, clientdata)
function mymodule.get_new_table_entry(dbase, self, clientdata)
local retval = {}
retval.table = cfe({ value=clientdata.table or "", label="Table", errtxt="Table does not exist", readonly=true, seq=0 })
local errtxt = "Table does not exist"
if clientdata.table and clientdata.table ~= "" then
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
local tables = db.listtables()
for i,t in ipairs(tables) do
......@@ -93,11 +109,15 @@ function mymodule.get_new_table_entry(db, self, clientdata)
return cfe({ type="group", value=retval, label="Database Table Entry", errtxt=errtxt })
end
function mymodule.get_table_entry(db, self, clientdata)
local retval = mymodule.get_new_table_entry(db, self, clientdata)
function mymodule.get_table_entry(dbase, self, clientdata)
local retval = mymodule.get_new_table_entry(dbase, self, clientdata)
if not retval.value.table.errtxt and retval.value.id then
retval.value.id.type = "text"
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
retval.errtxt = "Entry does not exist"
if retval.value.id then
......@@ -127,11 +147,11 @@ function mymodule.get_table_entry(db, self, clientdata)
return retval
end
function mymodule.create_table_entry(db, self, entry, action)
return mymodule.update_table_entry(db, self, entry, action, true)
function mymodule.create_table_entry(dbase, self, entry, action)
return mymodule.update_table_entry(dbase, self, entry, action, true)
end
function mymodule.update_table_entry(db, self, entry, action, create)
function mymodule.update_table_entry(dbase, self, entry, action, create)
local success = true
local errtxt
-- Validate the settings
......@@ -150,6 +170,10 @@ function mymodule.update_table_entry(db, self, entry, action, create)
end
if success then
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
local tables = db.listtables()
success = false
......@@ -203,14 +227,14 @@ function mymodule.update_table_entry(db, self, entry, action, create)
return entry
end
function mymodule.get_delete_table_entry(db, self, clientdata)
function mymodule.get_delete_table_entry(dbase, self, clientdata)
local retval = {}
retval.table = cfe({ value=clientdata.table or "", label="Table", seq=0 })
retval.id = cfe({ value=clientdata.id or "", label="ID", seq=1 })
return cfe({ type="group", value=retval, label="Delete Database Table Entry" })
end
function mymodule.delete_table_entry(db, self, entry)
function mymodule.delete_table_entry(dbase, self, entry)
entry.errtxt = "Failed to delete table entry"
if entry.value.table.value == "" then
entry.value.table.errtxt = "Invalid table"
......@@ -218,6 +242,10 @@ function mymodule.delete_table_entry(db, self, entry)
entry.value.id.errtxt = "Invalid entry"
else
local res, err = pcall(function()
local db = dbase
if type(dbase) == "function" then
db = dbase()
end
local connected = db.databaseconnect()
entry.value.table.errtxt = "Invalid table"
local tables = db.listtables()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment