Skip to content

Instantly share code, notes, and snippets.

@thehunmonkgroup
Created June 16, 2020 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehunmonkgroup/f92ca495c4c366d74fa9269320806cec to your computer and use it in GitHub Desktop.
Save thehunmonkgroup/f92ca495c4c366d74fa9269320806cec to your computer and use it in GitHub Desktop.
--[[
Wrapper for freeswitch.Dbh to fetch results into an indexed table of
assocative tables.
]]
function db_fetch(dbh, sql)
local rows = {}
dbh:query(sql, function(data)
local row = {}
for col, val in pairs(data) do
row[col] = val
end
table.insert(rows, row)
end)
return rows
end
--[[
Convenience function to return only the first row of a result.
]]
function db_fetch_one(dbh, sql)
return db_fetch(dbh, sql)[1]
end
--[[
Convenience function to return a single result.
]]
function db_result(dbh, sql)
local row = db_fetch_one(dbh, sql)
if row then
local _, val = next(row)
return val
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment