Merge pull request #1453 from moteus/db_break_loop

Fix. Break query loop accordin FS documentation
This commit is contained in:
FusionPBX 2016-02-26 08:57:20 -07:00
commit a0c35e92f9
4 changed files with 63 additions and 3 deletions

View File

@ -77,6 +77,54 @@ local function new_database(backend)
assert(db:connected())
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return 1
end)
assert(x == 1, ("Got %d expected %d"):format(x, 1))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return -1
end)
assert(x == 1, ("Got %d expected %d"):format(x, 1))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return 0
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return true
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return false
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return "1"
end)
assert(x == 1, ("Got %d expected %d"):format(x, 2))
end
assert("1" == db:first_value("select 1 as v union all select 2 as v"))
local t = assert(db:first_row("select '1' as v union all select '2' as v"))
@ -99,6 +147,11 @@ local function new_database(backend)
db:release()
assert(not db:connected())
-- second close
db:release()
assert(not db:connected())
log.info('self_test Database - pass')
end

View File

@ -54,7 +54,8 @@ function LuaSQLDatabase:query(sql, fn)
local row, err = cur:fetch({}, "a")
if not row then break end
local ok, ret = pcall(fn, apply_names(row, colnames, ""))
if (not ok) or (type(ret) == 'number' and ret > 0) then
ret = tonumber(ret)
if (not ok) or (ret and ret ~= 0) then
break
end
end

View File

@ -38,7 +38,10 @@ function OdbcDatabase:query(sql, fn)
self._rows_affected = nil
if fn then
return self._dbh:neach(sql, function(row)
return fn(remove_null(row, odbc.NULL, ""))
local n = tonumber((fn(remove_null(row, odbc.NULL, ""))))
if n and n ~= 0 then
return true
end
end)
end
local ok, err = self._dbh:exec(sql)

View File

@ -39,7 +39,10 @@ function OdbcPoolDatabase:query(sql, fn)
if fn then
ok, err = cli:acquire(self._timeout, function(dbh)
local ok, err = dbh:neach(sql, function(row)
return fn(remove_null(row, odbc.NULL, ""))
local n = tonumber((fn(remove_null(row, odbc.NULL, ""))))
if n and n ~= 0 then
return true
end
end)
if err and not ok then
log.errf("Can not execute sql: %s\n%s", tostring(err), sql)