diff options
Diffstat (limited to 'postgresql-model.lua')
-rw-r--r-- | postgresql-model.lua | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/postgresql-model.lua b/postgresql-model.lua index efb86d4..8a27ac4 100644 --- a/postgresql-model.lua +++ b/postgresql-model.lua @@ -4,9 +4,12 @@ local mymodule = {} modelfunctions = require("modelfunctions") fs = require("acf.fs") format = require("acf.format") +db = require("acf.db") +dbmodelfunctions = require("dbmodelfunctions") -- Set variables local confdfile = "/etc/conf.d/postgresql" +local conffile = "postgresql.conf" local processname = "postgresql" local packagename = "postgresql" @@ -14,6 +17,7 @@ local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin local datadirectory local filelist +local confcontent function mymodule.set_processname(p) processname = p @@ -25,12 +29,42 @@ end local determinefilelist = function() if not filelist then - datadirectory = format.get_ini_entry(fs.read_file(confdfile) or "", "", "PGDATA") + datadirectory = datadirectory or format.get_ini_entry(fs.read_file(confdfile) or "", "", "PGDATA") filelist = fs.find_files_as_array(".*%.conf", datadirectory) end return filelist end +local getconfvalue = function(param) + datadirectory = datadirectory or format.get_ini_entry(fs.read_file(confdfile) or "", "", "PGDATA") + confcontent = confcontent or fs.read_file(datadirectory.."/"..conffile) or "" + conftable = conftable or format.parse_configfile(confcontent) + local val + if conftable then + val = conftable[param] + end + if val then + -- Remove starting '=', if present + val = string.match(val, "[ =]*(.*)") + -- Remove "'" + val = string.match(val, "[^']+") + end + return val +end + +local determineconnection = function() + -- Determine the connection parameters from the config files + local listen_addresses = getconfvalue("listen_addresses") + if listen_addresses == "" or listen_addresses == "*" or listen_addresses == "localhost" then + listen_addresses = "" + elseif string.find(listen_addresses, "'") then + listen_addresses = string.match(listen_addresses, "[^']+") + end + local port = getconfvalue("port") + if not port then port = "5432" end + return db.create(db.engine.postgresql, nil, nil, nil, listen_addresses, port) +end + -- ################################################################################ -- PUBLIC FUNCTIONS @@ -71,4 +105,10 @@ function mymodule.updatefiledetails(self, filedetails) return modelfunctions.setfiledetails(self, filedetails, determinefilelist()) end +for n,f in pairs(dbmodelfunctions) do + mymodule[n] = function(...) + return f(determineconnection, ...) + end +end + return mymodule |