Created
June 16, 2020 04:04
-
-
Save thehunmonkgroup/f92ca495c4c366d74fa9269320806cec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
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